annotate tests/test_configuration.py @ 1051:971b4d67e82a

serve: Fix problems with assets disappearing between servings. When an asset file changes, its source's pipeline is re-run. But that created a bake record that only had that pipeline's output, so the other outputs were incorrectly considered empty and therefore any stray files were removed. Now we copy over bake records for the pipelines we don't run.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 26 Jan 2018 18:05:02 -0800
parents 7e51d14097cb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import copy
67
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
2 import yaml
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import pytest
67
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
4 from collections import OrderedDict
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
5 from piecrust.configuration import (
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
6 Configuration, ConfigurationLoader, merge_dicts,
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
7 MERGE_APPEND_LISTS, MERGE_PREPEND_LISTS, MERGE_OVERWRITE_VALUES)
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 @pytest.mark.parametrize('values, expected', [
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
11 (None, {}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
12 ({'foo': 'bar'}, {'foo': 'bar'})
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
13 ])
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 def test_config_init(values, expected):
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 config = Configuration(values)
440
32c7c2d219d2 performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents: 204
diff changeset
16 assert config.getAll() == expected
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 def test_config_set_all():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 config = Configuration()
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
21 config.setAll({'foo': 'bar'})
440
32c7c2d219d2 performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents: 204
diff changeset
22 assert config.getAll() == {'foo': 'bar'}
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 def test_config_get_and_set():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 config = Configuration({'foo': 'bar', 'answer': 42})
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 assert config.get('foo') == 'bar'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 assert config.get('answer') == 42
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 config.set('foo', 'something')
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 assert config.get('foo') == 'something'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 def test_config_get_and_set_nested():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 config = Configuration({
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
36 'foo': [4, 2],
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
37 'bar': {
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
38 'child1': 'one',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
39 'child2': 'two'
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
40 }
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
41 })
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 assert config.get('foo') == [4, 2]
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 assert config.get('bar/child1') == 'one'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 assert config.get('bar/child2') == 'two'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 config.set('bar/child1', 'other one')
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 config.set('bar/child3', 'new one')
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 assert config.get('bar/child1') == 'other one'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 assert config.get('bar/child3') == 'new one'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def test_config_get_missing():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 config = Configuration({'foo': 'bar'})
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 assert config.get('baz') is None
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 def test_config_has():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 config = Configuration({'foo': 'bar'})
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 assert config.has('foo') is True
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 assert config.has('baz') is False
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
204
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
63 def test_config_deep_set_non_existing():
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
64 config = Configuration({'foo': 'bar'})
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
65 assert config.get('baz') is None
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
66 config.set('baz/or/whatever', 'something')
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
67 assert config.has('baz') is True
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
68 assert config.has('baz/or') is True
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
69 assert config.get('baz/or/whatever') == 'something'
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
70
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
71
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
72 def test_config_deep_set_existing():
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
73 config = Configuration({'foo': 'bar', 'baz': {'wat': 'nothing'}})
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
74 assert config.has('baz') is True
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
75 assert config.get('baz/wat') == 'nothing'
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
76 assert config.get('baz/or') is None
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
77 config.set('baz/or/whatever', 'something')
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
78 assert config.has('baz') is True
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
79 assert config.has('baz/or') is True
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
80 assert config.get('baz/or/whatever') == 'something'
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
81
f98451237371 internal: Add ability to get a default value if a config value doesn't exist.
Ludovic Chabant <ludovic@chabant.com>
parents: 107
diff changeset
82
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 @pytest.mark.parametrize('local, incoming, expected', [
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
84 ({}, {}, {}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
85 ({'foo': 'bar'}, {}, {'foo': 'bar'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
86 ({}, {'foo': 'bar'}, {'foo': 'bar'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
87 ({'foo': 'bar'}, {'foo': 'other'}, {'foo': 'other'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
88 ({'foo': [1, 2]}, {'foo': [3]}, {'foo': [3, 1, 2]}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
89 ({'foo': [1, 2]}, {'foo': 'bar'}, {'foo': 'bar'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
90 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': 'bar'}, {'foo': 'bar'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
91 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'other': 3}},
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
92 {'foo': {'bar': 1, 'baz': 2, 'other': 3}}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
93 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'baz': 10}},
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
94 {'foo': {'bar': 1, 'baz': 10}})
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
95 ])
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 def test_merge_dicts(local, incoming, expected):
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 local2 = copy.deepcopy(local)
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 merge_dicts(local2, incoming)
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 assert local2 == expected
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 def test_config_merge():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 config = Configuration({
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
104 'foo': [4, 2],
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
105 'bar': {
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
106 'child1': 'one',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
107 'child2': 'two'
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
108 }
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
109 })
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 other = Configuration({
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
111 'baz': True,
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
112 'blah': 'blah blah',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
113 'bar': {
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
114 'child1': 'other one',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
115 'child10': 'ten'
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
116 }
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
117 })
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 config.merge(other)
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 expected = {
978
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
121 'foo': [4, 2],
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
122 'baz': True,
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
123 'blah': 'blah blah',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
124 'bar': {
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
125 'child1': 'other one',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
126 'child2': 'two',
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
127 'child10': 'ten'
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
128 }
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
129 }
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
130 assert config.getAll() == expected
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
131
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
132
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
133 @pytest.mark.parametrize('mode, expected', [
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
134 (MERGE_APPEND_LISTS,
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
135 {'foo': [4, 2, 1, 0], 'bar': 'something'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
136 (MERGE_PREPEND_LISTS,
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
137 {'foo': [1, 0, 4, 2], 'bar': 'something'}),
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
138 (MERGE_OVERWRITE_VALUES,
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
139 {'foo': [4, 2], 'bar': 'other thing'})
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
140 ])
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
141 def test_config_merge_with_mode(mode, expected):
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
142 config = Configuration({
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
143 'foo': [4, 2],
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
144 'bar': 'something'
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
145 })
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
146 other = {'foo': [1, 0], 'bar': 'other thing'}
7e51d14097cb config: Properly pass the merge mode to the `merge_dicts` function.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
147 config.merge(other, mode=mode)
440
32c7c2d219d2 performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents: 204
diff changeset
148 assert config.getAll() == expected
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149
67
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
150
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
151 def test_ordered_loader():
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
152 sample = """
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
153 one:
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
154 two: fish
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
155 red: fish
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
156 blue: fish
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
157 two:
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
158 a: yes
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
159 b: no
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
160 c: null
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
161 """
107
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
162 data = yaml.load(sample, Loader=ConfigurationLoader)
67
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
163 assert type(data) is OrderedDict
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
164 assert list(data['one'].keys()) == ['two', 'red', 'blue']
563ce5dd02af I don't care what the YAML spec says, ordered maps are the only sane way.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
165
107
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
166
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
167 def test_load_time1():
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
168 sample = """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
169 time: 21:35
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
170 """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
171 data = yaml.load(sample, Loader=ConfigurationLoader)
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
172 assert type(data['time']) is int
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
173 assert data['time'] == (21 * 60 * 60 + 35 * 60)
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
174
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
175
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
176 def test_load_time2():
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
177 sample = """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
178 time: 21:35:50
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
179 """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
180 data = yaml.load(sample, Loader=ConfigurationLoader)
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
181 assert type(data['time']) is int
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
182 assert data['time'] == (21 * 60 * 60 + 35 * 60 + 50)
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
183