Add records variable in templatetags, allowing to extend the pagination template...
[django-pagination.git] / pagination / tests.py
1 """
2 >>> from django.core.paginator import Paginator
3 >>> from pagination.templatetags.pagination_tags import paginate
4 >>> from django.template import Template, Context
5
6 >>> p = Paginator(range(15), 2)
7 >>> pg = paginate({'paginator': p, 'page_obj': p.page(1)})
8 >>> pg['pages']
9 [1, 2, 3, 4, 5, 6, 7, 8]
10 >>> pg['records']['first']
11 1
12 >>> pg['records']['last']
13 2
14
15 >>> p = Paginator(range(15), 2)
16 >>> pg = paginate({'paginator': p, 'page_obj': p.page(8)})
17 >>> pg['pages']
18 [1, 2, 3, 4, 5, 6, 7, 8]
19 >>> pg['records']['first']
20 15
21 >>> pg['records']['last']
22 15
23
24 >>> p = Paginator(range(17), 2)
25 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
26 [1, 2, 3, 4, 5, 6, 7, 8, 9]
27
28 >>> p = Paginator(range(19), 2)
29 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
30 [1, 2, 3, 4, None, 7, 8, 9, 10]
31
32 >>> p = Paginator(range(21), 2)
33 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
34 [1, 2, 3, 4, None, 8, 9, 10, 11]
35
36 # Testing orphans
37 >>> p = Paginator(range(5), 2, 1)
38 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
39 [1, 2]
40
41 >>> p = Paginator(range(21), 2, 1)
42 >>> pg = paginate({'paginator': p, 'page_obj': p.page(1)})
43 >>> pg['pages']
44 [1, 2, 3, 4, None, 7, 8, 9, 10]
45 >>> pg['records']['first']
46 1
47 >>> pg['records']['last']
48 2
49
50 >>> p = Paginator(range(21), 2, 1)
51 >>> pg = paginate({'paginator': p, 'page_obj': p.page(10)})
52 >>> pg['pages']
53 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
54 >>> pg['records']['first']
55 19
56 >>> pg['records']['last']
57 21
58
59 >>> t = Template("{% load pagination_tags %}{% autopaginate var 2 %}{% paginate %}")
60
61 >>> from django.http import HttpRequest as DjangoHttpRequest
62 >>> class HttpRequest(DjangoHttpRequest):
63 ...     page = 1
64
65 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
66 u'\\n\\n<div class="pagination">...
67 >>>
68 >>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
69 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
70 u'\\n\\n<div class="pagination">...
71 >>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
72 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
73 u'\\n\\n<div class="pagination">...
74 >>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
75 >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
76 u'\\n\\n<div class="pagination">...
77 >>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
78 >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
79 u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
80 >>>
81
82 # Testing InfinitePaginator
83
84 >>> from paginator import InfinitePaginator
85
86 >>> InfinitePaginator
87 <class 'pagination.paginator.InfinitePaginator'>
88 >>> p = InfinitePaginator(range(20), 2, link_template='/bacon/page/%d')
89 >>> p.validate_number(2)
90 2
91 >>> p.orphans
92 0
93 >>> p3 = p.page(3)
94 >>> p3
95 <Page 3>
96 >>> p3.end_index()
97 6
98 >>> p3.has_next()
99 True
100 >>> p3.has_previous()
101 True
102 >>> p.page(10).has_next()
103 False
104 >>> p.page(1).has_previous()
105 False
106 >>> p3.next_link()
107 '/bacon/page/4'
108 >>> p3.previous_link()
109 '/bacon/page/2'
110
111 # Testing FinitePaginator
112
113 >>> from paginator import FinitePaginator
114
115 >>> FinitePaginator
116 <class 'pagination.paginator.FinitePaginator'>
117 >>> p = FinitePaginator(range(20), 2, offset=10, link_template='/bacon/page/%d')
118 >>> p.validate_number(2)
119 2
120 >>> p.orphans
121 0
122 >>> p3 = p.page(3)
123 >>> p3
124 <Page 3>
125 >>> p3.start_index()
126 10
127 >>> p3.end_index()
128 6
129 >>> p3.has_next()
130 True
131 >>> p3.has_previous()
132 True
133 >>> p3.next_link()
134 '/bacon/page/4'
135 >>> p3.previous_link()
136 '/bacon/page/2'
137
138 >>> p = FinitePaginator(range(20), 20, offset=10, link_template='/bacon/page/%d')
139 >>> p2 = p.page(2)
140 >>> p2
141 <Page 2>
142 >>> p2.has_next()
143 False
144 >>> p3.has_previous()
145 True
146 >>> p2.next_link()
147
148 >>> p2.previous_link()
149 '/bacon/page/1'
150
151 >>> from pagination.middleware import PaginationMiddleware
152 >>> from django.core.handlers.wsgi import WSGIRequest
153 >>> from StringIO import StringIO
154 >>> middleware = PaginationMiddleware()
155 >>> request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart', 'wsgi.input': StringIO()})
156 >>> middleware.process_request(request)
157 >>> request.upload_handlers.append('asdf')
158 """