File: test_saveLoadNetworks.py

package info (click to toggle)
mgltools-networkeditor 1.5.7-4
  • links: PTS, VCS
  • area: non-free
  • in suites: buster
  • size: 1,312 kB
  • sloc: python: 17,905; sh: 78; makefile: 10
file content (464 lines) | stat: -rw-r--r-- 16,341 bytes parent folder | download | duplicates (4)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#########################################################################
#
# Date: Jul 2004  Author: Daniel Stoffler
#
#       stoffler@scripps.edu
#
#       The Scripps Research Institute (TSRI)
#       Molecular Graphics Lab
#       La Jolla, CA 92037, USA
#
# Copyright: Daniel Stoffler, and TSRI
#
#########################################################################

import sys, os
from time import sleep

from NetworkEditor.net import Network
from NetworkEditor.simpleNE import NetworkNode, NetworkBuilder
from NetworkEditor.Tests.nodes import DialNode, PrintNode, PassNode, \
     Operator3Node
from NetworkEditor.Tests import helper

ed = None

###############################
## implement setUp and tearDown
###############################

def setUp():
    global ed
    ed = NetworkBuilder("test save and load networks", withShell=0,
                        visibleWidth=400, visibleHeight=300)
    ed.master.update()
    ed.configure(withThreads=0)


def tearDown():
    ed.exit_cb()
    import gc
    gc.collect()

##########################
## Helper methods
##########################

def pause(sleepTime=None):
    if sleepTime is None:
        from NetworkEditor.Tests import pauseLength as sleepTime
    ed.master.update()
    sleep(sleepTime)


##########################
## Tests
##########################


############################################################################
###################### TESTS FOR SAVING A NETWORK ##########################
############################################################################
    
def test_01_saveLoadDialUnchanged():
    # Test if we can save and restore a dial node, with a value set to 42
    # and that the node (and the value) are restored properly
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=18, posy=20)
    w = node1.inputPorts[0].widget
    w.set(42.0)
    assert w.get() == 42.0,"Expected 42.0, got %s"%w.get()
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    assert len(ed.currentNetwork.nodes) == 1,\
           "Expected 1, got %s"%len(ed.currentNetwork.nodes)
    w = ed.currentNetwork.nodes[0].inputPorts[0].widget
    assert w.get() == 42.0,"Expected 42.0, got %s"%w.get()
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_02_saveLoadDialUnbindWidget():
    # Test if we can save and restore a dial node with the dial widget set to
    # 42 and then unbind the widget
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=18, posy=20)
    w = node1.inputPorts[0].widget
    w.set(42.0)
    node1.inputPorts[0].unbindWidget()
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    node1 =  ed.currentNetwork.nodes[0]
    assert node1.inputPorts[0].widget is None,\
           "Expected None, got %s"%node1.inputPorts[0].widget
    assert node1.inputPorts[0]._previousWidgetDescr is not None,\
          "_previousWidgetDescr is %s"%node1.inputPorts[0]._previousWidgetDescr
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_03_saveLoadDialUnbindRebindWidget():
    # Test if we can save and restore a dial node with the dial widget set to
    # 42 and then unbound, and rebind the widget after restoring and the value
    # set back properly
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=18,
                                  posy=20)
    w = node1.inputPorts[0].widget
    w.set(42.0)
    node1.inputPorts[0].unbindWidget()
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    assert len(ed.currentNetwork.nodes) == 1,\
           "Expected 1, got %s"%len(ed.currentNetwork.nodes)
    node1 =  ed.currentNetwork.nodes[0]
    assert node1.inputPorts[0].widget is None,\
           "Expected None, got %s"%node1.inputPorts[0].widget
    assert node1.inputPorts[0]._previousWidgetDescr is not None,\
         "_previousWidgetDescr is %s"%node1.inputPorts[0]._previousWidgetDescr
    node1.inputPorts[0].rebindWidget()
    assert node1.inputPorts[0].widget is not None,\
           "widget is %s"%node1.inputPorts[0].widget
    w = node1.inputPorts[0].widget
    assert w.get() == 42.0,"Expected 42.0, got %s"%w.get()
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_04_saveLoadDialDeleteWidget():
    # Test if we can save and restore a dial node with the dial widget set to
    # 42 and then delete the widget
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=18,
                                  posy=20)
    w = node1.inputPorts[0].widget
    w.set(42.0)
    node1.inputPorts[0].deleteWidget()
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    node1 =  ed.currentNetwork.nodes[0]
    assert node1.inputPorts[0].widget is None,\
           "Expected None, got %s"%node1.inputPorts[0].widget
    assert node1.inputPorts[0]._previousWidgetDescr is None,\
           "Expected None, got %s"%node1.inputPorts[0]._previousWidgetDescr
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_05_saveLoadCreateNewWidget():
    # Test if we can save and restore a pass node to which we bind a widget
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net,nType='Pass', name='Pass', posx=18, posy=20)
    node1.inputPorts[0].createWidget(descr={'initialValue': 0.0, 'master': 'ParamPanel', 'increment': 0.0, 'labelCfg': {'text': 'label'}, 'class': 'NEDial'})
    assert node1.inputPorts[0].widget is not None,\
           "widget is %s"%node1.inputPorts[0].widget
    node1.inputPorts[0].widget.set(42.0,0)
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    node1 =  ed.currentNetwork.nodes[0]
    assert node1.inputPorts[0].widget is not None,\
           "widget is %s"%node1.inputPorts[0].widget
    assert node1.inputPorts[0].widget.get() == 42.0,\
           "Expected 42.0, got s"%node1.inputPorts[0].widget.get()
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_06_saveLoadNetwork():
    # first, build a network: dial->pass->print
    # test, if we can reload this (with connections
    node1, node2, node3 = helper.buildSmallNetwork(ed.currentNetwork)
    pause()
    # make sure there is no such file
    os.system("rm -f tmpFoo_net.py*")
    # save the file
    ed.saveNetwork('tmpFoo_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpFoo_net.py')
    pause()
    assert len(ed.currentNetwork.nodes) == 3,\
           "Expected 3, got %s"%len(ed.currentNetwork.nodes)
    assert ed.currentNetwork.name == 'tmpFoo',\
           "Expected 'tmpFoo', got '%s'"%ed.currentNetwork.name
    # the nice people we are, we clean up after us
    os.system("rm -f tmpFoo_net.py*")


