2 # Copyright 2022 Orange
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
18 Utility functions for unit tests.
25 from unittest.mock import Mock
28 # -----------------------------------------------------------------------------------------
30 # -----------------------------------------------------------------------------------------
32 def setup_logging(log_filename="ut_behave_tests.log", *, log_to_console=False):
33 """Setup logging for unit tests.
36 - re-use the global "behave_tests" logger
37 - if `log_to_console` is True, log messages up to INFO level to the console
38 (can be useful when running tests with unittest instead of pytest)
39 - if `log_filename` is provided, log messages up to DEBUG level to the log file
41 logger = logging.getLogger("behave_tests")
42 logger.setLevel(logging.DEBUG)
43 formatter = logging.Formatter("%(levelname)s:%(filename)s:%(lineno)s: %(message)s")
45 # Configure logging to the console
47 ch = logging.StreamHandler()
48 ch.setLevel(logging.INFO)
49 ch.setFormatter(formatter)
52 # Configure logging to the log file
53 if log_filename is not None:
54 fh = logging.FileHandler(filename=log_filename, mode='w')
55 fh.setLevel(logging.DEBUG)
56 fh.setFormatter(formatter)
62 # -----------------------------------------------------------------------------------------
64 # -----------------------------------------------------------------------------------------
66 def get_test_data_dir() -> pathlib.Path:
67 """Get absolute path of the test_data/ dir.
69 We need this because the unit tests can be run from different locations
70 depending on the context (tox, development, ...)
72 return pathlib.Path(__file__).parent / 'test_data'
75 def stub_requests_get(testapi_url):
76 """Mock a request to TestAPI results database.
78 Instead of doing a real request, build a filename from the URL suffix, find
79 the file in the `test_data` directory and return the contents of the file.
82 testapi_url: a URL starting with `http://127.0.0.1:8000/api/v1/results?`
83 and followed by the results file name without extension.
86 A mock of a `requests.Response` object with the attributes `text` and
87 `status_code` and the method `json()`.
90 filename_prefix = testapi_url.replace('http://127.0.0.1:8000/api/v1/results?', '')
91 if filename_prefix == testapi_url:
92 raise ValueError("For unit tests, TestAPI URL must start with "
93 "http://127.0.0.1:8000/api/v1/results?")
94 page_filename = get_test_data_dir() / (filename_prefix + ".json")
96 with open(page_filename, 'r', encoding='utf-8') as results:
97 response.text = results.read()
98 response.json = lambda: json.loads(response.text)
99 response.status_code = 200
101 except FileNotFoundError as e:
102 logging.getLogger("behave_tests").exception(e)
103 raise ValueError(f"No test data available for TestAPI URL: {testapi_url}") from e
106 class TestStubRequestsGet(unittest.TestCase):
107 def test_valid_url(self):
108 response = stub_requests_get("http://127.0.0.1:8000/api/v1/results?"
109 "project=nfvbench&case=characterization&criteria=PASS&page=1")
110 self.assertEqual(200, response.status_code)
111 self.assertEqual("nfvbench", response.json()["results"][0]["project_name"])
113 def test_bad_prefix(self):
114 with self.assertRaises(ValueError):
115 stub_requests_get("http://no.way/api/v1/results?" "dummy_suffix")
117 def test_file_not_found(self):
118 with self.assertRaises(ValueError):
119 stub_requests_get("http://127.0.0.1:8000/api/v1/results?" "dummy_suffix")