Skip to content

Commit

Permalink
Rename total_pages_path parameter, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burnash committed May 6, 2024
1 parent dd528c8 commit ad48c0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class PageNumberPaginator(RangePaginator):
client = RESTClient(
base_url="https://api.example.com",
paginator=PageNumberPaginator(
total_pages_path="total_pages"
total_path="total_pages"
)
)
Expand Down Expand Up @@ -218,15 +218,15 @@ def __init__(
self,
initial_page: int = 1,
page_param: str = "page",
total_pages_path: jsonpath.TJsonPath = "total",
total_path: jsonpath.TJsonPath = "total",
maximum_page: Optional[int] = None,
):
"""
Args:
initial_page (int): The initial page number.
page_param (str): The query parameter name for the page number.
Defaults to 'page'.
total_pages_path (jsonpath.TJsonPath): The JSONPath expression for
total_path (jsonpath.TJsonPath): The JSONPath expression for
the total number of pages. Defaults to 'total'.
maximum_page (int): The maximum page number. If provided, pagination
will stop once this page is reached or exceeded, even if more
Expand All @@ -236,7 +236,7 @@ def __init__(
super().__init__(
param_name=page_param,
initial_value=initial_page,
total_path=total_pages_path,
total_path=total_path,
value_step=1,
maximum_value=maximum_page,
error_message_items="pages",
Expand Down
24 changes: 23 additions & 1 deletion tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,21 @@ def test_init_request(self):
assert next_request.params["offset"] == 165
assert next_request.params["limit"] == 42

def test_maximum_offset(self):
paginator = OffsetPaginator(offset=0, limit=50, maximum_offset=100, total_path=None)
response = Mock(Response, json=lambda: {"items": []})
paginator.update_state(response) # Offset 0 to 50
assert paginator.current_value == 50
assert paginator.has_next_page is True

paginator.update_state(response) # Offset 50 to 100
assert paginator.current_value == 100
assert paginator.has_next_page is False


class TestPageNumberPaginator:
def test_update_state(self):
paginator = PageNumberPaginator(initial_page=1, total_pages_path="total_pages")
paginator = PageNumberPaginator(initial_page=1, total_path="total_pages")
response = Mock(Response, json=lambda: {"total_pages": 3})
paginator.update_state(response)
assert paginator.current_value == 2
Expand Down Expand Up @@ -270,3 +281,14 @@ def test_update_request(self):
paginator.update_state(response)
paginator.update_request(request)
assert request.params["page"] == 3

def test_maximum_page(self):
paginator = PageNumberPaginator(initial_page=1, maximum_page=3, total_path=None)
response = Mock(Response, json=lambda: {"items": []})
paginator.update_state(response) # Page 1
assert paginator.current_value == 2
assert paginator.has_next_page is True

paginator.update_state(response) # Page 2
assert paginator.current_value == 3
assert paginator.has_next_page is False

0 comments on commit ad48c0c

Please sign in to comment.