Mercurial > piecrust2
comparison tests/test_dataproviders_pageiterator.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 | |
children | 45ad976712ec |
comparison
equal
deleted
inserted
replaced
973:8419daaa7a0e | 974:72f17534d58e |
---|---|
1 import mock | |
2 from piecrust.dataproviders.pageiterator import PageIterator | |
3 from piecrust.page import Page, PageConfiguration | |
4 | |
5 | |
6 def test_skip(): | |
7 it = PageIterator(range(12)) | |
8 it.skip(5) | |
9 assert it.total_count == 12 | |
10 assert len(it) == 7 | |
11 assert list(it) == list(range(5, 12)) | |
12 | |
13 | |
14 def test_limit(): | |
15 it = PageIterator(range(12)) | |
16 it.limit(4) | |
17 assert it.total_count == 12 | |
18 assert len(it) == 4 | |
19 assert list(it) == list(range(4)) | |
20 | |
21 | |
22 def test_slice(): | |
23 it = PageIterator(range(12)) | |
24 it.slice(3, 4) | |
25 assert it.total_count == 12 | |
26 assert len(it) == 4 | |
27 assert list(it) == list(range(3, 7)) | |
28 | |
29 | |
30 def test_natural_sort(): | |
31 it = PageIterator([4, 3, 1, 2, 0]) | |
32 it.sort() | |
33 assert it.total_count == 5 | |
34 assert len(it) == 5 | |
35 assert list(it) == list(range(5)) | |
36 | |
37 | |
38 def test_natural_sort_reversed(): | |
39 it = PageIterator([4, 3, 1, 2, 0]) | |
40 it.sort(reverse=True) | |
41 assert it.total_count == 5 | |
42 assert len(it) == 5 | |
43 assert list(it) == list(reversed(range(5))) | |
44 | |
45 | |
46 class TestItem(object): | |
47 def __init__(self, value): | |
48 self.name = str(value) | |
49 self.foo = value | |
50 | |
51 def __eq__(self, other): | |
52 return other.name == self.name | |
53 | |
54 | |
55 def test_setting_sort(): | |
56 it = PageIterator([TestItem(v) for v in [4, 3, 1, 2, 0]]) | |
57 it.sort('foo') | |
58 assert it.total_count == 5 | |
59 assert len(it) == 5 | |
60 assert list(it) == [TestItem(v) for v in range(5)] | |
61 | |
62 | |
63 def test_setting_sort_reversed(): | |
64 it = PageIterator([TestItem(v) for v in [4, 3, 1, 2, 0]]) | |
65 it.sort('foo', reverse=True) | |
66 assert it.total_count == 5 | |
67 assert len(it) == 5 | |
68 assert list(it) == [TestItem(v) for v in reversed(range(5))] | |
69 | |
70 | |
71 def test_filter(): | |
72 page = mock.MagicMock(spec=Page) | |
73 page.config = PageConfiguration() | |
74 page.config.set('threes', {'is_foo': 3}) | |
75 it = PageIterator([TestItem(v) for v in [3, 2, 3, 1, 4, 3]], | |
76 current_page=page) | |
77 it.filter('threes') | |
78 assert it.total_count == 3 | |
79 assert len(it) == 3 | |
80 assert list(it) == [TestItem(3), TestItem(3), TestItem(3)] | |
81 | |
82 | |
83 def test_magic_filter(): | |
84 it = PageIterator([TestItem(v) for v in [3, 2, 3, 1, 4, 3]]) | |
85 it.is_foo(3) | |
86 assert it.total_count == 3 | |
87 assert len(it) == 3 | |
88 assert list(it) == [TestItem(3), TestItem(3), TestItem(3)] | |
89 |