comparison tests/test_sources_base.py @ 979:45ad976712ec

tests: Big push to get the tests to pass again. - Lots of fixes everywhere in the code. - Try to handle debug logging in the multiprocessing worker pool when running in pytest. Not perfect, but usable for now. - Replace all `.md` test files with `.html` since now a auto-format extension always sets the format. - Replace `out` with `outfiles` in most places since now blog archives are added to the bake output and I don't want to add expected outputs for blog archives everywhere.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 29 Oct 2017 22:51:57 -0700
parents 72f17534d58e
children 8adc27285d93
comparison
equal deleted inserted replaced
978:7e51d14097cb 979:45ad976712ec
1 import os 1 import os
2 import pytest 2 import pytest
3 from piecrust.app import PieCrust
4 from .mockutil import mock_fs, mock_fs_scope 3 from .mockutil import mock_fs, mock_fs_scope
4 from .pathutil import slashfix
5 5
6 6
7 @pytest.mark.parametrize('fs_fac, expected_paths, expected_slugs', [ 7 @pytest.mark.parametrize('fs_fac, expected_paths, expected_slugs', [
8 (lambda: mock_fs(), [], []), 8 (lambda: mock_fs(), [], []),
9 (lambda: mock_fs().withPage('test/foo.html'), 9 (lambda: mock_fs().withPage('test/foo.html'),
17 (lambda: mock_fs().withPage('test/foo/bar.md'), 17 (lambda: mock_fs().withPage('test/foo/bar.md'),
18 ['foo/bar.md'], ['foo/bar']), 18 ['foo/bar.md'], ['foo/bar']),
19 (lambda: mock_fs().withPage('test/foo/bar.ext'), 19 (lambda: mock_fs().withPage('test/foo/bar.ext'),
20 ['foo/bar.ext'], ['foo/bar.ext']), 20 ['foo/bar.ext'], ['foo/bar.ext']),
21 ]) 21 ])
22 def test_default_source_factories(fs_fac, expected_paths, expected_slugs): 22 def test_default_source_items(fs_fac, expected_paths, expected_slugs):
23 fs = fs_fac() 23 fs = fs_fac()
24 fs.withConfig({ 24 fs.withConfig({
25 'site': { 25 'site': {
26 'sources': { 26 'sources': {
27 'test': {}}, 27 'test': {}},
29 {'url': '/%path%', 'source': 'test'}] 29 {'url': '/%path%', 'source': 'test'}]
30 } 30 }
31 }) 31 })
32 fs.withDir('kitchen/test') 32 fs.withDir('kitchen/test')
33 with mock_fs_scope(fs): 33 with mock_fs_scope(fs):
34 app = PieCrust(fs.path('kitchen'), cache=False) 34 app = fs.getApp()
35 s = app.getSource('test') 35 s = app.getSource('test')
36 facs = list(s.buildPageFactories()) 36 items = list(s.getAllContents())
37 paths = [f.rel_path for f in facs] 37 paths = [os.path.relpath(f.spec, s.fs_endpoint_path) for f in items]
38 assert paths == expected_paths 38 assert paths == slashfix(expected_paths)
39 slugs = [f.metadata['slug'] for f in facs] 39 slugs = [f.metadata['route_params']['slug'] for f in items]
40 assert slugs == expected_slugs 40 assert slugs == expected_slugs
41 41
42 42
43 @pytest.mark.parametrize( 43 @pytest.mark.parametrize(
44 'ref_path, expected_path, expected_metadata', 44 'fs_fac, ref_path, expected_path, expected_metadata', [
45 [ 45 (lambda: mock_fs().withPage('test/foo.html'),
46 ('foo.html', '/kitchen/test/foo.html', {'slug': 'foo'}), 46 'foo.html',
47 ('foo/bar.html', '/kitchen/test/foo/bar.html', 47 'test/foo.html',
48 {'slug': 'foo'}),
49 (lambda: mock_fs().withPage('test/foo/bar.html'),
50 'foo/bar.html',
51 'test/foo/bar.html',
48 {'slug': 'foo/bar'}), 52 {'slug': 'foo/bar'}),
53
49 ]) 54 ])
50 def test_default_source_resolve_ref(ref_path, expected_path, 55 def test_default_source_find_item(fs_fac, ref_path, expected_path,
51 expected_metadata): 56 expected_metadata):
52 fs = mock_fs() 57 fs = fs_fac()
53 fs.withConfig({ 58 fs.withConfig({
54 'site': { 59 'site': {
55 'sources': { 60 'sources': {
56 'test': {}}, 61 'test': {}},
57 'routes': [ 62 'routes': [
58 {'url': '/%path%', 'source': 'test'}] 63 {'url': '/%path%', 'source': 'test'}]
59 } 64 }
60 }) 65 })
61 expected_path = fs.path(expected_path).replace('/', os.sep)
62 with mock_fs_scope(fs): 66 with mock_fs_scope(fs):
63 app = PieCrust(fs.path('kitchen'), cache=False) 67 app = fs.getApp()
64 s = app.getSource('test') 68 s = app.getSource('test')
65 actual_path, actual_metadata = s.resolveRef(ref_path) 69 item = s.findContent({'slug': ref_path})
66 assert actual_path == expected_path 70 assert item is not None
67 assert actual_metadata == expected_metadata 71 assert os.path.relpath(item.spec, app.root_dir) == \
72 slashfix(expected_path)
73 assert item.metadata['route_params'] == expected_metadata