Skip to content

Commit

Permalink
fix: PageNumberPaginator not reset when iterating through multiple pa…
Browse files Browse the repository at this point in the history
…rent ressources
  • Loading branch information
paul-godhouse committed Oct 3, 2024
1 parent 543044e commit 0f5ecb6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import warnings
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional
from urllib.parse import urlparse, urljoin
from urllib.parse import urljoin, urlparse

from requests import Request, Response

from requests import Response, Request
from dlt.common import jsonpath


Expand Down Expand Up @@ -127,6 +128,7 @@ def __init__(
" provided."
)
self.param_name = param_name
self.initial_value = initial_value
self.current_value = initial_value
self.value_step = value_step
self.base_index = base_index
Expand All @@ -136,6 +138,8 @@ def __init__(
self.stop_after_empty_page = stop_after_empty_page

def init_request(self, request: Request) -> None:
self._has_next_page = True
self.current_value = self.initial_value
if request.params is None:
request.params = {}

Expand Down
29 changes: 29 additions & 0 deletions tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,35 @@ def test_update_state(self):
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.has_next_page is False


def test_init_request(self):
paginator = PageNumberPaginator(base_page=1, total_path=None)
request = Mock(Request)
request.params = {}
response = Mock(Response, json=lambda: "OK")

assert paginator.current_value == 1
assert paginator.has_next_page is True
paginator.init_request(request)

paginator.update_state(response, data=NON_EMPTY_PAGE)
paginator.update_request(request)

assert paginator.current_value == 2
assert paginator.has_next_page is True
assert request.params["page"] == 2

paginator.update_state(response, data=None)
paginator.update_request(request)

assert paginator.current_value == 2
assert paginator.has_next_page is False

paginator.init_request(request)
assert paginator.current_value == 1
assert paginator.has_next_page is True


def test_update_state_with_string_total_pages(self):
paginator = PageNumberPaginator(base_page=1, page=1)
response = Mock(Response, json=lambda: {"total": "3"})
Expand Down

0 comments on commit 0f5ecb6

Please sign in to comment.