forked from django-json-api/django-rest-framework-json-api
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pagination.py
79 lines (64 loc) · 2.58 KB
/
test_pagination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
fromcollectionsimportOrderedDict
fromrest_framework.requestimportRequest
fromrest_framework.testimportAPIRequestFactory
fromrest_framework.utils.urlsimportreplace_query_param
fromrest_framework_json_api.paginationimportLimitOffsetPagination
factory=APIRequestFactory()
classTestLimitOffset:
"""
Unit tests for `pagination.LimitOffsetPagination`.
"""
defsetup(self):
classExamplePagination(LimitOffsetPagination):
default_limit=10
max_limit=15
self.pagination=ExamplePagination()
self.queryset=range(1, 101)
self.base_url='http://testserver/'
defpaginate_queryset(self, request):
returnlist(self.pagination.paginate_queryset(self.queryset, request))
defget_paginated_content(self, queryset):
response=self.pagination.get_paginated_response(queryset)
returnresponse.data
defget_test_request(self, arguments):
returnRequest(factory.get('/', arguments))
deftest_valid_offset_limit(self):
"""
Basic test, assumes offset and limit are given.
"""
offset=10
limit=5
count=len(self.queryset)
last_offset=count-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),
])
}
}
assertqueryset==list(range(offset+1, next_offset+1))
assertcontent==expected_content