comparison tests/test_data_linker.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 f987b29d6fab
children 45ad976712ec
comparison
equal deleted inserted replaced
973:8419daaa7a0e 974:72f17534d58e
1 import os.path
2 import pytest 1 import pytest
3 from piecrust.data.linker import Linker 2 from piecrust.data.linker import Linker
4 from .mockutil import mock_fs, mock_fs_scope 3 from .mockutil import mock_fs, mock_fs_scope, get_simple_content_item
4
5
6 @pytest.mark.parametrize(
7 'fs_fac, page_path, expected',
8 [
9 (lambda: mock_fs().withPage('pages/foo'), 'foo',
10 []),
11 ((lambda: mock_fs()
12 .withPage('pages/foo')
13 .withPage('pages/bar')),
14 'foo',
15 ['/bar']),
16 ((lambda: mock_fs()
17 .withPage('pages/baz')
18 .withPage('pages/something')
19 .withPage('pages/something/else')
20 .withPage('pages/foo')
21 .withPage('pages/bar')),
22 'foo',
23 ['/bar', '/baz', '/something']),
24 ((lambda: mock_fs()
25 .withPage('pages/something/else')
26 .withPage('pages/foo')
27 .withPage('pages/something/good')
28 .withPage('pages/bar')),
29 'something/else',
30 ['/something/good'])
31 ])
32 def test_linker_siblings(fs_fac, page_path, expected):
33 fs = fs_fac()
34 fs.withConfig()
35 with mock_fs_scope(fs):
36 app = fs.getApp()
37 app.config.set('site/pretty_urls', True)
38 src = app.getSource('pages')
39 item = get_simple_content_item(app, page_path)
40 linker = Linker(src, item)
41 actual = list(linker.siblings)
42 assert sorted(map(lambda i: i.url, actual)) == sorted(expected)
5 43
6 44
7 @pytest.mark.parametrize( 45 @pytest.mark.parametrize(
8 'fs_fac, page_path, expected', 46 'fs_fac, page_path, expected',
9 [ 47 [
10 (lambda: mock_fs().withPage('pages/foo'), 'foo.md', 48 (lambda: mock_fs().withPage('pages/foo'), 'foo.md',
11 # is_dir, name, is_self, data 49 []),
12 [(False, 'foo', True, '/foo')]),
13 ((lambda: mock_fs() 50 ((lambda: mock_fs()
14 .withPage('pages/foo') 51 .withPage('pages/foo')
15 .withPage('pages/bar')), 52 .withPage('pages/bar')),
16 'foo.md', 53 'foo',
17 [(False, 'bar', False, '/bar'), (False, 'foo', True, '/foo')]), 54 []),
18 ((lambda: mock_fs() 55 ((lambda: mock_fs()
19 .withPage('pages/baz') 56 .withPage('pages/baz')
20 .withPage('pages/something') 57 .withPage('pages/foo')
21 .withPage('pages/something/else') 58 .withPage('pages/foo/more')
22 .withPage('pages/foo') 59 .withPage('pages/foo/even_more')),
23 .withPage('pages/bar')), 60 'foo',
24 'foo.md', 61 ['/foo/more', '/foo/even_more'])
25 [(False, 'bar', False, '/bar'),
26 (False, 'baz', False, '/baz'),
27 (False, 'foo', True, '/foo'),
28 (True, 'something', False, '/something')]),
29 ((lambda: mock_fs()
30 .withPage('pages/something/else')
31 .withPage('pages/foo')
32 .withPage('pages/something/good')
33 .withPage('pages/bar')),
34 'something/else.md',
35 [(False, 'else', True, '/something/else'),
36 (False, 'good', False, '/something/good')])
37 ]) 62 ])
38 def test_linker_iteration(fs_fac, page_path, expected): 63 def test_linker_children(fs_fac, page_path, expected):
39 fs = fs_fac() 64 fs = fs_fac()
40 fs.withConfig() 65 fs.withConfig()
41 with mock_fs_scope(fs): 66 with mock_fs_scope(fs):
42 app = fs.getApp() 67 app = fs.getApp()
43 app.config.set('site/pretty_urls', True) 68 app.config.set('site/pretty_urls', True)
44 src = app.getSource('pages') 69 src = app.getSource('pages')
45 linker = Linker(src, os.path.dirname(page_path), 70 item = get_simple_content_item(app, page_path)
46 root_page_path=page_path) 71 linker = Linker(src, item)
47 actual = list(iter(linker)) 72 actual = list(linker.children)
48 73 assert sorted(map(lambda i: i.url, actual)) == sorted(expected)
49 assert len(actual) == len(expected)
50 for (a, e) in zip(actual, expected):
51 is_dir, name, is_self, url = e
52 assert a.is_dir == is_dir
53 assert a.name == name
54 assert a.is_self == is_self
55 assert a.url == url
56
57
58 @pytest.mark.parametrize(
59 'fs_fac, page_path, expected',
60 [
61 (lambda: mock_fs().withPage('pages/foo'), 'foo.md',
62 [('/foo', True)]),
63 ((lambda: mock_fs()
64 .withPage('pages/foo')
65 .withPage('pages/bar')),
66 'foo.md',
67 [('/bar', False), ('/foo', True)]),
68 ((lambda: mock_fs()
69 .withPage('pages/baz')
70 .withPage('pages/something/else')
71 .withPage('pages/foo')
72 .withPage('pages/bar')),
73 'foo.md',
74 [('/bar', False), ('/baz', False),
75 ('/foo', True), ('/something/else', False)]),
76 ((lambda: mock_fs()
77 .withPage('pages/something/else')
78 .withPage('pages/foo')
79 .withPage('pages/something/good')
80 .withPage('pages/bar')),
81 'something/else.md',
82 [('/something/else', True),
83 ('/something/good', False)])
84 ])
85 def test_recursive_linker_iteration(fs_fac, page_path, expected):
86 fs = fs_fac()
87 fs.withConfig()
88 with mock_fs_scope(fs):
89 app = fs.getApp()
90 app.config.set('site/pretty_urls', True)
91 src = app.getSource('pages')
92 linker = Linker(src, os.path.dirname(page_path),
93 root_page_path=page_path)
94 actual = list(iter(linker.allpages))
95
96 assert len(actual) == len(expected)
97 for i, (a, e) in enumerate(zip(actual, expected)):
98 assert a.is_dir is False
99 assert a.url == e[0]
100 assert a.is_self == e[1]
101