-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): make pagination configurable
This adds the possiblity to configure the pagination via environment variables and query params: - Add possibility to let the client configure the page size via the `page_size` query param - Add env var `PAGINATION_ENABLED` to completely disable pagination - Add env var `PAGINATION_DEFAULT_PAGE_SIZE` to configure the default page size - Add env var `PAGINATION_MAX_PAGE_SIZE` to configure the max value of the `page_size` query param
- Loading branch information
Showing
4 changed files
with
50 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.conf import settings | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
|
||
class APIPagination(PageNumberPagination): | ||
page_size_query_param = "page_size" | ||
max_page_size = settings.PAGINATION_MAX_PAGE_SIZE |
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,26 @@ | ||
import pytest | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"query_params,expected", | ||
[ | ||
({"page_size": 10}, 10), | ||
({"page_size": 120}, 110), # max page size reached | ||
({}, 100), # default page size | ||
], | ||
) | ||
def test_pagination(db, client, template_factory, query_params, expected, mocker): | ||
mocker.patch( | ||
"document_merge_service.api.pagination.APIPagination.max_page_size", 110 | ||
) | ||
|
||
template_factory.create_batch(120) | ||
|
||
response = client.get(reverse("template-list"), data=query_params) | ||
|
||
assert response.status_code == status.HTTP_200_OK | ||
result = response.json() | ||
assert result["count"] == 120 | ||
assert len(result["results"]) == expected |
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