Mercurial > piecrust2
comparison tests/test_data_assetor.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 | eb958151c8dc |
children | e7b865f8f335 |
comparison
equal
deleted
inserted
replaced
328:2a5996e0d3ec | 329:422052d2e978 |
---|---|
1 import pytest | 1 import pytest |
2 from mock import MagicMock | 2 from mock import MagicMock |
3 from piecrust.data.assetor import (Assetor, UnsupportedAssetsError, | 3 from piecrust.data.assetor import ( |
4 build_base_url) | 4 Assetor, UnsupportedAssetsError, build_base_url) |
5 from .mockutil import mock_fs, mock_fs_scope | 5 from .mockutil import mock_fs, mock_fs_scope |
6 | 6 |
7 | 7 |
8 @pytest.mark.parametrize('fs, expected', [ | 8 @pytest.mark.parametrize('fs, site_root, expected', [ |
9 (mock_fs().withPage('pages/foo/bar'), {}), | 9 (mock_fs().withPage('pages/foo/bar'), '/', {}), |
10 (mock_fs() | 10 (mock_fs() |
11 .withPage('pages/foo/bar') | 11 .withPage('pages/foo/bar') |
12 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), | 12 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), |
13 '/', | |
13 {'one': 'one'}), | 14 {'one': 'one'}), |
14 (mock_fs() | 15 (mock_fs() |
15 .withPage('pages/foo/bar') | 16 .withPage('pages/foo/bar') |
16 .withPageAsset('pages/foo/bar', 'one.txt', 'one') | 17 .withPageAsset('pages/foo/bar', 'one.txt', 'one') |
17 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), | 18 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), |
19 '/', | |
20 {'one': 'one', 'two': 'two'}), | |
21 | |
22 (mock_fs().withPage('pages/foo/bar'), '/whatever', {}), | |
23 (mock_fs() | |
24 .withPage('pages/foo/bar') | |
25 .withPageAsset('pages/foo/bar', 'one.txt', 'one'), | |
26 '/whatever', | |
27 {'one': 'one'}), | |
28 (mock_fs() | |
29 .withPage('pages/foo/bar') | |
30 .withPageAsset('pages/foo/bar', 'one.txt', 'one') | |
31 .withPageAsset('pages/foo/bar', 'two.txt', 'two'), | |
32 '/whatever', | |
18 {'one': 'one', 'two': 'two'}) | 33 {'one': 'one', 'two': 'two'}) |
19 ]) | 34 ]) |
20 def test_assets(fs, expected): | 35 def test_assets(fs, site_root, expected): |
36 fs.withConfig({'site': {'root': site_root}}) | |
21 with mock_fs_scope(fs): | 37 with mock_fs_scope(fs): |
22 page = MagicMock() | 38 page = MagicMock() |
23 page.app = fs.getApp(cache=False) | 39 page.app = fs.getApp(cache=False) |
24 page.app.env.base_asset_url_format = '%uri%' | 40 page.app.env.base_asset_url_format = '%uri%' |
25 page.path = fs.path('/kitchen/pages/foo/bar.md') | 41 page.path = fs.path('/kitchen/pages/foo/bar.md') |
26 assetor = Assetor(page, '/foo/bar') | 42 assetor = Assetor(page, site_root.rstrip('/') + '/foo/bar') |
27 for en in expected.keys(): | 43 for en in expected.keys(): |
28 assert hasattr(assetor, en) | 44 assert hasattr(assetor, en) |
29 path = '/foo/bar/%s.txt' % en | 45 path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en |
30 assert getattr(assetor, en) == path | 46 assert getattr(assetor, en) == path |
31 assert assetor[en] == path | 47 assert assetor[en] == path |
32 | 48 |
33 | 49 |
34 def test_missing_asset(): | 50 def test_missing_asset(): |