Mercurial > piecrust2
comparison tests/test_templating_jinjaengine.py @ 974:72f17534d58e
tests: First pass on making unit tests work again.
- Fix all imports
- Add more helper functions to work with mock file-systems
- Simplify some code by running chef directly on the mock FS
- Fix a couple tests
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 17 Oct 2017 01:07:30 -0700 |
parents | ab5c6a8ae90a |
children | 45ad976712ec |
comparison
equal
deleted
inserted
replaced
973:8419daaa7a0e | 974:72f17534d58e |
---|---|
1 import pytest | 1 import pytest |
2 from .mockutil import ( | 2 from .mockutil import mock_fs, mock_fs_scope |
3 mock_fs, mock_fs_scope, get_simple_page, render_simple_page) | 3 from .rdrutil import render_simple_page |
4 | 4 |
5 | 5 |
6 app_config = { | 6 app_config = { |
7 'site': { | 7 'site': { |
8 'default_format': 'none', | 8 'default_format': 'none', |
9 'default_template_engine': 'jinja'}, | 9 'default_template_engine': 'jinja'}, |
10 'foo': 'bar'} | 10 'foo': 'bar'} |
11 page_config = {'layout': 'none'} | 11 page_config = {'layout': 'none'} |
12 | 12 |
13 open_patches = ['jinja2.environment', 'jinja2.utils'] | 13 open_patches = ['jinja2.environment', 'jinja2.utils'] |
14 | 14 |
15 | 15 |
16 @pytest.mark.parametrize( | 16 @pytest.mark.parametrize( |
17 'contents, expected', | 17 'contents, expected', |
18 [ | 18 [ |
19 ("Raw text", "Raw text"), | 19 ("Raw text", "Raw text"), |
20 ("This is {{foo}}", "This is bar"), | 20 ("This is {{foo}}", "This is bar"), |
21 ("Info:\nMy URL: {{page.url}}\n", | 21 ("Info:\nMy URL: {{page.url}}\n", |
22 "Info:\nMy URL: /foo.html") | 22 "Info:\nMy URL: /foo.html") |
23 ]) | 23 ]) |
24 def test_simple(contents, expected): | 24 def test_simple(contents, expected): |
25 fs = (mock_fs() | 25 fs = (mock_fs() |
26 .withConfig(app_config) | 26 .withConfig(app_config) |
27 .withPage('pages/foo', config=page_config, contents=contents)) | 27 .withPage('pages/foo', config=page_config, contents=contents)) |
28 with mock_fs_scope(fs, open_patches=open_patches): | 28 with mock_fs_scope(fs, open_patches=open_patches): |
29 app = fs.getApp() | 29 app = fs.getApp() |
30 page = get_simple_page(app, 'foo.md') | 30 page = fs.getSimplePage('foo.md') |
31 route = app.getSourceRoute('pages', None) | 31 route = app.getSourceRoute('pages') |
32 route_metadata = {'slug': 'foo'} | 32 route_metadata = {'slug': 'foo'} |
33 output = render_simple_page(page, route, route_metadata) | 33 output = render_simple_page(page, route, route_metadata) |
34 assert output == expected | 34 assert output == expected |
35 | 35 |
36 | 36 |
37 def test_layout(): | 37 def test_layout(): |
38 contents = "Blah\n" | 38 contents = "Blah\n" |
39 layout = "{{content}}\nFor site: {{foo}}\n" | 39 layout = "{{content}}\nFor site: {{foo}}\n" |
40 expected = "Blah\n\nFor site: bar" | 40 expected = "Blah\n\nFor site: bar" |
41 fs = (mock_fs() | 41 fs = (mock_fs() |
42 .withConfig(app_config) | 42 .withConfig(app_config) |
43 .withAsset('templates/blah.jinja', layout) | 43 .withAsset('templates/blah.jinja', layout) |
44 .withPage('pages/foo', config={'layout': 'blah'}, | 44 .withPage('pages/foo', config={'layout': 'blah'}, |
45 contents=contents)) | 45 contents=contents)) |
46 with mock_fs_scope(fs, open_patches=open_patches): | 46 with mock_fs_scope(fs, open_patches=open_patches): |
47 app = fs.getApp() | 47 app = fs.getApp() |
48 page = get_simple_page(app, 'foo.md') | 48 page = fs.getSimplePage('foo.md') |
49 route = app.getSourceRoute('pages', None) | 49 route = app.getSourceRoute('pages', None) |
50 route_metadata = {'slug': 'foo'} | 50 route_metadata = {'slug': 'foo'} |
51 output = render_simple_page(page, route, route_metadata) | 51 output = render_simple_page(page, route, route_metadata) |
52 assert output == expected | 52 assert output == expected |
53 | 53 |
55 def test_partial(): | 55 def test_partial(): |
56 contents = "Info:\n{% include 'page_info.jinja' %}\n" | 56 contents = "Info:\n{% include 'page_info.jinja' %}\n" |
57 partial = "- URL: {{page.url}}\n- SLUG: {{page.slug}}\n" | 57 partial = "- URL: {{page.url}}\n- SLUG: {{page.slug}}\n" |
58 expected = "Info:\n- URL: /foo.html\n- SLUG: foo" | 58 expected = "Info:\n- URL: /foo.html\n- SLUG: foo" |
59 fs = (mock_fs() | 59 fs = (mock_fs() |
60 .withConfig(app_config) | 60 .withConfig(app_config) |
61 .withAsset('templates/page_info.jinja', partial) | 61 .withAsset('templates/page_info.jinja', partial) |
62 .withPage('pages/foo', config=page_config, contents=contents)) | 62 .withPage('pages/foo', config=page_config, contents=contents)) |
63 with mock_fs_scope(fs, open_patches=open_patches): | 63 with mock_fs_scope(fs, open_patches=open_patches): |
64 app = fs.getApp() | 64 app = fs.getApp() |
65 page = get_simple_page(app, 'foo.md') | 65 page = fs.getSimplePage('foo.md') |
66 route = app.getSourceRoute('pages', None) | 66 route = app.getSourceRoute('pages', None) |
67 route_metadata = {'slug': 'foo'} | 67 route_metadata = {'slug': 'foo'} |
68 output = render_simple_page(page, route, route_metadata) | 68 output = render_simple_page(page, route, route_metadata) |
69 assert output == expected | 69 assert output == expected |
70 | 70 |