comparison tests/test_routing.py @ 262:61145dcd56e0

routing: Better generate URLs according to the site configuration. This fixes problems now that even the chef server generates URLs that are appropriate given the `pretty_urls` and `trailing_slash` settings. Add some tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 24 Feb 2015 08:06:25 -0800
parents 4b0c87e7df73
children 422052d2e978
comparison
equal deleted inserted replaced
261:b51ddb0c260b 262:61145dcd56e0
1 import mock 1 import mock
2 import pytest 2 import pytest
3 from piecrust.routing import Route 3 from piecrust.routing import Route
4 from .mockutil import get_mock_app
4 5
5 6
6 @pytest.mark.parametrize( 7 @pytest.mark.parametrize(
7 'config, metadata, expected', 8 'config, metadata, expected',
8 [ 9 [
56 route = Route(app, config) 57 route = Route(app, config)
57 assert route.uri_pattern == config['url'].lstrip('/') 58 assert route.uri_pattern == config['url'].lstrip('/')
58 m = route.matchUri(uri) 59 m = route.matchUri(uri)
59 assert m == expected_match 60 assert m == expected_match
60 61
62
63 @pytest.mark.parametrize(
64 'slug, page_num, pretty, expected',
65 [
66 # Pretty URLs
67 ('', 1, True, ''),
68 ('', 2, True, '2'),
69 ('foo', 1, True, 'foo'),
70 ('foo', 2, True, 'foo/2'),
71 ('foo/bar', 1, True, 'foo/bar'),
72 ('foo/bar', 2, True, 'foo/bar/2'),
73 ('foo.ext', 1, True, 'foo.ext'),
74 ('foo.ext', 2, True, 'foo.ext/2'),
75 ('foo/bar.ext', 1, True, 'foo/bar.ext'),
76 ('foo/bar.ext', 2, True, 'foo/bar.ext/2'),
77 ('foo.bar.ext', 1, True, 'foo.bar.ext'),
78 ('foo.bar.ext', 2, True, 'foo.bar.ext/2'),
79 # Ugly URLs
80 ('', 1, False, ''),
81 ('', 2, False, '2.html'),
82 ('foo', 1, False, 'foo.html'),
83 ('foo', 2, False, 'foo/2.html'),
84 ('foo/bar', 1, False, 'foo/bar.html'),
85 ('foo/bar', 2, False, 'foo/bar/2.html'),
86 ('foo.ext', 1, False, 'foo.ext'),
87 ('foo.ext', 2, False, 'foo/2.ext'),
88 ('foo/bar.ext', 1, False, 'foo/bar.ext'),
89 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'),
90 ('foo.bar.ext', 1, False, 'foo.bar.ext'),
91 ('foo.bar.ext', 2, False, 'foo.bar/2.ext')
92 ])
93 def test_get_uri(slug, page_num, pretty, expected):
94 app = get_mock_app()
95 app.config.set('site/root', '/blah')
96 app.config.set('site/pretty_urls', pretty)
97 app.config.set('site/trailing_slash', False)
98 app.config.set('__cache/pagination_suffix_format', '/%(num)d')
99
100 config = {'url': '/%path:slug%', 'source': 'blah'}
101 route = Route(app, config)
102 uri = route.getUri({'slug': slug}, sub_num=page_num)
103 assert uri == ('/blah/' + expected)
104