Mercurial > piecrust2
comparison tests/test_data_paginator.py @ 6:f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sat, 16 Aug 2014 08:15:30 -0700 |
| parents | |
| children | f130365568ff |
comparison
equal
deleted
inserted
replaced
| 5:474c9882decf | 6:f5ca5c5bed85 |
|---|---|
| 1 import math | |
| 2 import pytest | |
| 3 from piecrust.data.base import IPaginationSource | |
| 4 from piecrust.data.paginator import Paginator | |
| 5 | |
| 6 | |
| 7 class MockSource(list, IPaginationSource): | |
| 8 def __init__(self, count): | |
| 9 for i in range(count): | |
| 10 self.append('item %d' % i) | |
| 11 | |
| 12 def getItemsPerPage(self): | |
| 13 return 5 | |
| 14 | |
| 15 def getSourceIterator(self): | |
| 16 return None | |
| 17 | |
| 18 def getSorterIterator(self, it): | |
| 19 return None | |
| 20 | |
| 21 def getTailIterator(self, it): | |
| 22 return None | |
| 23 | |
| 24 def getPaginationFilter(self, page): | |
| 25 return None | |
| 26 | |
| 27 | |
| 28 @pytest.mark.parametrize('uri, page_num, count', [ | |
| 29 ('', 1, 0), | |
| 30 ('', 1, 4), | |
| 31 ('', 1, 5), | |
| 32 ('', 1, 8), | |
| 33 ('', 1, 14), | |
| 34 ('', 2, 8), | |
| 35 ('', 2, 14), | |
| 36 ('', 3, 14), | |
| 37 ('blog', 1, 0), | |
| 38 ('blog', 1, 4), | |
| 39 ('blog', 1, 5), | |
| 40 ('blog', 1, 8), | |
| 41 ('blog', 1, 14), | |
| 42 ('blog', 2, 8), | |
| 43 ('blog', 2, 14), | |
| 44 ('blog', 3, 14) | |
| 45 ]) | |
| 46 def test_paginator(uri, page_num, count): | |
| 47 source = MockSource(count) | |
| 48 p = Paginator(None, source, uri, page_num) | |
| 49 | |
| 50 if count <= 5: | |
| 51 # All posts fit on the page | |
| 52 assert p.prev_page_number is None | |
| 53 assert p.prev_page is None | |
| 54 assert p.this_page_number == 1 | |
| 55 assert p.this_page == uri | |
| 56 assert p.next_page_number is None | |
| 57 assert p.next_page is None | |
| 58 elif page_num == 1: | |
| 59 # First page in many | |
| 60 assert p.prev_page_number is None | |
| 61 assert p.prev_page is None | |
| 62 assert p.this_page_number == 1 | |
| 63 assert p.this_page == uri | |
| 64 assert p.next_page_number == 2 | |
| 65 np = '2' if uri == '' else (uri + '/2') | |
| 66 assert p.next_page == np | |
| 67 else: | |
| 68 # Page in the middle of it all | |
| 69 assert p.prev_page_number == page_num - 1 | |
| 70 if page_num == 2: | |
| 71 assert p.prev_page == uri | |
| 72 else: | |
| 73 pp = str(page_num - 1) if uri == '' else ( | |
| 74 '%s/%d' % (uri, page_num - 1)) | |
| 75 assert p.prev_page == pp | |
| 76 | |
| 77 assert p.this_page_number == page_num | |
| 78 tp = str(page_num) if uri == '' else ( | |
| 79 '%s/%d' % (uri, page_num)) | |
| 80 assert p.this_page == tp | |
| 81 | |
| 82 if page_num * 5 > count: | |
| 83 assert p.next_page_number is None | |
| 84 assert p.next_page is None | |
| 85 else: | |
| 86 assert p.next_page_number == page_num + 1 | |
| 87 np = str(page_num + 1) if uri == '' else ( | |
| 88 '%s/%d' % (uri, page_num + 1)) | |
| 89 assert p.next_page == np | |
| 90 | |
| 91 assert p.total_post_count == count | |
| 92 page_count = math.ceil(count / 5.0) | |
| 93 assert p.total_page_count == page_count | |
| 94 assert p.all_page_numbers() == list(range(1, page_count + 1)) | |
| 95 | |
| 96 for radius in range(1, 8): | |
| 97 width = radius * 2 + 1 | |
| 98 if page_count == 0: | |
| 99 nums = [] | |
| 100 else: | |
| 101 nums = list(filter( | |
| 102 lambda i: i >= 1 and i <= page_count, | |
| 103 range(page_num - radius, page_num + radius + 1))) | |
| 104 if len(nums) < width: | |
| 105 to_add = width - len(nums) | |
| 106 if nums[0] > 1: | |
| 107 to_add = min(to_add, nums[0] - 1) | |
| 108 nums = list(range(1, to_add + 1)) + nums | |
| 109 else: | |
| 110 to_add = min(to_add, page_count - nums[-1]) | |
| 111 nums = nums + list(range(nums[-1] + 1, nums[-1] + to_add + 1)) | |
| 112 assert nums == p.all_page_numbers(radius) | |
| 113 | |
| 114 itp = count | |
| 115 if count > 5: | |
| 116 if page_num * 5 < count: | |
| 117 itp = 5 | |
| 118 else: | |
| 119 itp = count % 5 | |
| 120 assert p.items_this_page == itp | |
| 121 | |
| 122 indices = list(range(count)) | |
| 123 indices = indices[(page_num - 1) * 5 : (page_num - 1) * 5 + itp] | |
| 124 expected = list(['item %d' % i for i in indices]) | |
| 125 items = list(p.items) | |
| 126 assert items == expected | |
| 127 |
