comparison tests/test_data_assetor.py @ 32:43091c9837bf

Fix problems with asset URLs.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 19 Aug 2014 14:30:19 -0700
parents f5ca5c5bed85
children 62c7a97c8340
comparison
equal deleted inserted replaced
31:8c15fc45d712 32:43091c9837bf
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 (Assetor, UnsupportedAssetsError,
4 build_base_url)
4 from .mockutil import mock_fs, mock_fs_scope 5 from .mockutil import mock_fs, mock_fs_scope
5 6
6 7
7 @pytest.mark.parametrize('fs, expected', [ 8 @pytest.mark.parametrize('fs, expected', [
8 (mock_fs().withPage('foo/bar'), {}), 9 (mock_fs().withPage('pages/foo/bar'), {}),
9 (mock_fs() 10 (mock_fs()
10 .withPage('foo/bar') 11 .withPage('pages/foo/bar')
11 .withPageAsset('foo/bar', 'one.txt', 'one'), 12 .withPageAsset('pages/foo/bar', 'one.txt', 'one'),
12 {'one': 'one'}), 13 {'one': 'one'}),
13 (mock_fs() 14 (mock_fs()
14 .withPage('foo/bar') 15 .withPage('pages/foo/bar')
15 .withPageAsset('foo/bar', 'one.txt', 'one') 16 .withPageAsset('pages/foo/bar', 'one.txt', 'one')
16 .withPageAsset('foo/bar', 'two.txt', 'two'), 17 .withPageAsset('pages/foo/bar', 'two.txt', 'two'),
17 {'one': 'one', 'two': 'two'}) 18 {'one': 'one', 'two': 'two'})
18 ]) 19 ])
19 def test_assets(fs, expected): 20 def test_assets(fs, expected):
20 with mock_fs_scope(fs): 21 with mock_fs_scope(fs):
21 page = MagicMock() 22 page = MagicMock()
22 page.app = fs.getApp() 23 page.app = fs.getApp()
24 page.app.env.base_asset_url_format = '%uri%'
23 page.path = fs.path('/kitchen/_content/pages/foo/bar.md') 25 page.path = fs.path('/kitchen/_content/pages/foo/bar.md')
24 assetor = Assetor(page, '/foo/bar') 26 assetor = Assetor(page, '/foo/bar')
25 for en in expected.keys(): 27 for en in expected.keys():
26 assert hasattr(assetor, en) 28 assert hasattr(assetor, en)
27 path = '/foo/bar/%s.txt' % en 29 path = '/foo/bar/%s.txt' % en
29 assert assetor[en] == path 31 assert assetor[en] == path
30 32
31 33
32 def test_missing_asset(): 34 def test_missing_asset():
33 with pytest.raises(KeyError): 35 with pytest.raises(KeyError):
34 fs = mock_fs().withPage('foo/bar') 36 fs = mock_fs().withPage('pages/foo/bar')
35 with mock_fs_scope(fs): 37 with mock_fs_scope(fs):
36 page = MagicMock() 38 page = MagicMock()
37 page.app = fs.getApp() 39 page.app = fs.getApp()
38 page.path = fs.path('/kitchen/_content/pages/foo/bar.md') 40 page.path = fs.path('/kitchen/_content/pages/foo/bar.md')
39 assetor = Assetor(page, '/foo/bar') 41 assetor = Assetor(page, '/foo/bar')
41 43
42 44
43 def test_multiple_assets_with_same_name(): 45 def test_multiple_assets_with_same_name():
44 with pytest.raises(UnsupportedAssetsError): 46 with pytest.raises(UnsupportedAssetsError):
45 fs = (mock_fs() 47 fs = (mock_fs()
46 .withPage('foo/bar') 48 .withPage('pages/foo/bar')
47 .withPageAsset('foo/bar', 'one.txt', 'one text') 49 .withPageAsset('pages/foo/bar', 'one.txt', 'one text')
48 .withPageAsset('foo/bar', 'one.jpg', 'one picture')) 50 .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture'))
49 with mock_fs_scope(fs): 51 with mock_fs_scope(fs):
50 page = MagicMock() 52 page = MagicMock()
51 page.app = fs.getApp() 53 page.app = fs.getApp()
52 page.path = fs.path('/kitchen/_content/pages/foo/bar.md') 54 page.path = fs.path('/kitchen/_content/pages/foo/bar.md')
53 assetor = Assetor(page, '/foo/bar') 55 assetor = Assetor(page, '/foo/bar')
54 assetor['one'] 56 assetor['one']
55 57
58
59 @pytest.mark.parametrize('url_format, pretty_urls, uri, expected', [
60 ('%uri%', True, '/foo', '/foo/'),
61 ('%uri%', True, '/foo/2', '/foo/'),
62 ('%uri%', True, '/foo.ext', '/foo.ext/'),
63 ('%uri%', True, '/foo.ext/2', '/foo.ext/'),
64 ('%uri%', False, '/foo.html', '/foo/'),
65 ('%uri%', False, '/foo/2.html', '/foo/'),
66 ])
67 def test_build_base_url(url_format, pretty_urls, uri, expected):
68 app = MagicMock()
69 app.env = MagicMock()
70 app.env.base_asset_url_format = url_format
71 app.config = {
72 'site/pretty_urls': pretty_urls,
73 '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
74 assets_path = 'foo/bar-assets'
75 actual = build_base_url(app, uri, assets_path)
76 assert actual == expected
77