Skip to content

Commit

Permalink
refactors magic to telling name
Browse files Browse the repository at this point in the history
  • Loading branch information
willi-mueller committed Aug 19, 2024
1 parent 1f26fe7 commit 83bab15
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from .conftest import assert_pagination

NON_EMPTY_PAGE = [{"some": "data"}]


@pytest.mark.usefixtures("mock_api_server")
class TestHeaderLinkPaginator:
Expand Down Expand Up @@ -242,7 +244,7 @@ class TestOffsetPaginator:
def test_update_state(self):
paginator = OffsetPaginator(offset=0, limit=10)
response = Mock(Response, json=lambda: {"total": 20})
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.current_value == 10
assert paginator.has_next_page is True

Expand All @@ -253,21 +255,21 @@ def test_update_state(self):
def test_update_state_with_string_total(self):
paginator = OffsetPaginator(0, 10)
response = Mock(Response, json=lambda: {"total": "20"})
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.current_value == 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, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)

def test_update_state_without_total(self):
paginator = OffsetPaginator(0, 10)
response = Mock(Response, json=lambda: {})
with pytest.raises(ValueError):
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)

def test_init_request(self):
paginator = OffsetPaginator(offset=123, limit=42)
Expand All @@ -281,7 +283,7 @@ def test_init_request(self):

response = Mock(Response, json=lambda: {"total": 200})

paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)

# Test for the next request
next_request = Mock(spec=Request)
Expand All @@ -295,11 +297,11 @@ def test_init_request(self):
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, data=[{}]) # Offset 0 to 50
paginator.update_state(response, data=NON_EMPTY_PAGE) # Offset 0 to 50
assert paginator.current_value == 50
assert paginator.has_next_page is True

paginator.update_state(response, data=[{}]) # Offset 50 to 100
paginator.update_state(response, data=NON_EMPTY_PAGE) # Offset 50 to 100
assert paginator.current_value == 100
assert paginator.has_next_page is False

Expand Down Expand Up @@ -362,57 +364,57 @@ class TestPageNumberPaginator:
def test_update_state(self):
paginator = PageNumberPaginator(base_page=1, page=1, total_path="total_pages")
response = Mock(Response, json=lambda: {"total_pages": 3})
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.current_value == 2
assert paginator.has_next_page is True

paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.current_value == 3
assert paginator.has_next_page is True

# Test for reaching the end
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.has_next_page is False

def test_update_state_with_string_total_pages(self):
paginator = PageNumberPaginator(base_page=1, page=1)
response = Mock(Response, json=lambda: {"total": "3"})
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
assert paginator.current_value == 2
assert paginator.has_next_page is True

def test_update_state_with_invalid_total_pages(self):
paginator = PageNumberPaginator(base_page=1, page=1)
response = Mock(Response, json=lambda: {"total_pages": "invalid"})
with pytest.raises(ValueError):
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)

def test_update_state_without_total_pages(self):
paginator = PageNumberPaginator(base_page=1, page=1)
response = Mock(Response, json=lambda: {})
with pytest.raises(ValueError):
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)

def test_update_request(self):
paginator = PageNumberPaginator(base_page=1, page=1, page_param="page")
request = Mock(Request)
response = Mock(Response, json=lambda: {"total": 3})
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
request.params = {}
paginator.update_request(request)
assert request.params["page"] == 2
paginator.update_state(response, data=[{}])
paginator.update_state(response, data=NON_EMPTY_PAGE)
paginator.update_request(request)
assert request.params["page"] == 3

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

paginator.update_state(response, data=[{}]) # Page 2
paginator.update_state(response, data=NON_EMPTY_PAGE) # Page 2
assert paginator.current_value == 3
assert paginator.has_next_page is False

Expand Down

0 comments on commit 83bab15

Please sign in to comment.