11import unittest
2- from python_workflow_definition .shared import get_dict , get_list
2+ from python_workflow_definition .shared import (
3+ get_dict ,
4+ get_list ,
5+ get_kwargs ,
6+ get_source_handles ,
7+ convert_nodes_list_to_dict ,
8+ update_node_names ,
9+ set_result_node ,
10+ remove_result ,
11+ EDGES_LABEL ,
12+ NODES_LABEL ,
13+ SOURCE_LABEL ,
14+ SOURCE_PORT_LABEL ,
15+ TARGET_LABEL ,
16+ TARGET_PORT_LABEL ,
17+ )
318
419
520class TestShared (unittest .TestCase ):
@@ -8,3 +23,134 @@ def test_get_dict(self):
823
924 def test_get_list (self ):
1025 self .assertEqual ([1 , 2 , 3 ], get_list (a = 1 , b = 2 , c = 3 ))
26+
27+ def test_get_kwargs (self ):
28+ lst = [
29+ {
30+ TARGET_PORT_LABEL : "a" ,
31+ SOURCE_LABEL : "s1" ,
32+ SOURCE_PORT_LABEL : "sp1" ,
33+ },
34+ {
35+ TARGET_PORT_LABEL : "b" ,
36+ SOURCE_LABEL : "s2" ,
37+ SOURCE_PORT_LABEL : "sp2" ,
38+ },
39+ ]
40+ self .assertEqual (
41+ {
42+ "a" : {SOURCE_LABEL : "s1" , SOURCE_PORT_LABEL : "sp1" },
43+ "b" : {SOURCE_LABEL : "s2" , SOURCE_PORT_LABEL : "sp2" },
44+ },
45+ get_kwargs (lst ),
46+ )
47+
48+ def test_get_kwargs_empty (self ):
49+ self .assertEqual ({}, get_kwargs ([]))
50+
51+ def test_get_source_handles (self ):
52+ edges_lst = [
53+ {SOURCE_LABEL : "s1" , SOURCE_PORT_LABEL : "sp1" },
54+ {SOURCE_LABEL : "s1" , SOURCE_PORT_LABEL : "sp2" },
55+ {SOURCE_LABEL : "s2" , SOURCE_PORT_LABEL : "sp3" },
56+ ]
57+ self .assertEqual (
58+ {"s1" : ["sp1" , "sp2" ], "s2" : ["sp3" ]}, get_source_handles (edges_lst )
59+ )
60+
61+ def test_get_source_handles_no_port (self ):
62+ edges_lst = [
63+ {SOURCE_LABEL : "s1" , SOURCE_PORT_LABEL : None },
64+ {SOURCE_LABEL : "s1" , SOURCE_PORT_LABEL : None },
65+ ]
66+ self .assertEqual ({"s1" : [0 , 1 ]}, get_source_handles (edges_lst ))
67+
68+ def test_get_source_handles_empty (self ):
69+ self .assertEqual ({}, get_source_handles ([]))
70+
71+ def test_convert_nodes_list_to_dict (self ):
72+ nodes_list = [
73+ {"id" : 1 , "name" : "n1" },
74+ {"id" : 2 , "value" : "v2" },
75+ {"id" : 0 , "name" : "n0" },
76+ ]
77+ self .assertEqual (
78+ {"0" : "n0" , "1" : "n1" , "2" : "v2" },
79+ convert_nodes_list_to_dict (nodes_list ),
80+ )
81+
82+ def test_convert_nodes_list_to_dict_empty (self ):
83+ self .assertEqual ({}, convert_nodes_list_to_dict ([]))
84+
85+ def test_update_node_names (self ):
86+ workflow_dict = {
87+ NODES_LABEL : [
88+ {"id" : 0 , "type" : "input" , "name" : "" },
89+ {"id" : 1 , "type" : "input" , "name" : "" },
90+ {"id" : 2 , "type" : "input" , "name" : "" },
91+ {"id" : 3 , "type" : "other" , "name" : "" },
92+ ],
93+ EDGES_LABEL : [
94+ {SOURCE_LABEL : 0 , TARGET_PORT_LABEL : "a" },
95+ {SOURCE_LABEL : 1 , TARGET_PORT_LABEL : "b" },
96+ {SOURCE_LABEL : 2 , TARGET_PORT_LABEL : "a" },
97+ ],
98+ }
99+ update_node_names (workflow_dict )
100+ self .assertEqual ("a_0" , workflow_dict [NODES_LABEL ][0 ]["name" ])
101+ self .assertEqual ("b" , workflow_dict [NODES_LABEL ][1 ]["name" ])
102+ self .assertEqual ("a_1" , workflow_dict [NODES_LABEL ][2 ]["name" ])
103+
104+ def test_set_result_node (self ):
105+ workflow_dict = {
106+ NODES_LABEL : [{"id" : 0 , "type" : "input" }],
107+ EDGES_LABEL : [],
108+ }
109+ set_result_node (workflow_dict )
110+ self .assertEqual (2 , len (workflow_dict [NODES_LABEL ]))
111+ self .assertEqual ("output" , workflow_dict [NODES_LABEL ][1 ]["type" ])
112+ self .assertEqual (1 , len (workflow_dict [EDGES_LABEL ]))
113+
114+ def test_set_result_node_already_exists (self ):
115+ workflow_dict = {
116+ NODES_LABEL : [
117+ {"id" : 0 , "type" : "input" },
118+ {"id" : 1 , "type" : "output" },
119+ ],
120+ EDGES_LABEL : [{SOURCE_LABEL : 0 , TARGET_LABEL : 1 }],
121+ }
122+ set_result_node (workflow_dict )
123+ self .assertEqual (3 , len (workflow_dict [NODES_LABEL ]))
124+ self .assertEqual ("output" , workflow_dict [NODES_LABEL ][2 ]["type" ])
125+ self .assertEqual (2 , len (workflow_dict [EDGES_LABEL ]))
126+
127+ def test_set_result_node_no_end_node (self ):
128+ workflow_dict = {
129+ NODES_LABEL : [{"id" : 0 }, {"id" : 1 }],
130+ EDGES_LABEL : [
131+ {SOURCE_LABEL : 0 , TARGET_LABEL : 1 },
132+ {SOURCE_LABEL : 1 , TARGET_LABEL : 0 },
133+ ],
134+ }
135+ with self .assertRaises (IndexError ):
136+ set_result_node (workflow_dict )
137+
138+ def test_remove_result (self ):
139+ workflow_dict = {
140+ NODES_LABEL : [
141+ {"id" : 0 , "type" : "input" },
142+ {"id" : 1 , "type" : "output" },
143+ ],
144+ EDGES_LABEL : [{SOURCE_LABEL : 0 , TARGET_LABEL : 1 }],
145+ }
146+ new_workflow = remove_result (workflow_dict )
147+ self .assertEqual (1 , len (new_workflow [NODES_LABEL ]))
148+ self .assertEqual (0 , len (new_workflow [EDGES_LABEL ]))
149+
150+ def test_remove_result_no_output (self ):
151+ workflow_dict = {
152+ NODES_LABEL : [{"id" : 0 , "type" : "input" }],
153+ EDGES_LABEL : [],
154+ }
155+ with self .assertRaises (IndexError ):
156+ remove_result (workflow_dict )
0 commit comments