| 
 | 1 | +from collections import OrderedDict  | 
 | 2 | + | 
 | 3 | +from rest_framework.request import Request  | 
 | 4 | +from rest_framework.test import APIRequestFactory  | 
 | 5 | +from rest_framework.utils.urls import replace_query_param  | 
 | 6 | + | 
 | 7 | +from rest_framework_json_api.pagination import LimitOffsetPagination  | 
 | 8 | + | 
 | 9 | + | 
 | 10 | +factory = APIRequestFactory()  | 
 | 11 | + | 
 | 12 | + | 
 | 13 | +class TestLimitOffset:  | 
 | 14 | +    """  | 
 | 15 | +    Unit tests for `pagination.LimitOffsetPagination`.  | 
 | 16 | +    """  | 
 | 17 | + | 
 | 18 | +    def setup(self):  | 
 | 19 | +        class ExamplePagination(LimitOffsetPagination):  | 
 | 20 | +            default_limit = 10  | 
 | 21 | +            max_limit = 15  | 
 | 22 | + | 
 | 23 | +        self.pagination = ExamplePagination()  | 
 | 24 | +        self.queryset = range(1, 101)  | 
 | 25 | +        self.base_url = 'http://testserver/'  | 
 | 26 | + | 
 | 27 | +    def paginate_queryset(self, request):  | 
 | 28 | +        return list(self.pagination.paginate_queryset(self.queryset, request))  | 
 | 29 | + | 
 | 30 | +    def get_paginated_content(self, queryset):  | 
 | 31 | +        response = self.pagination.get_paginated_response(queryset)  | 
 | 32 | +        return response.data  | 
 | 33 | + | 
 | 34 | +    def get_test_request(self, arguments):  | 
 | 35 | +        return Request(factory.get('/', arguments))  | 
 | 36 | + | 
 | 37 | +    def test_valid_offset_limit(self):  | 
 | 38 | +        """  | 
 | 39 | +        Basic test, assumes offset and limit are given.  | 
 | 40 | +        """  | 
 | 41 | +        offset = 10  | 
 | 42 | +        limit = 5  | 
 | 43 | +        count = len(self.queryset)  | 
 | 44 | +        last_offset = count - limit  | 
 | 45 | +        next_offset = 15  | 
 | 46 | +        prev_offset = 5  | 
 | 47 | + | 
 | 48 | +        request = self.get_test_request({  | 
 | 49 | +            self.pagination.limit_query_param: limit,  | 
 | 50 | +            self.pagination.offset_query_param: offset  | 
 | 51 | +        })  | 
 | 52 | +        base_url = replace_query_param(self.base_url, self.pagination.limit_query_param, limit)  | 
 | 53 | +        last_url = replace_query_param(base_url, self.pagination.offset_query_param, last_offset)  | 
 | 54 | +        first_url = base_url  | 
 | 55 | +        next_url = replace_query_param(base_url, self.pagination.offset_query_param, next_offset)  | 
 | 56 | +        prev_url = replace_query_param(base_url, self.pagination.offset_query_param, prev_offset)  | 
 | 57 | +        queryset = self.paginate_queryset(request)  | 
 | 58 | +        content = self.get_paginated_content(queryset)  | 
 | 59 | +        next_offset = offset + limit  | 
 | 60 | + | 
 | 61 | +        expected_content = {  | 
 | 62 | +            'results': list(range(offset + 1, next_offset + 1)),  | 
 | 63 | +            'links': OrderedDict([  | 
 | 64 | +                ('first', first_url),  | 
 | 65 | +                ('last', last_url),  | 
 | 66 | +                ('next', next_url),  | 
 | 67 | +                ('prev', prev_url),  | 
 | 68 | +            ]),  | 
 | 69 | +            'meta': {  | 
 | 70 | +                'pagination': OrderedDict([  | 
 | 71 | +                    ('count', count),  | 
 | 72 | +                    ('limit', limit),  | 
 | 73 | +                    ('offset', offset),  | 
 | 74 | +                ])  | 
 | 75 | +            }  | 
 | 76 | +        }  | 
 | 77 | + | 
 | 78 | +        assert queryset == list(range(offset + 1, next_offset + 1))  | 
 | 79 | +        assert content == expected_content  | 
0 commit comments