• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zincware / ZnFlow / 12348896592

16 Dec 2024 08:40AM UTC coverage: 96.786% (-0.01%) from 96.799%
12348896592

Pull #128

github

web-flow
Merge a9a215782 into 899bf9436
Pull Request #128: raise error on `append` and `extend` connections

49 of 51 new or added lines in 2 files covered. (96.08%)

4 existing lines in 3 files now uncovered.

2710 of 2800 relevant lines covered (96.79%)

3.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.52
/tests/test_graph.py
1
import dataclasses
4✔
2

3
import pytest
4✔
4
import zninit
4✔
5

6
import znflow
4✔
7

8

9
class PlainNode(znflow.Node):
4✔
10
    def __init__(self, value):
4✔
11
        self.value = value
×
12

13
    def run(self):
4✔
14
        self.value += 1
×
15

16

17
@dataclasses.dataclass
4✔
18
class DataclassNode(znflow.Node):
4✔
19
    value: int
4✔
20

21
    def run(self):
4✔
22
        self.value += 1
4✔
23

24

25
class ZnInitNode(zninit.ZnInit, znflow.Node):
4✔
26
    value: int = zninit.Descriptor()
4✔
27

28
    def run(self):
4✔
29
        self.value += 1
×
30

31

32
class ComputeSum(znflow.Node):
4✔
33
    def __init__(self, *args):
4✔
34
        self.args = args
4✔
35

36
    def run(self):
4✔
37
        self.result = sum(x.value for x in self.args)
4✔
38

39

40
@znflow.nodify
4✔
41
def compute_sum(*args):
4✔
42
    return sum(args)
4✔
43

44

45
def test_run_graph():
4✔
46
    with znflow.DiGraph() as graph:
4✔
47
        node1 = DataclassNode(value=42)
4✔
48
        node2 = DataclassNode(value=18)
4✔
49
        node3 = compute_sum(node1.value, node2.value)
4✔
50

51
    assert node1.uuid in graph
4✔
52
    assert node2.uuid in graph
4✔
53
    assert node3.uuid in graph
4✔
54

55
    assert node1.value == 42
4✔
56
    assert node2.value == 18
4✔
57

58
    graph.run()
4✔
59

60
    assert node1.value == 43
4✔
61
    assert node2.value == 19
4✔
62
    assert node3.result == 62
4✔
63

64

65
def test_nested_graph():
4✔
66
    """Test nested DiGraph."""
67
    with znflow.DiGraph():
4✔
68
        with pytest.raises(ValueError):
4✔
69
            with znflow.DiGraph():
4✔
UNCOV
70
                pass
1✔
71

72

73
def test_changed_graph():
4✔
74
    """Test changed DiGraph."""
75
    with pytest.raises(ValueError):
4✔
76
        with znflow.DiGraph():
4✔
77
            znflow.base.NodeBaseMixin._graph_ = znflow.DiGraph()
4✔
78
    znflow.base.NodeBaseMixin._graph_ = znflow.empty_graph  # reset after test
4✔
79

80

81
def test_add_others():
4✔
82
    graph = znflow.DiGraph()
4✔
83
    with pytest.raises(ValueError):
4✔
84
        # it is only possible to add classes inheriting from NodeBaseMixin
85
        graph.add_node(42)
4✔
86

87

88
def test_add_connections():
4✔
89
    graph = znflow.DiGraph()
4✔
90
    with pytest.raises(ValueError):
4✔
91
        # it is only to connect Connection and NodeBaseMixin
92
        graph.add_connections(42, 42)
4✔
93

94

95
@dataclasses.dataclass
4✔
96
class ParameterContainer:
4✔
97
    value: int
4✔
98

99

100
def test_add_not_Node():
4✔
101
    parameter = ParameterContainer(value=42)
4✔
102
    with znflow.DiGraph() as graph:
4✔
103
        # ParameterContainer is not of type NodeBaseMixin,
104
        #  therefore, it won't be added to the graph
105
        node1 = DataclassNode(value=parameter.value)
4✔
106
        assert parameter.value == 42
4✔
107

108
    assert node1.value == 42
4✔
109

110
    graph.run()
4✔
111
    assert len(graph) == 1
4✔
112
    assert node1.value == 43
4✔
113

114

115
def test_not_added_to_graph():
4✔
116
    """Test, if a Node has a deliberately disabled _graph_ attribute
117

118
    You can set instance._graph_ = None to disable adding the node to the graph.
119
    """
120
    node1 = DataclassNode(value=42)
4✔
121
    node1._graph_ = None
4✔
122
    assert node1.value == 42
4✔
123

124
    with znflow.DiGraph() as graph:
4✔
125
        assert node1._graph_ is None
4✔
126
        node2 = DataclassNode(value=18)
4✔
127
        assert node2._graph_ is graph
4✔
128

129
        assert node1.value == 42
4✔
130

131
        node3 = compute_sum(node1.value, node2.value)  # test getattr
4✔
132
        node4 = ComputeSum(node1, node2)  # test AttributeToConnection
4✔
133
        assert node3.args[0] == 42  # not a connection
4✔
134
        assert isinstance(node3.args[1], znflow.Connection)
4✔
135

136
    assert node1.uuid not in graph
4✔
137
    assert node2.uuid in graph
4✔
138
    assert node3.uuid in graph
4✔
139
    assert node4.uuid in graph
4✔
140

141
    assert node1.value == 42
4✔
142
    assert node2.value == 18
4✔
143

144
    assert len(graph) == 3
4✔
145

146
    graph.run()
4✔
147

148
    assert node1.value == 42  # the value is not +1 because
4✔
149
    #  it was not added to the graph
150
    assert node2.value == 19
4✔
151
    assert node3.result == 61
4✔
152
    assert node4.result == 61
4✔
153

154

155
def test_disable_graph():
4✔
156
    graph = znflow.DiGraph()
4✔
157
    with graph:
4✔
158
        node1 = DataclassNode(value=42)
4✔
159
        assert node1._graph_ is graph
4✔
160
        with znflow.base.disable_graph():
4✔
161
            assert node1._graph_ is znflow.empty_graph
4✔
162
        assert node1._graph_ is graph
4✔
163
    assert node1._graph_ is znflow.empty_graph
4✔
164

165

166
def test_get_attribute():
4✔
167
    graph = znflow.DiGraph()
4✔
168
    with graph:
4✔
169
        node1 = DataclassNode(value=42)
4✔
170
        assert znflow.base.get_attribute(node1, "value") == 42
4✔
171
        assert isinstance(node1.value, znflow.Connection)
4✔
172

173
    assert znflow.base.get_attribute(node1, "value") == 42
4✔
174
    with pytest.raises(AttributeError):
4✔
175
        znflow.base.get_attribute(node1, "not_existing")
4✔
176

177
    assert znflow.base.get_attribute(node1, "not_existing", None) is None
4✔
178
    assert znflow.base.get_attribute(node1, "not_existing", 13) == 13
4✔
179

180

181
def test_node_not_on_graph():
4✔
182
    node1 = DataclassNode(value=42)
4✔
183

184
    with pytest.raises(ValueError):
4✔
185
        with znflow.DiGraph():
4✔
186
            with pytest.raises(ValueError):
4✔
187
                _ = DataclassNode(value=node1.value)
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc