Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions imednet/core/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ def _iter_sync(self) -> Iterator[Any]:
# Raw list endpoints do not support pagination params
response: httpx.Response = client.get(self.path, params=self.params)
payload = response.json()
if isinstance(payload, list):
yield from payload
else:
yield from []
if not isinstance(payload, list):
raise TypeError(f"API response must be a list, got {type(payload).__name__}")
yield from payload


class AsyncJsonListPaginator(AsyncPaginator):
Expand All @@ -119,9 +118,7 @@ async def _iter_async(self) -> AsyncIterator[Any]:
# Raw list endpoints do not support pagination params
response: httpx.Response = await client.get(self.path, params=self.params)
payload = response.json()
if isinstance(payload, list):
for item in payload:
yield item
else:
# Fallback for empty or malformed response
pass
if not isinstance(payload, list):
raise TypeError(f"API response must be a list, got {type(payload).__name__}")
for item in payload:
yield item
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ faker = "^24.9"
pre-commit = "^4.2.0"
virtualenv = "^20.36.1"
isort = "^6.0.1"
werkzeug = "^3.1.5"
werkzeug = "^3.1.6"
sphinx = "^6.2.0"
sphinx-autodoc-typehints = "*"
sphinx-rtd-theme = "^3.0.2"
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/test_json_list_paginator_robustness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from unittest.mock import Mock

import pytest

from imednet.core.paginator import AsyncJsonListPaginator, JsonListPaginator


class MockClient:
def __init__(self, response_data):
self.response_data = response_data

def get(self, path, params=None):
response = Mock()
response.json.return_value = self.response_data
return response


class MockAsyncClient:
def __init__(self, response_data):
self.response_data = response_data

async def get(self, path, params=None):
response = Mock()
response.json.return_value = self.response_data
return response


def test_json_list_paginator_raises_on_dict():
"""
Test that JsonListPaginator raises TypeError when the API returns a dictionary
instead of the expected list.
"""
# Simulate an error response or unexpected object structure
client = MockClient({"error": "Something went wrong", "details": "Unexpected format"})
paginator = JsonListPaginator(client, "/path")

# Currently this fails (returns empty list), we want it to raise TypeError
with pytest.raises(TypeError, match="API response must be a list"):
list(paginator)


def test_json_list_paginator_raises_on_none():
"""Test that JsonListPaginator raises TypeError when the API returns null."""
client = MockClient(None)
paginator = JsonListPaginator(client, "/path")

with pytest.raises(TypeError, match="API response must be a list"):
list(paginator)


@pytest.mark.asyncio
async def test_async_json_list_paginator_raises_on_dict():
"""
Test that AsyncJsonListPaginator raises TypeError when the API returns a dictionary
instead of the expected list.
"""
client = MockAsyncClient({"error": "Async error"})
paginator = AsyncJsonListPaginator(client, "/path") # type: ignore

with pytest.raises(TypeError, match="API response must be a list"):
[item async for item in paginator]


@pytest.mark.asyncio
async def test_async_json_list_paginator_raises_on_none():
"""Test that AsyncJsonListPaginator raises TypeError when the API returns null."""
client = MockAsyncClient(None)
paginator = AsyncJsonListPaginator(client, "/path") # type: ignore

with pytest.raises(TypeError, match="API response must be a list"):
[item async for item in paginator]