annotate tests/test_data_assetor.py @ 380:f33712c4cfab

routing: Fix bugs with matching URLs with correct route but missing metadata. When matching a route like `/foo/%slug%` against an URL like `/foo`, the route will (correctly) return a match, but it will be completely missing the `slug` metadata, resulting in problems elsewhere. This change makes it so that any missing route metadata will be filled in with an empty string. And because this means generated URLs may differ from the incoming URL when using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the chef server handle those discrepancies.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 May 2015 00:34:21 -0700
parents 422052d2e978
children e7b865f8f335
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import pytest
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from mock import MagicMock
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
3 from piecrust.data.assetor import (
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
4 Assetor, UnsupportedAssetsError, build_base_url)
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from .mockutil import mock_fs, mock_fs_scope
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
8 @pytest.mark.parametrize('fs, site_root, expected', [
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
9 (mock_fs().withPage('pages/foo/bar'), '/', {}),
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 (mock_fs()
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
11 .withPage('pages/foo/bar')
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
12 .withPageAsset('pages/foo/bar', 'one.txt', 'one'),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
13 '/',
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 {'one': 'one'}),
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 (mock_fs()
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
16 .withPage('pages/foo/bar')
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
17 .withPageAsset('pages/foo/bar', 'one.txt', 'one')
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
18 .withPageAsset('pages/foo/bar', 'two.txt', 'two'),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
19 '/',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
20 {'one': 'one', 'two': 'two'}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
21
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
22 (mock_fs().withPage('pages/foo/bar'), '/whatever', {}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
23 (mock_fs()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
24 .withPage('pages/foo/bar')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
25 .withPageAsset('pages/foo/bar', 'one.txt', 'one'),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
26 '/whatever',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
27 {'one': 'one'}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
28 (mock_fs()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
29 .withPage('pages/foo/bar')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
30 .withPageAsset('pages/foo/bar', 'one.txt', 'one')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
31 .withPageAsset('pages/foo/bar', 'two.txt', 'two'),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
32 '/whatever',
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 {'one': 'one', 'two': 'two'})
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 ])
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
35 def test_assets(fs, site_root, expected):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
36 fs.withConfig({'site': {'root': site_root}})
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 with mock_fs_scope(fs):
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 page = MagicMock()
85
3471ffa059b2 Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
39 page.app = fs.getApp(cache=False)
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
40 page.app.env.base_asset_url_format = '%uri%'
36
485682a6de50 New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
41 page.path = fs.path('/kitchen/pages/foo/bar.md')
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
42 assetor = Assetor(page, site_root.rstrip('/') + '/foo/bar')
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 for en in expected.keys():
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 assert hasattr(assetor, en)
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
45 path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 assert getattr(assetor, en) == path
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 assert assetor[en] == path
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 def test_missing_asset():
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 with pytest.raises(KeyError):
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
52 fs = mock_fs().withPage('pages/foo/bar')
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 with mock_fs_scope(fs):
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 page = MagicMock()
85
3471ffa059b2 Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
55 page.app = fs.getApp(cache=False)
36
485682a6de50 New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
56 page.path = fs.path('/kitchen/pages/foo/bar.md')
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 assetor = Assetor(page, '/foo/bar')
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 assetor['this_doesnt_exist']
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 def test_multiple_assets_with_same_name():
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 with pytest.raises(UnsupportedAssetsError):
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 fs = (mock_fs()
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
64 .withPage('pages/foo/bar')
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
65 .withPageAsset('pages/foo/bar', 'one.txt', 'one text')
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
66 .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture'))
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 with mock_fs_scope(fs):
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 page = MagicMock()
85
3471ffa059b2 Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
69 page.app = fs.getApp(cache=False)
36
485682a6de50 New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
70 page.path = fs.path('/kitchen/pages/foo/bar.md')
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 assetor = Assetor(page, '/foo/bar')
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 assetor['one']
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
74
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
75 @pytest.mark.parametrize('url_format, pretty_urls, uri, expected', [
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
76 ('%uri%', True, '/foo', '/foo/'),
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
77 ('%uri%', True, '/foo.ext', '/foo.ext/'),
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
78 ('%uri%', False, '/foo.html', '/foo/'),
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 32
diff changeset
79 ('%uri%', False, '/foo.ext', '/foo/'),
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
80 ])
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
81 def test_build_base_url(url_format, pretty_urls, uri, expected):
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
82 app = MagicMock()
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
83 app.env = MagicMock()
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
84 app.env.base_asset_url_format = url_format
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
85 app.config = {
316
eb958151c8dc tests: Bad me, the tests were broken. Now they're fixed.
Ludovic Chabant <ludovic@chabant.com>
parents: 85
diff changeset
86 'site/root': '/',
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 32
diff changeset
87 'site/pretty_urls': pretty_urls}
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
88 assets_path = 'foo/bar-assets'
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
89 actual = build_base_url(app, uri, assets_path)
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
90 assert actual == expected
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
91