Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14444 -- Convert the pagination doctests to unittests. We have…

… always been at war with doctests. Thanks to Gabriel Hurley for the patch.

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

0 notes on commit 103a201

Please sign in to comment.
Something went wrong with that request. Please try again.