Mercurial > piecrust2
annotate tests/test_routing.py @ 1188:a7c43131d871
bake: Fix file write flushing problem with Python 3.8+
Writing the cache files fails in Python 3.8 because it looks like flushing
behaviour has changed. We need to explicitly flush. And even then, in very
rare occurrences, it looks like it can still run into racing conditions,
so we do a very hacky and ugly "retry" loop when fetching cached data :(
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 15 Jun 2021 22:36:23 -0700 |
parents | 45ad976712ec |
children |
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( |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
30 'config, params, uri_params, expected', |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
31 [ |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
32 ({'url': '/%foo%'}, ['foo'], {'foo': 'bar'}, True), |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
33 ({'url': '/%foo%'}, ['foo'], {'zoo': 'zar', 'foo': 'bar'}, True), |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
34 ({'url': '/%foo%'}, ['foo'], {'zoo': 'zar'}, False), |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
35 ({'url': '/%foo%/%zoo%'}, ['foo', 'zoo'], {'zoo': 'zar'}, False) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
36 ]) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
37 def test_matches_parameters(config, params, uri_params, expected): |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
38 app = get_mock_app() |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
39 app.config.set('site/root', '/') |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
40 app.sources = [_getMockSource('blah', params)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
41 |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 config.setdefault('source', 'blah') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 route = Route(app, config) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
44 m = route.matchesParameters(uri_params) |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 assert m == expected |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
49 '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
|
50 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
51 ('/', '/%foo%', ['foo'], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
52 ('/', '/%foo%', [('foo', 'path')], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
53 ('/', '/%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
|
54 ('/', '/%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
|
55 ('/something', '/%foo%', ['foo'], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
56 ('/something', '/%foo%', [('foo', 'path')], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
57 ('/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
|
58 ('/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
|
59 ['foo', 'bar']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
60 ('/~johndoe', '/%foo%', ['foo'], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
61 ('/~johndoe', '/%foo%', [('foo', 'path')], ['foo']), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
62 ('/~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
|
63 ('/~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
|
64 ]) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
65 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
|
66 expected_func_parameters): |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
67 app = get_mock_app() |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
68 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
|
69 app.sources = [_getMockSource('blah', params)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
70 |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
71 config = {'url': route_pattern, 'source': 'blah'} |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
72 route = Route(app, config) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
73 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
|
74 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
75 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
76 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
77 '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
|
78 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
79 ('/', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
80 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
81 'something', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
82 {'foo': 'something'}), |
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/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
86 None), |
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', 'path')], |
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 {'foo': 'something/other'}), |
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 '', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
94 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
95 ('/', {'url': '/prefix/%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 'prefix/something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
98 {'foo': 'something/other'}), |
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/', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
102 {'foo': ''}), |
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': ''}), |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
107 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
108 ('/blah', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
109 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
110 'something', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
111 {'foo': 'something'}), |
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/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
115 None), |
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', 'path')], |
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 {'foo': 'something/other'}), |
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 '', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
123 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
124 ('/blah', {'url': '/prefix/%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 'prefix/something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
127 {'foo': 'something/other'}), |
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/', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
131 {'foo': ''}), |
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': ''}), |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
136 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
137 ('/~johndoe', {'url': '/%foo%'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
138 ['foo'], |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
139 'something', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
140 {'foo': 'something'}), |
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/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
144 None), |
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', 'path')], |
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 {'foo': 'something/other'}), |
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 '', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
152 {'foo': ''}), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
153 ('/~johndoe', {'url': '/prefix/%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 'prefix/something/other', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
156 {'foo': 'something/other'}), |
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/', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
160 {'foo': ''}), |
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 ]) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
166 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
|
167 site_root = site_root.rstrip('/') + '/' |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
168 app = get_mock_app() |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
169 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
|
170 app.sources = [_getMockSource('blah', params)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
171 |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
172 config.setdefault('source', 'blah') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 route = Route(app, config) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
174 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
|
175 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
|
176 assert m == expected_match |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
177 |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
178 |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
179 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
180 'site_root', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
181 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
182 ('/'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
183 ('/whatever'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
184 ('/~johndoe') |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
185 ]) |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
186 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
|
187 with pytest.raises(Exception): |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
188 app = get_mock_app() |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
189 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
|
190 app.sources = [_getMockSource('blah', [('slug', 'path')])] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
191 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
192 config = {'url': '/%slug%', 'source': 'blah'} |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
193 route = Route(app, config) |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
194 route.matchUri('notabsuri') |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
195 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
196 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
197 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
198 'slug, page_num, pretty, expected', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
199 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
200 # Pretty URLs |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
201 ('', 1, True, ''), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
202 ('', 2, True, '2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
203 ('foo', 1, True, 'foo'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
204 ('foo', 2, True, 'foo/2'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
205 ('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
|
206 ('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
|
207 ('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
|
208 ('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
|
209 ('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
|
210 ('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
|
211 ('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
|
212 ('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
|
213 # Ugly URLs |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
214 ('', 1, False, ''), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
215 ('', 2, False, '2.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
216 ('foo', 1, False, 'foo.html'), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
217 ('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
|
218 ('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
|
219 ('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
|
220 ('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
|
221 ('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
|
222 ('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
|
223 ('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
|
224 ('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
|
225 ('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
|
226 ]) |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
227 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
|
228 for root in ['/', '/blah/', '/~johndoe/']: |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
229 app = get_mock_app() |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
236 config = {'url': '/%slug%', 'source': 'blah'} |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
237 route = Route(app, config) |
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
238 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
|
239 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
|
240 |