Debian Codegraph @

tests/u/test_context_node.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
18 from textwrap import dedent
19
20 import pytest
21
22 from cylc.flow.context_node import ContextNode
23
24
25 with ContextNode('a') as a:
26 with ContextNode('b') as b:
27 c = ContextNode('c')
28 d = ContextNode('d')
29
30
31 def test_context_node_tree():
32 """Ensure parents and children are correctly organised."""
33 assert a.name == 'a'
34 assert a._parent is None
35 assert a._children == {'b': b, 'd': d}
36 assert b.name == 'b'
37 assert b._parent == a
38 assert b._children == {'c': c}
39 assert c.name == 'c'
40 assert c._parent == b
41 assert c._children is None
42
43
44 def test_data_state():
45 """Ensure the in-class state remains intact."""
46 # DATA should start clean
47 assert ContextNode.DATA == {}
48
49 # nodes add themselves to DATA then remove themselves from it
50 with ContextNode('foo'):
51 pass
52
53 # so DATA should end clean
54 assert ContextNode.DATA == {}
55
56 # however erroneously creating leaf-nodes outside of a context
57 # will make a mess
58 a = ContextNode('a')
59 assert ContextNode.DATA == {a: set()}
60
61 # just to make sure this doesn't result in hard-to-debug errors
62 # we make sure this doesn't prevent us creating new trees
63 with ContextNode('foo') as foo:
64 bar = ContextNode('bar')
65 assert ContextNode.DATA == {a: set(), foo: {a}}
66 assert list(foo.walk()) == [(0, foo), (1, bar)]
67
68 # finally clean up for niceness
69 del ContextNode.DATA[a]
70 del ContextNode.DATA[foo]
71
72
73 def test_context_iter():
74 """Test iterating over child nodes."""
75 assert list(a) == [b, d]
76 assert list(b) == [c]
77 assert list(c) == []
78
79
80 def test_context_contains():
81 """Test `node.__contains__`."""
82 assert 'b' in a
83 assert 'c' in b
84 assert 'c' not in a
85
86
87 def test_context_getitem():
88 """Test `node.__getitem__`."""
89 assert a['b'] == b
90 assert b['c'] == c
91 with pytest.raises(KeyError):
92 assert b['d']
93 with pytest.raises(TypeError):
94 assert c['b']
95
96
97 def test_context_str():
98 """Test the string representation of a node."""
99 assert str(a) == 'a'
100 assert str(b) == 'b'
101 assert str(c) == 'c'
102
103
104 def test_context_repr():
105 """Test the Python representation of a node."""
106 assert repr(a) == 'a'
107 assert repr(b) == 'a/b'
108 assert repr(c) == 'a/b/c'
109
110
111 def test_context_walk():
112 """Test walking the tree."""
113 assert list(a.walk()) == [
114 (0, a),
115 (1, b),
116 (2, c),
117 (1, d)
118 ]
119 assert list(b.walk()) == [
120 (0, b),
121 (1, c),
122 ]
123 assert list(c.walk()) == [
124 (0, c)
125 ]
126
127
128 def test_context_walk_depth():
129 assert list(a.walk(depth=1)) == [
130 (0, a),
131 (1, b),
132 (1, d)
133 ]
134
135
136 def test_context_tree():
137 """Test the string representation of the tree."""
138 assert a.tree() == dedent('''
139 a
140 b
141 c
142 d
143 ''').strip()
144 assert b.tree() == dedent('''
145 b
146 c
147 ''').strip()
148 assert c.tree() == dedent('''
149 c
150 ''').strip()
151
152
153 def test_context_is_root():
154 """Test root node detection."""
155 assert a.is_root()
156 assert not b.is_root()
157 assert not c.is_root()
158
159
160 def test_context_is_leaf():
161 """Test leaf node detection."""
162 assert not a.is_leaf()
163 assert not b.is_leaf()
164 assert c.is_leaf()
165
166
167 def test_context_parents():
168 """Test linearised ancestry."""
169 assert list(a.parents()) == []
170 assert list(b.parents()) == [a]
171 assert list(c.parents()) == [b, a]
172
173
174 def test_re_enter():
175 """It should be able to re __enter__ multiple times."""
176 # create a node with one child
177 with ContextNode('x') as x:
178 ContextNode('y')
179 assert 'y' in x
180
181 # go back and add another child retrospectively
182 with x:
183 ContextNode('z')
184 assert 'y' in x
185 assert 'z' in x