2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-10-09 00:00:55 +04:00
|
|
|
import datetime
|
2012-10-11 15:55:16 +04:00
|
|
|
from decimal import Decimal
|
2012-10-01 18:49:19 +04:00
|
|
|
from django.core.paginator import Paginator
|
2012-09-30 20:31:28 +04:00
|
|
|
from django.test import TestCase
|
2012-11-08 01:07:24 +04:00
|
|
|
from django.utils import unittest
|
2012-12-13 20:57:27 +04:00
|
|
|
from rest_framework import generics, status, pagination, filters, serializers
|
2012-11-08 01:07:24 +04:00
|
|
|
from rest_framework.compat import django_filters
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2014-04-17 11:53:44 +04:00
|
|
|
from .models import BasicModel, FilterableItem
|
2012-09-30 20:31:28 +04:00
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2012-09-30 20:31:28 +04:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
# Helper function to split arguments out of an url
|
|
|
|
def split_arguments_from_url(url):
|
|
|
|
if '?' not in url:
|
|
|
|
return url
|
|
|
|
|
|
|
|
path, args = url.split('?')
|
|
|
|
args = dict(r.split('=') for r in args.split('&'))
|
|
|
|
return path, args
|
|
|
|
|
2012-09-30 20:31:28 +04:00
|
|
|
|
2012-10-03 12:26:15 +04:00
|
|
|
class RootView(generics.ListCreateAPIView):
|
2012-09-30 20:31:28 +04:00
|
|
|
"""
|
|
|
|
Example description for OPTIONS.
|
|
|
|
"""
|
|
|
|
model = BasicModel
|
|
|
|
paginate_by = 10
|
|
|
|
|
|
|
|
|
2012-11-15 17:35:34 +04:00
|
|
|
class DefaultPageSizeKwargView(generics.ListAPIView):
|
|
|
|
"""
|
2012-11-17 02:45:57 +04:00
|
|
|
View for testing default paginate_by_param usage
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
|
|
|
model = BasicModel
|
|
|
|
|
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
class PaginateByParamView(generics.ListAPIView):
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
2012-11-17 02:45:57 +04:00
|
|
|
View for testing custom paginate_by_param usage
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
|
|
|
model = BasicModel
|
2012-11-17 02:45:57 +04:00
|
|
|
paginate_by_param = 'page_size'
|
2012-11-15 17:35:34 +04:00
|
|
|
|
|
|
|
|
2013-08-26 20:05:36 +04:00
|
|
|
class MaxPaginateByView(generics.ListAPIView):
|
|
|
|
"""
|
|
|
|
View for testing custom max_paginate_by usage
|
|
|
|
"""
|
|
|
|
model = BasicModel
|
2013-08-28 15:52:38 +04:00
|
|
|
paginate_by = 3
|
|
|
|
max_paginate_by = 5
|
2013-08-26 20:05:36 +04:00
|
|
|
paginate_by_param = 'page_size'
|
|
|
|
|
|
|
|
|
2012-10-01 18:49:19 +04:00
|
|
|
class IntegrationTestPagination(TestCase):
|
|
|
|
"""
|
|
|
|
Integration tests for paginated list views.
|
|
|
|
"""
|
|
|
|
|
2012-09-30 20:31:28 +04:00
|
|
|
def setUp(self):
|
|
|
|
"""
|
2012-10-09 00:00:55 +04:00
|
|
|
Create 26 BasicModel instances.
|
2012-09-30 20:31:28 +04:00
|
|
|
"""
|
|
|
|
for char in 'abcdefghijklmnopqrstuvwxyz':
|
|
|
|
BasicModel(text=char * 3).save()
|
|
|
|
self.objects = BasicModel.objects
|
|
|
|
self.data = [
|
|
|
|
{'id': obj.id, 'text': obj.text}
|
|
|
|
for obj in self.objects.all()
|
|
|
|
]
|
|
|
|
self.view = RootView.as_view()
|
|
|
|
|
|
|
|
def test_get_paginated_root_view(self):
|
|
|
|
"""
|
2012-10-03 12:26:15 +04:00
|
|
|
GET requests to paginated ListCreateAPIView should return paginated results.
|
2012-09-30 20:31:28 +04:00
|
|
|
"""
|
|
|
|
request = factory.get('/')
|
2013-03-13 15:42:12 +04:00
|
|
|
# Note: Database queries are a `SELECT COUNT`, and `SELECT <fields>`
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
response = self.view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 26)
|
|
|
|
self.assertEqual(response.data['results'], self.data[:10])
|
|
|
|
self.assertNotEqual(response.data['next'], None)
|
|
|
|
self.assertEqual(response.data['previous'], None)
|
2012-09-30 20:31:28 +04:00
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get(*split_arguments_from_url(response.data['next']))
|
2013-03-13 15:42:12 +04:00
|
|
|
with self.assertNumQueries(2):
|
|
|
|
response = self.view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 26)
|
|
|
|
self.assertEqual(response.data['results'], self.data[10:20])
|
|
|
|
self.assertNotEqual(response.data['next'], None)
|
|
|
|
self.assertNotEqual(response.data['previous'], None)
|
2012-09-30 20:31:28 +04:00
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get(*split_arguments_from_url(response.data['next']))
|
2013-03-13 15:42:12 +04:00
|
|
|
with self.assertNumQueries(2):
|
|
|
|
response = self.view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 26)
|
|
|
|
self.assertEqual(response.data['results'], self.data[20:])
|
|
|
|
self.assertEqual(response.data['next'], None)
|
|
|
|
self.assertNotEqual(response.data['previous'], None)
|
2012-10-01 18:49:19 +04:00
|
|
|
|
2012-10-11 13:32:51 +04:00
|
|
|
|
2012-10-09 00:00:55 +04:00
|
|
|
class IntegrationTestPaginationAndFiltering(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create 50 FilterableItem instances.
|
|
|
|
"""
|
2012-10-11 16:19:29 +04:00
|
|
|
base_data = ('a', Decimal('0.25'), datetime.date(2012, 10, 8))
|
2012-10-09 00:00:55 +04:00
|
|
|
for i in range(26):
|
|
|
|
text = chr(i + ord(base_data[0])) * 3 # Produces string 'aaa', 'bbb', etc.
|
|
|
|
decimal = base_data[1] + i
|
|
|
|
date = base_data[2] - datetime.timedelta(days=i * 2)
|
|
|
|
FilterableItem(text=text, decimal=decimal, date=date).save()
|
|
|
|
|
|
|
|
self.objects = FilterableItem.objects
|
|
|
|
self.data = [
|
2013-03-21 12:41:54 +04:00
|
|
|
{'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date}
|
2013-03-01 16:16:00 +04:00
|
|
|
for obj in self.objects.all()
|
2012-10-09 00:00:55 +04:00
|
|
|
]
|
|
|
|
|
2013-09-13 14:55:16 +04:00
|
|
|
@unittest.skipUnless(django_filters, 'django-filter not installed')
|
2013-03-13 16:45:54 +04:00
|
|
|
def test_get_django_filter_paginated_filtered_root_view(self):
|
2012-10-09 00:00:55 +04:00
|
|
|
"""
|
|
|
|
GET requests to paginated filtered ListCreateAPIView should return
|
|
|
|
paginated results. The next and previous links should preserve the
|
|
|
|
filtered parameters.
|
|
|
|
"""
|
2013-03-13 16:45:54 +04:00
|
|
|
class DecimalFilter(django_filters.FilterSet):
|
|
|
|
decimal = django_filters.NumberFilter(lookup_type='lt')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = FilterableItem
|
|
|
|
fields = ['text', 'decimal', 'date']
|
|
|
|
|
|
|
|
class FilterFieldsRootView(generics.ListCreateAPIView):
|
|
|
|
model = FilterableItem
|
|
|
|
paginate_by = 10
|
|
|
|
filter_class = DecimalFilter
|
2013-05-18 00:57:11 +04:00
|
|
|
filter_backends = (filters.DjangoFilterBackend,)
|
2013-03-13 16:45:54 +04:00
|
|
|
|
|
|
|
view = FilterFieldsRootView.as_view()
|
|
|
|
|
|
|
|
EXPECTED_NUM_QUERIES = 2
|
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get('/', {'decimal': '15.20'})
|
2013-03-13 16:45:54 +04:00
|
|
|
with self.assertNumQueries(EXPECTED_NUM_QUERIES):
|
|
|
|
response = view(request).render()
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 15)
|
|
|
|
self.assertEqual(response.data['results'], self.data[:10])
|
|
|
|
self.assertNotEqual(response.data['next'], None)
|
|
|
|
self.assertEqual(response.data['previous'], None)
|
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get(*split_arguments_from_url(response.data['next']))
|
2013-03-13 16:45:54 +04:00
|
|
|
with self.assertNumQueries(EXPECTED_NUM_QUERIES):
|
|
|
|
response = view(request).render()
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 15)
|
|
|
|
self.assertEqual(response.data['results'], self.data[10:15])
|
|
|
|
self.assertEqual(response.data['next'], None)
|
|
|
|
self.assertNotEqual(response.data['previous'], None)
|
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get(*split_arguments_from_url(response.data['previous']))
|
2013-03-13 16:45:54 +04:00
|
|
|
with self.assertNumQueries(EXPECTED_NUM_QUERIES):
|
|
|
|
response = view(request).render()
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 15)
|
|
|
|
self.assertEqual(response.data['results'], self.data[:10])
|
|
|
|
self.assertNotEqual(response.data['next'], None)
|
|
|
|
self.assertEqual(response.data['previous'], None)
|
|
|
|
|
|
|
|
def test_get_basic_paginated_filtered_root_view(self):
|
|
|
|
"""
|
|
|
|
Same as `test_get_django_filter_paginated_filtered_root_view`,
|
|
|
|
except using a custom filter backend instead of the django-filter
|
|
|
|
backend,
|
|
|
|
"""
|
|
|
|
|
|
|
|
class DecimalFilterBackend(filters.BaseFilterBackend):
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
return queryset.filter(decimal__lt=Decimal(request.GET['decimal']))
|
|
|
|
|
|
|
|
class BasicFilterFieldsRootView(generics.ListCreateAPIView):
|
|
|
|
model = FilterableItem
|
|
|
|
paginate_by = 10
|
2013-05-18 00:57:11 +04:00
|
|
|
filter_backends = (DecimalFilterBackend,)
|
2013-03-13 16:45:54 +04:00
|
|
|
|
|
|
|
view = BasicFilterFieldsRootView.as_view()
|
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get('/', {'decimal': '15.20'})
|
2013-03-13 16:45:54 +04:00
|
|
|
with self.assertNumQueries(2):
|
|
|
|
response = view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 15)
|
|
|
|
self.assertEqual(response.data['results'], self.data[:10])
|
|
|
|
self.assertNotEqual(response.data['next'], None)
|
|
|
|
self.assertEqual(response.data['previous'], None)
|
2012-10-09 00:00:55 +04:00
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get(*split_arguments_from_url(response.data['next']))
|
2013-03-13 16:45:54 +04:00
|
|
|
with self.assertNumQueries(2):
|
|
|
|
response = view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 15)
|
|
|
|
self.assertEqual(response.data['results'], self.data[10:15])
|
|
|
|
self.assertEqual(response.data['next'], None)
|
|
|
|
self.assertNotEqual(response.data['previous'], None)
|
2012-10-09 00:00:55 +04:00
|
|
|
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get(*split_arguments_from_url(response.data['previous']))
|
2013-03-13 16:45:54 +04:00
|
|
|
with self.assertNumQueries(2):
|
|
|
|
response = view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['count'], 15)
|
|
|
|
self.assertEqual(response.data['results'], self.data[:10])
|
|
|
|
self.assertNotEqual(response.data['next'], None)
|
|
|
|
self.assertEqual(response.data['previous'], None)
|
2012-10-09 00:00:55 +04:00
|
|
|
|
2012-10-01 18:49:19 +04:00
|
|
|
|
2012-12-13 20:57:27 +04:00
|
|
|
class PassOnContextPaginationSerializer(pagination.PaginationSerializer):
|
|
|
|
class Meta:
|
|
|
|
object_serializer_class = serializers.Serializer
|
|
|
|
|
|
|
|
|
2012-10-01 18:49:19 +04:00
|
|
|
class UnitTestPagination(TestCase):
|
|
|
|
"""
|
2012-11-15 17:35:34 +04:00
|
|
|
Unit tests for pagination of primitive objects.
|
2012-10-01 18:49:19 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.objects = [char * 3 for char in 'abcdefghijklmnopqrstuvwxyz']
|
|
|
|
paginator = Paginator(self.objects, 10)
|
|
|
|
self.first_page = paginator.page(1)
|
|
|
|
self.last_page = paginator.page(3)
|
|
|
|
|
|
|
|
def test_native_pagination(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = pagination.PaginationSerializer(self.first_page)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data['count'], 26)
|
|
|
|
self.assertEqual(serializer.data['next'], '?page=2')
|
|
|
|
self.assertEqual(serializer.data['previous'], None)
|
|
|
|
self.assertEqual(serializer.data['results'], self.objects[:10])
|
2012-10-01 18:49:19 +04:00
|
|
|
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = pagination.PaginationSerializer(self.last_page)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data['count'], 26)
|
|
|
|
self.assertEqual(serializer.data['next'], None)
|
|
|
|
self.assertEqual(serializer.data['previous'], '?page=2')
|
|
|
|
self.assertEqual(serializer.data['results'], self.objects[20:])
|
2012-11-15 17:35:34 +04:00
|
|
|
|
2012-12-13 20:57:27 +04:00
|
|
|
def test_context_available_in_result(self):
|
2012-12-14 23:59:21 +04:00
|
|
|
"""
|
|
|
|
Ensure context gets passed through to the object serializer.
|
|
|
|
"""
|
2013-01-08 01:37:44 +04:00
|
|
|
serializer = PassOnContextPaginationSerializer(self.first_page, context={'foo': 'bar'})
|
2012-12-14 23:59:21 +04:00
|
|
|
serializer.data
|
2012-12-13 20:57:27 +04:00
|
|
|
results = serializer.fields[serializer.results_field]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.context, results.context)
|
2012-12-13 20:57:27 +04:00
|
|
|
|
2012-11-15 17:35:34 +04:00
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
class TestUnpaginated(TestCase):
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
2012-11-17 02:45:57 +04:00
|
|
|
Tests for list views without pagination.
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create 13 BasicModel instances.
|
|
|
|
"""
|
|
|
|
for i in range(13):
|
|
|
|
BasicModel(text=i).save()
|
|
|
|
self.objects = BasicModel.objects
|
|
|
|
self.data = [
|
2014-08-19 16:28:07 +04:00
|
|
|
{'id': obj.id, 'text': obj.text}
|
|
|
|
for obj in self.objects.all()
|
2012-11-15 17:35:34 +04:00
|
|
|
]
|
|
|
|
self.view = DefaultPageSizeKwargView.as_view()
|
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
def test_unpaginated(self):
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
|
|
|
Tests the default page size for this view.
|
|
|
|
no page size --> no limit --> no meta data
|
|
|
|
"""
|
|
|
|
request = factory.get('/')
|
2012-11-17 02:45:57 +04:00
|
|
|
response = self.view(request)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.data, self.data)
|
2012-11-15 17:35:34 +04:00
|
|
|
|
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
class TestCustomPaginateByParam(TestCase):
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
|
|
|
Tests for list views with default page size kwarg
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create 13 BasicModel instances.
|
|
|
|
"""
|
|
|
|
for i in range(13):
|
|
|
|
BasicModel(text=i).save()
|
|
|
|
self.objects = BasicModel.objects
|
|
|
|
self.data = [
|
2014-08-19 16:28:07 +04:00
|
|
|
{'id': obj.id, 'text': obj.text}
|
|
|
|
for obj in self.objects.all()
|
2012-11-15 17:35:34 +04:00
|
|
|
]
|
2012-11-17 02:45:57 +04:00
|
|
|
self.view = PaginateByParamView.as_view()
|
2012-11-15 17:35:34 +04:00
|
|
|
|
|
|
|
def test_default_page_size(self):
|
|
|
|
"""
|
|
|
|
Tests the default page size for this view.
|
|
|
|
no page size --> no limit --> no meta data
|
|
|
|
"""
|
|
|
|
request = factory.get('/')
|
|
|
|
response = self.view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.data, self.data)
|
2012-11-15 17:35:34 +04:00
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
def test_paginate_by_param(self):
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
2012-11-17 02:45:57 +04:00
|
|
|
If paginate_by_param is set, the new kwarg should limit per view requests.
|
2012-11-15 17:35:34 +04:00
|
|
|
"""
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get('/', {'page_size': 5})
|
2012-11-15 17:35:34 +04:00
|
|
|
response = self.view(request).render()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.data['count'], 13)
|
|
|
|
self.assertEqual(response.data['results'], self.data[:5])
|
2012-12-13 15:07:56 +04:00
|
|
|
|
|
|
|
|
2013-08-26 20:05:36 +04:00
|
|
|
class TestMaxPaginateByParam(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for list views with max_paginate_by kwarg
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create 13 BasicModel instances.
|
|
|
|
"""
|
|
|
|
for i in range(13):
|
|
|
|
BasicModel(text=i).save()
|
|
|
|
self.objects = BasicModel.objects
|
|
|
|
self.data = [
|
|
|
|
{'id': obj.id, 'text': obj.text}
|
|
|
|
for obj in self.objects.all()
|
|
|
|
]
|
|
|
|
self.view = MaxPaginateByView.as_view()
|
|
|
|
|
|
|
|
def test_max_paginate_by(self):
|
|
|
|
"""
|
2013-08-28 15:52:38 +04:00
|
|
|
If max_paginate_by is set, it should limit page size for the view.
|
2013-08-26 20:05:36 +04:00
|
|
|
"""
|
2014-01-30 17:27:09 +04:00
|
|
|
request = factory.get('/', data={'page_size': 10})
|
2013-08-26 20:05:36 +04:00
|
|
|
response = self.view(request).render()
|
|
|
|
self.assertEqual(response.data['count'], 13)
|
2013-08-28 15:52:38 +04:00
|
|
|
self.assertEqual(response.data['results'], self.data[:5])
|
2013-08-26 20:05:36 +04:00
|
|
|
|
|
|
|
def test_max_paginate_by_without_page_size_param(self):
|
|
|
|
"""
|
2013-08-28 15:52:38 +04:00
|
|
|
If max_paginate_by is set, but client does not specifiy page_size,
|
|
|
|
standard `paginate_by` behavior should be used.
|
2013-08-26 20:05:36 +04:00
|
|
|
"""
|
|
|
|
request = factory.get('/')
|
|
|
|
response = self.view(request).render()
|
|
|
|
self.assertEqual(response.data['results'], self.data[:3])
|
|
|
|
|
|
|
|
|
2014-08-19 16:54:52 +04:00
|
|
|
# Tests for context in pagination serializers
|
2013-01-27 00:55:09 +04:00
|
|
|
|
2012-12-13 15:07:56 +04:00
|
|
|
class CustomField(serializers.Field):
|
|
|
|
def to_native(self, value):
|
2014-08-19 16:54:52 +04:00
|
|
|
if 'view' not in self.context:
|
2012-12-13 15:07:56 +04:00
|
|
|
raise RuntimeError("context isn't getting passed into custom field")
|
|
|
|
return "value"
|
|
|
|
|
|
|
|
|
|
|
|
class BasicModelSerializer(serializers.Serializer):
|
|
|
|
text = CustomField()
|
|
|
|
|
2013-01-24 12:58:19 +04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(BasicModelSerializer, self).__init__(*args, **kwargs)
|
2014-08-19 16:54:52 +04:00
|
|
|
if 'view' not in self.context:
|
2013-01-24 12:58:19 +04:00
|
|
|
raise RuntimeError("context isn't getting passed into serializer init")
|
|
|
|
|
2012-12-13 15:07:56 +04:00
|
|
|
|
|
|
|
class TestContextPassedToCustomField(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
BasicModel.objects.create(text='ala ma kota')
|
|
|
|
|
|
|
|
def test_with_pagination(self):
|
|
|
|
class ListView(generics.ListCreateAPIView):
|
|
|
|
model = BasicModel
|
|
|
|
serializer_class = BasicModelSerializer
|
|
|
|
paginate_by = 1
|
|
|
|
|
|
|
|
self.view = ListView.as_view()
|
|
|
|
request = factory.get('/')
|
|
|
|
response = self.view(request).render()
|
|
|
|
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2013-01-27 00:55:09 +04:00
|
|
|
|
|
|
|
|
2014-08-19 16:54:52 +04:00
|
|
|
# Tests for custom pagination serializers
|
2013-01-27 00:55:09 +04:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
2014-09-03 18:49:32 +04:00
|
|
|
class CustomFooSerializer(serializers.Serializer):
|
|
|
|
foo = serializers.CharField()
|
|
|
|
|
|
|
|
|
|
|
|
class CustomFooPaginationSerializer(pagination.PaginationSerializer):
|
|
|
|
class Meta:
|
|
|
|
object_serializer_class = CustomFooSerializer
|
|
|
|
|
|
|
|
|
2013-01-27 00:55:09 +04:00
|
|
|
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):
|
2013-06-28 20:17:39 +04:00
|
|
|
request = APIRequestFactory().get('/foobar')
|
2013-01-27 00:55:09 +04:00
|
|
|
serializer = CustomPaginationSerializer(
|
|
|
|
instance=self.page,
|
|
|
|
context={'request': request}
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
'links': {
|
|
|
|
'next': 'http://testserver/foobar?page=2',
|
|
|
|
'prev': None
|
|
|
|
},
|
|
|
|
'total_results': 4,
|
|
|
|
'objects': ['john', 'paul']
|
|
|
|
}
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2013-10-20 07:43:23 +04:00
|
|
|
|
2014-09-03 18:49:32 +04:00
|
|
|
def test_custom_pagination_serializer_with_custom_object_serializer(self):
|
|
|
|
objects = [
|
|
|
|
{'foo': 'bar'},
|
|
|
|
{'foo': 'spam'}
|
|
|
|
]
|
|
|
|
paginator = Paginator(objects, 1)
|
|
|
|
page = paginator.page(1)
|
|
|
|
serializer = CustomFooPaginationSerializer(page)
|
|
|
|
serializer.data
|
|
|
|
|
2013-10-20 07:43:23 +04:00
|
|
|
|
|
|
|
class NonIntegerPage(object):
|
|
|
|
|
|
|
|
def __init__(self, paginator, object_list, prev_token, token, next_token):
|
|
|
|
self.paginator = paginator
|
|
|
|
self.object_list = object_list
|
|
|
|
self.prev_token = prev_token
|
|
|
|
self.token = token
|
|
|
|
self.next_token = next_token
|
|
|
|
|
|
|
|
def has_next(self):
|
|
|
|
return not not self.next_token
|
|
|
|
|
|
|
|
def next_page_number(self):
|
|
|
|
return self.next_token
|
|
|
|
|
|
|
|
def has_previous(self):
|
|
|
|
return not not self.prev_token
|
|
|
|
|
|
|
|
def previous_page_number(self):
|
|
|
|
return self.prev_token
|
|
|
|
|
|
|
|
|
|
|
|
class NonIntegerPaginator(object):
|
|
|
|
|
|
|
|
def __init__(self, object_list, per_page):
|
|
|
|
self.object_list = object_list
|
|
|
|
self.per_page = per_page
|
|
|
|
|
|
|
|
def count(self):
|
|
|
|
# pretend like we don't know how many pages we have
|
|
|
|
return None
|
|
|
|
|
|
|
|
def page(self, token=None):
|
|
|
|
if token:
|
|
|
|
try:
|
|
|
|
first = self.object_list.index(token)
|
|
|
|
except ValueError:
|
|
|
|
first = 0
|
|
|
|
else:
|
|
|
|
first = 0
|
|
|
|
n = len(self.object_list)
|
|
|
|
last = min(first + self.per_page, n)
|
|
|
|
prev_token = self.object_list[last - (2 * self.per_page)] if first else None
|
|
|
|
next_token = self.object_list[last] if last < n else None
|
|
|
|
return NonIntegerPage(self, self.object_list[first:last], prev_token, token, next_token)
|
|
|
|
|
|
|
|
|
|
|
|
class TestNonIntegerPagination(TestCase):
|
|
|
|
def test_custom_pagination_serializer(self):
|
|
|
|
objects = ['john', 'paul', 'george', 'ringo']
|
|
|
|
paginator = NonIntegerPaginator(objects, 2)
|
|
|
|
|
|
|
|
request = APIRequestFactory().get('/foobar')
|
|
|
|
serializer = CustomPaginationSerializer(
|
|
|
|
instance=paginator.page(),
|
|
|
|
context={'request': request}
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
'links': {
|
|
|
|
'next': 'http://testserver/foobar?page={0}'.format(objects[2]),
|
|
|
|
'prev': None
|
|
|
|
},
|
|
|
|
'total_results': None,
|
|
|
|
'objects': objects[:2]
|
|
|
|
}
|
|
|
|
self.assertEqual(serializer.data, expected)
|
|
|
|
|
|
|
|
request = APIRequestFactory().get('/foobar')
|
|
|
|
serializer = CustomPaginationSerializer(
|
|
|
|
instance=paginator.page('george'),
|
|
|
|
context={'request': request}
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
'links': {
|
|
|
|
'next': None,
|
|
|
|
'prev': 'http://testserver/foobar?page={0}'.format(objects[0]),
|
|
|
|
},
|
|
|
|
'total_results': None,
|
|
|
|
'objects': objects[2:]
|
|
|
|
}
|
|
|
|
self.assertEqual(serializer.data, expected)
|