Mercurial > piecrust2
annotate tests/test_dataproviders_blog.py @ 1165:a928ee22c20a
cm: Upgrade Jinja2 version.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 04 Oct 2019 09:00:57 -0700 |
parents | 45ad976712ec |
children |
rev | line source |
---|---|
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 from .mockutil import mock_fs, mock_fs_scope |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 from .rdrutil import render_simple_page |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 def _get_post_tokens(i, posts_per_month=2, posts_per_year=5, first_year=2001): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 year = first_year + int(i / posts_per_year) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 i_in_year = i % posts_per_year |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 month = int(i_in_year / posts_per_month) + 1 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 day = i_in_year % posts_per_month + 1 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 return (year, month, day, i + 1) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 def test_blog_provider_archives(): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 fs = (mock_fs() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 .withConfig({ |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 'site': { |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 'default_layout': 'none', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 'default_format': 'none' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 } |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 }) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 .withPages( |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 20, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 lambda i: ('posts/%04d-%02d-%02d_post-%d.md' % |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 _get_post_tokens(i)), |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 lambda i: {'title': "Post %02d" % (i + 1), 'format': 'none'}, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 lambda i: "This is post %02d" % (i + 1)) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 .withPage('pages/allposts.html', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 {'layout': 'none'}, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 "{%for p in blog.posts-%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 "{{p.title}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 "{%endfor%}\n") |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 .withPage('pages/allyears.html', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 {'layout': 'none'}, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 "{%for y in blog.years-%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 "YEAR={{y}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 "{%for p in y.posts-%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 "{{p.title}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 "{%endfor%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 "{%endfor%}") |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 .withFile('kitchen/templates/_year.html', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 "YEAR={{year}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 "{%for p in archives-%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 "{{p.title}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 "{%endfor%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 "\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 "{%for m in monthly_archives-%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 "MONTH={{m.timestamp|date('%m')}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 "{%for p in m.posts-%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 "{{p.title}}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 "{%endfor%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 "{%endfor%}")) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 with mock_fs_scope(fs): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 fs.runChef('bake', '-o', fs.path('counter')) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 # Check `allposts`. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 # Should have all the posts. Duh. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 expected = '\n'.join(map(lambda i: "Post %02d" % i, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 range(20, 0, -1))) + '\n' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 actual = fs.getFileEntry('counter/allposts.html') |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 assert expected == actual |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 # Check `allyears`. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 # Should have all the years, each with 5 posts in reverse |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 # chronological order. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 expected = '' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 cur_index = 20 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 for y in range(2004, 2000, -1): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 expected += ('YEAR=%04d\n' % y) + '\n'.join( |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 map(lambda i: "Post %02d" % i, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 range(cur_index, cur_index - 5, -1))) + '\n\n' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 cur_index -= 5 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 actual = fs.getFileEntry('counter/allyears.html') |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 assert expected == actual |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 # Check each yearly page. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 # Should have both the posts for that year (5 posts) in |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 # chronological order, followed by the months for that year |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 # (3 months) and the posts in each month (2, 2, and 1). |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 cur_index = 1 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 for y in range(2001, 2005): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 orig_index = cur_index |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 expected = ('YEAR=%04d\n' % y) + '\n'.join( |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 map(lambda i: "Post %02d" % i, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 range(cur_index, cur_index + 5))) + '\n' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 expected += "\n\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 orig_final_index = cur_index |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 cur_index = orig_index |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 for m in range(1, 4): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 expected += 'MONTH=%02d\n' % m |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 expected += '\n'.join( |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 map(lambda i: "Post %02d" % i, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 range(cur_index, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 min(cur_index + 2, orig_index + 5)))) + '\n' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 expected += '\n' |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 cur_index += 2 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 cur_index = orig_final_index |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 actual = fs.getFileEntry('counter/archives/%04d.html' % y) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 assert expected == actual |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 cur_index += 5 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 def test_blog_provider_tags(): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 fs = (mock_fs() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 .withConfig() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 .withPage('posts/2015-03-01_one.md', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 {'title': 'One', 'tags': ['Foo']}) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 .withPage('posts/2015-03-02_two.md', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 {'title': 'Two', 'tags': ['Foo']}) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 .withPage('posts/2015-03-03_three.md', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 {'title': 'Three', 'tags': ['Bar']}) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 .withPage('pages/tags.md', |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 {'format': 'none', 'layout': 'none'}, |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 "{%for c in blog.tags%}\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 "{{c.name}} ({{c.post_count}})\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 "{%endfor%}\n")) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 with mock_fs_scope(fs): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 page = fs.getSimplePage('tags.md') |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 actual = render_simple_page(page) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 expected = "\nBar (1)\n\nFoo (2)\n" |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 assert actual == expected |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 |