Skip to content

Commit 00ad1c5

Browse files
committed
[tests] Added unit tests for custom relationships (user_def_relationship)
1 parent ebc6b6f commit 00ad1c5

File tree

4 files changed

+207
-3
lines changed

4 files changed

+207
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__all__ = [
2+
"RelationshipAttributes",
3+
"UserDefRelationship",
4+
]
5+
6+
from .relationship_attributes import RelationshipAttributes
7+
from .user_def_relationship import UserDefRelationship

pyatlan/model/assets/relations/user_def_relationship.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ def user_def_relationship_from(
104104
self, related: Asset, semantic: SaveSemantic = SaveSemantic.REPLACE
105105
) -> UserDefRelationship.UserDefRelationshipFrom:
106106
if related.guid:
107-
return UserDefRelationship.UserDefRelationshipfrom._create_ref(
107+
return UserDefRelationship.UserDefRelationshipFrom._create_ref(
108108
type_name=related.type_name,
109109
guid=related.guid,
110110
semantic=semantic,
111111
relationship_attributes=self,
112112
)
113113

114114
# If the related asset does not have a GUID, we use qualifiedName
115-
return UserDefRelationship.UserDefRelationshipfrom._create_ref(
115+
return UserDefRelationship.UserDefRelationshipFrom._create_ref(
116116
type_name=related.type_name,
117117
unique_attributes={"qualifiedName": related.qualified_name},
118118
semantic=semantic,

pyatlan/model/search.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,6 @@ class IndexSearchRequest(SearchRequest):
20262026
)
20272027
include_relationship_attributes: Optional[bool] = Field(
20282028
default=False,
2029-
alias="includeRelationshipAttributes",
20302029
description=(
20312030
"Whether to include relationship-level attributes "
20322031
"for any relationships to each asset (True) or not (False). "
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
from unittest.mock import patch
2+
3+
import pytest
4+
5+
from pyatlan.model.assets import AtlasGlossaryTerm
6+
from pyatlan.model.assets.relations import UserDefRelationship
7+
8+
9+
@pytest.fixture()
10+
def mock_asset_guid():
11+
with patch("pyatlan.utils.random") as mock_random:
12+
mock_random.random.return_value = 123456789
13+
yield mock_random
14+
15+
16+
def _assert_relationship(relationship):
17+
assert relationship
18+
assert relationship.guid
19+
assert relationship.type_name
20+
assert relationship.attributes
21+
assert relationship.attributes.relationship_attributes
22+
assert relationship.attributes.relationship_attributes.attributes
23+
assert (
24+
relationship.attributes.relationship_attributes.type_name
25+
== "UserDefRelationship"
26+
)
27+
assert relationship.attributes.relationship_attributes.attributes.from_type_label
28+
assert relationship.attributes.relationship_attributes.attributes.from_type_label
29+
30+
31+
def test_user_def_relationship_deserialization():
32+
raw_json = {
33+
"typeName": "AtlasGlossaryTerm",
34+
"attributes": {
35+
"userDefRelationshipTo": [
36+
{
37+
"guid": "90a43be2-f700-4f78-8512-4a38f129a901",
38+
"typeName": "AtlasGlossaryTerm",
39+
"attributes": {
40+
"relationshipAttributes": {
41+
"typeName": "UserDefRelationship",
42+
"attributes": {
43+
"fromTypeLabel": "test0-from-label",
44+
"toTypeLabel": "test0-to-label",
45+
},
46+
},
47+
"name": "test-term0",
48+
},
49+
"uniqueAttributes": {
50+
"qualifiedName": "Orpt6s3r9CNLFXftuvVpZ@KhgDjmb3kdRvft9TEzv5W"
51+
},
52+
},
53+
],
54+
"qualifiedName": "i9QXU6yl19Grdk8d6yVYT@KhgDjmb3kdRvft9TEzv9W",
55+
"userDefRelationshipFrom": [
56+
{
57+
"guid": "90a43be2-f700-4f78-8512-4a38f121a901",
58+
"typeName": "AtlasGlossaryTerm",
59+
"attributes": {
60+
"relationshipAttributes": {
61+
"typeName": "UserDefRelationship",
62+
"attributes": {
63+
"fromTypeLabel": "test1-from-label",
64+
"toTypeLabel": "test1-to-label",
65+
},
66+
},
67+
"name": "test-term1",
68+
},
69+
"uniqueAttributes": {
70+
"qualifiedName": "Orpt6s3r9CNLFXftuvVpZ@KhgDjmb3kdRvft9TEzv5W"
71+
},
72+
},
73+
{
74+
"guid": "90a43be2-f700-4f78-8512-4a38f121a911",
75+
"typeName": "AtlasGlossaryTerm",
76+
"attributes": {
77+
"relationshipAttributes": {
78+
"typeName": "UserDefRelationship",
79+
"attributes": {
80+
"fromTypeLabel": "test3-from-label",
81+
"toTypeLabel": "test3-to-label",
82+
},
83+
},
84+
"name": "test-term3",
85+
},
86+
"uniqueAttributes": {
87+
"qualifiedName": "Orpt6s3r9CNLFXftuvVpZ@KhgDjmb3kdRvft9TEzv5W"
88+
},
89+
},
90+
],
91+
"name": "test-term2",
92+
},
93+
"guid": "977b55f9-084c-460f-bf3b-cea5fa740e20",
94+
"status": "ACTIVE",
95+
"displayText": "test-term2",
96+
"classificationNames": [],
97+
"classifications": [],
98+
"meaningNames": [],
99+
"meanings": [],
100+
"isIncomplete": False,
101+
"labels": [],
102+
"createdBy": "test-user",
103+
"updatedBy": "service-account-apikey",
104+
"createTime": 1750078015371,
105+
"updateTime": 1750156224299,
106+
}
107+
term = AtlasGlossaryTerm(**raw_json)
108+
assert term.name and term.guid and term.qualified_name
109+
to_relation1 = term.user_def_relationship_to[0]
110+
from_relation1 = term.user_def_relationship_from[0]
111+
fron_relation2 = term.user_def_relationship_from[1]
112+
_assert_relationship(to_relation1)
113+
_assert_relationship(from_relation1)
114+
_assert_relationship(fron_relation2)
115+
116+
117+
def test_user_def_relationship_serialization(mock_asset_guid):
118+
expected_json = {
119+
"typeName": "AtlasGlossaryTerm",
120+
"attributes": {
121+
"qualifiedName": "test-term-qn",
122+
"userDefRelationshipTo": [
123+
{
124+
"typeName": "AtlasGlossaryTerm",
125+
"guid": "test-term2-guid",
126+
"relationshipAttributes": {
127+
"typeName": "UserDefRelationship",
128+
"attributes": {
129+
"fromTypeLabel": "test1-from-label",
130+
"toTypeLabel": "test1-to-label",
131+
},
132+
},
133+
"relationshipType": "UserDefRelationship",
134+
},
135+
{
136+
"typeName": "AtlasGlossaryTerm",
137+
"guid": "test-term3-guid",
138+
"relationshipAttributes": {
139+
"typeName": "UserDefRelationship",
140+
"attributes": {
141+
"fromTypeLabel": "test2-from-label",
142+
"toTypeLabel": "test2-to-label",
143+
},
144+
},
145+
"relationshipType": "UserDefRelationship",
146+
},
147+
],
148+
"userDefRelationshipFrom": [
149+
{
150+
"typeName": "AtlasGlossaryTerm",
151+
"guid": "test-term0-guid",
152+
"relationshipAttributes": {
153+
"typeName": "UserDefRelationship",
154+
"attributes": {
155+
"fromTypeLabel": "test0-from-label",
156+
"toTypeLabel": "test0-to-label",
157+
},
158+
},
159+
"relationshipType": "UserDefRelationship",
160+
}
161+
],
162+
"name": "test-term",
163+
"anchor": {
164+
"typeName": "AtlasGlossary",
165+
"attributes": {"qualifiedName": "test-glossary-guid"},
166+
"guid": "test-glossary-guid",
167+
},
168+
},
169+
"guid": "-1234567890000000000000000",
170+
}
171+
172+
term1 = AtlasGlossaryTerm.updater(
173+
qualified_name="test-term-qn",
174+
name="test-term",
175+
glossary_guid="test-glossary-guid",
176+
)
177+
178+
term0 = AtlasGlossaryTerm.ref_by_guid("test-term0-guid")
179+
term2 = AtlasGlossaryTerm.ref_by_guid("test-term2-guid")
180+
term3 = AtlasGlossaryTerm.ref_by_guid("test-term3-guid")
181+
182+
udr_from0 = UserDefRelationship(
183+
from_type_label="test0-from-label", to_type_label="test0-to-label"
184+
)
185+
udr_to1 = UserDefRelationship(
186+
from_type_label="test1-from-label", to_type_label="test1-to-label"
187+
)
188+
udr_to2 = UserDefRelationship(
189+
from_type_label="test2-from-label", to_type_label="test2-to-label"
190+
)
191+
192+
term1.user_def_relationship_from = [udr_from0.user_def_relationship_from(term0)]
193+
term1.user_def_relationship_to = [
194+
udr_to1.user_def_relationship_to(term2),
195+
udr_to2.user_def_relationship_to(term3),
196+
]
197+
198+
assert term1.dict(by_alias=True, exclude_unset=True) == expected_json

0 commit comments

Comments
 (0)