annotate tests/test_data_linker.py @ 404:27b10024f8d8

linker: Add ability to return the parent and ancestors of a page. Add reference documentation. Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 24 May 2015 18:15:22 -0700
parents b51ddb0c260b
children e7b865f8f335
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
404
27b10024f8d8 linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 261
diff changeset
1 import os.path
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import pytest
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
3 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
4 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
5
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 @pytest.mark.parametrize(
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 'fs, page_path, expected',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 [
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 (mock_fs().withPage('pages/foo'), 'foo.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
11 # is_dir, name, is_self, data
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
12 [(False, 'foo', True, '/foo')]),
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 'foo.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
17 [(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
18 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 .withPage('pages/baz')
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
20 .withPage('pages/something')
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 'foo.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
25 [(False, 'bar', False, '/bar'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
26 (False, 'baz', False, '/baz'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
27 (False, 'foo', True, '/foo'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
28 (True, 'something', False, '/something')]),
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 .withPage('pages/something/good')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 'something/else.md',
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
35 [(False, 'else', True, '/something/else'),
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
36 (False, 'good', False, '/something/good')])
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 ])
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 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
39 with mock_fs_scope(fs):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 app = fs.getApp()
261
b51ddb0c260b tests: Fix linker tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 237
diff changeset
41 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
42 src = app.getSource('pages')
404
27b10024f8d8 linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 261
diff changeset
43 linker = Linker(src, os.path.dirname(page_path),
27b10024f8d8 linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 261
diff changeset
44 root_page_path=page_path)
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 actual = list(iter(linker))
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 assert len(actual) == len(expected)
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
48 for (a, e) in zip(actual, expected):
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
49 is_dir, name, is_self, url = e
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
50 assert a.is_dir == is_dir
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
51 assert a.name == name
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
52 assert a.is_self == is_self
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
53 assert a.url == url
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 @pytest.mark.parametrize(
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 'fs, page_path, expected',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 [
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 (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
60 [('/foo', True)]),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 'foo.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 [('/bar', False), ('/foo', True)]),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 .withPage('pages/baz')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 'foo.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 [('/bar', False), ('/baz', False),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 ('/foo', True), ('/something/else', False)]),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 ((mock_fs()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 .withPage('pages/something/else')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 .withPage('pages/foo')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 .withPage('pages/something/good')
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 .withPage('pages/bar')),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 'something/else.md',
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 [('/something/else', True),
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 ('/something/good', False)])
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 ])
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 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
84 with mock_fs_scope(fs):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 app = fs.getApp()
261
b51ddb0c260b tests: Fix linker tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 237
diff changeset
86 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
87 src = app.getSource('pages')
404
27b10024f8d8 linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 261
diff changeset
88 linker = Linker(src, os.path.dirname(page_path),
27b10024f8d8 linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 261
diff changeset
89 root_page_path=page_path)
237
879fe1457e48 data: `Linker` refactor.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
90 actual = list(iter(linker.allpages))
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 assert len(actual) == len(expected)
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 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
94 assert a.is_dir is False
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 assert a.url == e[0]
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 assert a.is_self == e[1]
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97