Mercurial > piecrust2
annotate tests/test_routing.py @ 273:d70a4adb61dd
themes: Add the `chef themes` command
The `themes` command lets the user create or override a theme easily.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 01 Mar 2015 09:07:36 -0800 |
parents | 61145dcd56e0 |
children | 422052d2e978 |
rev | line source |
---|---|
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import mock |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import pytest |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.routing import Route |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
4 from .mockutil import get_mock_app |
177
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 @pytest.mark.parametrize( |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 'config, metadata, expected', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 [ |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 ({'url': '/%foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 {'foo': 'bar'}, True), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 ({'url': '/%foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 {'zoo': 'zar', 'foo': 'bar'}, True), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 ({'url': '/%foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 {'zoo': 'zar'}, False), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 ({'url': '/%foo%/%zoo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 {'zoo': 'zar'}, False) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 ]) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 def test_matches_metadata(config, metadata, expected): |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 app = mock.Mock() |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 app.config = {'site/root': '/'} |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 config.setdefault('source', 'blah') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 route = Route(app, config) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 m = route.matchesMetadata(metadata) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 assert m == expected |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 @pytest.mark.parametrize( |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 'config, uri, expected_match', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 [ |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 ({'url': '/%foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 'something', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 {'foo': 'something'}), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 ({'url': '/%foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 'something/other', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 None), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 ({'url': '/%path:foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 'something/other', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 {'foo': 'something/other'}), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 ({'url': '/%path:foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 '', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 {'foo': ''}), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 ({'url': '/prefix/%path:foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 'prefix/something/other', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 {'foo': 'something/other'}), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 ({'url': '/prefix/%path:foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 'prefix/', |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 {'foo': ''}), |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 ({'url': '/prefix/%path:foo%'}, |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 'prefix', |
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 ]) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 def test_match_uri(config, uri, expected_match): |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 app = mock.Mock() |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 app.config = {'site/root': '/'} |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 config.setdefault('source', 'blah') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 route = Route(app, config) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 assert route.uri_pattern == config['url'].lstrip('/') |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 m = route.matchUri(uri) |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 assert m == expected_match |
4b0c87e7df73
tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
62 |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
63 @pytest.mark.parametrize( |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
64 'slug, page_num, pretty, expected', |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
65 [ |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
66 # Pretty URLs |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
67 ('', 1, True, ''), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
68 ('', 2, True, '2'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
69 ('foo', 1, True, 'foo'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
70 ('foo', 2, True, 'foo/2'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
71 ('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
|
72 ('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
|
73 ('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
|
74 ('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
|
75 ('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
|
76 ('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
|
77 ('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
|
78 ('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
|
79 # Ugly URLs |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
80 ('', 1, False, ''), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
81 ('', 2, False, '2.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
82 ('foo', 1, False, 'foo.html'), |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
83 ('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
|
84 ('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
|
85 ('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
|
86 ('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
|
87 ('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
|
88 ('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
|
89 ('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
|
90 ('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
|
91 ('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
|
92 ]) |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
93 def test_get_uri(slug, page_num, pretty, expected): |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
94 app = get_mock_app() |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
95 app.config.set('site/root', '/blah') |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
96 app.config.set('site/pretty_urls', pretty) |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
97 app.config.set('site/trailing_slash', False) |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
98 app.config.set('__cache/pagination_suffix_format', '/%(num)d') |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
99 |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
100 config = {'url': '/%path:slug%', 'source': 'blah'} |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
101 route = Route(app, config) |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
102 uri = route.getUri({'slug': slug}, sub_num=page_num) |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
103 assert uri == ('/blah/' + expected) |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
177
diff
changeset
|
104 |