diff 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
line wrap: on
line diff
--- a/tests/test_routing.py	Tue Feb 24 08:04:49 2015 -0800
+++ b/tests/test_routing.py	Tue Feb 24 08:06:25 2015 -0800
@@ -1,6 +1,7 @@
 import mock
 import pytest
 from piecrust.routing import Route
+from .mockutil import get_mock_app
 
 
 @pytest.mark.parametrize(
@@ -58,3 +59,46 @@
     m = route.matchUri(uri)
     assert m == expected_match
 
+
+@pytest.mark.parametrize(
+        'slug, page_num, pretty, expected',
+        [
+            # Pretty URLs
+            ('', 1, True, ''),
+            ('', 2, True, '2'),
+            ('foo', 1, True, 'foo'),
+            ('foo', 2, True, 'foo/2'),
+            ('foo/bar', 1, True, 'foo/bar'),
+            ('foo/bar', 2, True, 'foo/bar/2'),
+            ('foo.ext', 1, True, 'foo.ext'),
+            ('foo.ext', 2, True, 'foo.ext/2'),
+            ('foo/bar.ext', 1, True, 'foo/bar.ext'),
+            ('foo/bar.ext', 2, True, 'foo/bar.ext/2'),
+            ('foo.bar.ext', 1, True, 'foo.bar.ext'),
+            ('foo.bar.ext', 2, True, 'foo.bar.ext/2'),
+            # Ugly URLs
+            ('', 1, False, ''),
+            ('', 2, False, '2.html'),
+            ('foo', 1, False, 'foo.html'),
+            ('foo', 2, False, 'foo/2.html'),
+            ('foo/bar', 1, False, 'foo/bar.html'),
+            ('foo/bar', 2, False, 'foo/bar/2.html'),
+            ('foo.ext', 1, False, 'foo.ext'),
+            ('foo.ext', 2, False, 'foo/2.ext'),
+            ('foo/bar.ext', 1, False, 'foo/bar.ext'),
+            ('foo/bar.ext', 2, False, 'foo/bar/2.ext'),
+            ('foo.bar.ext', 1, False, 'foo.bar.ext'),
+            ('foo.bar.ext', 2, False, 'foo.bar/2.ext')
+            ])
+def test_get_uri(slug, page_num, pretty, expected):
+    app = get_mock_app()
+    app.config.set('site/root', '/blah')
+    app.config.set('site/pretty_urls', pretty)
+    app.config.set('site/trailing_slash', False)
+    app.config.set('__cache/pagination_suffix_format', '/%(num)d')
+
+    config = {'url': '/%path:slug%', 'source': 'blah'}
+    route = Route(app, config)
+    uri = route.getUri({'slug': slug}, sub_num=page_num)
+    assert uri == ('/blah/' + expected)
+