Mercurial > piecrust2
comparison tests/test_data_paginator.py @ 974:72f17534d58e
tests: First pass on making unit tests work again.
- Fix all imports
- Add more helper functions to work with mock file-systems
- Simplify some code by running chef directly on the mock FS
- Fix a couple tests
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 17 Oct 2017 01:07:30 -0700 |
parents | 4b1019bb2533 |
children |
comparison
equal
deleted
inserted
replaced
973:8419daaa7a0e | 974:72f17534d58e |
---|---|
1 import math | 1 import math |
2 import mock | |
3 import pytest | 2 import pytest |
4 from piecrust.data.paginator import Paginator | 3 from piecrust.data.paginator import Paginator |
5 from piecrust.sources.interfaces import IPaginationSource | |
6 | 4 |
7 | 5 |
8 class MockSource(list, IPaginationSource): | 6 class MockSource(list): |
9 def __init__(self, count): | 7 def __init__(self, count): |
10 for i in range(count): | 8 for i in range(count): |
11 self.append('item %d' % i) | 9 self.append('item %d' % i) |
12 | 10 |
13 def getItemsPerPage(self): | |
14 return 5 | |
15 | |
16 def getSourceIterator(self): | |
17 return None | |
18 | |
19 def getSorterIterator(self, it): | |
20 return None | |
21 | |
22 def getTailIterator(self, it): | |
23 return None | |
24 | |
25 def getPaginationFilter(self, page): | |
26 return None | |
27 | |
28 | 11 |
29 @pytest.mark.parametrize('uri, page_num, count', [ | 12 @pytest.mark.parametrize('uri, page_num, count', [ |
30 ('', 1, 0), | 13 ('', 1, 0), |
31 ('', 1, 4), | 14 ('', 1, 4), |
32 ('', 1, 5), | 15 ('', 1, 5), |
33 ('', 1, 8), | 16 ('', 1, 8), |
34 ('', 1, 14), | 17 ('', 1, 14), |
35 ('', 2, 8), | 18 ('', 2, 8), |
36 ('', 2, 14), | 19 ('', 2, 14), |
37 ('', 3, 14), | 20 ('', 3, 14), |
38 ('blog', 1, 0), | 21 ('blog', 1, 0), |
39 ('blog', 1, 4), | 22 ('blog', 1, 4), |
40 ('blog', 1, 5), | 23 ('blog', 1, 5), |
41 ('blog', 1, 8), | 24 ('blog', 1, 8), |
42 ('blog', 1, 14), | 25 ('blog', 1, 14), |
43 ('blog', 2, 8), | 26 ('blog', 2, 8), |
44 ('blog', 2, 14), | 27 ('blog', 2, 14), |
45 ('blog', 3, 14) | 28 ('blog', 3, 14) |
46 ]) | 29 ]) |
47 def test_paginator(uri, page_num, count): | 30 def test_paginator(uri, page_num, count): |
48 def _get_mock_uri(sub_num): | 31 def _get_mock_uri(sub_num): |
49 res = uri | 32 res = uri |
50 if sub_num > 1: | 33 if sub_num > 1: |
51 if res != '' and not res.endswith('/'): | 34 if res != '' and not res.endswith('/'): |
52 res += '/' | 35 res += '/' |
53 res += '%d' % sub_num | 36 res += '%d' % sub_num |
54 return res | 37 return res |
55 | 38 |
56 source = MockSource(count) | 39 source = MockSource(count) |
57 p = Paginator(None, source, page_num=page_num) | 40 p = Paginator(source, None, page_num) |
41 p._items_per_page = 5 | |
58 p._getPageUri = _get_mock_uri | 42 p._getPageUri = _get_mock_uri |
59 | 43 |
60 if count <= 5: | 44 if count <= 5: |
61 # All posts fit on the page | 45 # All posts fit on the page |
62 assert p.prev_page_number is None | 46 assert p.prev_page_number is None |
79 assert p.prev_page_number == page_num - 1 | 63 assert p.prev_page_number == page_num - 1 |
80 if page_num == 2: | 64 if page_num == 2: |
81 assert p.prev_page == uri | 65 assert p.prev_page == uri |
82 else: | 66 else: |
83 pp = str(page_num - 1) if uri == '' else ( | 67 pp = str(page_num - 1) if uri == '' else ( |
84 '%s/%d' % (uri, page_num - 1)) | 68 '%s/%d' % (uri, page_num - 1)) |
85 assert p.prev_page == pp | 69 assert p.prev_page == pp |
86 | 70 |
87 assert p.this_page_number == page_num | 71 assert p.this_page_number == page_num |
88 tp = str(page_num) if uri == '' else ( | 72 tp = str(page_num) if uri == '' else ( |
89 '%s/%d' % (uri, page_num)) | 73 '%s/%d' % (uri, page_num)) |
90 assert p.this_page == tp | 74 assert p.this_page == tp |
91 | 75 |
92 if page_num * 5 > count: | 76 if page_num * 5 > count: |
93 assert p.next_page_number is None | 77 assert p.next_page_number is None |
94 assert p.next_page is None | 78 assert p.next_page is None |
95 else: | 79 else: |
96 assert p.next_page_number == page_num + 1 | 80 assert p.next_page_number == page_num + 1 |
97 np = str(page_num + 1) if uri == '' else ( | 81 np = str(page_num + 1) if uri == '' else ( |
98 '%s/%d' % (uri, page_num + 1)) | 82 '%s/%d' % (uri, page_num + 1)) |
99 assert p.next_page == np | 83 assert p.next_page == np |
100 | 84 |
101 assert p.total_post_count == count | 85 assert p.total_post_count == count |
102 page_count = math.ceil(count / 5.0) | 86 page_count = math.ceil(count / 5.0) |
103 assert p.total_page_count == page_count | 87 assert p.total_page_count == page_count |
116 if nums[0] > 1: | 100 if nums[0] > 1: |
117 to_add = min(to_add, nums[0] - 1) | 101 to_add = min(to_add, nums[0] - 1) |
118 nums = list(range(1, to_add + 1)) + nums | 102 nums = list(range(1, to_add + 1)) + nums |
119 else: | 103 else: |
120 to_add = min(to_add, page_count - nums[-1]) | 104 to_add = min(to_add, page_count - nums[-1]) |
121 nums = nums + list(range(nums[-1] + 1, nums[-1] + to_add + 1)) | 105 nums = nums + list(range(nums[-1] + 1, |
106 nums[-1] + to_add + 1)) | |
122 assert nums == p.all_page_numbers(radius) | 107 assert nums == p.all_page_numbers(radius) |
123 | 108 |
124 itp = count | 109 itp = count |
125 if count > 5: | 110 if count > 5: |
126 if page_num * 5 < count: | 111 if page_num * 5 < count: |
128 else: | 113 else: |
129 itp = count % 5 | 114 itp = count % 5 |
130 assert p.items_this_page == itp | 115 assert p.items_this_page == itp |
131 | 116 |
132 indices = list(range(count)) | 117 indices = list(range(count)) |
133 indices = indices[(page_num - 1) * 5 : (page_num - 1) * 5 + itp] | 118 indices = indices[(page_num - 1) * 5:(page_num - 1) * 5 + itp] |
134 expected = list(['item %d' % i for i in indices]) | 119 expected = list(['item %d' % i for i in indices]) |
135 items = list(p.items) | 120 items = list(p.items) |
136 assert items == expected | 121 assert items == expected |
137 | 122 |