Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTClient: ensure total count is an integer in OffsetPaginator #1172

Merged
merged 1 commit into from
Apr 3, 2024
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
8 changes: 8 additions & 0 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def update_state(self, response: Response) -> None:
f"Total count not found in response for {self.__class__.__name__}"
)

try:
total = int(total)
except ValueError:
raise ValueError(
f"Total count is not an integer in response for {self.__class__.__name__}. "
f"Expected an integer, got {total}"
)

self.offset += self.limit

if self.offset >= total:
Expand Down
13 changes: 13 additions & 0 deletions tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ def test_update_state(self):
paginator.update_state(response)
assert paginator.has_next_page is False

def test_update_state_with_string_total(self):
paginator = OffsetPaginator(0, 10)
response = Mock(Response, json=lambda: {"total": "20"})
paginator.update_state(response)
assert paginator.offset == 10
assert paginator.has_next_page is True

def test_update_state_with_invalid_total(self):
paginator = OffsetPaginator(0, 10)
response = Mock(Response, json=lambda: {"total": "invalid"})
with pytest.raises(ValueError):
paginator.update_state(response)

def test_update_state_without_total(self):
paginator = OffsetPaginator(0, 10)
response = Mock(Response, json=lambda: {})
Expand Down
Loading