-
-
Notifications
You must be signed in to change notification settings - Fork 388
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 e0aa06e
Showing
3 changed files
with
106 additions
and
7 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,85 @@ | ||
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.fixture | ||
def duplicate_categories(): | ||
yield [Category.objects.create(title=title) for title in ["A", "B", "B", "B", "C"]] | ||
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"] | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_cursor_pagination_duplicates(client: Client, duplicate_categories): | ||
# test cursors that require offets for duplicate values | ||
response = client.get("/api/events/categories", data={"limit": 2}) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "A"}, {"title": "B"}] | ||
|
||
response = client.get(response.json()["next"]) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "B"}, {"title": "B"}] | ||
|
||
response = client.get(response.json()["next"]) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "C"}] | ||
assert response.json()["next"] is None | ||
|
||
response = client.get(response.json()["previous"]) | ||
assert response.status_code == 200, response.json() | ||
assert response.json()["results"] == [{"title": "B"}, {"title": "B"}] | ||
assert response.json()["next"] is not None |