mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Test for custom pagination serializers. Also refs #604.
This commit is contained in:
parent
a51bca32fd
commit
4d43e9f7de
|
@ -252,6 +252,8 @@ class TestCustomPaginateByParam(TestCase):
|
|||
self.assertEquals(response.data['results'], self.data[:5])
|
||||
|
||||
|
||||
### Tests for context in pagination serializers
|
||||
|
||||
class CustomField(serializers.Field):
|
||||
def to_native(self, value):
|
||||
if not 'view' in self.context:
|
||||
|
@ -283,3 +285,40 @@ class TestContextPassedToCustomField(TestCase):
|
|||
response = self.view(request).render()
|
||||
|
||||
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
|
||||
### Tests for custom pagination serializers
|
||||
|
||||
class LinksSerializer(serializers.Serializer):
|
||||
next = pagination.NextPageField(source='*')
|
||||
prev = pagination.PreviousPageField(source='*')
|
||||
|
||||
|
||||
class CustomPaginationSerializer(pagination.BasePaginationSerializer):
|
||||
links = LinksSerializer(source='*') # Takes the page object as the source
|
||||
total_results = serializers.Field(source='paginator.count')
|
||||
|
||||
results_field = 'objects'
|
||||
|
||||
|
||||
class TestCustomPaginationSerializer(TestCase):
|
||||
def setUp(self):
|
||||
objects = ['john', 'paul', 'george', 'ringo']
|
||||
paginator = Paginator(objects, 2)
|
||||
self.page = paginator.page(1)
|
||||
|
||||
def test_custom_pagination_serializer(self):
|
||||
request = RequestFactory().get('/foobar')
|
||||
serializer = CustomPaginationSerializer(
|
||||
instance=self.page,
|
||||
context={'request': request}
|
||||
)
|
||||
expected = {
|
||||
'links': {
|
||||
'next': 'http://testserver/foobar?page=2',
|
||||
'prev': None
|
||||
},
|
||||
'total_results': 4,
|
||||
'objects': ['john', 'paul']
|
||||
}
|
||||
self.assertEquals(serializer.data, expected)
|
||||
|
|
Loading…
Reference in New Issue
Block a user