Skip to content

Commit 99027e3

Browse files
authored
Merge pull request #7 from ytlee93/master
change: add queryLineageResult visualizer unit test
2 parents 7b2bcf1 + 684d45d commit 99027e3

File tree

3 files changed

+100
-23
lines changed

3 files changed

+100
-23
lines changed

src/sagemaker/lineage/query.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ def __str__(self):
104104
"""
105105
return str(self.__dict__)
106106

107+
def __repr__(self):
108+
"""Define string representation of ``Edge``.
109+
110+
Format:
111+
{
112+
'source_arn': 'string', 'destination_arn': 'string',
113+
'association_type': 'string'
114+
}
115+
116+
"""
117+
return "\n\t" + str(self.__dict__)
118+
107119

108120
class Vertex:
109121
"""A vertex for a lineage graph."""
@@ -155,6 +167,19 @@ def __str__(self):
155167
"""
156168
return str(self.__dict__)
157169

170+
def __repr__(self):
171+
"""Define string representation of ``Vertex``.
172+
173+
Format:
174+
{
175+
'arn': 'string', 'lineage_entity': 'string',
176+
'lineage_source': 'string',
177+
'_session': <sagemaker.session.Session object>
178+
}
179+
180+
"""
181+
return "\n\t" + str(self.__dict__)
182+
158183
def to_lineage_object(self):
159184
"""Convert the ``Vertex`` object to its corresponding lineage object.
160185
@@ -312,29 +337,23 @@ def __str__(self):
312337
Format:
313338
{
314339
'edges':[
315-
"{
316-
'source_arn': 'string', 'destination_arn': 'string',
317-
'association_type': 'string'
318-
}",
319-
...
320-
],
340+
{'source_arn': 'string', 'destination_arn': 'string', 'association_type': 'string'},
341+
...],
342+
321343
'vertices':[
322-
"{
323-
'arn': 'string', 'lineage_entity': 'string',
324-
'lineage_source': 'string',
325-
'_session': <sagemaker.session.Session object>
326-
}",
327-
...
328-
],
329-
'startarn':[
330-
'string',
331-
...
332-
]
344+
{'arn': 'string', 'lineage_entity': 'string', 'lineage_source': 'string',
345+
'_session': <sagemaker.session.Session object>},
346+
...],
347+
348+
'startarn':['string', ...]
333349
}
334350
335351
"""
336-
result_dict = vars(self)
337-
return str({k: [str(val) for val in v] for k, v in result_dict.items()})
352+
return (
353+
"{\n"
354+
+ "\n\n".join("'{}': {},".format(key, val) for key, val in self.__dict__.items())
355+
+ "\n}"
356+
)
338357

339358
def _covert_edges_to_tuples(self):
340359
"""Convert edges to tuple format for visualizer."""

tests/integ/sagemaker/lineage/test_lineage_visualize.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ def test_wide_graph_visualize(sagemaker_session):
5757
lq_result = lq.query(start_arns=[wide_graph_root_arn])
5858
lq_result.visualize(path="wideGraph.html")
5959

60-
print("vertex len = ")
61-
print(len(lq_result.vertices))
62-
assert False
63-
6460
except Exception as e:
6561
print(e)
6662
assert False

tests/unit/sagemaker/lineage/test_query.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from sagemaker.lineage.lineage_trial_component import LineageTrialComponent
1919
from sagemaker.lineage.query import LineageEntityEnum, LineageSourceEnum, Vertex, LineageQuery
2020
import pytest
21+
import re
2122

2223

2324
def test_lineage_query(sagemaker_session):
@@ -524,3 +525,64 @@ def test_vertex_to_object_unconvertable(sagemaker_session):
524525

525526
with pytest.raises(ValueError):
526527
vertex.to_lineage_object()
528+
529+
530+
def test_get_visualization_elements(sagemaker_session):
531+
lineage_query = LineageQuery(sagemaker_session)
532+
sagemaker_session.sagemaker_client.query_lineage.return_value = {
533+
"Vertices": [
534+
{"Arn": "arn1", "Type": "Endpoint", "LineageType": "Artifact"},
535+
{"Arn": "arn2", "Type": "Model", "LineageType": "Context"},
536+
{
537+
"Arn": "arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext",
538+
"Type": "Model",
539+
"LineageType": "Context",
540+
},
541+
],
542+
"Edges": [{"SourceArn": "arn1", "DestinationArn": "arn2", "AssociationType": "Produced"}],
543+
}
544+
545+
query_response = lineage_query.query(
546+
start_arns=["arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext"]
547+
)
548+
549+
elements = query_response._get_visualization_elements()
550+
551+
assert elements["nodes"][0] == ("arn1", "Endpoint", "Artifact", False)
552+
assert elements["nodes"][1] == ("arn2", "Model", "Context", False)
553+
assert elements["nodes"][2] == (
554+
"arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext",
555+
"Model",
556+
"Context",
557+
True,
558+
)
559+
assert elements["edges"][0] == ("arn1", "arn2", "Produced")
560+
561+
562+
def test_query_lineage_result_str(sagemaker_session):
563+
lineage_query = LineageQuery(sagemaker_session)
564+
sagemaker_session.sagemaker_client.query_lineage.return_value = {
565+
"Vertices": [
566+
{"Arn": "arn1", "Type": "Endpoint", "LineageType": "Artifact"},
567+
{"Arn": "arn2", "Type": "Model", "LineageType": "Context"},
568+
],
569+
"Edges": [{"SourceArn": "arn1", "DestinationArn": "arn2", "AssociationType": "Produced"}],
570+
}
571+
572+
query_response = lineage_query.query(
573+
start_arns=["arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext"]
574+
)
575+
576+
response_str = query_response.__str__()
577+
pattern = r"Mock id='\d*'"
578+
replace = r"Mock id=''"
579+
response_str = re.sub(pattern, replace, response_str)
580+
581+
assert (
582+
response_str
583+
== "{\n'edges': [\n\t{'source_arn': 'arn1', 'destination_arn': 'arn2', 'association_type': 'Produced'}],"
584+
+ "\n\n'vertices': [\n\t{'arn': 'arn1', 'lineage_entity': 'Artifact', 'lineage_source': 'Endpoint', "
585+
+ "'_session': <Mock id=''>}, \n\t{'arn': 'arn2', 'lineage_entity': 'Context', 'lineage_source': "
586+
+ "'Model', '_session': <Mock id=''>}],\n\n'startarn': "
587+
+ "['arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext'],\n}"
588+
)

0 commit comments

Comments
 (0)