Mercurial > piecrust2
diff 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 |
line wrap: on
line diff
--- a/tests/test_data_assetor.py Tue Oct 17 01:04:10 2017 -0700 +++ b/tests/test_data_assetor.py Tue Oct 17 01:07:30 2017 -0700 @@ -1,48 +1,47 @@ import pytest -from mock import MagicMock -from piecrust.data.assetor import ( - Assetor, UnsupportedAssetsError, build_base_url) -from .mockutil import mock_fs, mock_fs_scope +from piecrust.data.assetor import Assetor, UnsupportedAssetsError +from .mockutil import mock_fs, mock_fs_scope, get_simple_page @pytest.mark.parametrize('fs_fac, site_root, expected', [ - (lambda: mock_fs().withPage('pages/foo/bar'), '/', {}), - (lambda: mock_fs() - .withPage('pages/foo/bar') - .withPageAsset('pages/foo/bar', 'one.txt', 'one'), - '/', - {'one': 'one'}), - (lambda: mock_fs() - .withPage('pages/foo/bar') - .withPageAsset('pages/foo/bar', 'one.txt', 'one') - .withPageAsset('pages/foo/bar', 'two.txt', 'two'), - '/', - {'one': 'one', 'two': 'two'}), + (lambda: mock_fs().withPage('pages/foo/bar'), '/', {}), + (lambda: mock_fs() + .withPage('pages/foo/bar') + .withPageAsset('pages/foo/bar', 'one.txt', 'one'), + '/', + {'one': 'one'}), + (lambda: mock_fs() + .withPage('pages/foo/bar') + .withPageAsset('pages/foo/bar', 'one.txt', 'one') + .withPageAsset('pages/foo/bar', 'two.txt', 'two'), + '/', + {'one': 'one', 'two': 'two'}), - (lambda: mock_fs().withPage('pages/foo/bar'), '/whatever', {}), - (lambda: mock_fs() - .withPage('pages/foo/bar') - .withPageAsset('pages/foo/bar', 'one.txt', 'one'), - '/whatever', - {'one': 'one'}), - (lambda: mock_fs() - .withPage('pages/foo/bar') - .withPageAsset('pages/foo/bar', 'one.txt', 'one') - .withPageAsset('pages/foo/bar', 'two.txt', 'two'), - '/whatever', - {'one': 'one', 'two': 'two'}) - ]) + (lambda: mock_fs().withPage('pages/foo/bar'), '/whatever', {}), + (lambda: mock_fs() + .withPage('pages/foo/bar') + .withPageAsset('pages/foo/bar', 'one.txt', 'one'), + '/whatever', + {'one': 'one'}), + (lambda: mock_fs() + .withPage('pages/foo/bar') + .withPageAsset('pages/foo/bar', 'one.txt', 'one') + .withPageAsset('pages/foo/bar', 'two.txt', 'two'), + '/whatever', + {'one': 'one', 'two': 'two'}) +]) def test_assets(fs_fac, site_root, expected): fs = fs_fac() fs.withConfig({'site': {'root': site_root}}) with mock_fs_scope(fs): - page = MagicMock() - page.app = fs.getApp(cache=False) - page.app.env.base_asset_url_format = '%uri%' - page.path = fs.path('/kitchen/pages/foo/bar.md') - assetor = Assetor(page, site_root.rstrip('/') + '/foo/bar') + app = fs.getApp() + app.config.set('site/asset_url_format', '%page_uri%/%filename%') + page = get_simple_page(app, 'foo/bar') + + assetor = Assetor(page) for en in expected.keys(): assert hasattr(assetor, en) + assert en in assetor path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en assert getattr(assetor, en) == path assert assetor[en] == path @@ -51,45 +50,28 @@ def test_missing_asset(): with pytest.raises(KeyError): fs = (mock_fs() - .withConfig() - .withPage('pages/foo/bar')) + .withConfig() + .withPage('pages/foo/bar')) with mock_fs_scope(fs): - page = MagicMock() - page.app = fs.getApp(cache=False) - page.path = fs.path('/kitchen/pages/foo/bar.md') - assetor = Assetor(page, '/foo/bar') + app = fs.getApp() + app.config.set('site/asset_url_format', '%page_uri%/%filename%') + page = get_simple_page(app, 'foo/bar') + + assetor = Assetor(page) assetor['this_doesnt_exist'] def test_multiple_assets_with_same_name(): with pytest.raises(UnsupportedAssetsError): fs = (mock_fs() - .withConfig() - .withPage('pages/foo/bar') - .withPageAsset('pages/foo/bar', 'one.txt', 'one text') - .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture')) + .withConfig() + .withPage('pages/foo/bar') + .withPageAsset('pages/foo/bar', 'one.txt', 'one text') + .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture')) with mock_fs_scope(fs): - page = MagicMock() - page.app = fs.getApp(cache=False) - page.path = fs.path('/kitchen/pages/foo/bar.md') - assetor = Assetor(page, '/foo/bar') - assetor['one'] - + app = fs.getApp() + app.config.set('site/asset_url_format', '%page_uri%/%filename%') + page = get_simple_page(app, 'foo/bar') -@pytest.mark.parametrize('url_format, pretty_urls, uri, expected', [ - ('%uri%', True, '/foo', '/foo/'), - ('%uri%', True, '/foo.ext', '/foo.ext/'), - ('%uri%', False, '/foo.html', '/foo/'), - ('%uri%', False, '/foo.ext', '/foo/'), - ]) -def test_build_base_url(url_format, pretty_urls, uri, expected): - app = MagicMock() - app.env = MagicMock() - app.env.base_asset_url_format = url_format - app.config = { - 'site/root': '/', - 'site/pretty_urls': pretty_urls} - assets_path = 'foo/bar-assets' - actual = build_base_url(app, uri, assets_path) - assert actual == expected - + assetor = Assetor(page) + assetor['one']