Mercurial > piecrust2
comparison tests/test_appconfig.py @ 805:fd694f1297c7
config: Cleanup config loading code. Add support for a `local.yml` config.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Mon, 10 Oct 2016 21:41:59 -0700 |
| parents | ab5c6a8ae90a |
| children | 72f17534d58e |
comparison
equal
deleted
inserted
replaced
| 804:08e6484a2600 | 805:fd694f1297c7 |
|---|---|
| 4 | 4 |
| 5 | 5 |
| 6 def test_config_default(): | 6 def test_config_default(): |
| 7 values = {} | 7 values = {} |
| 8 config = PieCrustConfiguration(values=values) | 8 config = PieCrustConfiguration(values=values) |
| 9 assert config.get('site/root') == '/' | |
| 10 assert len(config.get('site/sources')) == 3 # pages, posts, theme_pages | |
| 11 | |
| 12 | |
| 13 def test_config_default2(): | |
| 14 config = PieCrustConfiguration() | |
| 15 assert config.get('site/root') == '/' | 9 assert config.get('site/root') == '/' |
| 16 assert len(config.get('site/sources')) == 3 # pages, posts, theme_pages | 10 assert len(config.get('site/sources')) == 3 # pages, posts, theme_pages |
| 17 | 11 |
| 18 | 12 |
| 19 def test_config_site_override_title(): | 13 def test_config_site_override_title(): |
| 31 fs = mock_fs().withConfig(config) | 25 fs = mock_fs().withConfig(config) |
| 32 with mock_fs_scope(fs): | 26 with mock_fs_scope(fs): |
| 33 app = fs.getApp() | 27 app = fs.getApp() |
| 34 assert app.config.get('site/default_page_layout') == 'foo' | 28 assert app.config.get('site/default_page_layout') == 'foo' |
| 35 assert app.config.get('site/default_post_layout') == 'bar' | 29 assert app.config.get('site/default_post_layout') == 'bar' |
| 36 assert app.config.get('site/sources')['pages']['default_layout'] == 'foo' | 30 assert app.config.get('site/sources/pages/default_layout') == 'foo' |
| 37 assert app.config.get('site/sources')['pages']['items_per_page'] == 5 | 31 assert app.config.get('site/sources/pages/items_per_page') == 5 |
| 38 assert app.config.get('site/sources')['theme_pages']['default_layout'] == 'default' | 32 assert app.config.get( |
| 39 assert app.config.get('site/sources')['theme_pages']['items_per_page'] == 5 | 33 'site/sources/theme_pages/default_layout') == 'default' |
| 40 assert app.config.get('site/sources')['posts']['default_layout'] == 'bar' | 34 assert app.config.get('site/sources/theme_pages/items_per_page') == 5 |
| 41 assert app.config.get('site/sources')['posts']['items_per_page'] == 2 | 35 assert app.config.get('site/sources/posts/default_layout') == 'bar' |
| 36 assert app.config.get('site/sources/posts/items_per_page') == 2 | |
| 37 | |
| 42 | 38 |
| 43 def test_config_site_add_source(): | 39 def test_config_site_add_source(): |
| 44 config = {'site': { | 40 config = {'site': { |
| 45 'sources': {'notes': {}}, | 41 'sources': {'notes': {}}, |
| 46 'routes': [{'url': '/notes/%path:slug%', 'source': 'notes'}] | 42 'routes': [{'url': '/notes/%path:slug%', 'source': 'notes'}] |
| 47 }} | 43 }} |
| 48 fs = mock_fs().withConfig(config) | 44 fs = mock_fs().withConfig(config) |
| 49 with mock_fs_scope(fs): | 45 with mock_fs_scope(fs): |
| 50 app = fs.getApp() | 46 app = fs.getApp() |
| 51 # The order of routes is important. Sources, not so much. | 47 # The order of routes is important. Sources, not so much. |
| 52 assert (list( | 48 assert (list( |
| 53 map( | 49 map( |
| 54 lambda v: v.get('generator') or v['source'], | 50 lambda v: v.get('generator') or v['source'], |
| 55 app.config.get('site/routes'))) == | 51 app.config.get('site/routes'))) == |
| 56 ['notes', 'posts', 'posts_archives', 'posts_tags', 'posts_categories', 'pages', 'theme_pages']) | 52 [ |
| 57 assert list(app.config.get('site/sources').keys()) == [ | 53 'notes', 'posts', 'posts_archives', 'posts_tags', |
| 58 'theme_pages', 'pages', 'posts', 'notes'] | 54 'posts_categories', 'pages', 'theme_pages']) |
| 55 assert set(app.config.get('site/sources').keys()) == set([ | |
| 56 'theme_pages', 'pages', 'posts', 'notes']) | |
| 59 | 57 |
| 60 | 58 |
| 61 def test_config_site_add_source_in_both_site_and_theme(): | 59 def test_config_site_add_source_in_both_site_and_theme(): |
| 62 theme_config = {'site': { | 60 theme_config = {'site': { |
| 63 'sources': {'theme_notes': {}}, | 61 'sources': {'theme_notes': {}}, |
| 64 'routes': [{'url': '/theme_notes/%path:slug%', 'source': 'theme_notes'}] | 62 'routes': [{'url': '/theme_notes/%path:slug%', 'source': 'theme_notes'}] |
| 65 }} | 63 }} |
| 66 config = {'site': { | 64 config = {'site': { |
| 67 'sources': {'notes': {}}, | 65 'sources': {'notes': {}}, |
| 68 'routes': [{'url': '/notes/%path:slug%', 'source': 'notes'}] | 66 'routes': [{'url': '/notes/%path:slug%', 'source': 'notes'}] |
| 69 }} | 67 }} |
| 70 fs = (mock_fs() | 68 fs = (mock_fs() |
| 71 .withConfig(config) | 69 .withConfig(config) |
| 72 .withFile('kitchen/theme/theme_config.yml', yaml.dump(theme_config))) | 70 .withFile('kitchen/theme/theme_config.yml', yaml.dump(theme_config))) |
| 73 with mock_fs_scope(fs): | 71 with mock_fs_scope(fs): |
| 74 app = fs.getApp() | 72 app = fs.getApp() |
| 75 # The order of routes is important. Sources, not so much. | 73 # The order of routes is important. Sources, not so much. |
| 76 # `posts` shows up 3 times in routes (posts, tags, categories) | 74 # `posts` shows up 3 times in routes (posts, tags, categories) |
| 77 assert (list( | 75 assert (list( |
| 78 map( | 76 map( |
| 79 lambda v: v.get('generator') or v['source'], | 77 lambda v: v.get('generator') or v['source'], |
| 80 app.config.get('site/routes'))) == | 78 app.config.get('site/routes'))) == |
| 81 ['notes', 'posts', 'posts_archives', 'posts_tags', 'posts_categories', 'pages', 'theme_notes', 'theme_pages']) | 79 [ |
| 82 assert list(app.config.get('site/sources').keys()) == [ | 80 'notes', 'posts', 'posts_archives', 'posts_tags', |
| 83 'theme_pages', 'theme_notes', 'pages', 'posts', 'notes'] | 81 'posts_categories', 'pages', 'theme_notes', |
| 82 'theme_pages']) | |
| 83 assert set(app.config.get('site/sources').keys()) == set([ | |
| 84 'theme_pages', 'theme_notes', 'pages', 'posts', 'notes']) | |
| 84 | 85 |
| 86 | |
| 87 def test_multiple_blogs(): | |
| 88 config = {'site': {'blogs': ['aaa', 'bbb']}} | |
| 89 fs = mock_fs().withConfig(config) | |
| 90 with mock_fs_scope(fs): | |
| 91 app = fs.getApp() | |
| 92 assert app.config.get('site/blogs') == ['aaa', 'bbb'] | |
| 93 assert (list( | |
| 94 map( | |
| 95 lambda v: v.get('generator') or v['source'], | |
| 96 app.config.get('site/routes'))) == | |
| 97 [ | |
| 98 'aaa', 'aaa_archives', 'aaa_tags', 'aaa_categories', | |
| 99 'bbb', 'bbb_archives', 'bbb_tags', 'bbb_categories', | |
| 100 'pages', 'theme_pages']) | |
| 101 assert set(app.config.get('site/sources').keys()) == set([ | |
| 102 'aaa', 'bbb', 'pages', 'theme_pages']) | |
| 103 | |
| 104 | |
| 105 def test_custom_list_setting(): | |
| 106 config = {'blah': ['foo', 'bar']} | |
| 107 fs = mock_fs().withConfig(config) | |
| 108 with mock_fs_scope(fs): | |
| 109 app = fs.getApp() | |
| 110 assert app.config.get('blah') == ['foo', 'bar'] | |
| 111 | |
| 112 | |
| 113 def test_custom_list_setting_in_site_section(): | |
| 114 config = {'site': {'blah': ['foo', 'bar']}} | |
| 115 fs = mock_fs().withConfig(config) | |
| 116 with mock_fs_scope(fs): | |
| 117 app = fs.getApp() | |
| 118 assert app.config.get('site/blah') == ['foo', 'bar'] |
