Skip to content

Commit

Permalink
Ensure total count is an integer in OffsetPaginator (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
burnash authored Apr 3, 2024
1 parent ecb5aa0 commit ee33548
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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 @@ -88,6 +88,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 @@ -186,6 +186,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

0 comments on commit ee33548

Please sign in to comment.