Skip to content

Commit d2aa0d8

Browse files
committed
Add replace_terms method to AtlanClient
1 parent 433d473 commit d2aa0d8

3 files changed

Lines changed: 158 additions & 1 deletion

File tree

pyatlan/client/atlan.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,33 @@ def append_terms(
634634
term for term in existing_terms if term.relationship_status != "DELETED"
635635
)
636636
replacement_terms.extend(terms)
637-
asset.attributes.meanings = replacement_terms
637+
asset.terms = replacement_terms
638+
response = self.upsert(entity=asset)
639+
if assets := response.assets_updated(asset_type=asset_type):
640+
return assets[0]
641+
return asset
642+
643+
@validate_arguments()
644+
def replace_terms(
645+
self,
646+
asset_type: Type[A],
647+
terms: list[AtlasGlossaryTerm],
648+
guid: Optional[str] = None,
649+
qualified_name: Optional[str] = None,
650+
) -> A:
651+
if guid:
652+
if qualified_name:
653+
raise ValueError(
654+
"Either guid or qualified_name can be be specified not both"
655+
)
656+
asset = self.get_asset_by_guid(guid=guid, asset_type=asset_type)
657+
elif qualified_name:
658+
asset = self.get_asset_by_qualified_name(
659+
qualified_name=qualified_name, asset_type=asset_type
660+
)
661+
else:
662+
raise ValueError("Either guid or qualified name must be specified")
663+
asset.terms = terms
638664
response = self.upsert(entity=asset)
639665
if assets := response.assets_updated(asset_type=asset_type):
640666
return assets[0]

tests/integration/test_client.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,61 @@ def test_append_terms_using_ref_by_guid_for_term(
126126
database = client.get_asset_by_guid(guid=database.guid, asset_type=Database)
127127
assert len(database.terms) == 1
128128
assert database.terms[0].guid == term.guid
129+
130+
131+
def test_replace_a_term(
132+
client: AtlanClient,
133+
make_term: Callable[[str], AtlasGlossaryTerm],
134+
database: Database,
135+
):
136+
original_term = make_term("Term1")
137+
assert (
138+
database := client.append_terms(
139+
qualified_name=database.qualified_name,
140+
asset_type=Database,
141+
terms=[AtlasGlossaryTerm.ref_by_guid(guid=original_term.guid)],
142+
)
143+
)
144+
145+
replacemant_term = make_term("Term2")
146+
assert (
147+
database := client.replace_terms(
148+
guid=database.guid, asset_type=Database, terms=[replacemant_term]
149+
)
150+
)
151+
152+
database = client.get_asset_by_guid(guid=database.guid, asset_type=Database)
153+
assert len(database.terms) == 2
154+
deleted_terms = [t for t in database.terms if t.relationship_status == "DELETED"]
155+
assert len(deleted_terms) == 1
156+
assert deleted_terms[0].guid == original_term.guid
157+
active_terms = [t for t in database.terms if t.relationship_status != "DELETED"]
158+
assert len(active_terms) == 1
159+
assert active_terms[0].guid == replacemant_term.guid
160+
161+
162+
def test_replace_all_term(
163+
client: AtlanClient,
164+
make_term: Callable[[str], AtlasGlossaryTerm],
165+
database: Database,
166+
):
167+
original_term = make_term("Term1")
168+
assert (
169+
database := client.append_terms(
170+
qualified_name=database.qualified_name,
171+
asset_type=Database,
172+
terms=[AtlasGlossaryTerm.ref_by_guid(guid=original_term.guid)],
173+
)
174+
)
175+
176+
assert (
177+
database := client.replace_terms(
178+
guid=database.guid, asset_type=Database, terms=[]
179+
)
180+
)
181+
182+
database = client.get_asset_by_guid(guid=database.guid, asset_type=Database)
183+
assert len(database.terms) == 1
184+
deleted_terms = [t for t in database.terms if t.relationship_status == "DELETED"]
185+
assert len(deleted_terms) == 1
186+
assert deleted_terms[0].guid == original_term.guid

tests/unit/test_client.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,76 @@ def test_append_with_valid_guid_when_terms_present_returns_asset_with_combined_t
152152
assert len(updated_terms) == 2
153153
assert exisiting_term in updated_terms
154154
assert new_term in updated_terms
155+
156+
157+
@pytest.mark.parametrize(
158+
"guid, qualified_name, asset_type, terms, message",
159+
[
160+
(
161+
None,
162+
None,
163+
Table,
164+
[AtlasGlossaryTerm()],
165+
"Either guid or qualified name must be specified",
166+
),
167+
(
168+
"123",
169+
None,
170+
None,
171+
[AtlasGlossaryTerm()],
172+
"1 validation error for ReplaceTerms\\nasset_type\\n none is not an allowed value ",
173+
),
174+
(
175+
"123",
176+
"default/abc",
177+
Table,
178+
[AtlasGlossaryTerm()],
179+
"Either guid or qualified_name can be be specified not both",
180+
),
181+
(
182+
"123",
183+
None,
184+
Table,
185+
None,
186+
"1 validation error for ReplaceTerms\\nterms\\n none is not an allowed value ",
187+
),
188+
],
189+
)
190+
def test_replace_terms_with_invalid_parameter_raises_valueerror(
191+
guid,
192+
qualified_name,
193+
asset_type,
194+
terms,
195+
message,
196+
monkeypatch,
197+
):
198+
monkeypatch.setenv("ATLAN_BASE_URL", "https://name.atlan.com")
199+
monkeypatch.setenv("ATLAN_API_KEY", "abkj")
200+
client = AtlanClient()
201+
202+
with pytest.raises(ValueError, match=message):
203+
client.replace_terms(
204+
guid=guid, qualified_name=qualified_name, asset_type=asset_type, terms=terms
205+
)
206+
207+
208+
def test_replace_terms(
209+
monkeypatch,
210+
):
211+
monkeypatch.setenv("ATLAN_BASE_URL", "https://name.atlan.com")
212+
monkeypatch.setenv("ATLAN_API_KEY", "abkj")
213+
asset_type = Table
214+
with patch.multiple(
215+
AtlanClient, get_asset_by_guid=DEFAULT, upsert=DEFAULT
216+
) as mock_methods:
217+
table = Table()
218+
mock_methods["get_asset_by_guid"].return_value = table
219+
mock_methods["upsert"].return_value.assets_updated.return_value = [table]
220+
client = AtlanClient()
221+
guid = "123"
222+
terms = [AtlasGlossaryTerm()]
223+
224+
assert (
225+
asset := client.replace_terms(guid=guid, asset_type=asset_type, terms=terms)
226+
)
227+
assert asset.terms == terms

0 commit comments

Comments
 (0)