Mercurial > piecrust2
comparison tests/test_baking_baker.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 | a2d283d1033d |
children | 422052d2e978 |
comparison
equal
deleted
inserted
replaced
323:412537e91e45 | 324:65e6d72f3877 |
---|---|
1 import time | 1 import time |
2 import os.path | 2 import os.path |
3 import pytest | 3 import pytest |
4 from piecrust.baking.baker import PageBaker, Baker | 4 from piecrust.baking.baker import PageBaker, Baker |
5 from piecrust.baking.records import BakeRecord | 5 from piecrust.baking.records import BakeRecord |
6 from piecrust.routing import Route | |
7 from .mockutil import get_mock_app, mock_fs, mock_fs_scope | 6 from .mockutil import get_mock_app, mock_fs, mock_fs_scope |
8 | 7 |
9 | 8 |
10 @pytest.mark.parametrize('uri, pretty, expected', [ | 9 @pytest.mark.parametrize('uri, pretty, expected', [ |
11 # Pretty URLs | 10 # Pretty URLs |
73 structure = fs.getStructure('kitchen/_counter') | 72 structure = fs.getStructure('kitchen/_counter') |
74 assert structure == { | 73 assert structure == { |
75 '2010': {'01': {'01': {'post1.html': 'post one'}}}, | 74 '2010': {'01': {'01': {'post1.html': 'post one'}}}, |
76 'index.html': 'something'} | 75 'index.html': 'something'} |
77 | 76 |
77 | |
78 def test_removed(): | 78 def test_removed(): |
79 fs = (mock_fs() | 79 fs = (mock_fs() |
80 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page') | 80 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page') |
81 .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'}, "something")) | 81 .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'}, "something")) |
82 with mock_fs_scope(fs): | 82 with mock_fs_scope(fs): |
94 baker = Baker(app, out_dir) | 94 baker = Baker(app, out_dir) |
95 baker.bake() | 95 baker.bake() |
96 structure = fs.getStructure('kitchen/_counter') | 96 structure = fs.getStructure('kitchen/_counter') |
97 assert structure == { | 97 assert structure == { |
98 'index.html': 'something'} | 98 'index.html': 'something'} |
99 | |
99 | 100 |
100 def test_record_version_change(): | 101 def test_record_version_change(): |
101 fs = (mock_fs() | 102 fs = (mock_fs() |
102 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page')) | 103 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page')) |
103 with mock_fs_scope(fs): | 104 with mock_fs_scope(fs): |
120 baker.bake() | 121 baker.bake() |
121 assert mtime < os.path.getmtime(fs.path('kitchen/_counter/foo.html')) | 122 assert mtime < os.path.getmtime(fs.path('kitchen/_counter/foo.html')) |
122 finally: | 123 finally: |
123 BakeRecord.RECORD_VERSION -= 1 | 124 BakeRecord.RECORD_VERSION -= 1 |
124 | 125 |
126 | |
127 def test_bake_tags(): | |
128 tags = [ | |
129 ['foo'], | |
130 ['bar', 'whatever'], | |
131 ['foo', 'bar']] | |
132 | |
133 def config_factory(i): | |
134 c = {'title': 'Post %d' % (i + 1)} | |
135 c['tags'] = tags[i] | |
136 return c | |
137 | |
138 fs = (mock_fs() | |
139 .withPages(3, 'posts/2015-03-{idx1:02}_post{idx1:02}.md', | |
140 config_factory) | |
141 .withPage('pages/_tag.md', {'layout': 'none', 'format': 'none'}, | |
142 "Pages in {{tag}}\n" | |
143 "{%for p in pagination.posts -%}\n" | |
144 "{{p.title}}\n" | |
145 "{%endfor%}")) | |
146 with mock_fs_scope(fs): | |
147 out_dir = fs.path('kitchen/_counter') | |
148 app = fs.getApp() | |
149 baker = Baker(app, out_dir) | |
150 baker.bake() | |
151 | |
152 s = fs.getStructure('kitchen/_counter/tag') | |
153 assert s['foo.html'] == "Pages in foo\nPost 3\nPost 1\n" | |
154 assert s['bar.html'] == "Pages in bar\nPost 3\nPost 2\n" | |
155 assert s['whatever.html'] == "Pages in whatever\nPost 2\n" | |
156 | |
157 | |
158 def test_bake_categories(): | |
159 categories = [ | |
160 'foo', 'bar', 'foo'] | |
161 | |
162 def config_factory(i): | |
163 c = {'title': 'Post %d' % (i + 1)} | |
164 c['category'] = categories[i] | |
165 return c | |
166 | |
167 fs = (mock_fs() | |
168 .withConfig({'site': {'category_url': 'cat/%category%'}}) | |
169 .withPages(3, 'posts/2015-03-{idx1:02}_post{idx1:02}.md', | |
170 config_factory) | |
171 .withPage('pages/_category.md', {'layout': 'none', 'format': 'none'}, | |
172 "Pages in {{category}}\n" | |
173 "{%for p in pagination.posts -%}\n" | |
174 "{{p.title}}\n" | |
175 "{%endfor%}")) | |
176 with mock_fs_scope(fs): | |
177 out_dir = fs.path('kitchen/_counter') | |
178 app = fs.getApp() | |
179 baker = Baker(app, out_dir) | |
180 baker.bake() | |
181 | |
182 print(fs.getStructure('kitchen/_counter').keys()) | |
183 s = fs.getStructure('kitchen/_counter/cat') | |
184 assert s['foo.html'] == "Pages in foo\nPost 3\nPost 1\n" | |
185 assert s['bar.html'] == "Pages in bar\nPost 2\n" | |
186 |