From 60693a082504e31e8ff1b80c14df6c814556fb5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Fri, 1 Dec 2023 08:37:42 +0100 Subject: [PATCH] Add: Improve code coverage for NVDResults Test additional code paths that were not covered yet. --- pontos/nvd/api.py | 9 ++++- tests/nvd/test_api.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/pontos/nvd/api.py b/pontos/nvd/api.py index 00aa571c5..c22501907 100644 --- a/pontos/nvd/api.py +++ b/pontos/nvd/api.py @@ -324,7 +324,14 @@ async def _next_iterator(self) -> "NVDResults": return self def __repr__(self) -> str: - return f'<{self.__class__.__name__} url="{self._url}" total_results={self._total_results} start_index={self._start_index} current_index={self._current_index} results_per_page={self._results_per_page}>' + return ( + f"<{self.__class__.__name__} " + f'url="{self._url}" ' + f"total_results={self._total_results} " + f"start_index={self._start_index} " + f"current_index={self._current_index} " + f"results_per_page={self._results_per_page}>" + ) class NVDApi(ABC): diff --git a/tests/nvd/test_api.py b/tests/nvd/test_api.py index b86166050..45f172c4b 100644 --- a/tests/nvd/test_api.py +++ b/tests/nvd/test_api.py @@ -195,6 +195,9 @@ async def test_items(self): result = await anext(it) self.assertEqual(result.value, 6) + with self.assertRaises(StopAsyncIteration): + await anext(it) + async def test_aiter(self): response_mock = MagicMock(spec=Response) response_mock.json.side_effect = [ @@ -238,6 +241,9 @@ async def test_aiter(self): result = await anext(it) self.assertEqual(result.value, 6) + with self.assertRaises(StopAsyncIteration): + await anext(it) + async def test_len(self): response_mock = MagicMock(spec=Response) response_mock.json.return_value = { @@ -294,6 +300,9 @@ async def test_chunks(self): results = await anext(it) self.assertEqual([result.value for result in results], [4, 5, 6]) + with self.assertRaises(StopAsyncIteration): + await anext(it) + async def test_json(self): response_mock = MagicMock(spec=Response) response_mock.json.side_effect = [ @@ -466,3 +475,77 @@ async def test_response_error(self): "resultsPerPage": 3, } ) + + async def test_request_results_limit(self): + response_mock = MagicMock(spec=Response) + response_mock.json.side_effect = [ + { + "values": [1, 2, 3, 4], + "total_results": 5, + "results_per_page": 4, + }, + { + "values": [5], + "total_results": 5, + "results_per_page": 1, + }, + ] + api_mock = AsyncMock(spec=NVDApi) + api_mock._get.return_value = response_mock + + nvd_results: NVDResults[Result] = NVDResults( + api_mock, + {}, + result_func, + request_results=5, + ) + + json: dict[str, Any] = await nvd_results.json() # type: ignore + self.assertEqual(json["values"], [1, 2, 3, 4]) + self.assertEqual(json["total_results"], 5) + self.assertEqual(json["results_per_page"], 4) + + api_mock._get.assert_called_once_with(params={"startIndex": 0}) + api_mock.reset_mock() + + json: dict[str, Any] = await nvd_results.json() # type: ignore + self.assertEqual(json["values"], [5]) + self.assertEqual(json["total_results"], 5) + self.assertEqual(json["results_per_page"], 1) + + api_mock._get.assert_called_once_with( + params={"startIndex": 4, "resultsPerPage": 1} + ) + + async def test_repr(self): + response_mock = MagicMock(spec=Response) + response_mock.json.side_effect = [ + { + "values": [1, 2, 3, 4], + "total_results": 5, + "results_per_page": 4, + }, + { + "values": [5], + "total_results": 5, + "results_per_page": 1, + }, + ] + response_mock.url = "https://some.url&startIndex=0" + api_mock = AsyncMock(spec=NVDApi) + api_mock._get.return_value = response_mock + + nvd_results: NVDResults[Result] = NVDResults( + api_mock, + {}, + result_func, + ) + + await nvd_results + + self.assertEqual( + repr(nvd_results), + '", + )