annotate tests/test_routing.py @ 893:14cca285f73b

clean: PEP8.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 02 Jul 2017 22:20:48 -0700
parents 58ebf50235a5
children 72f17534d58e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
5 from piecrust.sources.base import PageSource
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
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
23 src = mock.MagicMock(spec=PageSource)
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(
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
30 'config, metadata, params, expected',
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 [
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 ({'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
33 {'foo': 'bar'}, ['foo'], True),
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 ({'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
35 {'zoo': 'zar', 'foo': 'bar'}, ['foo'], True),
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 ({'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
37 {'zoo': 'zar'}, ['foo'], False),
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 ({'url': '/%foo%/%zoo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
39 {'zoo': 'zar'}, ['foo', 'zoo'], False)
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
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(
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
53 'site_root, route_pattern, params, expected_func_parameters',
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 [
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
55 ('/', '/%foo%', ['foo'], ['foo']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
56 ('/', '/%foo%', [('foo', 'path')], ['foo']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
57 ('/', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
58 ('/', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
59 ('/something', '/%foo%', ['foo'], ['foo']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
60 ('/something', '/%foo%', [('foo', 'path')], ['foo']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
61 ('/something', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
62 ('/something', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
63 ('/~johndoe', '/%foo%', ['foo'], ['foo']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
64 ('/~johndoe', '/%foo%', [('foo', 'path')], ['foo']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
65 ('/~johndoe', '/%foo%/%bar%', ['foo', 'bar'], ['foo', 'bar']),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
66 ('/~johndoe', '/%foo%/%bar%', ['foo', ('bar', 'path')], ['foo', 'bar'])
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
67 ])
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
68 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
69 expected_func_parameters):
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
70 app = get_mock_app()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
71 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
72 app.sources = [_getMockSource('blah', params)]
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
73
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
74 config = {'url': route_pattern, 'source': 'blah'}
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
75 route = Route(app, config)
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
76 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
77
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 @pytest.mark.parametrize(
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
80 '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
81 [
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
82 ('/', {'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
83 ['foo'],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 'something',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 {'foo': 'something'}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
86 ('/', {'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
87 ['foo'],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 'something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 None),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
90 ('/', {'url': '/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
91 [('foo', 'path')],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 'something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 {'foo': 'something/other'}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
94 ('/', {'url': '/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
95 [('foo', 'path')],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 '',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 {'foo': ''}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
98 ('/', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
99 [('foo', 'path')],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 'prefix/something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 {'foo': 'something/other'}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
102 ('/', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
103 [('foo', 'path')],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 'prefix/',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 {'foo': ''}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
106 ('/', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
107 [('foo', 'path')],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
108 'prefix',
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
109 {'foo': ''}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
110
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
111 ('/blah', {'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
112 ['foo'],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
113 'something',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
114 {'foo': 'something'}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
115 ('/blah', {'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
116 ['foo'],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
117 'something/other',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
118 None),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
119 ('/blah', {'url': '/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
120 [('foo', 'path')],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
121 'something/other',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
122 {'foo': 'something/other'}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
123 ('/blah', {'url': '/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
124 [('foo', 'path')],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
125 '',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
126 {'foo': ''}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
127 ('/blah', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
128 [('foo', 'path')],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
129 'prefix/something/other',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
130 {'foo': 'something/other'}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
131 ('/blah', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
132 [('foo', 'path')],
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
133 'prefix/',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
134 {'foo': ''}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
135 ('/blah', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
136 [('foo', 'path')],
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 'prefix',
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
138 {'foo': ''}),
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
139
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
140 ('/~johndoe', {'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
141 ['foo'],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
142 'something',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
143 {'foo': 'something'}),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
144 ('/~johndoe', {'url': '/%foo%'},
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
145 ['foo'],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
146 'something/other',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
147 None),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
148 ('/~johndoe', {'url': '/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
149 [('foo', 'path')],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
150 'something/other',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
151 {'foo': 'something/other'}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
152 ('/~johndoe', {'url': '/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
153 [('foo', 'path')],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
154 '',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
155 {'foo': ''}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
156 ('/~johndoe', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
157 [('foo', 'path')],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
158 'prefix/something/other',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
159 {'foo': 'something/other'}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
160 ('/~johndoe', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
161 [('foo', 'path')],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
162 'prefix/',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
163 {'foo': ''}),
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
164 ('/~johndoe', {'url': '/prefix/%foo%'},
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
165 [('foo', 'path')],
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
166 'prefix',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
167 {'foo': ''}),
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 ])
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
169 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
170 site_root = site_root.rstrip('/') + '/'
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
171 app = get_mock_app()
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
172 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
173 app.sources = [_getMockSource('blah', params)]
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
174
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 config.setdefault('source', 'blah')
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 route = Route(app, config)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 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
178 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
179 assert m == expected_match
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
181
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
182 @pytest.mark.parametrize(
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
183 'site_root',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
184 [
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
185 ('/'),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
186 ('/whatever'),
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
187 ('/~johndoe')
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
188 ])
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
189 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
190 with pytest.raises(Exception):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
191 app = get_mock_app()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
192 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
193 app.sources = [_getMockSource('blah', [('slug', 'path')])]
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
194
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
195 config = {'url': '/%slug%', 'source': 'blah'}
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
196 route = Route(app, config)
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
197 route.matchUri('notabsuri')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
198
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 @pytest.mark.parametrize(
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
201 'slug, page_num, pretty, expected',
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
202 [
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
203 # Pretty URLs
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
204 ('', 1, True, ''),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
205 ('', 2, True, '2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
206 ('foo', 1, True, 'foo'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
207 ('foo', 2, True, 'foo/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
208 ('foo/bar', 1, True, 'foo/bar'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
209 ('foo/bar', 2, True, 'foo/bar/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
210 ('foo.ext', 1, True, 'foo.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
211 ('foo.ext', 2, True, 'foo.ext/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
212 ('foo/bar.ext', 1, True, 'foo/bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
213 ('foo/bar.ext', 2, True, 'foo/bar.ext/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
214 ('foo.bar.ext', 1, True, 'foo.bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
215 ('foo.bar.ext', 2, True, 'foo.bar.ext/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
216 # Ugly URLs
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
217 ('', 1, False, ''),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
218 ('', 2, False, '2.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
219 ('foo', 1, False, 'foo.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
220 ('foo', 2, False, 'foo/2.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
221 ('foo/bar', 1, False, 'foo/bar.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
222 ('foo/bar', 2, False, 'foo/bar/2.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
223 ('foo.ext', 1, False, 'foo.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
224 ('foo.ext', 2, False, 'foo/2.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
225 ('foo/bar.ext', 1, False, 'foo/bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
226 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
227 ('foo.bar.ext', 1, False, 'foo.bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
228 ('foo.bar.ext', 2, False, 'foo.bar/2.ext')
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
229 ])
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
230 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
231 for root in ['/', '/blah/', '/~johndoe/']:
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
232 app = get_mock_app()
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
233 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
234 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
235 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
236 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
237 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
238
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
239 config = {'url': '/%slug%', 'source': 'blah'}
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
240 route = Route(app, config)
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
241 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
242 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
243