Skip to content

Commit

Permalink
Add: Improve code coverage for NVDResults
Browse files Browse the repository at this point in the history
Test additional code paths that were not covered yet.
  • Loading branch information
bjoernricks authored and greenbonebot committed Dec 1, 2023
1 parent 7913e97 commit 60693a0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pontos/nvd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
83 changes: 83 additions & 0 deletions tests/nvd/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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),
'<NVDResults url="https://some.url&startIndex=0" '
"total_results=5 start_index=0 current_index=4 "
"results_per_page=None>",
)

0 comments on commit 60693a0

Please sign in to comment.