-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_graph.py
More file actions
408 lines (329 loc) · 14 KB
/
Copy pathtest_graph.py
File metadata and controls
408 lines (329 loc) · 14 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from meshroom.core.exception import CyclicDependencyError
from meshroom.core.graph import Graph
def test_depth():
graph = Graph("Tests tasks depth")
tA = graph.addNewNode("Ls", input="/tmp")
tB = graph.addNewNode("AppendText", inputText="echo B")
tC = graph.addNewNode("AppendText", inputText="echo C")
tA.output.connectTo(tB.input)
tB.output.connectTo(tC.input)
assert tA.depth == 0
assert tB.depth == 1
assert tC.depth == 2
def test_transitive_reduction():
graph = Graph("Tests tasks depth")
tA = graph.addNewNode("Ls", input="/tmp")
tB = graph.addNewNode("AppendText", inputText="echo B")
tC = graph.addNewNode("AppendText", inputText="echo C")
tD = graph.addNewNode("AppendText", inputText="echo D")
tE = graph.addNewNode("AppendFiles")
# C
# / \
# /---/---->\
# A -> B ---> E
# \ /
# \ /
# D
tA.output.connectTo(tE.input)
tA.output.connectTo(tB.input)
tB.output.connectTo(tC.input)
tB.output.connectTo(tD.input)
tB.output.connectTo(tE.input4)
tC.output.connectTo(tE.input3)
tD.output.connectTo(tE.input2)
flowEdges = graph.flowEdges()
flowEdgesRes = [(tB, tA),
(tD, tB),
(tC, tB),
(tE, tD),
(tE, tC),
]
assert set(flowEdgesRes) == set(flowEdges)
assert len(graph._nodesMinMaxDepths) == len(graph.nodes)
for node, (_, maxDepth) in graph._nodesMinMaxDepths.items():
assert node.depth == maxDepth
def test_graph_nodes_sorting():
graph = Graph("")
ls0 = graph.addNewNode("Ls")
ls1 = graph.addNewNode("Ls")
ls2 = graph.addNewNode("Ls")
assert graph.nodesOfType("Ls", sortedByIndex=True) == [ls0, ls1, ls2]
graph = Graph("")
# 'Random' creation order (what happens when loading a file)
ls2 = graph.addNewNode("Ls", name="Ls_2")
ls0 = graph.addNewNode("Ls", name="Ls_0")
ls1 = graph.addNewNode("Ls", name="Ls_1")
assert graph.nodesOfType("Ls", sortedByIndex=True) == [ls0, ls1, ls2]
def test_duplicate_nodes():
"""
Test nodes duplication.
"""
# n0 -- n1 -- n2
# \ \
# ---------- n3
g = Graph("")
n0 = g.addNewNode("Ls", input="/tmp")
n1 = g.addNewNode("Ls", input=n0.output)
n2 = g.addNewNode("Ls", input=n1.output)
n3 = g.addNewNode("AppendFiles", input=n1.output, input2=n2.output)
# Duplicate from n1
nodes_to_duplicate, _ = g.dfsOnDiscover(startNodes=[n1], reverse=True, dependenciesOnly=True)
nMap = g.duplicateNodes(srcNodes=nodes_to_duplicate)
for s, duplicated in nMap.items():
for d in duplicated:
assert s.nodeType == d.nodeType
# Check number of duplicated nodes and that every parent node has been duplicated once
assert len(nMap) == 3 and \
all([len(nMap[i]) == 1 for i in nMap.keys()])
# Check connections
# Access directly index 0 because we know there is a single duplicate for each parent node
assert nMap[n1][0].input.inputLink == n0.output
assert nMap[n2][0].input.inputLink == nMap[n1][0].output
assert nMap[n3][0].input.inputLink == nMap[n1][0].output
assert nMap[n3][0].input2.inputLink == nMap[n2][0].output
def test_rename_nodes():
"""
Test renaming nodes.
"""
graph = Graph("")
ls0 = graph.addNewNode("Ls")
ls1 = graph.addNewNode("Ls")
ls2 = graph.addNewNode("Ls")
# Test with empty string
assert ls0.name == "Ls_1"
graph.renameNode(ls0, "")
assert ls0.name == "Ls_1"
# Rename
graph.renameNode(ls0, "nodels")
assert ls0.name == "nodels"
graph.renameNode(ls1, "nodels")
assert ls1.name == "nodels_1"
graph.renameNode(ls2, "nodels")
assert ls2.name == "nodels_2"
# Check we cannot rename in locked mode
ls0.setLocked(True)
graph.renameNode(ls0, "lockedLs")
assert ls0.name == "nodels"
def test_empty_graph():
"""Test edge-case behavior on a graph with no nodes."""
graph = Graph("")
assert graph.nodes == []
assert graph.nodesOfType("Ls") == []
assert graph.flowEdges() == []
class TestDFS:
""" Tests for the graph DFS traversal methods. """
def test_dfs_on_finish_depth_diamond_graph(self):
graph = Graph("Tests tasks depth (diamond graph)")
tA = graph.addNewNode("Ls", input="/tmp")
tB = graph.addNewNode("AppendText", inputText="echo B")
tC = graph.addNewNode("AppendText", inputText="echo C")
tD = graph.addNewNode("AppendFiles")
tA.output.connectTo(tB.input)
tA.output.connectTo(tC.input)
tB.output.connectTo(tD.input)
tC.output.connectTo(tD.input2)
assert tA.depth == 0
assert tB.depth == 1
assert tC.depth == 1
assert tD.depth == 2
nodes, edges = graph.dfsOnFinish()
assert len(nodes) == 4
assert nodes[0] == tA
assert nodes[-1] == tD
assert len(edges) == 4
nodes, edges = graph.dfsOnFinish(startNodes=[tD])
assert len(nodes) == 4
assert nodes[0] == tA
assert nodes[-1] == tD
assert len(edges) == 4
nodes, edges = graph.dfsOnFinish(startNodes=[tB])
assert len(nodes) == 2
assert nodes[0] == tA
assert nodes[-1] == tB
assert len(edges) == 1
def test_dfs_on_finish_depth_diamond_graph2(self):
graph = Graph("Tests tasks depth (diamond graph 2)")
tA = graph.addNewNode("Ls", input="/tmp")
tB = graph.addNewNode("AppendText", inputText="echo B")
tC = graph.addNewNode("AppendText", inputText="echo C")
tD = graph.addNewNode("AppendText", inputText="echo D")
tE = graph.addNewNode("AppendFiles")
# C
# / \
# /---/----->\
# A -> B ---> E
# \ /
# \ /
# D
tA.output.connectTo(tB.input)
tB.output.connectTo(tC.input)
tB.output.connectTo(tD.input)
tA.output.connectTo(tE.input)
tB.output.connectTo(tE.input2)
tC.output.connectTo(tE.input3)
tD.output.connectTo(tE.input4)
assert tA.depth == 0
assert tB.depth == 1
assert tC.depth == 2
assert tD.depth == 2
assert tE.depth == 3
nodes, edges = graph.dfsOnFinish()
assert len(nodes) == 5
assert nodes[0] == tA
assert nodes[-1] == tE
assert len(edges) == 7
nodes, edges = graph.dfsOnFinish(startNodes=[tE])
assert len(nodes) == 5
assert nodes[0] == tA
assert nodes[-1] == tE
assert len(edges) == 7
nodes, edges = graph.dfsOnFinish(startNodes=[tD])
assert len(nodes) == 3
assert nodes[0] == tA
assert nodes[1] == tB
assert nodes[2] == tD
assert len(edges) == 2
nodes, edges = graph.dfsOnFinish(startNodes=[tB])
assert len(nodes) == 2
assert nodes[0] == tA
assert nodes[-1] == tB
assert len(edges) == 1
def test_reverse_dfs_on_discover(self):
graph = Graph("Test dfsOnDiscover(reverse=True)")
# ------------\
# / ~ C - E - F
# A - B
# ~ D
A = graph.addNewNode("Ls", input="/tmp")
B = graph.addNewNode("AppendText", inputText=A.output)
C = graph.addNewNode("AppendText", inputText=B.output)
D = graph.addNewNode("AppendText", inputText=B.output)
E = graph.addNewNode("Ls", input=C.output)
F = graph.addNewNode("AppendText", input=A.output, inputText=E.output)
# Get all nodes from A (use set, order not guaranteed)
nodes = graph.dfsOnDiscover(startNodes=[A], reverse=True)[0]
assert set(nodes) == {A, B, D, C, E, F}
# Get all nodes from B
nodes = graph.dfsOnDiscover(startNodes=[B], reverse=True)[0]
assert set(nodes) == {B, D, C, E, F}
# Get all nodes of type AppendText from B
nodes = graph.dfsOnDiscover(startNodes=[B], filterTypes=["AppendText"], reverse=True)[0]
assert set(nodes) == {B, D, C, F}
# Get all nodes from C (order guaranteed)
nodes = graph.dfsOnDiscover(startNodes=[C], reverse=True)[0]
assert nodes == [C, E, F]
# Get all nodes
nodes = graph.dfsOnDiscover(reverse=True)[0]
assert set(nodes) == {A, B, C, D, E, F}
def test_dfs_on_discover(self):
graph = Graph("Test dfsOnDiscover(reverse=False)")
# ------------\
# / ~ C - E - F
# A - B
# ~ D
# G
G = graph.addNewNode("Ls", input="/tmp")
A = graph.addNewNode("Ls", input="/tmp")
B = graph.addNewNode("AppendText", inputText=A.output)
C = graph.addNewNode("AppendText", inputText=B.output)
D = graph.addNewNode("AppendText", input=G.output, inputText=B.output)
E = graph.addNewNode("Ls", input=C.output)
F = graph.addNewNode("AppendText", input=A.output, inputText=E.output)
# Get all nodes from A (use set, order not guaranteed)
nodes = graph.dfsOnDiscover(startNodes=[A], reverse=False)[0]
assert set(nodes) == {A}
# Get all nodes from D
nodes = graph.dfsOnDiscover(startNodes=[D], reverse=False)[0]
assert set(nodes) == {A, B, D, G}
# Get all nodes from E
nodes = graph.dfsOnDiscover(startNodes=[E], reverse=False)[0]
assert set(nodes) == {A, B, C, E}
# Get all nodes from F
nodes = graph.dfsOnDiscover(startNodes=[F], reverse=False)[0]
assert set(nodes) == {A, B, C, E, F}
# Get all nodes of type AppendText from C
nodes = graph.dfsOnDiscover(startNodes=[C], filterTypes=["AppendText"], reverse=False)[0]
assert set(nodes) == {B, C}
# Get all nodes from D (order guaranteed)
nodes = graph.dfsOnDiscover(startNodes=[D], longestPathFirst=True, reverse=False)[0]
assert nodes == [D, B, A, G]
# Get all nodes
nodes = graph.dfsOnDiscover(reverse=False)[0]
assert set(nodes) == {A, B, C, D, E, F, G}
def test_no_duplicates_start_node_is_ancestor_of_another(self):
"""
Test that DFS does not visit the same node more than once when one startNode
is an ancestor (in the traversal direction) of another startNode.
Graph:
A -> B -> C
Starting DFS (reverse=False, toward roots) from both C and B: visiting C first
will traverse C -> B -> A. B should not be discovered or finished a second time
when it is encountered again as a startNode.
"""
graph = Graph("")
A = graph.addNewNode("Ls", input="/tmp")
B = graph.addNewNode("AppendText", inputText=A.output)
C = graph.addNewNode("AppendText", inputText=B.output)
nodes, _ = graph.dfsOnFinish(startNodes=[C, B])
# B must appear exactly once — it is visited when traversing from C, and
# must NOT be re-visited when it is encountered as a startNode.
assert len(nodes) == len(set(nodes)), "dfsOnFinish returned duplicate nodes"
assert len(nodes) == 3
assert set(nodes) == {A, B, C}
def test_no_duplicates_start_node_is_root_of_another(self):
"""
Test that DFS does not visit the same node more than once when one startNode
is the root of the whole graph, and also appears in the traversal path of
another startNode in the list.
Graph:
A -> B -> C
Starting DFS (reverse=False) from both C and A: visiting C will traverse
C -> B -> A. A should not be discovered or finished a second time when it is
encountered again as a startNode.
"""
graph = Graph("")
A = graph.addNewNode("Ls", input="/tmp")
B = graph.addNewNode("AppendText", inputText=A.output)
C = graph.addNewNode("AppendText", inputText=B.output)
nodes, _ = graph.dfsOnFinish(startNodes=[C, A])
# A must appear exactly once.
assert len(nodes) == len(set(nodes)), "dfsOnFinish returned duplicate nodes"
assert len(nodes) == 3
assert set(nodes) == {A, B, C}
def test_no_duplicates_reverse_start_node_is_descendant_of_another(self):
"""
Test that reverse DFS does not visit the same node more than once when one
startNode is a descendant (in the reverse traversal direction, i.e. toward
leaves) of another startNode.
Graph:
A -> B -> C
Starting reverse DFS from both A and B: visiting A first traverses A -> B -> C.
B and C should not be discovered a second time when B is encountered again as
a startNode.
"""
graph = Graph("")
A = graph.addNewNode("Ls", input="/tmp")
B = graph.addNewNode("AppendText", inputText=A.output)
C = graph.addNewNode("AppendText", inputText=B.output)
nodes, _ = graph.dfsOnDiscover(startNodes=[A, B], reverse=True)
# B must appear exactly once — it is visited when traversing forward from A,
# and must NOT be re-visited when encountered as a startNode.
assert len(nodes) == len(set(nodes)), "dfsOnDiscover(reverse=True) returned duplicate nodes"
assert set(nodes) == {A, B, C}
def test_no_duplicates_discover_start_node_is_ancestor_of_another(self):
"""
Test that dfsOnDiscover does not return duplicate nodes when one startNode
is an ancestor (in the traversal direction) of another startNode.
Graph:
A -> B -> C
Starting dfsOnDiscover (reverse=False) from both C and B: visiting C first
traverses C -> B -> A. B should not be discovered a second time when it is
encountered again as a startNode.
"""
graph = Graph("")
A = graph.addNewNode("Ls", input="/tmp")
B = graph.addNewNode("AppendText", inputText=A.output)
C = graph.addNewNode("AppendText", inputText=B.output)
nodes, _ = graph.dfsOnDiscover(startNodes=[C, B])
assert len(nodes) == len(set(nodes)), "dfsOnDiscover returned duplicate nodes"
assert set(nodes) == {A, B, C}