Ticket #14444: pagination_tests.diff

File pagination_tests.diff, 9.7 KB (added by Gabriel Hurley, 14 years ago)
  • tests/modeltests/pagination/models.py

     
    1 """
    2 30. Object pagination
    3 
    4 Django provides a framework for paginating a list of objects in a few lines
    5 of code. This is often useful for dividing search results or long lists of
    6 objects into easily readable pages.
    7 """
    8 
    91from django.db import models
    102
    113class Article(models.Model):
     
    146
    157    def __unicode__(self):
    168        return self.headline
    17 
    18 __test__ = {'API_TESTS':"""
    19 # Prepare a list of objects for pagination.
    20 >>> from datetime import datetime
    21 >>> for x in range(1, 10):
    22 ...     a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
    23 ...     a.save()
    24 
    25 ##################
    26 # Paginator/Page #
    27 ##################
    28 
    29 >>> from django.core.paginator import Paginator
    30 >>> paginator = Paginator(Article.objects.all(), 5)
    31 >>> paginator.count
    32 9
    33 >>> paginator.num_pages
    34 2
    35 >>> paginator.page_range
    36 [1, 2]
    37 
    38 # Get the first page.
    39 >>> p = paginator.page(1)
    40 >>> p
    41 <Page 1 of 2>
    42 >>> p.object_list
    43 [<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>, <Article: Article 5>]
    44 >>> p.has_next()
    45 True
    46 >>> p.has_previous()
    47 False
    48 >>> p.has_other_pages()
    49 True
    50 >>> p.next_page_number()
    51 2
    52 >>> p.previous_page_number()
    53 0
    54 >>> p.start_index()
    55 1
    56 >>> p.end_index()
    57 5
    58 
    59 # Get the second page.
    60 >>> p = paginator.page(2)
    61 >>> p
    62 <Page 2 of 2>
    63 >>> p.object_list
    64 [<Article: Article 6>, <Article: Article 7>, <Article: Article 8>, <Article: Article 9>]
    65 >>> p.has_next()
    66 False
    67 >>> p.has_previous()
    68 True
    69 >>> p.has_other_pages()
    70 True
    71 >>> p.next_page_number()
    72 3
    73 >>> p.previous_page_number()
    74 1
    75 >>> p.start_index()
    76 6
    77 >>> p.end_index()
    78 9
    79 
    80 # Empty pages raise EmptyPage.
    81 >>> paginator.page(0)
    82 Traceback (most recent call last):
    83 ...
    84 EmptyPage: ...
    85 >>> paginator.page(3)
    86 Traceback (most recent call last):
    87 ...
    88 EmptyPage: ...
    89 
    90 # Empty paginators with allow_empty_first_page=True.
    91 >>> paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True)
    92 >>> paginator.count
    93 0
    94 >>> paginator.num_pages
    95 1
    96 >>> paginator.page_range
    97 [1]
    98 
    99 # Empty paginators with allow_empty_first_page=False.
    100 >>> paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False)
    101 >>> paginator.count
    102 0
    103 >>> paginator.num_pages
    104 0
    105 >>> paginator.page_range
    106 []
    107 
    108 # Paginators work with regular lists/tuples, too -- not just with QuerySets.
    109 >>> paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
    110 >>> paginator.count
    111 9
    112 >>> paginator.num_pages
    113 2
    114 >>> paginator.page_range
    115 [1, 2]
    116 
    117 # Get the first page.
    118 >>> p = paginator.page(1)
    119 >>> p
    120 <Page 1 of 2>
    121 >>> p.object_list
    122 [1, 2, 3, 4, 5]
    123 >>> p.has_next()
    124 True
    125 >>> p.has_previous()
    126 False
    127 >>> p.has_other_pages()
    128 True
    129 >>> p.next_page_number()
    130 2
    131 >>> p.previous_page_number()
    132 0
    133 >>> p.start_index()
    134 1
    135 >>> p.end_index()
    136 5
    137 
    138 # Paginator can be passed other objects with a count() method.
    139 >>> class CountContainer:
    140 ...     def count(self):
    141 ...         return 42
    142 >>> paginator = Paginator(CountContainer(), 10)
    143 >>> paginator.count
    144 42
    145 >>> paginator.num_pages
    146 5
    147 >>> paginator.page_range
    148 [1, 2, 3, 4, 5]
    149 
    150 # Paginator can be passed other objects that implement __len__.
    151 >>> class LenContainer:
    152 ...     def __len__(self):
    153 ...         return 42
    154 >>> paginator = Paginator(LenContainer(), 10)
    155 >>> paginator.count
    156 42
    157 >>> paginator.num_pages
    158 5
    159 >>> paginator.page_range
    160 [1, 2, 3, 4, 5]
    161 
    162 
    163 ##################
    164 # Orphan support #
    165 ##################
    166 
    167 # Add a few more records to test out the orphans feature.
    168 >>> for x in range(10, 13):
    169 ...     Article(headline="Article %s" % x, pub_date=datetime(2006, 10, 6)).save()
    170 
    171 # With orphans set to 3 and 10 items per page, we should get all 12 items on a single page.
    172 >>> paginator = Paginator(Article.objects.all(), 10, orphans=3)
    173 >>> paginator.num_pages
    174 1
    175 
    176 # With orphans only set to 1, we should get two pages.
    177 >>> paginator = Paginator(Article.objects.all(), 10, orphans=1)
    178 >>> paginator.num_pages
    179 2
    180 """}
  • tests/modeltests/pagination/tests.py

     
     1from datetime import datetime
     2from operator import attrgetter
     3
     4from django.core.paginator import Paginator, InvalidPage, EmptyPage
     5from django.test import TestCase
     6
     7from models import Article
     8
     9class CountContainer:
     10    def count(self):
     11        return 42
     12
     13class LenContainer:
     14    def __len__(self):
     15        return 42
     16
     17class 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)
Back to Top