File: test_context_node.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (185 lines) | stat: -rw-r--r-- 4,530 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from textwrap import dedent

import pytest

from cylc.flow.context_node import ContextNode


with ContextNode('a') as a:
    with ContextNode('b') as b:
        c = ContextNode('c')
    d = ContextNode('d')


def test_context_node_tree():
    """Ensure parents and children are correctly organised."""
    assert a.name == 'a'
    assert a._parent is None
    assert a._children == {'b': b, 'd': d}
    assert b.name == 'b'
    assert b._parent == a
    assert b._children == {'c': c}
    assert c.name == 'c'
    assert c._parent == b
    assert c._children is None


def test_data_state():
    """Ensure the in-class state remains intact."""
    # DATA should start clean
    assert ContextNode.DATA == {}

    # nodes add themselves to DATA then remove themselves from it
    with ContextNode('foo'):
        pass

    # so DATA should end clean
    assert ContextNode.DATA == {}

    # however erroneously creating leaf-nodes outside of a context
    # will make a mess
    a = ContextNode('a')
    assert ContextNode.DATA == {a: set()}

    # just to make sure this doesn't result in hard-to-debug errors
    # we make sure this doesn't prevent us creating new trees
    with ContextNode('foo') as foo:
        bar = ContextNode('bar')
    assert ContextNode.DATA == {a: set(), foo: {a}}
    assert list(foo.walk()) == [(0, foo), (1, bar)]

    # finally clean up for niceness
    del ContextNode.DATA[a]
    del ContextNode.DATA[foo]


def test_context_iter():
    """Test iterating over child nodes."""
    assert list(a) == [b, d]
    assert list(b) == [c]
    assert list(c) == []


def test_context_contains():
    """Test `node.__contains__`."""
    assert 'b' in a
    assert 'c' in b
    assert 'c' not in a


def test_context_getitem():
    """Test `node.__getitem__`."""
    assert a['b'] == b
    assert b['c'] == c
    with pytest.raises(KeyError):
        assert b['d']
    with pytest.raises(TypeError):
        assert c['b']


def test_context_str():
    """Test the string representation of a node."""
    assert str(a) == 'a'
    assert str(b) == 'b'
    assert str(c) == 'c'


def test_context_repr():
    """Test the Python representation of a node."""
    assert repr(a) == 'a'
    assert repr(b) == 'a/b'
    assert repr(c) == 'a/b/c'


def test_context_walk():
    """Test walking the tree."""
    assert list(a.walk()) == [
        (0, a),
        (1, b),
        (2, c),
        (1, d)
    ]
    assert list(b.walk()) == [
        (0, b),
        (1, c),
    ]
    assert list(c.walk()) == [
        (0, c)
    ]


def test_context_walk_depth():
    assert list(a.walk(depth=1)) == [
        (0, a),
        (1, b),
        (1, d)
    ]


def test_context_tree():
    """Test the string representation of the tree."""
    assert a.tree() == dedent('''
        a
            b
                c
            d
    ''').strip()
    assert b.tree() == dedent('''
        b
            c
    ''').strip()
    assert c.tree() == dedent('''
        c
    ''').strip()


def test_context_is_root():
    """Test root node detection."""
    assert a.is_root()
    assert not b.is_root()
    assert not c.is_root()


def test_context_is_leaf():
    """Test leaf node detection."""
    assert not a.is_leaf()
    assert not b.is_leaf()
    assert c.is_leaf()


def test_context_parents():
    """Test linearised ancestry."""
    assert list(a.parents()) == []
    assert list(b.parents()) == [a]
    assert list(c.parents()) == [b, a]


def test_re_enter():
    """It should be able to re __enter__ multiple times."""
    # create a node with one child
    with ContextNode('x') as x:
        ContextNode('y')
    assert 'y' in x

    # go back and add another child retrospectively
    with x:
        ContextNode('z')
    assert 'y' in x
    assert 'z' in x