comparison tests/test_baking_baker.py @ 347:76c838453dbe

tests: Support for YAML-based baking tests. Convert old code-based ones.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 15 Apr 2015 16:39:35 -0700
parents 422052d2e978
children e7b865f8f335
comparison
equal deleted inserted replaced
346:89cc71928f6a 347:76c838453dbe
47 expected = os.path.normpath( 47 expected = os.path.normpath(
48 os.path.join('/destination', expected)) 48 os.path.join('/destination', expected))
49 assert expected == path 49 assert expected == path
50 50
51 51
52 def test_empty_bake():
53 fs = mock_fs()
54 with mock_fs_scope(fs):
55 out_dir = fs.path('kitchen/_counter')
56 assert not os.path.isdir(out_dir)
57 app = fs.getApp()
58 baker = Baker(app, out_dir)
59 baker.bake()
60 assert os.path.isdir(out_dir)
61 structure = fs.getStructure('kitchen/_counter')
62 assert list(structure.keys()) == ['index.html']
63
64
65 @pytest.mark.parametrize(
66 'site_root',
67 [
68 ('/'), ('/whatever')
69 ])
70 def test_simple_bake(site_root):
71 pconf = {'layout': 'none', 'format': 'none'}
72 fs = (mock_fs()
73 .withConfig({'site': {'root': site_root}})
74 .withPage('posts/2010-01-01_post1.md', pconf, 'post one')
75 .withPage('pages/about.md', pconf, 'URL: {{page.url}}')
76 .withPage('pages/_index.md', pconf, "something"))
77 with mock_fs_scope(fs):
78 out_dir = fs.path('kitchen/_counter')
79 app = fs.getApp()
80 baker = Baker(app, out_dir)
81 baker.bake()
82 structure = fs.getStructure('kitchen/_counter')
83 assert structure == {
84 '2010': {'01': {'01': {'post1.html': 'post one'}}},
85 'about.html': 'URL: %s' % (
86 site_root.rstrip('/') + '/about.html'),
87 'index.html': 'something'}
88
89
90 def test_removed(): 52 def test_removed():
91 fs = (mock_fs() 53 fs = (mock_fs()
92 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page') 54 .withPage('pages/foo.md', {'layout': 'none', 'format': 'none'}, 'a foo page')
93 .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'}, "something")) 55 .withPage('pages/_index.md', {'layout': 'none', 'format': 'none'}, "something"))
94 with mock_fs_scope(fs): 56 with mock_fs_scope(fs):
133 baker.bake() 95 baker.bake()
134 assert mtime < os.path.getmtime(fs.path('kitchen/_counter/foo.html')) 96 assert mtime < os.path.getmtime(fs.path('kitchen/_counter/foo.html'))
135 finally: 97 finally:
136 BakeRecord.RECORD_VERSION -= 1 98 BakeRecord.RECORD_VERSION -= 1
137 99
138
139 def test_bake_tags():
140 tags = [
141 ['foo'],
142 ['bar', 'whatever'],
143 ['foo', 'bar']]
144
145 def config_factory(i):
146 c = {'title': 'Post %d' % (i + 1)}
147 c['tags'] = tags[i]
148 return c
149
150 fs = (mock_fs()
151 .withPages(3, 'posts/2015-03-{idx1:02}_post{idx1:02}.md',
152 config_factory)
153 .withPage('pages/_tag.md', {'layout': 'none', 'format': 'none'},
154 "Pages in {{tag}}\n"
155 "{%for p in pagination.posts -%}\n"
156 "{{p.title}}\n"
157 "{%endfor%}"))
158 with mock_fs_scope(fs):
159 out_dir = fs.path('kitchen/_counter')
160 app = fs.getApp()
161 baker = Baker(app, out_dir)
162 r = baker.bake()
163 assert r.success is True
164
165 s = fs.getStructure('kitchen/_counter/tag')
166 assert s['foo.html'] == "Pages in foo\nPost 3\nPost 1\n"
167 assert s['bar.html'] == "Pages in bar\nPost 3\nPost 2\n"
168 assert s['whatever.html'] == "Pages in whatever\nPost 2\n"
169
170
171 def test_bake_categories():
172 categories = [
173 'foo', 'bar', 'foo']
174
175 def config_factory(i):
176 c = {'title': 'Post %d' % (i + 1)}
177 c['category'] = categories[i]
178 return c
179
180 fs = (mock_fs()
181 .withConfig({'site': {'category_url': 'cat/%category%'}})
182 .withPages(3, 'posts/2015-03-{idx1:02}_post{idx1:02}.md',
183 config_factory)
184 .withPage('pages/_category.md', {'layout': 'none', 'format': 'none'},
185 "Pages in {{category}}\n"
186 "{%for p in pagination.posts -%}\n"
187 "{{p.title}}\n"
188 "{%endfor%}"))
189 with mock_fs_scope(fs):
190 out_dir = fs.path('kitchen/_counter')
191 app = fs.getApp()
192 baker = Baker(app, out_dir)
193 baker.bake()
194
195 print(fs.getStructure('kitchen/_counter').keys())
196 s = fs.getStructure('kitchen/_counter/cat')
197 assert s['foo.html'] == "Pages in foo\nPost 3\nPost 1\n"
198 assert s['bar.html'] == "Pages in bar\nPost 2\n"
199