13
13
"""This module contains code to test SageMaker ``LineageQueryResult.visualize()``"""
14
14
from __future__ import absolute_import
15
15
import time
16
- import json
17
16
import os
18
17
19
18
import pytest
@@ -47,26 +46,22 @@ def test_wide_graph_visualize(sagemaker_session):
47
46
# \ \--> Artifact
48
47
# \---> ...
49
48
try :
50
- for i in range (10 ):
49
+ for i in range (200 ):
51
50
artifact_arn = lineage_resource_helper .create_artifact (artifact_name = name ())
52
51
lineage_resource_helper .create_association (
53
52
source_arn = wide_graph_root_arn , dest_arn = artifact_arn
54
53
)
55
- except Exception as e :
56
- print (e )
57
- lineage_resource_helper .clean_all ()
58
- assert False
59
54
60
- try :
61
55
lq = sagemaker .lineage .query .LineageQuery (sagemaker_session )
62
56
lq_result = lq .query (start_arns = [wide_graph_root_arn ])
63
57
lq_result .visualize (path = "wideGraph.html" )
58
+
64
59
except Exception as e :
65
60
print (e )
66
- lineage_resource_helper .clean_all ()
67
61
assert False
68
62
69
- lineage_resource_helper .clean_all ()
63
+ finally :
64
+ lineage_resource_helper .clean_all ()
70
65
71
66
72
67
@pytest .mark .skip ("visualizer load test" )
@@ -84,27 +79,23 @@ def test_long_graph_visualize(sagemaker_session):
84
79
source_arn = last_arn , dest_arn = new_artifact_arn
85
80
)
86
81
last_arn = new_artifact_arn
87
- except Exception as e :
88
- print (e )
89
- lineage_resource_helper .clean_all ()
90
- assert False
91
82
92
- try :
93
83
lq = sagemaker .lineage .query .LineageQuery (sagemaker_session )
94
84
lq_result = lq .query (
95
85
start_arns = [long_graph_root_arn ], direction = LineageQueryDirectionEnum .DESCENDANTS
96
86
)
97
87
# max depth = 10 -> graph rendered only has length of ten (in DESCENDANTS direction)
98
88
lq_result .visualize (path = "longGraph.html" )
89
+
99
90
except Exception as e :
100
91
print (e )
101
- lineage_resource_helper .clean_all ()
102
92
assert False
103
93
104
- lineage_resource_helper .clean_all ()
94
+ finally :
95
+ lineage_resource_helper .clean_all ()
105
96
106
97
107
- def test_graph_visualize (sagemaker_session ):
98
+ def test_graph_visualize (sagemaker_session , extract_data_from_html ):
108
99
lineage_resource_helper = LineageResourceHelper (sagemaker_session = sagemaker_session )
109
100
110
101
# create lineage data
@@ -141,24 +132,14 @@ def test_graph_visualize(sagemaker_session):
141
132
dest_arn = endpoint_context ,
142
133
association_type = "AssociatedWith" ,
143
134
)
144
- time .sleep (1 )
145
- except Exception as e :
146
- print (e )
147
- lineage_resource_helper .clean_all ()
148
- assert False
135
+ time .sleep (3 )
149
136
150
- # visualize
151
- try :
137
+ # visualize
152
138
lq = sagemaker .lineage .query .LineageQuery (sagemaker_session )
153
139
lq_result = lq .query (start_arns = [graph_startarn ])
154
140
lq_result .visualize (path = "testGraph.html" )
155
- except Exception as e :
156
- print (e )
157
- lineage_resource_helper .clean_all ()
158
- assert False
159
141
160
- # check generated graph info
161
- try :
142
+ # check generated graph info
162
143
fo = open ("testGraph.html" , "r" )
163
144
lines = fo .readlines ()
164
145
for line in lines :
@@ -167,108 +148,85 @@ def test_graph_visualize(sagemaker_session):
167
148
if "edges = " in line :
168
149
edge = line
169
150
170
- # extract node data
171
- start = node .find ("[" )
172
- end = node .find ("]" )
173
- res = node [start + 1 : end ].split ("}, " )
174
- res = [i + "}" for i in res ]
175
- res [- 1 ] = res [- 1 ][:- 1 ]
176
- node_dict = [json .loads (i ) for i in res ]
177
-
178
- # extract edge data
179
- start = edge .find ("[" )
180
- end = edge .find ("]" )
181
- res = edge [start + 1 : end ].split ("}, " )
182
- res = [i + "}" for i in res ]
183
- res [- 1 ] = res [- 1 ][:- 1 ]
184
- edge_dict = [json .loads (i ) for i in res ]
151
+ node_dict = extract_data_from_html (node )
152
+ edge_dict = extract_data_from_html (edge )
185
153
186
154
# check node number
187
155
assert len (node_dict ) == 5
188
156
189
- # check startarn
190
- found_value = next (
191
- dictionary for dictionary in node_dict if dictionary ["id" ] == graph_startarn
192
- )
193
- assert found_value ["color" ] == "#146eb4"
194
- assert found_value ["label" ] == "Model"
195
- assert found_value ["shape" ] == "star"
196
- assert found_value ["title" ] == "Artifact"
197
-
198
- # check image artifact
199
- found_value = next (
200
- dictionary for dictionary in node_dict if dictionary ["id" ] == image_artifact
201
- )
202
- assert found_value ["color" ] == "#146eb4"
203
- assert found_value ["label" ] == "Image"
204
- assert found_value ["shape" ] == "dot"
205
- assert found_value ["title" ] == "Artifact"
206
-
207
- # check dataset artifact
208
- found_value = next (
209
- dictionary for dictionary in node_dict if dictionary ["id" ] == dataset_artifact
210
- )
211
- assert found_value ["color" ] == "#146eb4"
212
- assert found_value ["label" ] == "DataSet"
213
- assert found_value ["shape" ] == "dot"
214
- assert found_value ["title" ] == "Artifact"
215
-
216
- # check modeldeploy action
217
- found_value = next (
218
- dictionary for dictionary in node_dict if dictionary ["id" ] == modeldeploy_action
219
- )
220
- assert found_value ["color" ] == "#88c396"
221
- assert found_value ["label" ] == "ModelDeploy"
222
- assert found_value ["shape" ] == "dot"
223
- assert found_value ["title" ] == "Action"
224
-
225
- # check endpoint context
226
- found_value = next (
227
- dictionary for dictionary in node_dict if dictionary ["id" ] == endpoint_context
228
- )
229
- assert found_value ["color" ] == "#ff9900"
230
- assert found_value ["label" ] == "Endpoint"
231
- assert found_value ["shape" ] == "dot"
232
- assert found_value ["title" ] == "Context"
157
+ expected_nodes = {
158
+ graph_startarn : {
159
+ "color" : "#146eb4" ,
160
+ "label" : "Model" ,
161
+ "shape" : "star" ,
162
+ "title" : "Artifact" ,
163
+ },
164
+ image_artifact : {
165
+ "color" : "#146eb4" ,
166
+ "label" : "Image" ,
167
+ "shape" : "dot" ,
168
+ "title" : "Artifact" ,
169
+ },
170
+ dataset_artifact : {
171
+ "color" : "#146eb4" ,
172
+ "label" : "DataSet" ,
173
+ "shape" : "dot" ,
174
+ "title" : "Artifact" ,
175
+ },
176
+ modeldeploy_action : {
177
+ "color" : "#88c396" ,
178
+ "label" : "ModelDeploy" ,
179
+ "shape" : "dot" ,
180
+ "title" : "Action" ,
181
+ },
182
+ endpoint_context : {
183
+ "color" : "#ff9900" ,
184
+ "label" : "Endpoint" ,
185
+ "shape" : "dot" ,
186
+ "title" : "Context" ,
187
+ },
188
+ }
189
+
190
+ # check node properties
191
+ for node in node_dict :
192
+ for label , val in expected_nodes [node ["id" ]].items ():
193
+ assert node [label ] == val
233
194
234
195
# check edge number
235
196
assert len (edge_dict ) == 4
236
197
237
- # check image_artifact -> model_artifact(startarn) edge
238
- found_value = next (
239
- dictionary for dictionary in edge_dict if dictionary [ "from" ] == image_artifact
240
- )
241
- assert found_value [ "to" ] == graph_startarn
242
- assert found_value [ "title" ] == "ContributedTo"
243
-
244
- # check dataset_artifact -> model_artifact(startarn) edge
245
- found_value = next (
246
- dictionary for dictionary in edge_dict if dictionary [ "from" ] == dataset_artifact
247
- )
248
- assert found_value [ "to" ] == graph_startarn
249
- assert found_value [ "title" ] == "AssociatedWith"
250
-
251
- # check model_artifact(startarn) -> modeldeploy_action edge
252
- found_value = next (
253
- dictionary for dictionary in edge_dict if dictionary [ "from" ] == graph_startarn
254
- )
255
- assert found_value [ "to" ] == modeldeploy_action
256
- assert found_value [ "title" ] == "ContributedTo"
257
-
258
- # check modeldeploy_action -> endpoint_context edge
259
- found_value = next (
260
- dictionary for dictionary in edge_dict if dictionary [ "from" ] == modeldeploy_action
261
- )
262
- assert found_value [ "to" ] == endpoint_context
263
- assert found_value [ "title" ] == "AssociatedWith"
198
+ expected_edges = {
199
+ image_artifact : {
200
+ "from" : image_artifact ,
201
+ "to" : graph_startarn ,
202
+ "title" : "ContributedTo" ,
203
+ },
204
+ dataset_artifact : {
205
+ "from" : dataset_artifact ,
206
+ "to" : graph_startarn ,
207
+ "title" : "AssociatedWith" ,
208
+ },
209
+ graph_startarn : {
210
+ "from" : graph_startarn ,
211
+ "to" : modeldeploy_action ,
212
+ "title" : "ContributedTo" ,
213
+ },
214
+ modeldeploy_action : {
215
+ "from" : modeldeploy_action ,
216
+ "to" : endpoint_context ,
217
+ "title" : "AssociatedWith" ,
218
+ },
219
+ }
220
+
221
+ # check edge properties
222
+ for edge in edge_dict :
223
+ for label , val in expected_edges [ edge [ "from" ]]. items ():
224
+ assert edge [ label ] == val
264
225
265
226
except Exception as e :
266
227
print (e )
267
- lineage_resource_helper .clean_all ()
268
- os .remove ("testGraph.html" )
269
228
assert False
270
229
271
- # delete generated test graph
272
- os .remove ("testGraph.html" )
273
- # clean lineage data
274
- lineage_resource_helper .clean_all ()
230
+ finally :
231
+ lineage_resource_helper .clean_all ()
232
+ os .remove ("testGraph.html" )
0 commit comments