Skip to content

Commit 9b5819d

Browse files
author
Yi-Ting Lee
committed
Merge branch 'unit-test'
2 parents 4b2e043 + f9d0ae1 commit 9b5819d

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
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/unit/sagemaker/lineage/test_query.py

Lines changed: 51 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,53 @@ 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+
"Edges": [{"SourceArn": "arn1", "DestinationArn": "arn2", "AssociationType": "Produced"}],
538+
}
539+
540+
query_response = lineage_query.query(
541+
start_arns=["arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext"]
542+
)
543+
544+
elements = query_response._get_visualization_elements()
545+
546+
assert elements["nodes"][0] == ("arn1", "Endpoint", "Artifact", False)
547+
assert elements["nodes"][1] == ("arn2", "Model", "Context", False)
548+
assert elements["edges"][0] == ("arn1", "arn2", "Produced")
549+
550+
551+
def test_query_lineage_result_str(sagemaker_session):
552+
lineage_query = LineageQuery(sagemaker_session)
553+
sagemaker_session.sagemaker_client.query_lineage.return_value = {
554+
"Vertices": [
555+
{"Arn": "arn1", "Type": "Endpoint", "LineageType": "Artifact"},
556+
{"Arn": "arn2", "Type": "Model", "LineageType": "Context"},
557+
],
558+
"Edges": [{"SourceArn": "arn1", "DestinationArn": "arn2", "AssociationType": "Produced"}],
559+
}
560+
561+
query_response = lineage_query.query(
562+
start_arns=["arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext"]
563+
)
564+
565+
response_str = query_response.__str__()
566+
pattern = "Mock id='\d*'"
567+
replace = "Mock id=''"
568+
response_str = re.sub(pattern, replace, response_str)
569+
570+
assert (
571+
response_str
572+
== "{\n'edges': [\n\t{'source_arn': 'arn1', 'destination_arn': 'arn2', 'association_type': 'Produced'}],"
573+
+ "\n\n'vertices': [\n\t{'arn': 'arn1', 'lineage_entity': 'Artifact', 'lineage_source': 'Endpoint', "
574+
+ "'_session': <Mock id=''>}, \n\t{'arn': 'arn2', 'lineage_entity': 'Context', 'lineage_source': "
575+
+ "'Model', '_session': <Mock id=''>}],\n\n'startarn': "
576+
+ "['arn:aws:sagemaker:us-west-2:0123456789012:context/mycontext'],\n}"
577+
)

0 commit comments

Comments
 (0)