comparison tests/test_sources_autoconfig.py @ 239:f43f19975671

sources: Refactor `autoconfig` source, add `OrderedPageSource`. Also add unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 15 Feb 2015 22:48:42 -0800
parents
children f130365568ff
comparison
equal deleted inserted replaced
238:4dce0e61b48c 239:f43f19975671
1 import pytest
2 from piecrust.sources.base import MODE_PARSING
3 from .mockutil import mock_fs, mock_fs_scope
4
5
6 @pytest.mark.parametrize(
7 'fs, src_config, expected_paths, expected_metadata',
8 [
9 (mock_fs(), {}, [], []),
10 (mock_fs().withPage('test/something.md'),
11 {},
12 ['something.md'],
13 [{'slug': 'something', 'config': {'foo': []}}]),
14 (mock_fs().withPage('test/bar/something.md'),
15 {},
16 ['bar/something.md'],
17 [{'slug': 'something', 'config': {'foo': ['bar']}}]),
18 (mock_fs().withPage('test/bar1/bar2/something.md'),
19 {},
20 ['bar1/bar2/something.md'],
21 [{'slug': 'something', 'config': {'foo': ['bar1', 'bar2']}}]),
22
23 (mock_fs().withPage('test/something.md'),
24 {'collapse_single_values': True},
25 ['something.md'],
26 [{'slug': 'something', 'config': {'foo': None}}]),
27 (mock_fs().withPage('test/bar/something.md'),
28 {'collapse_single_values': True},
29 ['bar/something.md'],
30 [{'slug': 'something', 'config': {'foo': 'bar'}}]),
31 (mock_fs().withPage('test/bar1/bar2/something.md'),
32 {'collapse_single_values': True},
33 ['bar1/bar2/something.md'],
34 [{'slug': 'something', 'config': {'foo': ['bar1', 'bar2']}}]),
35
36 (mock_fs().withPage('test/something.md'),
37 {'only_single_values': True},
38 ['something.md'],
39 [{'slug': 'something', 'config': {'foo': None}}]),
40 (mock_fs().withPage('test/bar/something.md'),
41 {'only_single_values': True},
42 ['bar/something.md'],
43 [{'slug': 'something', 'config': {'foo': 'bar'}}]),
44 ])
45 def test_autoconfig_source_factories(fs, src_config, expected_paths,
46 expected_metadata):
47 site_config = {
48 'sources': {
49 'test': {'type': 'autoconfig',
50 'setting_name': 'foo'}
51 },
52 'routes': [
53 {'url': '/%slug%', 'source': 'test'}]
54 }
55 site_config['sources']['test'].update(src_config)
56 fs.withConfig({'site': site_config})
57 fs.withDir('kitchen/test')
58 with mock_fs_scope(fs):
59 app = fs.getApp()
60 s = app.getSource('test')
61 facs = list(s.buildPageFactories())
62 paths = [f.rel_path for f in facs]
63 assert paths == expected_paths
64 metadata = [f.metadata for f in facs]
65 assert metadata == expected_metadata
66
67
68 def test_autoconfig_fails_if_multiple_folders():
69 site_config = {
70 'sources': {
71 'test': {'type': 'autoconfig',
72 'setting_name': 'foo',
73 'only_single_values': True}
74 }
75 }
76 fs = mock_fs().withConfig({'site': site_config})
77 fs.withPage('test/bar1/bar2/something.md')
78 with mock_fs_scope(fs):
79 app = fs.getApp()
80 s = app.getSource('test')
81 with pytest.raises(Exception):
82 list(s.buildPageFactories())
83
84
85 @pytest.mark.parametrize(
86 'fs, expected_paths, expected_metadata',
87 [
88 (mock_fs(), [], []),
89 (mock_fs().withPage('test/something.md'),
90 ['something.md'],
91 [{'slug': 'something', 'config': {'foo': 0}}]),
92 (mock_fs().withPage('test/08_something.md'),
93 ['08_something.md'],
94 [{'slug': 'something', 'config': {'foo': 8}}])
95 ])
96 def test_ordered_source_factories(fs, expected_paths, expected_metadata):
97 site_config = {
98 'sources': {
99 'test': {'type': 'ordered',
100 'setting_name': 'foo'}
101 },
102 'routes': [
103 {'url': '/%slug%', 'source': 'test'}]
104 }
105 fs.withConfig({'site': site_config})
106 fs.withDir('kitchen/test')
107 with mock_fs_scope(fs):
108 app = fs.getApp()
109 s = app.getSource('test')
110 facs = list(s.buildPageFactories())
111 paths = [f.rel_path for f in facs]
112 assert paths == expected_paths
113 metadata = [f.metadata for f in facs]
114 assert metadata == expected_metadata
115
116
117 @pytest.mark.parametrize(
118 'fs, route_path, expected_path, expected_metadata',
119 [
120 (mock_fs(), 'missing', None, None),
121 (mock_fs().withPage('test/something.md'),
122 'something', 'something.md',
123 {'slug': 'something', 'config': {'foo': 0}}),
124 (mock_fs().withPage('test/bar/something.md'),
125 'bar/something', 'bar/something.md',
126 {'slug': 'bar/something', 'config': {'foo': 0}}),
127 (mock_fs().withPage('test/42_something.md'),
128 'something', '42_something.md',
129 {'slug': 'something', 'config': {'foo': 42}}),
130 (mock_fs().withPage('test/bar/42_something.md'),
131 'bar/something', 'bar/42_something.md',
132 {'slug': 'bar/something', 'config': {'foo': 42}}),
133
134 ((mock_fs()
135 .withPage('test/42_something.md')
136 .withPage('test/43_other_something.md')),
137 'something', '42_something.md',
138 {'slug': 'something', 'config': {'foo': 42}}),
139 ])
140 def test_ordered_source_find(fs, route_path, expected_path,
141 expected_metadata):
142 site_config = {
143 'sources': {
144 'test': {'type': 'ordered',
145 'setting_name': 'foo'}
146 },
147 'routes': [
148 {'url': '/%slug%', 'source': 'test'}]
149 }
150 fs.withConfig({'site': site_config})
151 fs.withDir('kitchen/test')
152 with mock_fs_scope(fs):
153 app = fs.getApp()
154 s = app.getSource('test')
155 route_metadata = {'path': route_path}
156 fac_path, metadata = s.findPagePath(route_metadata, MODE_PARSING)
157 assert fac_path == expected_path
158 assert metadata == expected_metadata
159