-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5a11e4
commit 4b99b8c
Showing
3 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
from django.test import Client | ||
from someapp.models import Category | ||
|
||
|
||
@pytest.fixture | ||
def categories(): | ||
yield [Category.objects.create(title=title) for title in ["C", "E", "B", "D", "A"]] | ||
|
||
Category.objects.all().delete() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_cursor_pagination_single_page(client: Client, categories): | ||
response = client.get("/api/events/categories") | ||
assert response.status_code == 200, response.json() | ||
assert response.json() == { | ||
"results": [ | ||
{"title": "A"}, | ||
{"title": "B"}, | ||
{"title": "C"}, | ||
{"title": "D"}, | ||
{"title": "E"}, | ||
], | ||
"count": 5, | ||
"next": None, | ||
"previous": None, | ||
} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_cursor_pagination_iteration(client: Client, categories): | ||
response = client.get("/api/events/categories", data={"limit": 2}) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "A"}, {"title": "B"}] | ||
next_url = response.json()["next"] | ||
assert next_url is not None | ||
assert response.json()["previous"] is None | ||
assert response.json()["count"] == 5 | ||
|
||
# follow next page link | ||
response = client.get(next_url) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "C"}, {"title": "D"}] | ||
previous_url = response.json()["previous"] | ||
assert previous_url is not None | ||
assert response.json()["count"] == 5 | ||
|
||
# follow previous page link | ||
response = client.get(previous_url) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "A"}, {"title": "B"}] | ||
|
||
|
||
def test_invalid_cursor(client: Client): | ||
response = client.get("/api/events/categories", data={"cursor": "invalid"}) | ||
assert response.status_code == 422 | ||
assert "Invalid cursor." in response.json()["detail"][0]["msg"] |