annotate tests/test_data_provider.py @ 874:f4608e2e80ce

data: Optimize page data creation. `datetime.strftime` was pretty costly so it's no lazily-computed in some places, and replaced with some better alternative elsewhere.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 15 Jun 2017 07:31:50 -0700
parents ab5c6a8ae90a
children 72f17534d58e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 321
diff changeset
1 from piecrust.rendering import QualifiedPage, PageRenderingContext, render_page
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from .mockutil import mock_fs, mock_fs_scope
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 def test_blog_provider():
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 fs = (mock_fs()
674
f987b29d6fab tests: Add ability to run tests with a theme site.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
7 .withConfig()
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 .withPage('posts/2015-03-01_one.md',
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
9 {'title': 'One', 'tags': ['Foo']})
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 .withPage('posts/2015-03-02_two.md',
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
11 {'title': 'Two', 'tags': ['Foo']})
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 .withPage('posts/2015-03-03_three.md',
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
13 {'title': 'Three', 'tags': ['Bar']})
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
14 .withPage('pages/tags.md',
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 {'format': 'none', 'layout': 'none'},
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
16 "{%for c in blog.tags%}\n"
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 "{{c.name}} ({{c.post_count}})\n"
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 "{%endfor%}\n"))
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 with mock_fs_scope(fs):
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 app = fs.getApp()
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
21 page = app.getSource('pages').getPage({'slug': 'tags'})
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
22 route = app.getSourceRoute('pages', None)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 674
diff changeset
23 route_metadata = {'slug': 'tags'}
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 321
diff changeset
24 qp = QualifiedPage(page, route, route_metadata)
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 321
diff changeset
25 ctx = PageRenderingContext(qp)
321
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 rp = render_page(ctx)
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 expected = "\nBar (1)\n\nFoo (2)\n"
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 assert rp.content == expected
59d65654184e tests: Add a blog data provider test.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29