| 1 | from datetime import datetime |
| 2 | from operator import attrgetter |
| 3 | |
| 4 | from django.core.paginator import Paginator, InvalidPage, EmptyPage |
| 5 | from django.test import TestCase |
| 6 | |
| 7 | from models import Article |
| 8 | |
| 9 | class CountContainer: |
| 10 | def count(self): |
| 11 | return 42 |
| 12 | |
| 13 | class LenContainer: |
| 14 | def __len__(self): |
| 15 | return 42 |
| 16 | |
| 17 | class PaginationTests(TestCase): |
| 18 | def setUp(self): |
| 19 | # Prepare a list of objects for pagination. |
| 20 | for x in range(1, 10): |
| 21 | a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29)) |
| 22 | a.save() |
| 23 | |
| 24 | def test_paginator(self): |
| 25 | paginator = Paginator(Article.objects.all(), 5) |
| 26 | self.assertEqual(9, paginator.count) |
| 27 | self.assertEqual(2, paginator.num_pages) |
| 28 | self.assertEqual([1, 2], paginator.page_range) |
| 29 | |
| 30 | def test_first_page(self): |
| 31 | paginator = Paginator(Article.objects.all(), 5) |
| 32 | p = paginator.page(1) |
| 33 | self.assertEqual(u"<Page 1 of 2>", unicode(p)) |
| 34 | self.assertQuerysetEqual(p.object_list, [ |
| 35 | "<Article: Article 1>", |
| 36 | "<Article: Article 2>", |
| 37 | "<Article: Article 3>", |
| 38 | "<Article: Article 4>", |
| 39 | "<Article: Article 5>" |
| 40 | ] |
| 41 | ) |
| 42 | self.assertTrue(p.has_next()) |
| 43 | self.assertFalse(p.has_previous()) |
| 44 | self.assertTrue(p.has_other_pages()) |
| 45 | self.assertEqual(2, p.next_page_number()) |
| 46 | self.assertEqual(0, p.previous_page_number()) |
| 47 | self.assertEqual(1, p.start_index()) |
| 48 | self.assertEqual(5, p.end_index()) |
| 49 | |
| 50 | def test_last_page(self): |
| 51 | paginator = Paginator(Article.objects.all(), 5) |
| 52 | p = paginator.page(2) |
| 53 | self.assertEqual(u"<Page 2 of 2>", unicode(p)) |
| 54 | self.assertQuerysetEqual(p.object_list, [ |
| 55 | "<Article: Article 6>", |
| 56 | "<Article: Article 7>", |
| 57 | "<Article: Article 8>", |
| 58 | "<Article: Article 9>" |
| 59 | ] |
| 60 | ) |
| 61 | self.assertFalse(p.has_next()) |
| 62 | self.assertTrue(p.has_previous()) |
| 63 | self.assertTrue(p.has_other_pages()) |
| 64 | self.assertEqual(3, p.next_page_number()) |
| 65 | self.assertEqual(1, p.previous_page_number()) |
| 66 | self.assertEqual(6, p.start_index()) |
| 67 | self.assertEqual(9, p.end_index()) |
| 68 | |
| 69 | def test_empty_page(self): |
| 70 | paginator = Paginator(Article.objects.all(), 5) |
| 71 | self.assertRaises(EmptyPage, paginator.page, 0) |
| 72 | self.assertRaises(EmptyPage, paginator.page, 3) |
| 73 | |
| 74 | # Empty paginators with allow_empty_first_page=True. |
| 75 | paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True) |
| 76 | self.assertEqual(0, paginator.count) |
| 77 | self.assertEqual(1, paginator.num_pages) |
| 78 | self.assertEqual([1], paginator.page_range) |
| 79 | |
| 80 | # Empty paginators with allow_empty_first_page=False. |
| 81 | paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False) |
| 82 | self.assertEqual(0, paginator.count) |
| 83 | self.assertEqual(0, paginator.num_pages) |
| 84 | self.assertEqual([], paginator.page_range) |
| 85 | |
| 86 | def test_invalid_page(self): |
| 87 | paginator = Paginator(Article.objects.all(), 5) |
| 88 | self.assertRaises(InvalidPage, paginator.page, 7) |
| 89 | |
| 90 | def test_orphans(self): |
| 91 | # Add a few more records to test out the orphans feature. |
| 92 | for x in range(10, 13): |
| 93 | Article(headline="Article %s" % x, pub_date=datetime(2006, 10, 6)).save() |
| 94 | |
| 95 | # With orphans set to 3 and 10 items per page, we should get all 12 items on a single page. |
| 96 | paginator = Paginator(Article.objects.all(), 10, orphans=3) |
| 97 | self.assertEqual(1, paginator.num_pages) |
| 98 | |
| 99 | # With orphans only set to 1, we should get two pages. |
| 100 | paginator = Paginator(Article.objects.all(), 10, orphans=1) |
| 101 | self.assertEqual(2, paginator.num_pages) |
| 102 | |
| 103 | def test_paginate_list(self): |
| 104 | # Paginators work with regular lists/tuples, too -- not just with QuerySets. |
| 105 | paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5) |
| 106 | self.assertEqual(9, paginator.count) |
| 107 | self.assertEqual(2, paginator.num_pages) |
| 108 | self.assertEqual([1, 2], paginator.page_range) |
| 109 | p = paginator.page(1) |
| 110 | self.assertEqual(u"<Page 1 of 2>", unicode(p)) |
| 111 | self.assertEqual([1, 2, 3, 4, 5], p.object_list) |
| 112 | self.assertTrue(p.has_next()) |
| 113 | self.assertFalse(p.has_previous()) |
| 114 | self.assertTrue(p.has_other_pages()) |
| 115 | self.assertEqual(2, p.next_page_number()) |
| 116 | self.assertEqual(0, p.previous_page_number()) |
| 117 | self.assertEqual(1, p.start_index()) |
| 118 | self.assertEqual(5, p.end_index()) |
| 119 | |
| 120 | def test_paginate_misc_classes(self): |
| 121 | # Paginator can be passed other objects with a count() method. |
| 122 | paginator = Paginator(CountContainer(), 10) |
| 123 | self.assertEqual(42, paginator.count) |
| 124 | self.assertEqual(5, paginator.num_pages) |
| 125 | self.assertEqual([1, 2, 3, 4, 5], paginator.page_range) |
| 126 | |
| 127 | # Paginator can be passed other objects that implement __len__. |
| 128 | paginator = Paginator(LenContainer(), 10) |
| 129 | self.assertEqual(42, paginator.count) |
| 130 | self.assertEqual(5, paginator.num_pages) |
| 131 | self.assertEqual([1, 2, 3, 4, 5], paginator.page_range) |