Mercurial > piecrust2
comparison tests/test_data_assetor.py @ 974:72f17534d58e
tests: First pass on making unit tests work again.
- Fix all imports
- Add more helper functions to work with mock file-systems
- Simplify some code by running chef directly on the mock FS
- Fix a couple tests
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 17 Oct 2017 01:07:30 -0700 |
parents | f987b29d6fab |
children | 45ad976712ec |
comparison
equal
deleted
inserted
replaced
973:8419daaa7a0e | 974:72f17534d58e |
---|---|
1 import pytest | 1 import pytest |
2 from mock import MagicMock | 2 from piecrust.data.assetor import Assetor, UnsupportedAssetsError |
3 from piecrust.data.assetor import ( | 3 from .mockutil import mock_fs, mock_fs_scope, get_simple_page |
4 Assetor, UnsupportedAssetsError, build_base_url) | |
5 from .mockutil import mock_fs, mock_fs_scope | |
6 | 4 |
7 | 5 |
8 @pytest.mark.parametrize('fs_fac, site_root, expected', [ | 6 @pytest.mark.parametrize('fs_fac, site_root, expected', [ |
9 (lambda: mock_fs().withPage('pages/foo/bar'), '/', {}), | 7 (lambda: mock_fs().withPage('pages/foo/bar'), '/', {}), |
10 (lambda: mock_fs() | 8 (lambda: mock_fs() |
11 .withPage('pages/foo/bar') | 9 .withPage('pages/foo/bar') |
12 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), | 10 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), |
13 '/', | 11 '/', |
14 {'one': 'one'}), | 12 {'one': 'one'}), |
15 (lambda: mock_fs() | 13 (lambda: mock_fs() |
16 .withPage('pages/foo/bar') | 14 .withPage('pages/foo/bar') |
17 .withPageAsset('pages/foo/bar', 'one.txt', 'one') | 15 .withPageAsset('pages/foo/bar', 'one.txt', 'one') |
18 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), | 16 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), |
19 '/', | 17 '/', |
20 {'one': 'one', 'two': 'two'}), | 18 {'one': 'one', 'two': 'two'}), |
21 | 19 |
22 (lambda: mock_fs().withPage('pages/foo/bar'), '/whatever', {}), | 20 (lambda: mock_fs().withPage('pages/foo/bar'), '/whatever', {}), |
23 (lambda: mock_fs() | 21 (lambda: mock_fs() |
24 .withPage('pages/foo/bar') | 22 .withPage('pages/foo/bar') |
25 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), | 23 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), |
26 '/whatever', | 24 '/whatever', |
27 {'one': 'one'}), | 25 {'one': 'one'}), |
28 (lambda: mock_fs() | 26 (lambda: mock_fs() |
29 .withPage('pages/foo/bar') | 27 .withPage('pages/foo/bar') |
30 .withPageAsset('pages/foo/bar', 'one.txt', 'one') | 28 .withPageAsset('pages/foo/bar', 'one.txt', 'one') |
31 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), | 29 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), |
32 '/whatever', | 30 '/whatever', |
33 {'one': 'one', 'two': 'two'}) | 31 {'one': 'one', 'two': 'two'}) |
34 ]) | 32 ]) |
35 def test_assets(fs_fac, site_root, expected): | 33 def test_assets(fs_fac, site_root, expected): |
36 fs = fs_fac() | 34 fs = fs_fac() |
37 fs.withConfig({'site': {'root': site_root}}) | 35 fs.withConfig({'site': {'root': site_root}}) |
38 with mock_fs_scope(fs): | 36 with mock_fs_scope(fs): |
39 page = MagicMock() | 37 app = fs.getApp() |
40 page.app = fs.getApp(cache=False) | 38 app.config.set('site/asset_url_format', '%page_uri%/%filename%') |
41 page.app.env.base_asset_url_format = '%uri%' | 39 page = get_simple_page(app, 'foo/bar') |
42 page.path = fs.path('/kitchen/pages/foo/bar.md') | 40 |
43 assetor = Assetor(page, site_root.rstrip('/') + '/foo/bar') | 41 assetor = Assetor(page) |
44 for en in expected.keys(): | 42 for en in expected.keys(): |
45 assert hasattr(assetor, en) | 43 assert hasattr(assetor, en) |
44 assert en in assetor | |
46 path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en | 45 path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en |
47 assert getattr(assetor, en) == path | 46 assert getattr(assetor, en) == path |
48 assert assetor[en] == path | 47 assert assetor[en] == path |
49 | 48 |
50 | 49 |
51 def test_missing_asset(): | 50 def test_missing_asset(): |
52 with pytest.raises(KeyError): | 51 with pytest.raises(KeyError): |
53 fs = (mock_fs() | 52 fs = (mock_fs() |
54 .withConfig() | 53 .withConfig() |
55 .withPage('pages/foo/bar')) | 54 .withPage('pages/foo/bar')) |
56 with mock_fs_scope(fs): | 55 with mock_fs_scope(fs): |
57 page = MagicMock() | 56 app = fs.getApp() |
58 page.app = fs.getApp(cache=False) | 57 app.config.set('site/asset_url_format', '%page_uri%/%filename%') |
59 page.path = fs.path('/kitchen/pages/foo/bar.md') | 58 page = get_simple_page(app, 'foo/bar') |
60 assetor = Assetor(page, '/foo/bar') | 59 |
60 assetor = Assetor(page) | |
61 assetor['this_doesnt_exist'] | 61 assetor['this_doesnt_exist'] |
62 | 62 |
63 | 63 |
64 def test_multiple_assets_with_same_name(): | 64 def test_multiple_assets_with_same_name(): |
65 with pytest.raises(UnsupportedAssetsError): | 65 with pytest.raises(UnsupportedAssetsError): |
66 fs = (mock_fs() | 66 fs = (mock_fs() |
67 .withConfig() | 67 .withConfig() |
68 .withPage('pages/foo/bar') | 68 .withPage('pages/foo/bar') |
69 .withPageAsset('pages/foo/bar', 'one.txt', 'one text') | 69 .withPageAsset('pages/foo/bar', 'one.txt', 'one text') |
70 .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture')) | 70 .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture')) |
71 with mock_fs_scope(fs): | 71 with mock_fs_scope(fs): |
72 page = MagicMock() | 72 app = fs.getApp() |
73 page.app = fs.getApp(cache=False) | 73 app.config.set('site/asset_url_format', '%page_uri%/%filename%') |
74 page.path = fs.path('/kitchen/pages/foo/bar.md') | 74 page = get_simple_page(app, 'foo/bar') |
75 assetor = Assetor(page, '/foo/bar') | 75 |
76 assetor = Assetor(page) | |
76 assetor['one'] | 77 assetor['one'] |
77 | |
78 | |
79 @pytest.mark.parametrize('url_format, pretty_urls, uri, expected', [ | |
80 ('%uri%', True, '/foo', '/foo/'), | |
81 ('%uri%', True, '/foo.ext', '/foo.ext/'), | |
82 ('%uri%', False, '/foo.html', '/foo/'), | |
83 ('%uri%', False, '/foo.ext', '/foo/'), | |
84 ]) | |
85 def test_build_base_url(url_format, pretty_urls, uri, expected): | |
86 app = MagicMock() | |
87 app.env = MagicMock() | |
88 app.env.base_asset_url_format = url_format | |
89 app.config = { | |
90 'site/root': '/', | |
91 'site/pretty_urls': pretty_urls} | |
92 assets_path = 'foo/bar-assets' | |
93 actual = build_base_url(app, uri, assets_path) | |
94 assert actual == expected | |
95 |