diff tests/test_routing.py @ 329:422052d2e978

internal: Try handling URLs in a consistent way. * Now URLs passed to, and returned from, routes will always be absolute URLs, i.e. URLs including the site root. * Validate the site root at config loading time to make sure it starts and ends with a slash. * Get rid of unused stuff. * Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 31 Mar 2015 23:03:28 -0700
parents 61145dcd56e0
children 4b1019bb2533
line wrap: on
line diff
--- a/tests/test_routing.py	Tue Mar 31 22:38:56 2015 -0700
+++ b/tests/test_routing.py	Tue Mar 31 23:03:28 2015 -0700
@@ -1,4 +1,3 @@
-import mock
 import pytest
 from piecrust.routing import Route
 from .mockutil import get_mock_app
@@ -17,8 +16,8 @@
                 {'zoo': 'zar'}, False)
             ])
 def test_matches_metadata(config, metadata, expected):
-    app = mock.Mock()
-    app.config = {'site/root': '/'}
+    app = get_mock_app()
+    app.config.set('site/root', '/')
     config.setdefault('source', 'blah')
     route = Route(app, config)
     m = route.matchesMetadata(metadata)
@@ -26,41 +25,99 @@
 
 
 @pytest.mark.parametrize(
-        'config, uri, expected_match',
+        'site_root, route_pattern, expected_required_metadata',
         [
-            ({'url': '/%foo%'},
+            ('/', '/%foo%', set(['foo'])),
+            ('/', '/%path:foo%', set(['foo'])),
+            ('/', '/%foo%/%bar%', set(['foo', 'bar'])),
+            ('/', '/%foo%/%path:bar%', set(['foo', 'bar'])),
+            ('/something', '/%foo%', set(['foo'])),
+            ('/something', '/%path:foo%', set(['foo'])),
+            ('/something', '/%foo%/%bar%', set(['foo', 'bar'])),
+            ('/something', '/%foo%/%path:bar%', set(['foo', 'bar']))
+            ])
+def test_required_metadata(site_root, route_pattern,
+                           expected_required_metadata):
+    app = get_mock_app()
+    app.config.set('site/root', site_root.rstrip('/') + '/')
+    config = {'url': route_pattern, 'source': 'blah'}
+    route = Route(app, config)
+    assert route.required_source_metadata == expected_required_metadata
+
+
+@pytest.mark.parametrize(
+        'site_root, config, uri, expected_match',
+        [
+            ('/', {'url': '/%foo%'},
                 'something',
                 {'foo': 'something'}),
-            ({'url': '/%foo%'},
+            ('/', {'url': '/%foo%'},
                 'something/other',
                 None),
-            ({'url': '/%path:foo%'},
+            ('/', {'url': '/%path:foo%'},
                 'something/other',
                 {'foo': 'something/other'}),
-            ({'url': '/%path:foo%'},
+            ('/', {'url': '/%path:foo%'},
                 '',
                 {'foo': ''}),
-            ({'url': '/prefix/%path:foo%'},
+            ('/', {'url': '/prefix/%path:foo%'},
                 'prefix/something/other',
                 {'foo': 'something/other'}),
-            ({'url': '/prefix/%path:foo%'},
+            ('/', {'url': '/prefix/%path:foo%'},
                 'prefix/',
                 {'foo': ''}),
-            ({'url': '/prefix/%path:foo%'},
+            ('/', {'url': '/prefix/%path:foo%'},
+                'prefix',
+                {}),
+
+            ('/blah', {'url': '/%foo%'},
+                'something',
+                {'foo': 'something'}),
+            ('/blah', {'url': '/%foo%'},
+                'something/other',
+                None),
+            ('/blah', {'url': '/%path:foo%'},
+                'something/other',
+                {'foo': 'something/other'}),
+            ('/blah', {'url': '/%path:foo%'},
+                '',
+                {'foo': ''}),
+            ('/blah', {'url': '/prefix/%path:foo%'},
+                'prefix/something/other',
+                {'foo': 'something/other'}),
+            ('/blah', {'url': '/prefix/%path:foo%'},
+                'prefix/',
+                {'foo': ''}),
+            ('/blah', {'url': '/prefix/%path:foo%'},
                 'prefix',
                 {}),
             ])
-def test_match_uri(config, uri, expected_match):
-    app = mock.Mock()
-    app.config = {'site/root': '/'}
+def test_match_uri(site_root, config, uri, expected_match):
+    site_root = site_root.rstrip('/') + '/'
+    app = get_mock_app()
+    app.config.set('site/root', site_root)
     config.setdefault('source', 'blah')
     route = Route(app, config)
     assert route.uri_pattern == config['url'].lstrip('/')
-    m = route.matchUri(uri)
+    m = route.matchUri(site_root + uri)
     assert m == expected_match
 
 
 @pytest.mark.parametrize(
+        'site_root',
+        [
+            ('/'), ('/whatever')
+            ])
+def test_match_uri_requires_absolute_uri(site_root):
+    with pytest.raises(Exception):
+        app = get_mock_app()
+        app.config.set('site/root', site_root.rstrip('/') + '/')
+        config = {'url': '/%path:slug%', 'source': 'blah'}
+        route = Route(app, config)
+        route.matchUri('notabsuri')
+
+
+@pytest.mark.parametrize(
         'slug, page_num, pretty, expected',
         [
             # Pretty URLs
@@ -92,7 +149,7 @@
             ])
 def test_get_uri(slug, page_num, pretty, expected):
     app = get_mock_app()
-    app.config.set('site/root', '/blah')
+    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')