comparison tests/test_data_linker.py @ 212:701591ebfcba

data: Improve the Linker and RecursiveLinker features. Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 31 Jan 2015 11:50:50 -0800
parents
children 879fe1457e48
comparison
equal deleted inserted replaced
211:0b2d8f6df4ce 212:701591ebfcba
1 import pytest
2 from piecrust.data.linker import Linker, RecursiveLinker
3 from .mockutil import mock_fs, mock_fs_scope
4
5
6 @pytest.mark.parametrize(
7 'fs, page_path, expected',
8 [
9 (mock_fs().withPage('pages/foo'), 'foo.md',
10 [('/foo', True, False)]),
11 ((mock_fs()
12 .withPage('pages/foo')
13 .withPage('pages/bar')),
14 'foo.md',
15 [('/bar', False, False), ('/foo', True, False)]),
16 ((mock_fs()
17 .withPage('pages/baz')
18 .withPage('pages/something/else')
19 .withPage('pages/foo')
20 .withPage('pages/bar')),
21 'foo.md',
22 [('/bar', False, False), ('/baz', False, False),
23 ('/foo', True, False), ('something', False, True)]),
24 ((mock_fs()
25 .withPage('pages/something/else')
26 .withPage('pages/foo')
27 .withPage('pages/something/good')
28 .withPage('pages/bar')),
29 'something/else.md',
30 [('/something/else', True, False),
31 ('/something/good', False, False)])
32 ])
33 def test_linker_iteration(fs, page_path, expected):
34 with mock_fs_scope(fs):
35 app = fs.getApp()
36 src = app.getSource('pages')
37 linker = Linker(src, page_path=page_path)
38 actual = list(iter(linker))
39
40 assert len(actual) == len(expected)
41 for i, (a, e) in enumerate(zip(actual, expected)):
42 assert a.is_dir == e[2]
43 if a.is_dir:
44 assert a.name == e[0]
45 else:
46 assert a.url == e[0]
47 assert a.is_self == e[1]
48
49
50 @pytest.mark.parametrize(
51 'fs, page_path, expected',
52 [
53 (mock_fs().withPage('pages/foo'), 'foo.md',
54 [('/foo', True)]),
55 ((mock_fs()
56 .withPage('pages/foo')
57 .withPage('pages/bar')),
58 'foo.md',
59 [('/bar', False), ('/foo', True)]),
60 ((mock_fs()
61 .withPage('pages/baz')
62 .withPage('pages/something/else')
63 .withPage('pages/foo')
64 .withPage('pages/bar')),
65 'foo.md',
66 [('/bar', False), ('/baz', False),
67 ('/foo', True), ('/something/else', False)]),
68 ((mock_fs()
69 .withPage('pages/something/else')
70 .withPage('pages/foo')
71 .withPage('pages/something/good')
72 .withPage('pages/bar')),
73 'something/else.md',
74 [('/something/else', True),
75 ('/something/good', False)])
76 ])
77 def test_recursive_linker_iteration(fs, page_path, expected):
78 with mock_fs_scope(fs):
79 app = fs.getApp()
80 src = app.getSource('pages')
81 linker = RecursiveLinker(src, page_path=page_path)
82 actual = list(iter(linker))
83
84 assert len(actual) == len(expected)
85 for i, (a, e) in enumerate(zip(actual, expected)):
86 assert a.is_dir is False
87 assert a.url == e[0]
88 assert a.is_self == e[1]
89