annotate tests/test_data_iterators.py @ 840:7f3043f9f26f

internal: Don't check for a page repository, there's always one.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 15 Feb 2017 22:17:13 -0800
parents e35407c60e00
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import mock
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from piecrust.data.iterators import PageIterator
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from piecrust.page import Page, PageConfiguration
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 def test_skip():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 it = PageIterator(range(12))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 it.skip(5)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 assert it.total_count == 12
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 assert len(it) == 7
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 assert list(it) == list(range(5, 12))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 def test_limit():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 it = PageIterator(range(12))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 it.limit(4)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 assert it.total_count == 12
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 assert len(it) == 4
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 assert list(it) == list(range(4))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 def test_slice():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 it = PageIterator(range(12))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 it.slice(3, 4)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 assert it.total_count == 12
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 assert len(it) == 4
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 assert list(it) == list(range(3, 7))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 def test_natural_sort():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 it = PageIterator([4, 3, 1, 2, 0])
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 it.sort()
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 assert it.total_count == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 assert len(it) == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 assert list(it) == list(range(5))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 def test_natural_sort_reversed():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 it = PageIterator([4, 3, 1, 2, 0])
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 it.sort(reverse=True)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 assert it.total_count == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 assert len(it) == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 assert list(it) == list(reversed(range(5)))
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 class TestItem(object):
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 def __init__(self, value):
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 self.name = str(value)
233
4379d8f8f831 internal: Removing some dependency of filters and iterators on pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
49 self.foo = value
10
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def __eq__(self, other):
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 return other.name == self.name
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 def test_setting_sort():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 it = PageIterator([TestItem(v) for v in [4, 3, 1, 2, 0]])
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 it.sort('foo')
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 assert it.total_count == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 assert len(it) == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 assert list(it) == [TestItem(v) for v in range(5)]
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 def test_setting_sort_reversed():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 it = PageIterator([TestItem(v) for v in [4, 3, 1, 2, 0]])
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 it.sort('foo', reverse=True)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 assert it.total_count == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 assert len(it) == 5
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 assert list(it) == [TestItem(v) for v in reversed(range(5))]
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 def test_filter():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 page = mock.MagicMock(spec=Page)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 page.config = PageConfiguration()
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 page.config.set('threes', {'is_foo': 3})
729
e35407c60e00 templating: Make blog archives generator expose more templating data.
Ludovic Chabant <ludovic@chabant.com>
parents: 233
diff changeset
75 it = PageIterator([TestItem(v) for v in [3, 2, 3, 1, 4, 3]],
e35407c60e00 templating: Make blog archives generator expose more templating data.
Ludovic Chabant <ludovic@chabant.com>
parents: 233
diff changeset
76 current_page=page)
10
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 it.filter('threes')
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 assert it.total_count == 3
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 assert len(it) == 3
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 assert list(it) == [TestItem(3), TestItem(3), TestItem(3)]
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 def test_magic_filter():
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 it = PageIterator([TestItem(v) for v in [3, 2, 3, 1, 4, 3]])
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 it.is_foo(3)
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 assert it.total_count == 3
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 assert len(it) == 3
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 assert list(it) == [TestItem(3), TestItem(3), TestItem(3)]
cd35d356ccce Fix some bugs with iterators, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89