def test_07_saveLoadNetworkWithWidgetValue():
    net = ed.currentNetwork
    # first, build a network: dial->pass->print
    node1, node2, node3 = helper.buildSmallNetwork(net)
    pause()
    # now change the widget value of the dial node
    node1.inputPorts[0].widget.set(42.0)
    # make sure there is no such file
    os.system("rm -f tmpFoo_net.py*")
    # save the file
    ed.saveNetwork('tmpFoo_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpFoo_net.py')
    pause()
    # run the network
    ed.currentNetwork.run()
    # check for the value 42.0, find the dial node:
    n = None
    for n in ed.currentNetwork.nodes:
        if n.name == 'dial':
            break
    # did we find the node?
    assert n is not None,"n is: %s"%n
    assert n.outputPorts[0].data == 42.0,\
           "Expected 42.0, got %s"%n.outputPorts[0].data
    # the nice people we are, we clean up after us
    os.system("rm -f tmpFoo_net.py*")


def test_08_saveRestoreSpecialPorts():
    # this tests if we can save and restore a network with nodes that
    # have special ports visible, and the special ports are connected
    # add 2 nodes, show special ports, connect them
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=21, posy=40)
    node2 = helper.makeAndAddNode(net, nType='Pass', name='pass', posx=153, posy=149)
    node1.showSpecialPorts()
    node2.showSpecialPorts()
    node1.network.specialConnectNodes(node1, node2, "trigger", "runNode")
    # remove previous tmp file (if present)
    os.system("rm -f tmpSpecialConnections_net.py*")
    # save network
    ed.saveNetwork('tmpSpecialConnections_net.py')
    pause()
    # delete network
    ed.deleteNetwork(ed.currentNetwork)
    # load saved network
    ed.loadNetwork('tmpSpecialConnections_net.py')
    # check if we could load it
    assert ed.currentNetwork.name == 'tmpSpecialConnections',\
           "Expected 'tmpSpecialConnections', got '%s'"%ed.currentNetwork.name
    assert len(ed.currentNetwork.nodes) == 2,\
           "Expected 2, got %s"%len(ed.currentNetwork.nodes)
    # remove tmp file
    os.system("rm -f tmpSpecialConnections_net.py*")


def test_09_saveLoadDialDeleteInputPort():
    # Test if we can save and restore a dial node with a deleted input port
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=18, posy=20)
    node1.deletePort(node1.inputPortByName['dial'])
    code = """def doit(self):
    pass
"""
    node1.setFunction(code)
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    node1 =  ed.currentNetwork.nodes[0]
    assert len(node1.inputPorts) == 0,\
           "Expected 0, got %s"%len(node1.inputPorts)
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_10_saveLoadDialDeleteOutputPort():
    # Test if we can save and restore a dial node with a deleted output port
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial', posx=18, posy=20)
    node1.deletePort(node1.outputPortByName['value'])
    code = """def doit(self, dial):
    pass
"""
    node1.setFunction(code)
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    node1 =  ed.currentNetwork.nodes[0]
    assert len(node1.outputPorts) == 0,\
           "Expected 0, got %s"%len(node1.outputPorts)
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")


