Mercurial > piecrust2
comparison tests/test_configuration.py @ 978:7e51d14097cb
config: Properly pass the merge mode to the `merge_dicts` function.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 Oct 2017 22:46:41 -0700 |
parents | 32c7c2d219d2 |
children |
comparison
equal
deleted
inserted
replaced
977:84fc72a17f7a | 978:7e51d14097cb |
---|---|
1 import copy | 1 import copy |
2 import datetime | |
3 import yaml | 2 import yaml |
4 import pytest | 3 import pytest |
5 from collections import OrderedDict | 4 from collections import OrderedDict |
6 from piecrust.configuration import (Configuration, ConfigurationLoader, | 5 from piecrust.configuration import ( |
7 merge_dicts) | 6 Configuration, ConfigurationLoader, merge_dicts, |
7 MERGE_APPEND_LISTS, MERGE_PREPEND_LISTS, MERGE_OVERWRITE_VALUES) | |
8 | 8 |
9 | 9 |
10 @pytest.mark.parametrize('values, expected', [ | 10 @pytest.mark.parametrize('values, expected', [ |
11 (None, {}), | 11 (None, {}), |
12 ({'foo': 'bar'}, {'foo': 'bar'}) | 12 ({'foo': 'bar'}, {'foo': 'bar'}) |
13 ]) | 13 ]) |
14 def test_config_init(values, expected): | 14 def test_config_init(values, expected): |
15 config = Configuration(values) | 15 config = Configuration(values) |
16 assert config.getAll() == expected | 16 assert config.getAll() == expected |
17 | 17 |
18 | 18 |
31 assert config.get('foo') == 'something' | 31 assert config.get('foo') == 'something' |
32 | 32 |
33 | 33 |
34 def test_config_get_and_set_nested(): | 34 def test_config_get_and_set_nested(): |
35 config = Configuration({ | 35 config = Configuration({ |
36 'foo': [4, 2], | 36 'foo': [4, 2], |
37 'bar': { | 37 'bar': { |
38 'child1': 'one', | 38 'child1': 'one', |
39 'child2': 'two' | 39 'child2': 'two' |
40 } | 40 } |
41 }) | 41 }) |
42 assert config.get('foo') == [4, 2] | 42 assert config.get('foo') == [4, 2] |
43 assert config.get('bar/child1') == 'one' | 43 assert config.get('bar/child1') == 'one' |
44 assert config.get('bar/child2') == 'two' | 44 assert config.get('bar/child2') == 'two' |
45 | 45 |
46 config.set('bar/child1', 'other one') | 46 config.set('bar/child1', 'other one') |
79 assert config.has('baz/or') is True | 79 assert config.has('baz/or') is True |
80 assert config.get('baz/or/whatever') == 'something' | 80 assert config.get('baz/or/whatever') == 'something' |
81 | 81 |
82 | 82 |
83 @pytest.mark.parametrize('local, incoming, expected', [ | 83 @pytest.mark.parametrize('local, incoming, expected', [ |
84 ({}, {}, {}), | 84 ({}, {}, {}), |
85 ({'foo': 'bar'}, {}, {'foo': 'bar'}), | 85 ({'foo': 'bar'}, {}, {'foo': 'bar'}), |
86 ({}, {'foo': 'bar'}, {'foo': 'bar'}), | 86 ({}, {'foo': 'bar'}, {'foo': 'bar'}), |
87 ({'foo': 'bar'}, {'foo': 'other'}, {'foo': 'other'}), | 87 ({'foo': 'bar'}, {'foo': 'other'}, {'foo': 'other'}), |
88 ({'foo': [1, 2]}, {'foo': [3]}, {'foo': [3, 1, 2]}), | 88 ({'foo': [1, 2]}, {'foo': [3]}, {'foo': [3, 1, 2]}), |
89 ({'foo': [1, 2]}, {'foo': 'bar'}, {'foo': 'bar'}), | 89 ({'foo': [1, 2]}, {'foo': 'bar'}, {'foo': 'bar'}), |
90 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': 'bar'}, {'foo': 'bar'}), | 90 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': 'bar'}, {'foo': 'bar'}), |
91 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'other': 3}}, {'foo': {'bar': 1, 'baz': 2, 'other': 3}}), | 91 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'other': 3}}, |
92 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'baz': 10}}, {'foo': {'bar': 1, 'baz': 10}}) | 92 {'foo': {'bar': 1, 'baz': 2, 'other': 3}}), |
93 ]) | 93 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'baz': 10}}, |
94 {'foo': {'bar': 1, 'baz': 10}}) | |
95 ]) | |
94 def test_merge_dicts(local, incoming, expected): | 96 def test_merge_dicts(local, incoming, expected): |
95 local2 = copy.deepcopy(local) | 97 local2 = copy.deepcopy(local) |
96 merge_dicts(local2, incoming) | 98 merge_dicts(local2, incoming) |
97 assert local2 == expected | 99 assert local2 == expected |
98 | 100 |
99 | 101 |
100 def test_config_merge(): | 102 def test_config_merge(): |
101 config = Configuration({ | 103 config = Configuration({ |
102 'foo': [4, 2], | 104 'foo': [4, 2], |
103 'bar': { | 105 'bar': { |
104 'child1': 'one', | 106 'child1': 'one', |
105 'child2': 'two' | 107 'child2': 'two' |
106 } | 108 } |
107 }) | 109 }) |
108 other = Configuration({ | 110 other = Configuration({ |
109 'baz': True, | 111 'baz': True, |
110 'blah': 'blah blah', | 112 'blah': 'blah blah', |
111 'bar': { | 113 'bar': { |
112 'child1': 'other one', | 114 'child1': 'other one', |
113 'child10': 'ten' | 115 'child10': 'ten' |
114 } | 116 } |
115 }) | 117 }) |
116 config.merge(other) | 118 config.merge(other) |
117 | 119 |
118 expected = { | 120 expected = { |
119 'foo': [4, 2], | 121 'foo': [4, 2], |
120 'baz': True, | 122 'baz': True, |
121 'blah': 'blah blah', | 123 'blah': 'blah blah', |
122 'bar': { | 124 'bar': { |
123 'child1': 'other one', | 125 'child1': 'other one', |
124 'child2': 'two', | 126 'child2': 'two', |
125 'child10': 'ten' | 127 'child10': 'ten' |
126 } | 128 } |
127 } | 129 } |
130 assert config.getAll() == expected | |
131 | |
132 | |
133 @pytest.mark.parametrize('mode, expected', [ | |
134 (MERGE_APPEND_LISTS, | |
135 {'foo': [4, 2, 1, 0], 'bar': 'something'}), | |
136 (MERGE_PREPEND_LISTS, | |
137 {'foo': [1, 0, 4, 2], 'bar': 'something'}), | |
138 (MERGE_OVERWRITE_VALUES, | |
139 {'foo': [4, 2], 'bar': 'other thing'}) | |
140 ]) | |
141 def test_config_merge_with_mode(mode, expected): | |
142 config = Configuration({ | |
143 'foo': [4, 2], | |
144 'bar': 'something' | |
145 }) | |
146 other = {'foo': [1, 0], 'bar': 'other thing'} | |
147 config.merge(other, mode=mode) | |
128 assert config.getAll() == expected | 148 assert config.getAll() == expected |
129 | 149 |
130 | 150 |
131 def test_ordered_loader(): | 151 def test_ordered_loader(): |
132 sample = """ | 152 sample = """ |