Mercurial > piecrust2
annotate 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 |
rev | line source |
---|---|
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
1 import urllib.parse |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
2 import mock |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import pytest |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
4 from piecrust.routing import Route, RouteParameter |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
5 from piecrust.sources.base import ContentSource |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
6 from .mockutil import get_mock_app |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
9 def _getMockSource(name, params): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
10 route_params = [] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
11 for p in params: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
12 if isinstance(p, tuple): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
13 if p[1] == 'path': |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
14 t = RouteParameter.TYPE_PATH |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
15 elif p[1] == 'int2': |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
16 t = RouteParameter.TYPE_INT2 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
17 elif p[2] == 'int4': |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
18 t = RouteParameter.TYPE_INT4 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
19 route_params.append(RouteParameter(p[0], t)) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
20 else: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
21 route_params.append(RouteParameter(p, RouteParameter.TYPE_STRING)) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
22 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
23 src = mock.MagicMock(spec=ContentSource) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
24 src.name = name |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
25 src.getSupportedRouteParameters = lambda: route_params |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
26 return src |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
27 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
28 |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
30 'config, metadata, params, expected', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
31 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
32 ({'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
33 {'foo': 'bar'}, ['foo'], True), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
34 ({'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
35 {'zoo': 'zar', 'foo': 'bar'}, ['foo'], True), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
36 ({'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
37 {'zoo': 'zar'}, ['foo'], False), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
38 ({'url': '/%foo%/%zoo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
39 {'zoo': 'zar'}, ['foo', 'zoo'], False) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
40 ]) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
41 def test_matches_metadata(config, metadata, params, expected): |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
42 app = get_mock_app() |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
43 app.config.set('site/root', '/') |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
44 app.sources = [_getMockSource('blah', params)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
45 |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 config.setdefault('source', 'blah') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 route = Route(app, config) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 m = route.matchesMetadata(metadata) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 assert m == expected |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
53 'site_root, route_pattern, params, expected_func_parameters', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
54 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
55 ('/', '/%foo%', ['foo'], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
56 ('/', '/%foo%', [('foo', 'path')], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
57 ('/', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
58 ('/', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
59 ('/something', '/%foo%', ['foo'], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
60 ('/something', '/%foo%', [('foo', 'path')], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
61 ('/something', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
62 ('/something', '/%foo%/%bar%', ['foo', ('bar', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
63 ['foo', 'bar']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
64 ('/~johndoe', '/%foo%', ['foo'], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
65 ('/~johndoe', '/%foo%', [('foo', 'path')], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
66 ('/~johndoe', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
67 ('/~johndoe', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
68 ]) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
69 def test_required_metadata(site_root, route_pattern, params, |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
568
diff
changeset
|
70 expected_func_parameters): |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
71 app = get_mock_app() |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
72 app.config.set('site/root', site_root.rstrip('/') + '/') |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
73 app.sources = [_getMockSource('blah', params)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
74 |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
75 config = {'url': route_pattern, 'source': 'blah'} |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
76 route = Route(app, config) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
77 assert route.uri_params == expected_func_parameters |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
78 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
79 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
80 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
81 'site_root, config, params, uri, expected_match', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
82 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
83 ('/', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
84 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
85 'something', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
86 {'foo': 'something'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
87 ('/', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
88 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
89 'something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
90 None), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
91 ('/', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
92 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
93 'something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
94 {'foo': 'something/other'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
95 ('/', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
96 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
97 '', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
98 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
99 ('/', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
100 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
101 'prefix/something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
102 {'foo': 'something/other'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
103 ('/', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
104 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
105 'prefix/', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
106 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
107 ('/', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
108 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
109 'prefix', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
110 {'foo': ''}), |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
111 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
112 ('/blah', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
113 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
114 'something', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
115 {'foo': 'something'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
116 ('/blah', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
117 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
118 'something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
119 None), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
120 ('/blah', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
121 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
122 'something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
123 {'foo': 'something/other'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
124 ('/blah', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
125 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
126 '', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
127 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
128 ('/blah', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
129 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
130 'prefix/something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
131 {'foo': 'something/other'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
132 ('/blah', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
133 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
134 'prefix/', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
135 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
136 ('/blah', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
137 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
138 'prefix', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
139 {'foo': ''}), |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
140 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
141 ('/~johndoe', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
142 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
143 'something', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
144 {'foo': 'something'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
145 ('/~johndoe', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
146 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
147 'something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
148 None), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
149 ('/~johndoe', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
150 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
151 'something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
152 {'foo': 'something/other'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
153 ('/~johndoe', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
154 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
155 '', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
156 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
157 ('/~johndoe', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
158 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
159 'prefix/something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
160 {'foo': 'something/other'}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
161 ('/~johndoe', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
162 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
163 'prefix/', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
164 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
165 ('/~johndoe', {'url': '/prefix/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
166 [('foo', 'path')], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
167 'prefix', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
168 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
169 ]) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
170 def test_match_uri(site_root, config, params, uri, expected_match): |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
171 site_root = site_root.rstrip('/') + '/' |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
172 app = get_mock_app() |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
173 app.config.set('site/root', urllib.parse.quote(site_root)) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
174 app.sources = [_getMockSource('blah', params)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
175 |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
176 config.setdefault('source', 'blah') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
177 route = Route(app, config) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 assert route.uri_pattern == config['url'].lstrip('/') |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
179 m = route.matchUri(urllib.parse.quote(site_root) + uri) |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 assert m == expected_match |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
182 |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
183 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
184 'site_root', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
185 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
186 ('/'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
187 ('/whatever'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
188 ('/~johndoe') |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
189 ]) |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
190 def test_match_uri_requires_absolute_uri(site_root): |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
191 with pytest.raises(Exception): |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
192 app = get_mock_app() |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
193 app.config.set('site/root', site_root.rstrip('/') + '/') |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
194 app.sources = [_getMockSource('blah', [('slug', 'path')])] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
195 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
196 config = {'url': '/%slug%', 'source': 'blah'} |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
197 route = Route(app, config) |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
198 route.matchUri('notabsuri') |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
199 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
200 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
201 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
202 'slug, page_num, pretty, expected', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
203 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
204 # Pretty URLs |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
205 ('', 1, True, ''), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
206 ('', 2, True, '2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
207 ('foo', 1, True, 'foo'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
208 ('foo', 2, True, 'foo/2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
209 ('foo/bar', 1, True, 'foo/bar'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
210 ('foo/bar', 2, True, 'foo/bar/2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
211 ('foo.ext', 1, True, 'foo.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
212 ('foo.ext', 2, True, 'foo.ext/2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
213 ('foo/bar.ext', 1, True, 'foo/bar.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
214 ('foo/bar.ext', 2, True, 'foo/bar.ext/2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
215 ('foo.bar.ext', 1, True, 'foo.bar.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
216 ('foo.bar.ext', 2, True, 'foo.bar.ext/2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
217 # Ugly URLs |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
218 ('', 1, False, ''), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
219 ('', 2, False, '2.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
220 ('foo', 1, False, 'foo.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
221 ('foo', 2, False, 'foo/2.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
222 ('foo/bar', 1, False, 'foo/bar.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
223 ('foo/bar', 2, False, 'foo/bar/2.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
224 ('foo.ext', 1, False, 'foo.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
225 ('foo.ext', 2, False, 'foo/2.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
226 ('foo/bar.ext', 1, False, 'foo/bar.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
227 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
228 ('foo.bar.ext', 1, False, 'foo.bar.ext'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
229 ('foo.bar.ext', 2, False, 'foo.bar/2.ext') |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
230 ]) |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
231 def test_get_uri(slug, page_num, pretty, expected): |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
232 for root in ['/', '/blah/', '/~johndoe/']: |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
233 app = get_mock_app() |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
234 app.config.set('site/root', urllib.parse.quote(root)) |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
235 app.config.set('site/pretty_urls', pretty) |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
236 app.config.set('site/trailing_slash', False) |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
237 app.config.set('__cache/pagination_suffix_format', '/%(num)d') |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
238 app.sources = [_getMockSource('blah', [('slug', 'path')])] |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
239 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
240 config = {'url': '/%slug%', 'source': 'blah'} |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
241 route = Route(app, config) |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
242 uri = route.getUri({'slug': slug}, sub_num=page_num) |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
243 assert uri == (urllib.parse.quote(root) + expected) |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
244 |