Debian Codegraph @

tests/u/parsec/test_dict_tree.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from cylc.flow.parsec.OrderedDict import (
18 DictTree,
19 OrderedDictWithDefaults as ODD
20 )
21
22 import pytest
23
24
25 def test_eq():
26 """It compares to identical instances."""
27 assert DictTree({'a': 1}) == DictTree({'a': 1})
28 assert DictTree({'a': 1}) != DictTree({'a': 2})
29 assert DictTree({'a': 1}) != {'a': 1}
30
31
32 def test_nesting():
33 """It slices without duplicating data."""
34 this_a = dict(b=1)
35 this = dict(a=this_a)
36 that_a = dict(b=2)
37 that = dict(a=that_a)
38 both = DictTree(this, that)
39
40 sub = both['a']
41 assert sub._tree == (this_a, that_a)
42 r_this_a, r_that_a = sub._tree
43 assert id(r_this_a) == id(this_a)
44 assert id(r_that_a) == id(that_a)
45
46
47 def test_iter():
48 """It yields keys from all dictionaries."""
49 a = DictTree({'a': 1}, {'a': 2, 'b': 3})
50 assert list(sorted(a)) == ['a', 'b']
51
52
53 def test_iter_defaults():
54 """It doesn't yield keys from defaults."""
55 this = ODD(a=1)
56 that = ODD(b=2)
57 this.defaults = dict(c=3)
58 that.defaults = dict(d=4)
59 a = DictTree(this, that)
60 assert list(sorted(a)) == ['a', 'b']
61
62
63 def test_getitem():
64 """It returns values prioritised correctly via the `getitem` interface."""
65 this = {
66 'a': 1,
67 'b': 2,
68 'f': {
69 'x': 5,
70 'y': 6
71 }
72 }
73 that = {
74 'b': 3,
75 'd': 4,
76 'f': {
77 'y': 7,
78 'z': 8
79 }
80 }
81 a = DictTree(this, that)
82
83 # key from this
84 assert a['a'] == 1
85 # key from that
86 assert a['d'] == 4
87 # key from this overrides key from that
88 assert a['b'] == 2
89
90 # dict from both
91 f = a['f']
92 assert f._tree == (this['f'], that['f'])
93 assert f['x'] == 5 # key from this
94 assert f['y'] == 6 # key from this which is also in that
95 assert f['z'] == 8 # key from that
96
97 # mutate definition object
98 this['f']['y'] = 42
99 assert a['f']['y'] == 42
100
101
102 def test_get():
103 """It returns values correctly via the `get` interface."""
104 a = DictTree({'a': 1}, {'b': 2})
105
106 # key exists in this
107 assert a.get('a') == 1
108 assert a.get('a', 42) == 1
109 # key exists in that
110 assert a.get('b') == 2
111 assert a.get('b', 42) == 2
112 # key does not exist
113 assert a.get('e') is None
114 assert a.get('e', 42) == 42
115
116
117 def test_get_dict():
118 """It slices correctly via the `get` interface."""
119 a = DictTree({'a': {'b': 1}}, {'a': {'b': 2}})
120 assert a.get('a') == DictTree({'b': 1}, {'b': 2})
121
122
123 def test_odd_getitem():
124 """It returns default values if none of the dicts contains the key."""
125 this = ODD(a=1)
126 this.defaults_ = dict(c=3)
127 that = ODD(b=2)
128 that.defaults_ = dict(d=4)
129 both = DictTree(this, that)
130
131 # key from this
132 assert both['a'] == 1
133 # key from that
134 assert both['b'] == 2
135 # default from this
136 assert both['c'] == 3
137 # default from that
138 assert both['d'] == 4
139 # missing value
140 with pytest.raises(KeyError):
141 both['e']
142
143
144 def test_defaults_getitem_dict():
145 """It returns defaults if a value isn't set."""
146 this = ODD()
147 that = ODD(a=2)
148 both = DictTree(this, that)
149
150 # default in this gets overridden by value in that
151 this.defaults_ = dict(a=1)
152 that.defaults_ = dict()
153 assert both['a'] == 2
154
155 # default in first dict gets priority
156 this.defaults_ = dict(b=1)
157 that.defaults_ = dict(b=2)
158 assert both['b'] == 1
159
160
161 def test_defaults_getitem_nested():
162 """It preserves the defaults_ behaviour of ODD in nested dicts."""
163 this = ODD()
164 that = ODD(a=2)
165 both = DictTree(dict(a=this), dict(a=that))
166
167 # default in this gets overridden by value in that
168 this.defaults_ = dict(a=1)
169 that.defaults_ = dict()
170 assert both['a']['a'] == 2
171
172 # default in first dict gets priority
173 this.defaults_ = dict(b=1)
174 that.defaults_ = dict(b=2)
175 assert both['a']['b'] == 1