def test_11_saveLoadDeleteMultiplePorts():
    # Test if we can save and restore an operator3 node with the first and
    # and the last port deleted
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Operator3', name='Operator3', posx=18, posy=20)
    node1.deletePort(node1.inputPortByName['data'])
    node1.deletePort(node1.inputPortByName['applyToElements'])
    code = """def doit(self, operation, from_, to_):
    pass
"""
    node1.setFunction(code)
    # make sure there is no such file
    os.system("rm -f tmpOp3_net.py*")
    # save the file
    ed.saveNetwork('tmpOp3_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpOp3_net.py')
    pause()
    node1 =  ed.currentNetwork.nodes[0]
    assert len(node1.inputPorts) == 3,\
           "Expected 3, got %s"%len(node1.inputPorts) == 3
    assert node1.inputPorts[0].name == 'operation',\
           "Expected 'operation', got '%s'"%node1.inputPorts[0].name
    assert node1.inputPorts[1].name == 'from_',\
           "Expected 'from_', got '%s'"%node1.inputPorts[1].name
    assert node1.inputPorts[2].name == 'to_',\
           "Expected 'to_', got '%s'"%node1.inputPorts[2].name 
    # we clean up after us...
    os.system("rm -f tmpOp3_net.py*")


def test_12_saveLoadAddInputPort():
    # Test if we can save and restore a dial node with a new input port
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial',
                                   posx=18, posy=20)
    # 1 port
    assert len(node1.inputPorts) == 1,\
           "Expected 1, got %s"%len(node1.inputPorts)
    p = node1.addInputPort(name='NewTestPort')
    # 2 ports
    assert len(node1.inputPorts) == 2,\
           "Expected 2, gpt %s"%len(node1.inputPorts)
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    # do we have 2 input ports?
    node1 = ed.currentNetwork.nodes[0]
    assert len(node1.inputPorts) == 2,\
           "Expected 2, got %s"%len(node1.inputPorts)
    # is the code updated?
    assert "NewTestPort" in node1.sourceCode,\
           "node1.sourceCode is: %s"%node1.sourceCode
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*")   


def test_13_saveLoadAddOutputPort():
    # Test if we can save and restore a dial node with a new output port
    net = ed.currentNetwork
    node1 = helper.makeAndAddNode(net, nType='Dial', name='dial',
                                   posx=18, posy=20)
    oldcode = node1.sourceCode
    # 1 port
    assert len(node1.outputPorts) == 1,\
           "Expected 1, got %s"%len(node1.outputPorts)
    p = node1.addOutputPort(name='NewTestPort')
    # 2 ports
    assert len(node1.outputPorts) == 2,\
           "Expected 2, got %s"%len(node1.outputPorts)
    # make sure there is no such file
    os.system("rm -f tmpDial_net.py*")
    # save the file
    ed.saveNetwork('tmpDial_net.py')
    # and delete the network
    ed.deleteNetwork(ed.currentNetwork)
    pause()
    # load the file
    ed.loadNetwork('tmpDial_net.py')
    pause()
    # do we have 2 input ports?
    node1 = ed.currentNetwork.nodes[0]
    newcode = node1.sourceCode
    assert len(node1.outputPorts) == 2,\
           "Expected 2, got %s"%len(node1.outputPorts)
    # we clean up after us...
    os.system("rm -f tmpDial_net.py*") 


def test_14_saveLoadMacroNoConnections():
    """this tests if we can save and restore a macro node with no nodes in the
macro network, and no connections."""
    net = Network('save macro')
    ed.addNetwork(net)
    # add macro node
    from NetworkEditor.macros import MacroNode
    node0 = MacroNode(name='test macro')
    net.addNode(node0, 200, 200)
    node0.shrink()
    # make sure there is no such file
    os.system("rm -f tmpMacro_net.py*")
    # save the file
    ed.saveNetwork('tmpMacro_net.py')
    # and delete the network
    ed.deleteNetwork(net)
    pause()
    # load the file
    ed.loadNetwork('tmpMacro_net.py')
    pause()
    # we clean up after us...
    os.system("rm -f tmpMacro_net.py*") 
    # is macro node here?
    assert len(ed.currentNetwork.nodes) == 1