Permalink
Browse files

Merged pagination tests

It is simpler/cleaner to have all pagination tests in a single file.
Refs #16122.
  • Loading branch information...
1 parent cafb266 commit 45802e12481e86f7b9c1f392a54b9d25671214d9 @claudep claudep committed Nov 8, 2012
View
0 tests/modeltests/pagination/__init__.py
No changes.
View
134 tests/modeltests/pagination/tests.py
@@ -1,134 +0,0 @@
-from __future__ import absolute_import, unicode_literals
-
-from datetime import datetime
-
-from django.core.paginator import Paginator, InvalidPage, EmptyPage
-from django.test import TestCase
-from django.utils import six
-
-from .models import Article
-
-
-class CountContainer(object):
- def count(self):
- return 42
-
-class LenContainer(object):
- def __len__(self):
- return 42
-
-class PaginationTests(TestCase):
- def setUp(self):
- # Prepare a list of objects for pagination.
- for x in range(1, 10):
- a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
- a.save()
-
- def test_paginator(self):
- paginator = Paginator(Article.objects.all(), 5)
- self.assertEqual(9, paginator.count)
- self.assertEqual(2, paginator.num_pages)
- self.assertEqual([1, 2], list(paginator.page_range))
-
- def test_first_page(self):
- paginator = Paginator(Article.objects.all(), 5)
- p = paginator.page(1)
- self.assertEqual("<Page 1 of 2>", six.text_type(p))
- self.assertQuerysetEqual(p.object_list, [
- "<Article: Article 1>",
- "<Article: Article 2>",
- "<Article: Article 3>",
- "<Article: Article 4>",
- "<Article: Article 5>"
- ]
- )
- self.assertTrue(p.has_next())
- self.assertFalse(p.has_previous())
- self.assertTrue(p.has_other_pages())
- self.assertEqual(2, p.next_page_number())
- self.assertRaises(InvalidPage, p.previous_page_number)
- self.assertEqual(1, p.start_index())
- self.assertEqual(5, p.end_index())
-
- def test_last_page(self):
- paginator = Paginator(Article.objects.all(), 5)
- p = paginator.page(2)
- self.assertEqual("<Page 2 of 2>", six.text_type(p))
- self.assertQuerysetEqual(p.object_list, [
- "<Article: Article 6>",
- "<Article: Article 7>",
- "<Article: Article 8>",
- "<Article: Article 9>"
- ]
- )
- self.assertFalse(p.has_next())
- self.assertTrue(p.has_previous())
- self.assertTrue(p.has_other_pages())
- self.assertRaises(InvalidPage, p.next_page_number)
- self.assertEqual(1, p.previous_page_number())
- self.assertEqual(6, p.start_index())
- self.assertEqual(9, p.end_index())
-
- def test_empty_page(self):
- paginator = Paginator(Article.objects.all(), 5)
- self.assertRaises(EmptyPage, paginator.page, 0)
- self.assertRaises(EmptyPage, paginator.page, 3)
-
- # Empty paginators with allow_empty_first_page=True.
- paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True)
- self.assertEqual(0, paginator.count)
- self.assertEqual(1, paginator.num_pages)
- self.assertEqual([1], list(paginator.page_range))
-
- # Empty paginators with allow_empty_first_page=False.
- paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False)
- self.assertEqual(0, paginator.count)
- self.assertEqual(0, paginator.num_pages)
- self.assertEqual([], list(paginator.page_range))
-
- def test_invalid_page(self):
- paginator = Paginator(Article.objects.all(), 5)
- self.assertRaises(InvalidPage, paginator.page, 7)
-
- def test_orphans(self):
- # Add a few more records to test out the orphans feature.
- for x in range(10, 13):
- Article(headline="Article %s" % x, pub_date=datetime(2006, 10, 6)).save()
-
- # With orphans set to 3 and 10 items per page, we should get all 12 items on a single page.
- paginator = Paginator(Article.objects.all(), 10, orphans=3)
- self.assertEqual(1, paginator.num_pages)
-
- # With orphans only set to 1, we should get two pages.
- paginator = Paginator(Article.objects.all(), 10, orphans=1)
- self.assertEqual(2, paginator.num_pages)
-
- def test_paginate_list(self):
- # Paginators work with regular lists/tuples, too -- not just with QuerySets.
- paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)
- self.assertEqual(9, paginator.count)
- self.assertEqual(3, paginator.num_pages)
- self.assertEqual([1, 2, 3], list(paginator.page_range))
- p = paginator.page(2)
- self.assertEqual("<Page 2 of 3>", six.text_type(p))
- self.assertEqual([4, 5, 6], p.object_list)
- self.assertTrue(p.has_next())
- self.assertTrue(p.has_previous())
- self.assertTrue(p.has_other_pages())
- self.assertEqual(3, p.next_page_number())
- self.assertEqual(1, p.previous_page_number())
- self.assertEqual(4, p.start_index())
- self.assertEqual(6, p.end_index())
-
- def test_paginate_misc_classes(self):
- # Paginator can be passed other objects with a count() method.
- paginator = Paginator(CountContainer(), 10)
- self.assertEqual(42, paginator.count)
- self.assertEqual(5, paginator.num_pages)
- self.assertEqual([1, 2, 3, 4, 5], list(paginator.page_range))
-
- # Paginator can be passed other objects that implement __len__.
- paginator = Paginator(LenContainer(), 10)
- self.assertEqual(42, paginator.count)
- self.assertEqual(5, paginator.num_pages)
- self.assertEqual([1, 2, 3, 4, 5], list(paginator.page_range))
View
0 ...ssiontests/pagination_regress/__init__.py → tests/regressiontests/pagination/__init__.py
File renamed without changes.
View
8 tests/modeltests/pagination/models.py → tests/regressiontests/pagination/models.py
@@ -1,11 +1,3 @@
-"""
-30. Object pagination
-
-Django provides a framework for paginating a list of objects in a few lines
-of code. This is often useful for dividing search results or long lists of
-objects into easily readable pages.
-"""
-
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
View
108 ...gressiontests/pagination_regress/tests.py → tests/regressiontests/pagination/tests.py
@@ -1,9 +1,17 @@
-from __future__ import unicode_literals
+from __future__ import absolute_import, unicode_literals
-from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
-from django.utils.unittest import TestCase
+from datetime import datetime
-class PaginatorTests(TestCase):
+from django.core.paginator import (Paginator, EmptyPage, InvalidPage,
+ PageNotAnInteger)
+from django.test import TestCase
+from django.utils import six
+from django.utils import unittest
+
+from .models import Article
+
+
+class PaginationTests(unittest.TestCase):
"""
Tests for the Paginator and Page classes.
"""
@@ -31,15 +39,6 @@ def check_attribute(self, name, paginator, expected, params, coerce=None):
"For '%s', expected %s but got %s. Paginator parameters were: %s"
% (name, expected, got, params))
- def test_invalid_page_number(self):
- """
- Tests that invalid page numbers result in the correct exception being
- raised.
- """
- paginator = Paginator([1, 2, 3], 2)
- self.assertRaises(PageNotAnInteger, paginator.validate_number, None)
- self.assertRaises(PageNotAnInteger, paginator.validate_number, 'x')
-
def test_paginator(self):
"""
Tests the paginator attributes using varying inputs.
@@ -107,6 +106,38 @@ def test_paginator(self):
for params, output in tests:
self.check_paginator(params, output)
+ def test_invalid_page_number(self):
+ """
+ Tests that invalid page numbers result in the correct exception being
+ raised.
+ """
+ paginator = Paginator([1, 2, 3], 2)
+ self.assertRaises(InvalidPage, paginator.page, 3)
+ self.assertRaises(PageNotAnInteger, paginator.validate_number, None)
+ self.assertRaises(PageNotAnInteger, paginator.validate_number, 'x')
+ # With no content and allow_empty_first_page=True, 1 is a valid page number
+ paginator = Paginator([], 2)
+ self.assertEqual(paginator.validate_number(1), 1)
+
+ def test_paginate_misc_classes(self):
+ class CountContainer(object):
+ def count(self):
+ return 42
+ # Paginator can be passed other objects with a count() method.
+ paginator = Paginator(CountContainer(), 10)
+ self.assertEqual(42, paginator.count)
+ self.assertEqual(5, paginator.num_pages)
+ self.assertEqual([1, 2, 3, 4, 5], list(paginator.page_range))
+
+ # Paginator can be passed other objects that implement __len__.
+ class LenContainer(object):
+ def __len__(self):
+ return 42
+ paginator = Paginator(LenContainer(), 10)
+ self.assertEqual(42, paginator.count)
+ self.assertEqual(5, paginator.num_pages)
+ self.assertEqual([1, 2, 3, 4, 5], list(paginator.page_range))
+
def check_indexes(self, params, page_num, indexes):
"""
Helper method that instantiates a Paginator object from the passed
@@ -168,6 +199,7 @@ def test_page_indexes(self):
for params, first, last in tests:
self.check_indexes(params, 'first', first)
self.check_indexes(params, 'last', last)
+
# When no items and no empty first page, we should get EmptyPage error.
self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 0, False), 1, None)
self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 1, False), 1, None)
@@ -184,3 +216,53 @@ def test_page_sequence(self):
self.assertFalse('a' in page2)
self.assertEqual(''.join(page2), 'fghijk')
self.assertEqual(''.join(reversed(page2)), 'kjihgf')
+
+
+class ModelPaginationTests(TestCase):
+ """
+ Test pagination with Django model instances
+ """
+ def setUp(self):
+ # Prepare a list of objects for pagination.
+ for x in range(1, 10):
+ a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
+ a.save()
+
+ def test_first_page(self):
+ paginator = Paginator(Article.objects.all(), 5)
+ p = paginator.page(1)
+ self.assertEqual("<Page 1 of 2>", six.text_type(p))
+ self.assertQuerysetEqual(p.object_list, [
+ "<Article: Article 1>",
+ "<Article: Article 2>",
+ "<Article: Article 3>",
+ "<Article: Article 4>",
+ "<Article: Article 5>"
+ ]
+ )
+ self.assertTrue(p.has_next())
+ self.assertFalse(p.has_previous())
+ self.assertTrue(p.has_other_pages())
+ self.assertEqual(2, p.next_page_number())
+ self.assertRaises(InvalidPage, p.previous_page_number)
+ self.assertEqual(1, p.start_index())
+ self.assertEqual(5, p.end_index())
+
+ def test_last_page(self):
+ paginator = Paginator(Article.objects.all(), 5)
+ p = paginator.page(2)
+ self.assertEqual("<Page 2 of 2>", six.text_type(p))
+ self.assertQuerysetEqual(p.object_list, [
+ "<Article: Article 6>",
+ "<Article: Article 7>",
+ "<Article: Article 8>",
+ "<Article: Article 9>"
+ ]
+ )
+ self.assertFalse(p.has_next())
+ self.assertTrue(p.has_previous())
+ self.assertTrue(p.has_other_pages())
+ self.assertRaises(InvalidPage, p.next_page_number)
+ self.assertEqual(1, p.previous_page_number())
+ self.assertEqual(6, p.start_index())
+ self.assertEqual(9, p.end_index())
View
1 tests/regressiontests/pagination_regress/models.py
@@ -1 +0,0 @@
-# Models file for tests to run.

0 comments on commit 45802e1

Please sign in to comment.