annotate tests/test_data_linker.py @ 369:4b1019bb2533

serve: Giant refactor to change how we handle data when serving pages. * We need a distinction between source metadata and route metadata. In most cases they're the same, but in cases like taxonomy pages, route metadata contains more things that can't be in source metadata if we want to re-use cached pages. * Create a new `QualifiedPage` type which is a page with a specific route and route metadata. Pass this around in many places. * Instead of passing an URL around, use the route in the `QualifiedPage` to generate URLs. This is better since it removes the guess-work from trying to generate URLs for sub-pages. * Deep-copy app and page configurations before passing them around to things that could modify them, like data builders and such. * Exclude taxonomy pages from iterator data providers. * Properly nest iterator data providers for when the theme and user page sources are merged inside `site.pages`.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 18:47:10 -0700
parents b51ddb0c260b
children 27b10024f8d8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import pytest
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
2 from piecrust.data.linker import Linker
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from .mockutil import mock_fs, mock_fs_scope
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 @pytest.mark.parametrize(
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 'fs, page_path, expected',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 [
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 (mock_fs().withPage('pages/foo'), 'foo.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
10 # is_dir, name, is_self, data
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
11 [(False, 'foo', True, '/foo')]),
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 'foo.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
16 [(False, 'bar', False, '/bar'), (False, 'foo', True, '/foo')]),
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 .withPage('pages/baz')
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
19 .withPage('pages/something')
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 'foo.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
24 [(False, 'bar', False, '/bar'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
25 (False, 'baz', False, '/baz'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
26 (False, 'foo', True, '/foo'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
27 (True, 'something', False, '/something')]),
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 .withPage('pages/something/good')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 'something/else.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
34 [(False, 'else', True, '/something/else'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
35 (False, 'good', False, '/something/good')])
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 ])
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 def test_linker_iteration(fs, page_path, expected):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 with mock_fs_scope(fs):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 app = fs.getApp()
261
b51ddb0c260b tests: Fix linker tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 237
diff changeset
40 app.config.set('site/pretty_urls', True)
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 src = app.getSource('pages')
261
b51ddb0c260b tests: Fix linker tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 237
diff changeset
42 linker = Linker(src, page_path)
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 actual = list(iter(linker))
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 assert len(actual) == len(expected)
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
46 for (a, e) in zip(actual, expected):
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
47 is_dir, name, is_self, url = e
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
48 assert a.is_dir == is_dir
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
49 assert a.name == name
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
50 assert a.is_self == is_self
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
51 assert a.url == url
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 @pytest.mark.parametrize(
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 'fs, page_path, expected',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 [
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 (mock_fs().withPage('pages/foo'), 'foo.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 [('/foo', True)]),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 'foo.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 [('/bar', False), ('/foo', True)]),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 .withPage('pages/baz')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 'foo.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 [('/bar', False), ('/baz', False),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 ('/foo', True), ('/something/else', False)]),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 .withPage('pages/something/good')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 'something/else.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 [('/something/else', True),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 ('/something/good', False)])
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 ])
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 def test_recursive_linker_iteration(fs, page_path, expected):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 with mock_fs_scope(fs):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 app = fs.getApp()
261
b51ddb0c260b tests: Fix linker tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 237
diff changeset
84 app.config.set('site/pretty_urls', True)
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 src = app.getSource('pages')
261
b51ddb0c260b tests: Fix linker tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 237
diff changeset
86 linker = Linker(src, page_path)
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
87 actual = list(iter(linker.allpages))
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 assert len(actual) == len(expected)
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 for i, (a, e) in enumerate(zip(actual, expected)):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 assert a.is_dir is False
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 assert a.url == e[0]
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 assert a.is_self == e[1]
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94