annotate tests/test_configuration.py @ 784:6d8fe8e93a91

internal: Add missing timer scope.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 17 Aug 2016 22:01:26 -0700
parents 32c7c2d219d2
children 7e51d14097cb
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
107
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
2 import datetime
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
3 import yaml
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 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
5 from collections import OrderedDict
107
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
6 from piecrust.configuration import (Configuration, 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
7 merge_dicts)
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', [
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 (None, {}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 ({'foo': 'bar'}, {'foo': 'bar'})
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 ])
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({
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 'foo': [4, 2],
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 'bar': {
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 'child1': 'one',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 'child2': 'two'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 }
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 })
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', [
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 ({}, {}, {}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 ({'foo': 'bar'}, {}, {'foo': 'bar'}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 ({}, {'foo': 'bar'}, {'foo': 'bar'}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 ({'foo': 'bar'}, {'foo': 'other'}, {'foo': 'other'}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 ({'foo': [1, 2]}, {'foo': [3]}, {'foo': [3, 1, 2]}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 ({'foo': [1, 2]}, {'foo': 'bar'}, {'foo': 'bar'}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': 'bar'}, {'foo': 'bar'}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'other': 3}}, {'foo': {'bar': 1, 'baz': 2, 'other': 3}}),
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'baz': 10}}, {'foo': {'bar': 1, 'baz': 10}})
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 ])
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 def test_merge_dicts(local, incoming, expected):
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 local2 = copy.deepcopy(local)
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 merge_dicts(local2, incoming)
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 assert local2 == expected
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 def test_config_merge():
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 config = Configuration({
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 'foo': [4, 2],
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 'bar': {
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 'child1': 'one',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 'child2': 'two'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 }
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 })
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 other = Configuration({
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 'baz': True,
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 'blah': 'blah blah',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 'bar': {
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 'child1': 'other one',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 'child10': 'ten'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 }
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 })
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 config.merge(other)
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 expected = {
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 'foo': [4, 2],
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 'baz': True,
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 'blah': 'blah blah',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 'bar': {
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 'child1': 'other one',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 'child2': 'two',
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 'child10': 'ten'
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 }
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 }
440
32c7c2d219d2 performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents: 204
diff changeset
128 assert config.getAll() == expected
2
40fa08b261b9 Added unit tests (using `py.test`) for `Configuration`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129
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
130
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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141 """
107
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
142 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
143 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
144 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
145
107
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
146
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
147 def test_load_time1():
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
148 sample = """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
149 time: 21:35
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
150 """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
151 data = yaml.load(sample, Loader=ConfigurationLoader)
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
152 assert type(data['time']) is int
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
153 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
154
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
155
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
156 def test_load_time2():
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
157 sample = """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
158 time: 21:35:50
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
159 """
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
160 data = yaml.load(sample, Loader=ConfigurationLoader)
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
161 assert type(data['time']) is int
10fc9c8bf682 Better support for times in YAML interop.
Ludovic Chabant <ludovic@chabant.com>
parents: 67
diff changeset
162 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
163