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