Mercurial > piecrust2
view tests/test_data_paginator.py @ 369:4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
* We need a distinction between source metadata and route metadata. In most
cases they're the same, but in cases like taxonomy pages, route metadata
contains more things that can't be in source metadata if we want to re-use
cached pages.
* Create a new `QualifiedPage` type which is a page with a specific route
and route metadata. Pass this around in many places.
* Instead of passing an URL around, use the route in the `QualifiedPage` to
generate URLs. This is better since it removes the guess-work from trying
to generate URLs for sub-pages.
* Deep-copy app and page configurations before passing them around to things
that could modify them, like data builders and such.
* Exclude taxonomy pages from iterator data providers.
* Properly nest iterator data providers for when the theme and user page
sources are merged inside `site.pages`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 03 May 2015 18:47:10 -0700 |
parents | 498a917cd2d4 |
children | 72f17534d58e |
line wrap: on
line source
import math import mock import pytest from piecrust.data.paginator import Paginator from piecrust.sources.interfaces import IPaginationSource class MockSource(list, IPaginationSource): def __init__(self, count): for i in range(count): self.append('item %d' % i) def getItemsPerPage(self): return 5 def getSourceIterator(self): return None def getSorterIterator(self, it): return None def getTailIterator(self, it): return None def getPaginationFilter(self, page): return None @pytest.mark.parametrize('uri, page_num, count', [ ('', 1, 0), ('', 1, 4), ('', 1, 5), ('', 1, 8), ('', 1, 14), ('', 2, 8), ('', 2, 14), ('', 3, 14), ('blog', 1, 0), ('blog', 1, 4), ('blog', 1, 5), ('blog', 1, 8), ('blog', 1, 14), ('blog', 2, 8), ('blog', 2, 14), ('blog', 3, 14) ]) def test_paginator(uri, page_num, count): def _get_mock_uri(sub_num): res = uri if sub_num > 1: if res != '' and not res.endswith('/'): res += '/' res += '%d' % sub_num return res source = MockSource(count) p = Paginator(None, source, page_num=page_num) p._getPageUri = _get_mock_uri if count <= 5: # All posts fit on the page assert p.prev_page_number is None assert p.prev_page is None assert p.this_page_number == 1 assert p.this_page == uri assert p.next_page_number is None assert p.next_page is None elif page_num == 1: # First page in many assert p.prev_page_number is None assert p.prev_page is None assert p.this_page_number == 1 assert p.this_page == uri assert p.next_page_number == 2 np = '2' if uri == '' else (uri + '/2') assert p.next_page == np else: # Page in the middle of it all assert p.prev_page_number == page_num - 1 if page_num == 2: assert p.prev_page == uri else: pp = str(page_num - 1) if uri == '' else ( '%s/%d' % (uri, page_num - 1)) assert p.prev_page == pp assert p.this_page_number == page_num tp = str(page_num) if uri == '' else ( '%s/%d' % (uri, page_num)) assert p.this_page == tp if page_num * 5 > count: assert p.next_page_number is None assert p.next_page is None else: assert p.next_page_number == page_num + 1 np = str(page_num + 1) if uri == '' else ( '%s/%d' % (uri, page_num + 1)) assert p.next_page == np assert p.total_post_count == count page_count = math.ceil(count / 5.0) assert p.total_page_count == page_count assert p.all_page_numbers() == list(range(1, page_count + 1)) for radius in range(1, 8): width = radius * 2 + 1 if page_count == 0: nums = [] else: nums = list(filter( lambda i: i >= 1 and i <= page_count, range(page_num - radius, page_num + radius + 1))) if len(nums) < width: to_add = width - len(nums) if nums[0] > 1: to_add = min(to_add, nums[0] - 1) nums = list(range(1, to_add + 1)) + nums else: to_add = min(to_add, page_count - nums[-1]) nums = nums + list(range(nums[-1] + 1, nums[-1] + to_add + 1)) assert nums == p.all_page_numbers(radius) itp = count if count > 5: if page_num * 5 < count: itp = 5 else: itp = count % 5 assert p.items_this_page == itp indices = list(range(count)) indices = indices[(page_num - 1) * 5 : (page_num - 1) * 5 + itp] expected = list(['item %d' % i for i in indices]) items = list(p.items) assert items == expected