Skip to content

Commit

Permalink
✅ [#81] Fix/add tests for get_paginated_results / pagination_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jan 29, 2024
1 parent c537ce6 commit d729eee
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/test_get_paginated_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest

from zgw_consumers.constants import APITypes
from zgw_consumers.legacy.service import get_paginated_results
from zgw_consumers.models import Service
from zgw_consumers.service import get_paginated_results
from zgw_consumers.test import mock_service_oas_get

pytestmark = pytest.mark.django_db
Expand Down
77 changes: 77 additions & 0 deletions tests/test_pagination_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pytest

from zgw_consumers.client import build_client
from zgw_consumers.constants import APITypes
from zgw_consumers.models import Service
from zgw_consumers.service import pagination_helper

pytestmark = pytest.mark.django_db

BOOK_API_ROOT = "https://book.example.org/api/v1/"


def test_paginated_results(settings, requests_mock):
"""
check that function works and doesn't fall into infinite loop
"""
requests_mock.get(
f"{BOOK_API_ROOT}books",
complete_qs=True,
json={
"count": 2,
"next": f"{BOOK_API_ROOT}books?page=2",
"previous": None,
"results": [{"name": "A"}],
},
)
requests_mock.get(
f"{BOOK_API_ROOT}books?page=2",
complete_qs=True,
json={
"count": 2,
"next": None,
"previous": f"{BOOK_API_ROOT}books?page=1",
"results": [{"name": "B"}],
},
)

service = Service.objects.create(
api_type=APITypes.orc,
api_root=BOOK_API_ROOT,
)
client = build_client(service)

response = client.get("books")
response.raise_for_status()
data = response.json()

all_data = pagination_helper(client, data)

assert list(all_data) == [{"name": "A"}, {"name": "B"}]


def test_paginated_results_without_next(settings, requests_mock):
"""
check that function doesn't crash if next-link is missing
"""
requests_mock.get(
f"{BOOK_API_ROOT}books",
complete_qs=True,
json={
"count": 2,
"results": [{"name": "A"}],
},
)
service = Service.objects.create(
api_type=APITypes.orc,
api_root=BOOK_API_ROOT,
)
client = build_client(service)

response = client.get("books")
response.raise_for_status()
data = response.json()

all_data = pagination_helper(client, data)

assert list(all_data) == [{"name": "A"}]

0 comments on commit d729eee

Please sign in to comment.