forked from csaez/dgpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
230 lines (169 loc) · 7.06 KB
/
tests.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import functools
import unittest
import logging
import dgpy
logger = logging.getLogger("dgpy")
logger.addHandler(logging.StreamHandler())
def debug(func):
@functools.wraps(func)
def decorated(*args, **kwds):
level = logger.level
logger.setLevel(logging.DEBUG)
rval = None
try:
rval = func(*args, **kwds)
finally:
logger.setLevel(level)
return rval
return decorated
class AddNode(dgpy.VoidNode):
def initPorts(self):
super(AddNode, self).initPorts()
self.addInputPort("value1")
self.addInputPort("value2")
self.addOutputPort("result")
def evaluate(self):
super(AddNode, self).evaluate()
result = 0
for p in self._inputPorts.values():
msg = "{0}: {1}".format(p.name, p.value)
if p.isConnected:
msg += " (connected)"
logger.debug(msg)
if p.value is not None:
result += p.value
self.getOutputPort("result").value = result
logger.debug("---")
dgpy.registerNode(AddNode)
class UsageCase(unittest.TestCase):
def testAddRemoveNodes(self):
graph = dgpy.Graph()
node1 = graph.addNode("node1", dgpy.VoidNode)
self.assertEqual(len(graph.nodes), 1)
graph.removeNode(node1)
self.assertEqual(len(graph.nodes), 0)
def testDisconnect(self):
graph = dgpy.Graph()
node1 = graph.addNode("node1", AddNode)
node1.getInputPort("value1").value = 2
node1.getInputPort("value2").value = 3
node2 = graph.addNode("node2", AddNode, value1=5)
node2.getInputPort("value2").connect(node1.getOutputPort("result"))
self.assertTrue(node1.getOutputPort("result").isConnected)
self.assertTrue(node2.getInputPort("value2").isConnected)
node2.getInputPort("value2").disconnect()
self.assertFalse(node1.getOutputPort("result").isConnected)
self.assertFalse(node2.getInputPort("value2").isConnected)
def testGetter(self):
graph = dgpy.Graph()
graph.addNode("node1", AddNode)
self.assertIsNotNone(graph.get("node1.value1"))
self.assertIsNotNone(graph.get("node1.value2"))
self.assertIsNotNone(graph.get("node1.result"))
self.assertIsNotNone(graph.get("node1"))
self.assertIsNone(graph.get("foo"))
def testDataValidation(self):
self.assertIsNone(dgpy.Graph.fromData(1))
self.assertIsNone(dgpy.Graph.fromData("foo"))
self.assertIsNone(dgpy.Graph.fromData({"foo": "bar"}))
self.assertIsNone(dgpy.Graph.fromData({"dataType": "bar"}))
class PushModelCase(unittest.TestCase):
def __init__(self, *args):
super(PushModelCase, self).__init__(*args)
self.model = dgpy.PUSH
def testSingleNodeEvaluation(self):
graph = dgpy.Graph()
graph.model = self.model
node1 = graph.addNode("node1", AddNode)
node1.getInputPort("value1").value = 2
node1.getInputPort("value2").value = 3
self.assertEqual(node1.getOutputPort("result").value, 2+3)
return graph
def testModelIsolation(self):
graph = dgpy.Graph()
graph.model = None
node1 = graph.addNode("node1", AddNode)
node1.getInputPort("value1").value = 2
node1.getInputPort("value2").value = 3
self.assertIsNone(node1.getOutputPort("result").value)
return graph
def testNodeConnections(self):
graph = dgpy.Graph()
graph.model = self.model
node1 = graph.addNode("node1", AddNode)
node1.getInputPort("value1").value = 2
node1.getInputPort("value2").value = 3
node2 = graph.addNode("node2", AddNode, value1=5)
node2.getInputPort("value2").connect(node1.getOutputPort("result"))
self.assertEqual(node2.getOutputPort("result").value, 5 + 5)
return graph
def testPersistentConnections(self):
graph = dgpy.Graph()
graph.model = self.model
node1 = graph.addNode("node1", AddNode)
node1.getInputPort("value1").value = 2
node1.getInputPort("value2").value = 3
node2 = graph.addNode("node2", AddNode, value1=5)
node2.getInputPort("value2").connect(node1.getOutputPort("result"))
self.assertEqual(node2.getOutputPort("result").value, 5 + 5)
node1.getInputPort("value1").value = 10
self.assertEqual(node2.getOutputPort("result").value, 5 + 13)
return graph
def testBranching(self):
graph = self.testNodeConnections()
node1 = graph.getNode("node1")
node3 = graph.addNode("node3", AddNode, value1=8)
node3.getInputPort("value2").connect(node1.getOutputPort("result"))
self.assertEqual(node3.getOutputPort("result").value, 8 + 5)
return graph
def testBranchingPersistence(self):
graph = self.testBranching()
node1 = graph.getNode("node1")
node1.getInputPort("value1").value = 1
self.assertEqual(node1.getOutputPort("result").value, 1 + 3)
node3 = graph.getNode("node3")
self.assertEqual(node3.getOutputPort("result").value, 8 + 4)
node2 = graph.getNode("node2")
self.assertEqual(node2.getOutputPort("result").value, 5 + 4)
class PullModelCase(PushModelCase):
def __init__(self, *args):
super(PullModelCase, self).__init__(*args)
self.model = dgpy.PULL
def testEvaluationCount(self):
graph = self.testSingleNodeEvaluation()
node1 = graph.getNode("node1")
node1.getOutputPort("result").value
self.assertEqual(node1.evalCount, 1)
class SerializationCase(unittest.TestCase):
def testEmptyGraph(self):
graph1 = dgpy.Graph()
graph1.model = dgpy.PULL
data = graph1.serialize()
graph2 = dgpy.Graph.fromData(data)
self.assertEqual(graph1.model, graph2.model)
self.assertEqual(len(graph1.nodes), len(graph2.nodes))
def testOrphanNodes(self):
graph1 = dgpy.Graph()
graph1.model = dgpy.PULL
graph1.addNode("testingVoidNode", dgpy.VoidNode)
graph1.addNode("testingAddNode", AddNode, value1=2, value2=3)
data = graph1.serialize()
graph2 = dgpy.Graph.fromData(data)
self.assertDictEqual(data, graph2.serialize())
def testConnectedNodes(self):
graph = dgpy.Graph()
graph.model = dgpy.PULL
node1 = graph.addNode("node1", AddNode)
node1.getInputPort("value1").value = 2
node1.getInputPort("value2").value = 3
node2 = graph.addNode("node2", AddNode, value1=5)
node2.getInputPort("value2").connect(node1.getOutputPort("result"))
node3 = graph.addNode("node3", AddNode, value1=8)
node3.getInputPort("value2").connect(node2.getOutputPort("result"))
graph.addNode("testingVoidNode", dgpy.VoidNode)
data = graph.serialize()
graph2 = dgpy.Graph.fromData(data)
self.assertTrue(graph2.get("node2.value2").isConnected)
self.assertTrue(graph2.get("node3.value2").isConnected)
if __name__ == '__main__':
unittest.main(verbosity=2)