comparison tests/test_routing.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 58ebf50235a5
children 45ad976712ec
comparison
equal deleted inserted replaced
973:8419daaa7a0e 974:72f17534d58e
1 import urllib.parse 1 import urllib.parse
2 import mock 2 import mock
3 import pytest 3 import pytest
4 from piecrust.routing import Route, RouteParameter 4 from piecrust.routing import Route, RouteParameter
5 from piecrust.sources.base import PageSource 5 from piecrust.sources.base import ContentSource
6 from .mockutil import get_mock_app 6 from .mockutil import get_mock_app
7 7
8 8
9 def _getMockSource(name, params): 9 def _getMockSource(name, params):
10 route_params = [] 10 route_params = []
18 t = RouteParameter.TYPE_INT4 18 t = RouteParameter.TYPE_INT4
19 route_params.append(RouteParameter(p[0], t)) 19 route_params.append(RouteParameter(p[0], t))
20 else: 20 else:
21 route_params.append(RouteParameter(p, RouteParameter.TYPE_STRING)) 21 route_params.append(RouteParameter(p, RouteParameter.TYPE_STRING))
22 22
23 src = mock.MagicMock(spec=PageSource) 23 src = mock.MagicMock(spec=ContentSource)
24 src.name = name 24 src.name = name
25 src.getSupportedRouteParameters = lambda: route_params 25 src.getSupportedRouteParameters = lambda: route_params
26 return src 26 return src
27 27
28 28
29 @pytest.mark.parametrize( 29 @pytest.mark.parametrize(
30 'config, metadata, params, expected', 30 'config, metadata, params, expected',
31 [ 31 [
32 ({'url': '/%foo%'}, 32 ({'url': '/%foo%'},
33 {'foo': 'bar'}, ['foo'], True), 33 {'foo': 'bar'}, ['foo'], True),
34 ({'url': '/%foo%'}, 34 ({'url': '/%foo%'},
35 {'zoo': 'zar', 'foo': 'bar'}, ['foo'], True), 35 {'zoo': 'zar', 'foo': 'bar'}, ['foo'], True),
36 ({'url': '/%foo%'}, 36 ({'url': '/%foo%'},
37 {'zoo': 'zar'}, ['foo'], False), 37 {'zoo': 'zar'}, ['foo'], False),
38 ({'url': '/%foo%/%zoo%'}, 38 ({'url': '/%foo%/%zoo%'},
39 {'zoo': 'zar'}, ['foo', 'zoo'], False) 39 {'zoo': 'zar'}, ['foo', 'zoo'], False)
40 ]) 40 ])
41 def test_matches_metadata(config, metadata, params, expected): 41 def test_matches_metadata(config, metadata, params, expected):
42 app = get_mock_app() 42 app = get_mock_app()
43 app.config.set('site/root', '/') 43 app.config.set('site/root', '/')
44 app.sources = [_getMockSource('blah', params)] 44 app.sources = [_getMockSource('blah', params)]
45 45
48 m = route.matchesMetadata(metadata) 48 m = route.matchesMetadata(metadata)
49 assert m == expected 49 assert m == expected
50 50
51 51
52 @pytest.mark.parametrize( 52 @pytest.mark.parametrize(
53 'site_root, route_pattern, params, expected_func_parameters', 53 'site_root, route_pattern, params, expected_func_parameters',
54 [ 54 [
55 ('/', '/%foo%', ['foo'], ['foo']), 55 ('/', '/%foo%', ['foo'], ['foo']),
56 ('/', '/%foo%', [('foo', 'path')], ['foo']), 56 ('/', '/%foo%', [('foo', 'path')], ['foo']),
57 ('/', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']), 57 ('/', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']),
58 ('/', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']), 58 ('/', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']),
59 ('/something', '/%foo%', ['foo'], ['foo']), 59 ('/something', '/%foo%', ['foo'], ['foo']),
60 ('/something', '/%foo%', [('foo', 'path')], ['foo']), 60 ('/something', '/%foo%', [('foo', 'path')], ['foo']),
61 ('/something', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']), 61 ('/something', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']),
62 ('/something', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']), 62 ('/something', '/%foo%/%bar%', ['foo', ('bar', 'path')],
63 ('/~johndoe', '/%foo%', ['foo'], ['foo']), 63 ['foo', 'bar']),
64 ('/~johndoe', '/%foo%', [('foo', 'path')], ['foo']), 64 ('/~johndoe', '/%foo%', ['foo'], ['foo']),
65 ('/~johndoe', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']), 65 ('/~johndoe', '/%foo%', [('foo', 'path')], ['foo']),
66 ('/~johndoe', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']) 66 ('/~johndoe', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']),
67 ]) 67 ('/~johndoe', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar'])
68 ])
68 def test_required_metadata(site_root, route_pattern, params, 69 def test_required_metadata(site_root, route_pattern, params,
69 expected_func_parameters): 70 expected_func_parameters):
70 app = get_mock_app() 71 app = get_mock_app()
71 app.config.set('site/root', site_root.rstrip('/') + '/') 72 app.config.set('site/root', site_root.rstrip('/') + '/')
72 app.sources = [_getMockSource('blah', params)] 73 app.sources = [_getMockSource('blah', params)]
75 route = Route(app, config) 76 route = Route(app, config)
76 assert route.uri_params == expected_func_parameters 77 assert route.uri_params == expected_func_parameters
77 78
78 79
79 @pytest.mark.parametrize( 80 @pytest.mark.parametrize(
80 'site_root, config, params, uri, expected_match', 81 'site_root, config, params, uri, expected_match',
81 [ 82 [
82 ('/', {'url': '/%foo%'}, 83 ('/', {'url': '/%foo%'},
83 ['foo'], 84 ['foo'],
84 'something', 85 'something',
85 {'foo': 'something'}), 86 {'foo': 'something'}),
86 ('/', {'url': '/%foo%'}, 87 ('/', {'url': '/%foo%'},
87 ['foo'], 88 ['foo'],
88 'something/other', 89 'something/other',
89 None), 90 None),
90 ('/', {'url': '/%foo%'}, 91 ('/', {'url': '/%foo%'},
91 [('foo', 'path')], 92 [('foo', 'path')],
92 'something/other', 93 'something/other',
93 {'foo': 'something/other'}), 94 {'foo': 'something/other'}),
94 ('/', {'url': '/%foo%'}, 95 ('/', {'url': '/%foo%'},
95 [('foo', 'path')], 96 [('foo', 'path')],
96 '', 97 '',
97 {'foo': ''}), 98 {'foo': ''}),
98 ('/', {'url': '/prefix/%foo%'}, 99 ('/', {'url': '/prefix/%foo%'},
99 [('foo', 'path')], 100 [('foo', 'path')],
100 'prefix/something/other', 101 'prefix/something/other',
101 {'foo': 'something/other'}), 102 {'foo': 'something/other'}),
102 ('/', {'url': '/prefix/%foo%'}, 103 ('/', {'url': '/prefix/%foo%'},
103 [('foo', 'path')], 104 [('foo', 'path')],
104 'prefix/', 105 'prefix/',
105 {'foo': ''}), 106 {'foo': ''}),
106 ('/', {'url': '/prefix/%foo%'}, 107 ('/', {'url': '/prefix/%foo%'},
107 [('foo', 'path')], 108 [('foo', 'path')],
108 'prefix', 109 'prefix',
109 {'foo': ''}), 110 {'foo': ''}),
110 111
111 ('/blah', {'url': '/%foo%'}, 112 ('/blah', {'url': '/%foo%'},
112 ['foo'], 113 ['foo'],
113 'something', 114 'something',
114 {'foo': 'something'}), 115 {'foo': 'something'}),
115 ('/blah', {'url': '/%foo%'}, 116 ('/blah', {'url': '/%foo%'},
116 ['foo'], 117 ['foo'],
117 'something/other', 118 'something/other',
118 None), 119 None),
119 ('/blah', {'url': '/%foo%'}, 120 ('/blah', {'url': '/%foo%'},
120 [('foo', 'path')], 121 [('foo', 'path')],
121 'something/other', 122 'something/other',
122 {'foo': 'something/other'}), 123 {'foo': 'something/other'}),
123 ('/blah', {'url': '/%foo%'}, 124 ('/blah', {'url': '/%foo%'},
124 [('foo', 'path')], 125 [('foo', 'path')],
125 '', 126 '',
126 {'foo': ''}), 127 {'foo': ''}),
127 ('/blah', {'url': '/prefix/%foo%'}, 128 ('/blah', {'url': '/prefix/%foo%'},
128 [('foo', 'path')], 129 [('foo', 'path')],
129 'prefix/something/other', 130 'prefix/something/other',
130 {'foo': 'something/other'}), 131 {'foo': 'something/other'}),
131 ('/blah', {'url': '/prefix/%foo%'}, 132 ('/blah', {'url': '/prefix/%foo%'},
132 [('foo', 'path')], 133 [('foo', 'path')],
133 'prefix/', 134 'prefix/',
134 {'foo': ''}), 135 {'foo': ''}),
135 ('/blah', {'url': '/prefix/%foo%'}, 136 ('/blah', {'url': '/prefix/%foo%'},
136 [('foo', 'path')], 137 [('foo', 'path')],
137 'prefix', 138 'prefix',
138 {'foo': ''}), 139 {'foo': ''}),
139 140
140 ('/~johndoe', {'url': '/%foo%'}, 141 ('/~johndoe', {'url': '/%foo%'},
141 ['foo'], 142 ['foo'],
142 'something', 143 'something',
143 {'foo': 'something'}), 144 {'foo': 'something'}),
144 ('/~johndoe', {'url': '/%foo%'}, 145 ('/~johndoe', {'url': '/%foo%'},
145 ['foo'], 146 ['foo'],
146 'something/other', 147 'something/other',
147 None), 148 None),
148 ('/~johndoe', {'url': '/%foo%'}, 149 ('/~johndoe', {'url': '/%foo%'},
149 [('foo', 'path')], 150 [('foo', 'path')],
150 'something/other', 151 'something/other',
151 {'foo': 'something/other'}), 152 {'foo': 'something/other'}),
152 ('/~johndoe', {'url': '/%foo%'}, 153 ('/~johndoe', {'url': '/%foo%'},
153 [('foo', 'path')], 154 [('foo', 'path')],
154 '', 155 '',
155 {'foo': ''}), 156 {'foo': ''}),
156 ('/~johndoe', {'url': '/prefix/%foo%'}, 157 ('/~johndoe', {'url': '/prefix/%foo%'},
157 [('foo', 'path')], 158 [('foo', 'path')],
158 'prefix/something/other', 159 'prefix/something/other',
159 {'foo': 'something/other'}), 160 {'foo': 'something/other'}),
160 ('/~johndoe', {'url': '/prefix/%foo%'}, 161 ('/~johndoe', {'url': '/prefix/%foo%'},
161 [('foo', 'path')], 162 [('foo', 'path')],
162 'prefix/', 163 'prefix/',
163 {'foo': ''}), 164 {'foo': ''}),
164 ('/~johndoe', {'url': '/prefix/%foo%'}, 165 ('/~johndoe', {'url': '/prefix/%foo%'},
165 [('foo', 'path')], 166 [('foo', 'path')],
166 'prefix', 167 'prefix',
167 {'foo': ''}), 168 {'foo': ''}),
168 ]) 169 ])
169 def test_match_uri(site_root, config, params, uri, expected_match): 170 def test_match_uri(site_root, config, params, uri, expected_match):
170 site_root = site_root.rstrip('/') + '/' 171 site_root = site_root.rstrip('/') + '/'
171 app = get_mock_app() 172 app = get_mock_app()
172 app.config.set('site/root', urllib.parse.quote(site_root)) 173 app.config.set('site/root', urllib.parse.quote(site_root))
173 app.sources = [_getMockSource('blah', params)] 174 app.sources = [_getMockSource('blah', params)]
178 m = route.matchUri(urllib.parse.quote(site_root) + uri) 179 m = route.matchUri(urllib.parse.quote(site_root) + uri)
179 assert m == expected_match 180 assert m == expected_match
180 181
181 182
182 @pytest.mark.parametrize( 183 @pytest.mark.parametrize(
183 'site_root', 184 'site_root',
184 [ 185 [
185 ('/'), 186 ('/'),
186 ('/whatever'), 187 ('/whatever'),
187 ('/~johndoe') 188 ('/~johndoe')
188 ]) 189 ])
189 def test_match_uri_requires_absolute_uri(site_root): 190 def test_match_uri_requires_absolute_uri(site_root):
190 with pytest.raises(Exception): 191 with pytest.raises(Exception):
191 app = get_mock_app() 192 app = get_mock_app()
192 app.config.set('site/root', site_root.rstrip('/') + '/') 193 app.config.set('site/root', site_root.rstrip('/') + '/')
193 app.sources = [_getMockSource('blah', [('slug', 'path')])] 194 app.sources = [_getMockSource('blah', [('slug', 'path')])]
196 route = Route(app, config) 197 route = Route(app, config)
197 route.matchUri('notabsuri') 198 route.matchUri('notabsuri')
198 199
199 200
200 @pytest.mark.parametrize( 201 @pytest.mark.parametrize(
201 'slug, page_num, pretty, expected', 202 'slug, page_num, pretty, expected',
202 [ 203 [
203 # Pretty URLs 204 # Pretty URLs
204 ('', 1, True, ''), 205 ('', 1, True, ''),
205 ('', 2, True, '2'), 206 ('', 2, True, '2'),
206 ('foo', 1, True, 'foo'), 207 ('foo', 1, True, 'foo'),
207 ('foo', 2, True, 'foo/2'), 208 ('foo', 2, True, 'foo/2'),
208 ('foo/bar', 1, True, 'foo/bar'), 209 ('foo/bar', 1, True, 'foo/bar'),
209 ('foo/bar', 2, True, 'foo/bar/2'), 210 ('foo/bar', 2, True, 'foo/bar/2'),
210 ('foo.ext', 1, True, 'foo.ext'), 211 ('foo.ext', 1, True, 'foo.ext'),
211 ('foo.ext', 2, True, 'foo.ext/2'), 212 ('foo.ext', 2, True, 'foo.ext/2'),
212 ('foo/bar.ext', 1, True, 'foo/bar.ext'), 213 ('foo/bar.ext', 1, True, 'foo/bar.ext'),
213 ('foo/bar.ext', 2, True, 'foo/bar.ext/2'), 214 ('foo/bar.ext', 2, True, 'foo/bar.ext/2'),
214 ('foo.bar.ext', 1, True, 'foo.bar.ext'), 215 ('foo.bar.ext', 1, True, 'foo.bar.ext'),
215 ('foo.bar.ext', 2, True, 'foo.bar.ext/2'), 216 ('foo.bar.ext', 2, True, 'foo.bar.ext/2'),
216 # Ugly URLs 217 # Ugly URLs
217 ('', 1, False, ''), 218 ('', 1, False, ''),
218 ('', 2, False, '2.html'), 219 ('', 2, False, '2.html'),
219 ('foo', 1, False, 'foo.html'), 220 ('foo', 1, False, 'foo.html'),
220 ('foo', 2, False, 'foo/2.html'), 221 ('foo', 2, False, 'foo/2.html'),
221 ('foo/bar', 1, False, 'foo/bar.html'), 222 ('foo/bar', 1, False, 'foo/bar.html'),
222 ('foo/bar', 2, False, 'foo/bar/2.html'), 223 ('foo/bar', 2, False, 'foo/bar/2.html'),
223 ('foo.ext', 1, False, 'foo.ext'), 224 ('foo.ext', 1, False, 'foo.ext'),
224 ('foo.ext', 2, False, 'foo/2.ext'), 225 ('foo.ext', 2, False, 'foo/2.ext'),
225 ('foo/bar.ext', 1, False, 'foo/bar.ext'), 226 ('foo/bar.ext', 1, False, 'foo/bar.ext'),
226 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'), 227 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'),
227 ('foo.bar.ext', 1, False, 'foo.bar.ext'), 228 ('foo.bar.ext', 1, False, 'foo.bar.ext'),
228 ('foo.bar.ext', 2, False, 'foo.bar/2.ext') 229 ('foo.bar.ext', 2, False, 'foo.bar/2.ext')
229 ]) 230 ])
230 def test_get_uri(slug, page_num, pretty, expected): 231 def test_get_uri(slug, page_num, pretty, expected):
231 for root in ['/', '/blah/', '/~johndoe/']: 232 for root in ['/', '/blah/', '/~johndoe/']:
232 app = get_mock_app() 233 app = get_mock_app()
233 app.config.set('site/root', urllib.parse.quote(root)) 234 app.config.set('site/root', urllib.parse.quote(root))
234 app.config.set('site/pretty_urls', pretty) 235 app.config.set('site/pretty_urls', pretty)