Mercurial > piecrust2
comparison tests/test_serving.py @ 324:65e6d72f3877
bake/serve: Fix how taxonomy index pages are setup and rendered.
* Properly use the taxonomy's setting name where appropriate.
* Delete duplicated (and sometimes incorrect) code in 2 places to setup
filtering on an index page and consolidate it on the `PageRenderingContext`.
* Add unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 Mar 2015 23:08:37 -0700 |
parents | d47d9493bb0a |
children | d8c28e496bb3 |
comparison
equal
deleted
inserted
replaced
323:412537e91e45 | 324:65e6d72f3877 |
---|---|
1 import re | 1 import re |
2 import pytest | 2 import pytest |
3 import mock | 3 import mock |
4 from piecrust.data.filters import ( | |
5 PaginationFilter, HasFilterClause, IsFilterClause, | |
6 page_value_accessor) | |
7 from piecrust.rendering import PageRenderingContext, render_page | |
4 from piecrust.serving import find_routes | 8 from piecrust.serving import find_routes |
5 from piecrust.sources.base import REALM_USER, REALM_THEME | 9 from piecrust.sources.base import REALM_USER, REALM_THEME |
10 from .mockutil import mock_fs, mock_fs_scope | |
6 | 11 |
7 | 12 |
8 @pytest.mark.parametrize('uri, route_specs, expected', | 13 @pytest.mark.parametrize('uri, route_specs, expected', |
9 [ | 14 [ |
10 ('/', | 15 ('/', |
31 route, metadata = matching[i] | 36 route, metadata = matching[i] |
32 exp_source, exp_md = expected[i] | 37 exp_source, exp_md = expected[i] |
33 assert route.source_name == exp_source | 38 assert route.source_name == exp_source |
34 assert metadata == exp_md | 39 assert metadata == exp_md |
35 | 40 |
41 | |
42 @pytest.mark.parametrize( | |
43 'tag, expected_indices', | |
44 [ | |
45 ('foo', [1, 2, 4, 5, 6]), | |
46 ('bar', [2, 3, 4, 6, 8]), | |
47 ('whatever', [5, 8]), | |
48 ('unique', [7]), | |
49 ('missing', None) | |
50 ]) | |
51 def test_serve_tag_page(tag, expected_indices): | |
52 tags = [ | |
53 ['foo'], | |
54 ['foo', 'bar'], | |
55 ['bar'], | |
56 ['bar', 'foo'], | |
57 ['foo', 'whatever'], | |
58 ['foo', 'bar'], | |
59 ['unique'], | |
60 ['whatever', 'bar']] | |
61 | |
62 def config_factory(i): | |
63 c = {'title': 'Post %d' % (i + 1)} | |
64 c['tags'] = list(tags[i]) | |
65 return c | |
66 | |
67 fs = (mock_fs() | |
68 .withPages(8, 'posts/2015-03-{idx1:02}_post{idx1:02}.md', | |
69 config_factory) | |
70 .withPage('pages/_tag.md', {'layout': 'none', 'format': 'none'}, | |
71 "Pages in {{tag}}\n" | |
72 "{%for p in pagination.posts -%}\n" | |
73 "{{p.title}}\n" | |
74 "{%endfor%}")) | |
75 with mock_fs_scope(fs): | |
76 app = fs.getApp() | |
77 page = app.getSource('pages').getPage({'slug': '_tag'}) | |
78 taxonomy = app.getTaxonomy('tags') | |
79 | |
80 ctx = PageRenderingContext(page, '/tag/' + tag) | |
81 ctx.setTaxonomyFilter(taxonomy, tag) | |
82 rp = render_page(ctx) | |
83 | |
84 expected = "Pages in %s\n" % tag | |
85 if expected_indices: | |
86 for i in reversed(expected_indices): | |
87 expected += "Post %d\n" % i | |
88 assert expected == rp.content | |
89 | |
90 | |
91 @pytest.mark.parametrize( | |
92 'category, expected_indices', | |
93 [ | |
94 ('foo', [1, 2, 4]), | |
95 ('bar', [3, 6]), | |
96 ('missing', None) | |
97 ]) | |
98 def test_serve_category_page(category, expected_indices): | |
99 categories = [ | |
100 'foo', 'foo', 'bar', 'foo', None, 'bar'] | |
101 | |
102 def config_factory(i): | |
103 c = {'title': 'Post %d' % (i + 1)} | |
104 if categories[i]: | |
105 c['category'] = categories[i] | |
106 return c | |
107 | |
108 fs = (mock_fs() | |
109 .withPages(6, 'posts/2015-03-{idx1:02}_post{idx1:02}.md', | |
110 config_factory) | |
111 .withPage('pages/_category.md', {'layout': 'none', 'format': 'none'}, | |
112 "Pages in {{category}}\n" | |
113 "{%for p in pagination.posts -%}\n" | |
114 "{{p.title}}\n" | |
115 "{%endfor%}")) | |
116 with mock_fs_scope(fs): | |
117 app = fs.getApp() | |
118 page = app.getSource('pages').getPage({'slug': '_category'}) | |
119 taxonomy = app.getTaxonomy('categories') | |
120 | |
121 ctx = PageRenderingContext(page, '/' + category) | |
122 ctx.setTaxonomyFilter(taxonomy, category) | |
123 rp = render_page(ctx) | |
124 | |
125 expected = "Pages in %s\n" % category | |
126 if expected_indices: | |
127 for i in reversed(expected_indices): | |
128 expected += "Post %d\n" % i | |
129 assert expected == rp.content | |
130 |