Skip to content

Commit

Permalink
Either total_path or maximum_value or stop_after_empty_pages is required
Browse files Browse the repository at this point in the history
  • Loading branch information
willi-mueller committed Aug 12, 2024
1 parent e9ecf88 commit 5e78dcc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
18 changes: 12 additions & 6 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ def __init__(
a page contains no result items. Defaults to `True`.
"""
super().__init__()
if total_path is None and maximum_value is None:
raise ValueError("Either `total_path` or `maximum_value` must be provided.")
if total_path is None and maximum_value is None and not stop_after_empty_page:
raise ValueError(
"Either `total_path` or `maximum_value` or stop_after_empty_page must be provided."
)
self.param_name = param_name
self.current_value = initial_value
self.value_step = value_step
Expand Down Expand Up @@ -260,8 +262,10 @@ def __init__(
stop_after_empty_page (bool): Whether pagination should stop when
a page contains no result items. Defaults to `True`.
"""
if total_path is None and maximum_page is None:
raise ValueError("Either `total_path` or `maximum_page` must be provided.")
if total_path is None and maximum_page is None and not stop_after_empty_page:
raise ValueError(
"Either `total_path` or `maximum_page` or `stop_after_empty_page` must be provided."
)

page = page if page is not None else base_page

Expand Down Expand Up @@ -365,8 +369,10 @@ def __init__(
stop_after_empty_page (bool): Whether pagination should stop when
a page contains no result items. Defaults to `True`.
"""
if total_path is None and maximum_offset is None:
raise ValueError("Either `total_path` or `maximum_offset` must be provided.")
if total_path is None and maximum_offset is None and not stop_after_empty_page:
raise ValueError(
"Either `total_path` or `maximum_offset` or `stop_after_empty_page` must be provided."
)
super().__init__(
param_name=offset_param,
initial_value=offset,
Expand Down
56 changes: 56 additions & 0 deletions tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,36 @@ def test_stop_after_empty_page(self):
paginator.update_state(response, no_data_found) # Page 1
assert paginator.has_next_page is False

def test_guarantee_termination(self):
OffsetPaginator(
limit=10,
total_path=None,
)

OffsetPaginator(
limit=10,
total_path=None,
maximum_offset=1,
stop_after_empty_page=False,
)

with pytest.raises(ValueError) as e:
OffsetPaginator(
limit=10,
total_path=None,
stop_after_empty_page=False,
)
assert e.match("`total_path` or `maximum_offset` or `stop_after_empty_page` must be provided")

with pytest.raises(ValueError) as e:
OffsetPaginator(
limit=10,
total_path=None,
stop_after_empty_page=False,
maximum_offset=None,
)
assert e.match("`total_path` or `maximum_offset` or `stop_after_empty_page` must be provided")


@pytest.mark.usefixtures("mock_api_server")
class TestPageNumberPaginator:
Expand Down Expand Up @@ -431,6 +461,32 @@ def test_client_pagination_zero_based(self, rest_client):

assert_pagination(pages)

def test_guarantee_termination(self):
PageNumberPaginator(
total_path=None,
)

PageNumberPaginator(
total_path=None,
maximum_page=1,
stop_after_empty_page=False,
)

with pytest.raises(ValueError) as e:
PageNumberPaginator(
total_path=None,
stop_after_empty_page=False,
)
assert e.match("`total_path` or `maximum_page` or `stop_after_empty_page` must be provided")

with pytest.raises(ValueError) as e:
PageNumberPaginator(
total_path=None,
stop_after_empty_page=False,
maximum_page=None,
)
assert e.match("`total_path` or `maximum_page` or `stop_after_empty_page` must be provided")


@pytest.mark.usefixtures("mock_api_server")
class TestJSONResponseCursorPaginator:
Expand Down

0 comments on commit 5e78dcc

Please sign in to comment.