annotate tests/test_data_assetor.py @ 1145:e94737572542

serve: Fix an issue where false positive matches were rendered as the requested page. Now we try to render the page, but also try to detect for the most common "empty" pages.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 22:08:51 -0700
parents 11b9d0c8bd62
children
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
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
2 from piecrust.data.assetor import Assetor, UnsupportedAssetsError
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
3 from .mockutil import mock_fs, mock_fs_scope, get_simple_page
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
6 @pytest.mark.parametrize('fs_fac, site_root, expected', [
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
7 (lambda: mock_fs().withPage('pages/foo/bar'), '/', {}),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
8 (lambda: mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
9 .withPage('pages/foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
10 .withPageAsset('pages/foo/bar', 'one.txt', 'one'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
11 '/',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
12 {'one': 'one'}),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
13 (lambda: mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
14 .withPage('pages/foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
15 .withPageAsset('pages/foo/bar', 'one.txt', 'one')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
16 .withPageAsset('pages/foo/bar', 'two.txt', 'two'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
17 '/',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
18 {'one': 'one', 'two': 'two'}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
19
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
20 (lambda: mock_fs().withPage('pages/foo/bar'), '/whatever', {}),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
21 (lambda: mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
22 .withPage('pages/foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
23 .withPageAsset('pages/foo/bar', 'one.txt', 'one'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
24 '/whatever',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
25 {'one': 'one'}),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
26 (lambda: mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
27 .withPage('pages/foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
28 .withPageAsset('pages/foo/bar', 'one.txt', 'one')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
29 .withPageAsset('pages/foo/bar', 'two.txt', 'two'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
30 '/whatever',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
31 {'one': 'one', 'two': 'two'})
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
32 ])
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
33 def test_assets(fs_fac, site_root, expected):
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
34 fs = fs_fac()
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 316
diff changeset
35 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
36 with mock_fs_scope(fs):
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
37 app = fs.getApp()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
38 app.config.set('site/asset_url_format', '%page_uri%/%filename%')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
39 page = get_simple_page(app, 'foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
40
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
41 assetor = Assetor(page)
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 for en in expected.keys():
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
43 assert en in assetor
6
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
1115
11b9d0c8bd62 tests: Fix assetor tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
46 assert str(assetor[en]) == path
11b9d0c8bd62 tests: Fix assetor tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
47 assert str(getattr(assetor, en)) == path
6
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):
674
f987b29d6fab tests: Add ability to run tests with a theme site.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
52 fs = (mock_fs()
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
53 .withConfig()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
54 .withPage('pages/foo/bar'))
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 with mock_fs_scope(fs):
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
56 app = fs.getApp()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
57 app.config.set('site/asset_url_format', '%page_uri%/%filename%')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
58 page = get_simple_page(app, 'foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
59
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
60 assetor = Assetor(page)
6
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 assetor['this_doesnt_exist']
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 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
65 with pytest.raises(UnsupportedAssetsError):
f5ca5c5bed85 More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 fs = (mock_fs()
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
67 .withConfig()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
68 .withPage('pages/foo/bar')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
69 .withPageAsset('pages/foo/bar', 'one.txt', 'one text')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
70 .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
71 with mock_fs_scope(fs):
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
72 app = fs.getApp()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
73 app.config.set('site/asset_url_format', '%page_uri%/%filename%')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
74 page = get_simple_page(app, 'foo/bar')
32
43091c9837bf Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
75
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
76 assetor = Assetor(page)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
77 assetor['one']