Mercurial > piecrust2
annotate tests/test_data_paginator.py @ 1137:10fd55b9ccfb
templating: Fix Inukshuk `paginate` function.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 24 Apr 2018 21:27:46 -0700 |
parents | 72f17534d58e |
children |
rev | line source |
---|---|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import math |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import pytest |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.data.paginator import Paginator |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
6 class MockSource(list): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 def __init__(self, count): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 for i in range(count): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 self.append('item %d' % i) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 @pytest.mark.parametrize('uri, page_num, count', [ |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
13 ('', 1, 0), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
14 ('', 1, 4), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
15 ('', 1, 5), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
16 ('', 1, 8), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
17 ('', 1, 14), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
18 ('', 2, 8), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
19 ('', 2, 14), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
20 ('', 3, 14), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
21 ('blog', 1, 0), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
22 ('blog', 1, 4), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
23 ('blog', 1, 5), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
24 ('blog', 1, 8), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
25 ('blog', 1, 14), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
26 ('blog', 2, 8), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
27 ('blog', 2, 14), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
28 ('blog', 3, 14) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
29 ]) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 def test_paginator(uri, page_num, count): |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
352
diff
changeset
|
31 def _get_mock_uri(sub_num): |
352
498a917cd2d4
pagination: Make pagination use routes to generate proper URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
32 res = uri |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
352
diff
changeset
|
33 if sub_num > 1: |
352
498a917cd2d4
pagination: Make pagination use routes to generate proper URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
34 if res != '' and not res.endswith('/'): |
498a917cd2d4
pagination: Make pagination use routes to generate proper URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
35 res += '/' |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
352
diff
changeset
|
36 res += '%d' % sub_num |
352
498a917cd2d4
pagination: Make pagination use routes to generate proper URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
37 return res |
498a917cd2d4
pagination: Make pagination use routes to generate proper URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
38 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 source = MockSource(count) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
40 p = Paginator(source, None, page_num) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
41 p._items_per_page = 5 |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
352
diff
changeset
|
42 p._getPageUri = _get_mock_uri |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 if count <= 5: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 # All posts fit on the page |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 assert p.prev_page_number is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 assert p.prev_page is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 assert p.this_page_number == 1 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 assert p.this_page == uri |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 assert p.next_page_number is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 assert p.next_page is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 elif page_num == 1: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 # First page in many |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 assert p.prev_page_number is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 assert p.prev_page is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 assert p.this_page_number == 1 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 assert p.this_page == uri |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 assert p.next_page_number == 2 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 np = '2' if uri == '' else (uri + '/2') |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 assert p.next_page == np |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 else: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 # Page in the middle of it all |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 assert p.prev_page_number == page_num - 1 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 if page_num == 2: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 assert p.prev_page == uri |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 else: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 pp = str(page_num - 1) if uri == '' else ( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
68 '%s/%d' % (uri, page_num - 1)) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 assert p.prev_page == pp |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 assert p.this_page_number == page_num |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 tp = str(page_num) if uri == '' else ( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
73 '%s/%d' % (uri, page_num)) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 assert p.this_page == tp |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 if page_num * 5 > count: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 assert p.next_page_number is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 assert p.next_page is None |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 else: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 assert p.next_page_number == page_num + 1 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 np = str(page_num + 1) if uri == '' else ( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
82 '%s/%d' % (uri, page_num + 1)) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 assert p.next_page == np |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 assert p.total_post_count == count |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 page_count = math.ceil(count / 5.0) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 assert p.total_page_count == page_count |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 assert p.all_page_numbers() == list(range(1, page_count + 1)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 for radius in range(1, 8): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 width = radius * 2 + 1 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 if page_count == 0: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 nums = [] |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 else: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 nums = list(filter( |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 lambda i: i >= 1 and i <= page_count, |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 range(page_num - radius, page_num + radius + 1))) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 if len(nums) < width: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 to_add = width - len(nums) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 if nums[0] > 1: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 to_add = min(to_add, nums[0] - 1) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 nums = list(range(1, to_add + 1)) + nums |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 else: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 to_add = min(to_add, page_count - nums[-1]) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
105 nums = nums + list(range(nums[-1] + 1, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
106 nums[-1] + to_add + 1)) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 assert nums == p.all_page_numbers(radius) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 itp = count |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 if count > 5: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 if page_num * 5 < count: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 itp = 5 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 else: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 itp = count % 5 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 assert p.items_this_page == itp |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 indices = list(range(count)) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
118 indices = indices[(page_num - 1) * 5:(page_num - 1) * 5 + itp] |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 expected = list(['item %d' % i for i in indices]) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 items = list(p.items) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 assert items == expected |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 |