diff --git a/tests/modeltests/pagination/tests.py b/tests/modeltests/pagination/tests.py deleted file mode 100644 index 12ce0e3ecb..0000000000 --- a/tests/modeltests/pagination/tests.py +++ /dev/null @@ -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("", six.text_type(p)) - self.assertQuerysetEqual(p.object_list, [ - "", - "", - "", - "", - "" - ] - ) - 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("", six.text_type(p)) - self.assertQuerysetEqual(p.object_list, [ - "", - "", - "", - "" - ] - ) - 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("", 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)) diff --git a/tests/modeltests/pagination/__init__.py b/tests/regressiontests/pagination/__init__.py similarity index 100% rename from tests/modeltests/pagination/__init__.py rename to tests/regressiontests/pagination/__init__.py diff --git a/tests/modeltests/pagination/models.py b/tests/regressiontests/pagination/models.py similarity index 59% rename from tests/modeltests/pagination/models.py rename to tests/regressiontests/pagination/models.py index 779d3029ba..9dc8d4b776 100644 --- a/tests/modeltests/pagination/models.py +++ b/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 diff --git a/tests/regressiontests/pagination_regress/tests.py b/tests/regressiontests/pagination/tests.py similarity index 72% rename from tests/regressiontests/pagination_regress/tests.py rename to tests/regressiontests/pagination/tests.py index e98352e006..a49f9b8fa1 100644 --- a/tests/regressiontests/pagination_regress/tests.py +++ b/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 @@ class PaginatorTests(TestCase): "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 @@ class PaginatorTests(TestCase): 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 @@ class PaginatorTests(TestCase): 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 @@ class PaginatorTests(TestCase): 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("", six.text_type(p)) + self.assertQuerysetEqual(p.object_list, [ + "", + "", + "", + "", + "" + ] + ) + 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("", six.text_type(p)) + self.assertQuerysetEqual(p.object_list, [ + "", + "", + "", + "" + ] + ) + 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()) diff --git a/tests/regressiontests/pagination_regress/__init__.py b/tests/regressiontests/pagination_regress/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/regressiontests/pagination_regress/models.py b/tests/regressiontests/pagination_regress/models.py deleted file mode 100644 index cde172db68..0000000000 --- a/tests/regressiontests/pagination_regress/models.py +++ /dev/null @@ -1 +0,0 @@ -# Models file for tests to run.