annotate tests/test_sources_base.py @ 1145:e94737572542

serve: Fix an issue where false positive matches were rendered as the requested page. Now we try to render the page, but also try to detect for the most common "empty" pages.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 22:08:51 -0700
parents 8adc27285d93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import pytest
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from .mockutil import mock_fs, mock_fs_scope
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
4 from .pathutil import slashfix
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 363
diff changeset
7 @pytest.mark.parametrize('fs_fac, expected_paths, expected_slugs', [
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
8 (lambda: mock_fs(), [], []),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
9 (lambda: mock_fs().withPage('test/foo.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
10 ['foo.html'], ['foo']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
11 (lambda: mock_fs().withPage('test/foo.md'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
12 ['foo.md'], ['foo']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
13 (lambda: mock_fs().withPage('test/foo.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
14 ['foo.ext'], ['foo.ext']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
15 (lambda: mock_fs().withPage('test/foo/bar.html'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
16 ['foo/bar.html'], ['foo/bar']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
17 (lambda: mock_fs().withPage('test/foo/bar.md'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
18 ['foo/bar.md'], ['foo/bar']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
19 (lambda: mock_fs().withPage('test/foo/bar.ext'),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
20 ['foo/bar.ext'], ['foo/bar.ext']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
21 ])
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
22 def test_default_source_items(fs_fac, expected_paths, expected_slugs):
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 363
diff changeset
23 fs = fs_fac()
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 fs.withConfig({
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 'site': {
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 'sources': {
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 'test': {}},
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 'routes': [
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 {'url': '/%path%', 'source': 'test'}]
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
30 }
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
31 })
36
485682a6de50 New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents: 30
diff changeset
32 fs.withDir('kitchen/test')
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 with mock_fs_scope(fs):
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
34 app = fs.getApp()
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 s = app.getSource('test')
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
36 items = list(s.getAllContents())
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
37 paths = [os.path.relpath(f.spec, s.fs_endpoint_path) for f in items]
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
38 assert paths == slashfix(expected_paths)
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
39 slugs = [f.metadata['route_params']['slug'] for f in items]
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 assert slugs == expected_slugs
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
363
dd25bd3ce1f9 serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 286
diff changeset
43 @pytest.mark.parametrize(
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
44 'fs_fac, ref_path, expected_path, expected_metadata', [
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
45 (lambda: mock_fs().withPage('test/foo.html'),
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
46 'foo.html',
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
47 'test/foo.html',
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
48 {'slug': 'foo'}),
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
49 (lambda: mock_fs().withPage('test/foo/bar.html'),
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
50 'foo/bar.html',
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
51 'test/foo/bar.html',
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
52 {'slug': 'foo/bar'}),
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
53
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
54 ])
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
55 def test_default_source_find_item(fs_fac, ref_path, expected_path,
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
56 expected_metadata):
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
57 fs = fs_fac()
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 fs.withConfig({
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 'site': {
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 'sources': {
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 'test': {}},
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 'routes': [
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 {'url': '/%path%', 'source': 'test'}]
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
64 }
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
65 })
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 with mock_fs_scope(fs):
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
67 app = fs.getApp()
30
4bd840ae75cd Fix stupid bug in default source, add some unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 s = app.getSource('test')
989
8adc27285d93 bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
69 item = s.findContentFromRoute({'slug': ref_path})
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
70 assert item is not None
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
71 assert os.path.relpath(item.spec, app.root_dir) == \
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
72 slashfix(expected_path)
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 974
diff changeset
73 assert item.metadata['route_params'] == expected_metadata