diff --git a/example/tests/unit/test_pagination.py b/example/tests/unit/test_pagination.py deleted file mode 100644 index 83570e7c..00000000 --- a/example/tests/unit/test_pagination.py +++ /dev/null @@ -1,92 +0,0 @@ -from collections import OrderedDict - -from rest_framework.request import Request -from rest_framework.test import APIRequestFactory -from rest_framework.utils.urls import replace_query_param - -from rest_framework_json_api import pagination - -factory = APIRequestFactory() - - -class TestLimitOffset: - """ - Unit tests for `pagination.JsonApiLimitOffsetPagination`. - """ - - def setup_method(self): - class ExamplePagination(pagination.JsonApiLimitOffsetPagination): - default_limit = 10 - max_limit = 15 - - self.pagination = ExamplePagination() - self.queryset = range(1, 101) - self.base_url = "http://testserver/" - - def paginate_queryset(self, request): - return list(self.pagination.paginate_queryset(self.queryset, request)) - - def get_paginated_content(self, queryset): - response = self.pagination.get_paginated_response(queryset) - return response.data - - def get_test_request(self, arguments): - return Request(factory.get("/", arguments)) - - def test_valid_offset_limit(self): - """ - Basic test, assumes offset and limit are given. - """ - offset = 10 - limit = 5 - count = len(self.queryset) - last_offset = (count // limit) * limit - next_offset = 15 - prev_offset = 5 - - request = self.get_test_request( - { - self.pagination.limit_query_param: limit, - self.pagination.offset_query_param: offset, - } - ) - base_url = replace_query_param( - self.base_url, self.pagination.limit_query_param, limit - ) - last_url = replace_query_param( - base_url, self.pagination.offset_query_param, last_offset - ) - first_url = base_url - next_url = replace_query_param( - base_url, self.pagination.offset_query_param, next_offset - ) - prev_url = replace_query_param( - base_url, self.pagination.offset_query_param, prev_offset - ) - queryset = self.paginate_queryset(request) - content = self.get_paginated_content(queryset) - next_offset = offset + limit - - expected_content = { - "results": list(range(offset + 1, next_offset + 1)), - "links": OrderedDict( - [ - ("first", first_url), - ("last", last_url), - ("next", next_url), - ("prev", prev_url), - ] - ), - "meta": { - "pagination": OrderedDict( - [ - ("count", count), - ("limit", limit), - ("offset", offset), - ] - ) - }, - } - - assert queryset == list(range(offset + 1, next_offset + 1)) - assert content == expected_content diff --git a/tests/test_pagination.py b/tests/test_pagination.py new file mode 100644 index 00000000..c09ac71c --- /dev/null +++ b/tests/test_pagination.py @@ -0,0 +1,55 @@ +from collections import OrderedDict + +from rest_framework.request import Request + +from rest_framework_json_api.pagination import JsonApiLimitOffsetPagination + + +class TestLimitOffsetPagination: + def test_get_paginated_response(self, rf): + pagination = JsonApiLimitOffsetPagination() + queryset = range(1, 101) + offset = 10 + limit = 5 + count = len(queryset) + + request = Request( + rf.get( + "/", + { + pagination.limit_query_param: limit, + pagination.offset_query_param: offset, + }, + ) + ) + queryset = list(pagination.paginate_queryset(queryset, request)) + content = pagination.get_paginated_response(queryset).data + + expected_content = { + "results": list(range(11, 16)), + "links": OrderedDict( + [ + ("first", "http://testserver/?page%5Blimit%5D=5"), + ( + "last", + "http://testserver/?page%5Blimit%5D=5&page%5Boffset%5D=100", + ), + ( + "next", + "http://testserver/?page%5Blimit%5D=5&page%5Boffset%5D=15", + ), + ("prev", "http://testserver/?page%5Blimit%5D=5&page%5Boffset%5D=5"), + ] + ), + "meta": { + "pagination": OrderedDict( + [ + ("count", count), + ("limit", limit), + ("offset", offset), + ] + ) + }, + } + + assert content == expected_content diff --git a/example/tests/unit/test_settings.py b/tests/test_settings.py similarity index 100% rename from example/tests/unit/test_settings.py rename to tests/test_settings.py