comparison tests/test_dataproviders_blog.py @ 979:45ad976712ec

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