added page_size_kwarg tests

This commit is contained in:
Stephan Groß 2012-11-15 14:35:34 +01:00
parent 3ae203a018
commit a701a21587

View File

@ -34,6 +34,29 @@ if django_filters:
filter_backend = filters.DjangoFilterBackend
class DefaultPageSizeKwargView(generics.ListAPIView):
"""
View for testing default page_size usage
"""
model = BasicModel
class CustomPageSizeKwargView(generics.ListAPIView):
"""
View for testing custom page_size usage
"""
model = BasicModel
page_size_kwarg = 'ps'
class NonePageSizeKwargView(generics.ListAPIView):
"""
View for testing None page_size usage
"""
model = BasicModel
page_size_kwarg = None
class IntegrationTestPagination(TestCase):
"""
Integration tests for paginated list views.
@ -135,7 +158,7 @@ class IntegrationTestPaginationAndFiltering(TestCase):
class UnitTestPagination(TestCase):
"""
Unit tests for pagination of primative objects.
Unit tests for pagination of primitive objects.
"""
def setUp(self):
@ -156,3 +179,121 @@ class UnitTestPagination(TestCase):
self.assertEquals(serializer.data['next'], None)
self.assertEquals(serializer.data['previous'], '?page=2')
self.assertEquals(serializer.data['results'], self.objects[20:])
class TestDefaultPageSizeKwarg(TestCase):
"""
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 = [
{'id': obj.id, 'text': obj.text}
for obj in self.objects.all()
]
self.view = DefaultPageSizeKwargView.as_view()
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()
self.assertEquals(response.data, self.data)
def test_default_page_size_kwarg(self):
"""
If page_size_kwarg is set not set, the default page_size kwarg should limit per view requests.
"""
request = factory.get('/?page_size=5')
response = self.view(request).render()
self.assertEquals(response.data['count'], 13)
self.assertEquals(response.data['results'], self.data[:5])
class TestCustomPageSizeKwarg(TestCase):
"""
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 = [
{'id': obj.id, 'text': obj.text}
for obj in self.objects.all()
]
self.view = CustomPageSizeKwargView.as_view()
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()
self.assertEquals(response.data, self.data)
def test_disabled_default_page_size_kwarg(self):
"""
If page_size_kwarg is set set, the default page_size kwarg should not work.
"""
request = factory.get('/?page_size=5')
response = self.view(request).render()
self.assertEquals(response.data, self.data)
def test_custom_page_size_kwarg(self):
"""
If page_size_kwarg is set set, the new kwarg should limit per view requests.
"""
request = factory.get('/?ps=5')
response = self.view(request).render()
self.assertEquals(response.data['count'], 13)
self.assertEquals(response.data['results'], self.data[:5])
class TestNonePageSizeKwarg(TestCase):
"""
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 = [
{'id': obj.id, 'text': obj.text}
for obj in self.objects.all()
]
self.view = NonePageSizeKwargView.as_view()
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()
self.assertEquals(response.data, self.data)
def test_none_page_size_kwarg(self):
"""
If page_size_kwarg is set to None, custom page_size per request should be disabled.
"""
request = factory.get('/?page_size=5')
response = self.view(request).render()
self.assertEquals(response.data, self.data)