Skip to content

Commit 42eb2da

Browse files
committed
[tests] Fixed/updated tests for AtlanTag/Name
1 parent 81ea57e commit 42eb2da

3 files changed

Lines changed: 135 additions & 159 deletions

File tree

pyatlan/model/core.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def __new__(cls, *args, **kwargs):
4141
return obj
4242

4343
def __init__(self, display_text: str):
44-
from pyatlan.client.atlan import AtlanClient
45-
46-
if not (
47-
id := AtlanClient.get_current_client().atlan_tag_cache.get_id_for_name(
48-
display_text
49-
)
50-
):
51-
raise ValueError(f"{display_text} is not a valid Classification")
44+
# from pyatlan.client.atlan import AtlanClient
45+
46+
# if not (
47+
# id := AtlanClient.get_current_client().atlan_tag_cache.get_id_for_name(
48+
# display_text
49+
# )
50+
# ):
51+
# raise ValueError(f"{display_text} is not a valid Classification")
5252
self._display_text = display_text
5353
self._id = id
5454

@@ -65,7 +65,7 @@ def get_deleted_sentinel(cls) -> "AtlanTagName":
6565

6666
@classmethod
6767
def __get_validators__(cls):
68-
yield cls._convert_to_display_text
68+
yield cls._convert_to_tag_name
6969

7070
def __str__(self):
7171
return self._display_text
@@ -82,20 +82,26 @@ def __eq__(self, other):
8282
and self._display_text == other._display_text
8383
)
8484

85-
@classmethod
86-
def _convert_to_display_text(cls, data):
87-
from pyatlan.client.atlan import AtlanClient
85+
# @classmethod
86+
# def _convert_to_display_text(cls, data):
87+
# from pyatlan.client.atlan import AtlanClient
8888

89+
# if isinstance(data, AtlanTagName):
90+
# return data
91+
92+
# if (
93+
# display_text
94+
# := AtlanClient.get_current_client().atlan_tag_cache.get_name_for_id(data)
95+
# ):
96+
# return AtlanTagName(display_text)
97+
# else:
98+
# return cls.get_deleted_sentinel()
99+
100+
@classmethod
101+
def _convert_to_tag_name(cls, data):
89102
if isinstance(data, AtlanTagName):
90103
return data
91-
92-
if (
93-
display_text
94-
:= AtlanClient.get_current_client().atlan_tag_cache.get_name_for_id(data)
95-
):
96-
return AtlanTagName(display_text)
97-
else:
98-
return cls.get_deleted_sentinel()
104+
return AtlanTagName(data) if data else cls.get_deleted_sentinel()
99105

100106
@staticmethod
101107
def json_encode_atlan_tag(atlan_tag_name: "AtlanTagName"):
@@ -237,7 +243,7 @@ class AtlanTag(AtlanObject):
237243
class Config:
238244
extra = "forbid"
239245

240-
type_name: Optional[Union[str, AtlanTagName]] = Field(
246+
type_name: Optional[AtlanTagName] = Field(
241247
default=None,
242248
description="Name of the type definition that defines this instance.\n",
243249
alias="typeName",

tests/unit/test_atlan_tag_name.py

Lines changed: 76 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# Copyright 2022 Atlan Pte. Ltd.
33
import pytest
4+
from pydantic.v1 import parse_obj_as
45

56
import pyatlan.cache.atlan_tag_cache
67
from pyatlan.client.atlan import AtlanClient
7-
from pyatlan.model.constants import DELETED_
8+
from pyatlan.model.assets import Purpose
89
from pyatlan.model.core import AtlanTagName
910

1011
ATLAN_TAG_ID = "yiB7RLvdC2yeryLPjaDeHM"
@@ -32,23 +33,6 @@ def current_client(client, monkeypatch):
3233
)
3334

3435

35-
def test_init_with_bad_atlan_tag_name_raises_value_error(
36-
current_client: AtlanClient, monkeypatch
37-
):
38-
def get_id_for_name(_, __):
39-
return None
40-
41-
monkeypatch.setattr(
42-
pyatlan.cache.atlan_tag_cache.AtlanTagCache,
43-
"get_id_for_name",
44-
get_id_for_name,
45-
)
46-
with pytest.raises(
47-
ValueError, match=f"{GOOD_ATLAN_TAG_NAME} is not a valid Classification"
48-
):
49-
AtlanTagName(GOOD_ATLAN_TAG_NAME)
50-
51-
5236
@pytest.fixture()
5337
def good_atlan_tag(current_client: AtlanClient, monkeypatch):
5438
def get_id_for_name(_, value):
@@ -83,7 +67,7 @@ def get_id_for_name(_, value):
8367
def test_convert_to_display_text_when_atlan_tag_passed_returns_same_atlan_tag(
8468
good_atlan_tag,
8569
):
86-
assert good_atlan_tag is AtlanTagName._convert_to_display_text(good_atlan_tag)
70+
assert good_atlan_tag is AtlanTagName._convert_to_tag_name(good_atlan_tag)
8771

8872

8973
def test_convert_to_display_text_when_bad_string(
@@ -98,100 +82,91 @@ def get_name_for_id(_, __):
9882
get_name_for_id,
9983
)
10084

101-
assert (
102-
AtlanTagName._convert_to_display_text("bad").__repr__()
103-
== f"AtlanTagName('{DELETED_}')"
104-
)
85+
assert AtlanTagName._convert_to_tag_name("bad").__repr__() == "AtlanTagName('bad')"
86+
10587

88+
def test_convert_to_tag_name(current_client: AtlanClient, monkeypatch):
89+
sut = AtlanTagName._convert_to_tag_name(ATLAN_TAG_ID)
10690

107-
def test_convert_to_display_text_when_id(current_client: AtlanClient, monkeypatch):
91+
assert str(sut) == ATLAN_TAG_ID
92+
93+
94+
def test_json_encode_atlan_tag(good_atlan_tag):
95+
assert AtlanTagName.json_encode_atlan_tag(good_atlan_tag) == ATLAN_TAG_ID
96+
97+
98+
def test_asset_tag_name_field_deserialization(current_client: AtlanClient, monkeypatch):
10899
def get_name_for_id(_, __):
109-
return GOOD_ATLAN_TAG_NAME
100+
return None
110101

111-
def get_id_for_name(_, value):
112-
assert value == GOOD_ATLAN_TAG_NAME
113-
return GOOD_ATLAN_TAG_NAME
102+
def get_id_for_name(_, __):
103+
return None
114104

115105
monkeypatch.setattr(
116106
pyatlan.cache.atlan_tag_cache.AtlanTagCache,
117107
"get_id_for_name",
118108
get_id_for_name,
119109
)
110+
120111
monkeypatch.setattr(
121112
pyatlan.cache.atlan_tag_cache.AtlanTagCache,
122113
"get_name_for_id",
123114
get_name_for_id,
124115
)
125-
126-
sut = AtlanTagName._convert_to_display_text(ATLAN_TAG_ID)
127-
128-
assert str(sut) == GOOD_ATLAN_TAG_NAME
129-
130-
131-
def test_json_encode_atlan_tag(good_atlan_tag):
132-
assert AtlanTagName.json_encode_atlan_tag(good_atlan_tag) == ATLAN_TAG_ID
133-
134-
135-
# def test_asset_tag_name_field_deserialization(current_client: AtlanClient, monkeypatch):
136-
# def get_name_for_id(_, __):
137-
# return None
138-
139-
# def get_id_for_name(_, __):
140-
# return None
141-
142-
# monkeypatch.setattr(
143-
# pyatlan.cache.atlan_tag_cache.AtlanTagCache,
144-
# "get_id_for_name",
145-
# get_id_for_name,
146-
# )
147-
148-
# monkeypatch.setattr(
149-
# pyatlan.cache.atlan_tag_cache.AtlanTagCache,
150-
# "get_name_for_id",
151-
# get_name_for_id,
152-
# )
153-
# # Simulate a `Purpose` asset with `purpose_atlan_tags` of type `AtlanTagName`
154-
# purpose_asset = {
155-
# "typeName": "Purpose",
156-
# "attributes": {
157-
# # AtlanTagName
158-
# "purposeClassifications": [
159-
# "some-deleted-purpose-tag-1",
160-
# "some-deleted-purpose-tag-2",
161-
# ],
162-
# },
163-
# "guid": "9f7a35f4-8d37-4273-81ec-c497a83a2472",
164-
# "status": "ACTIVE",
165-
# "classifications": [
166-
# # AtlanTag
167-
# {
168-
# "typeName": "some-deleted-purpose-tag-1",
169-
# "entityGuid": "82683fb9-1501-4627-a5d0-0da9be64c0d5",
170-
# "entityStatus": "DELETED",
171-
# "propagate": False,
172-
# "removePropagationsOnEntityDelete": True,
173-
# "restrictPropagationThroughLineage": True,
174-
# "restrictPropagationThroughHierarchy": False,
175-
# },
176-
# {
177-
# "typeName": "some-deleted-purpose-tag-2",
178-
# "entityGuid": "82683fb9-1501-4627-a5d0-0da9be64c0d5",
179-
# "entityStatus": "DELETED",
180-
# "propagate": False,
181-
# "removePropagationsOnEntityDelete": True,
182-
# "restrictPropagationThroughLineage": True,
183-
# "restrictPropagationThroughHierarchy": False,
184-
# },
185-
# ],
186-
# }
187-
# purpose = parse_obj_as(Purpose, purpose_asset)
188-
# assert purpose and isinstance(purpose, Purpose)
189-
190-
# # Verify that deleted tags are correctly set to `None`
191-
# # assert purpose.atlan_tags == [AtlanTagName('(DELETED)')]
192-
# assert purpose.atlan_tags and len(purpose.atlan_tags) == 2
193-
# assert purpose.atlan_tags[0].type_name.__repr__() == f"AtlanTagName('{DELETED_}')"
194-
# assert purpose.atlan_tags[1].type_name.__repr__() == f"AtlanTagName('{DELETED_}')"
195-
# assert purpose.purpose_atlan_tags and len(purpose.purpose_atlan_tags) == 2
196-
# assert purpose.purpose_atlan_tags[0].__repr__() == f"AtlanTagName('{DELETED_}')"
197-
# assert purpose.purpose_atlan_tags[1].__repr__() == f"AtlanTagName('{DELETED_}')"
116+
# Simulate a `Purpose` asset with `purpose_atlan_tags` of type `AtlanTagName`
117+
purpose_asset = {
118+
"typeName": "Purpose",
119+
"attributes": {
120+
# AtlanTagName
121+
"purposeClassifications": [
122+
"some-deleted-purpose-tag-1",
123+
"some-deleted-purpose-tag-2",
124+
],
125+
},
126+
"guid": "9f7a35f4-8d37-4273-81ec-c497a83a2472",
127+
"status": "ACTIVE",
128+
"classifications": [
129+
# AtlanTag
130+
{
131+
"typeName": "some-deleted-purpose-tag-1",
132+
"entityGuid": "82683fb9-1501-4627-a5d0-0da9be64c0d5",
133+
"entityStatus": "DELETED",
134+
"propagate": False,
135+
"removePropagationsOnEntityDelete": True,
136+
"restrictPropagationThroughLineage": True,
137+
"restrictPropagationThroughHierarchy": False,
138+
},
139+
{
140+
"typeName": "some-deleted-purpose-tag-2",
141+
"entityGuid": "82683fb9-1501-4627-a5d0-0da9be64c0d5",
142+
"entityStatus": "DELETED",
143+
"propagate": False,
144+
"removePropagationsOnEntityDelete": True,
145+
"restrictPropagationThroughLineage": True,
146+
"restrictPropagationThroughHierarchy": False,
147+
},
148+
],
149+
}
150+
purpose = parse_obj_as(Purpose, purpose_asset)
151+
assert purpose and isinstance(purpose, Purpose)
152+
153+
# Verify that deleted tags are correctly set to `None`
154+
# assert purpose.atlan_tags == [AtlanTagName('(DELETED)')]
155+
assert purpose.atlan_tags and len(purpose.atlan_tags) == 2
156+
assert (
157+
purpose.atlan_tags[0].type_name.__repr__()
158+
== "AtlanTagName('some-deleted-purpose-tag-1')"
159+
)
160+
assert (
161+
purpose.atlan_tags[1].type_name.__repr__()
162+
== "AtlanTagName('some-deleted-purpose-tag-2')"
163+
)
164+
assert purpose.purpose_atlan_tags and len(purpose.purpose_atlan_tags) == 2
165+
assert (
166+
purpose.purpose_atlan_tags[0].__repr__()
167+
== "AtlanTagName('some-deleted-purpose-tag-1')"
168+
)
169+
assert (
170+
purpose.purpose_atlan_tags[1].__repr__()
171+
== "AtlanTagName('some-deleted-purpose-tag-2')"
172+
)

tests/unit/test_core.py

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

33
from typing import no_type_check
4-
from unittest.mock import MagicMock, call
4+
from unittest.mock import MagicMock
55

66
import pytest
77
from pydantic.v1 import Field
88

99
from pyatlan.client.atlan import AtlanClient
10-
from pyatlan.model.core import AtlanObject, AtlanTagName
10+
from pyatlan.model.core import AtlanObject, AtlanTag, AtlanTagName
1111

1212
DISPLAY_TEXT = "Something"
1313

@@ -46,51 +46,46 @@ def test_get_deleted_sentinel(self):
4646
assert "(DELETED)" == str(sentinel)
4747
assert id(sentinel) == id(AtlanTagName.get_deleted_sentinel())
4848

49-
def test_atlan_tag_name_when_name_found_returns_atlan_tag_name(
50-
self, mock_tag_cache
51-
):
52-
mock_tag_cache.get_id_for_name.return_value = "123"
53-
54-
sut = AtlanTagName(DISPLAY_TEXT)
55-
56-
assert DISPLAY_TEXT == str(sut)
57-
mock_tag_cache.get_id_for_name.assert_called_once_with(DISPLAY_TEXT)
49+
# def test_atlan_tag_name_when_name_found_returns_atlan_tag_name(
50+
# self, mock_tag_cache
51+
# ):
52+
# mock_tag_cache.get_id_for_name.return_value = "123"
5853

59-
def test_atlan_tag_name_when_name_not_found_raise_value_error(self, mock_tag_cache):
60-
mock_tag_cache.get_id_for_name.return_value = None
54+
# sut = AtlanTagName(DISPLAY_TEXT)
6155

62-
with pytest.raises(
63-
ValueError, match=f"{DISPLAY_TEXT} is not a valid Classification"
64-
):
65-
AtlanTagName(DISPLAY_TEXT)
56+
# assert DISPLAY_TEXT == str(sut)
57+
# mock_tag_cache.get_id_for_name.assert_called_once_with(DISPLAY_TEXT)
6658

67-
def test_json_encode_atlan_tag_returns_internal_code(self, mock_tag_cache):
68-
internal_value = "123"
69-
mock_tag_cache.get_id_for_name.return_value = internal_value
70-
sut = AtlanTagName(DISPLAY_TEXT)
59+
# def test_atlan_tag_name_when_name_not_found_raise_value_error(self, mock_tag_cache):
60+
# mock_tag_cache.get_id_for_name.return_value = None
7161

72-
assert internal_value == AtlanTagName.json_encode_atlan_tag(sut)
73-
mock_tag_cache.get_id_for_name.assert_has_calls(
74-
[call(DISPLAY_TEXT), call(DISPLAY_TEXT)]
75-
)
62+
# with pytest.raises(
63+
# ValueError, match=f"{DISPLAY_TEXT} is not a valid Classification"
64+
# ):
65+
# AtlanTagName(DISPLAY_TEXT)
7666

67+
# def test_json_encode_atlan_tag_returns_internal_code(self, mock_tag_cache):
68+
# internal_value = "123"
69+
# mock_tag_cache.get_id_for_name.return_value = internal_value
70+
# sut = AtlanTagName(DISPLAY_TEXT)
7771

78-
# class TestAtlanTag:
79-
# def test_atlan_tag_when_tag_name_is_found(self, mock_tag_cache):
80-
# mock_tag_cache.get_name_for_id.return_value = DISPLAY_TEXT
72+
# assert internal_value == AtlanTagName.json_encode_atlan_tag(sut)
73+
# mock_tag_cache.get_id_for_name.assert_has_calls(
74+
# [call(DISPLAY_TEXT), call(DISPLAY_TEXT)]
75+
# )
8176

82-
# sut = AtlanTag(**{"typeName": "123"})
8377

84-
# assert str(sut.type_name) == DISPLAY_TEXT
78+
class TestAtlanTag:
79+
def test_atlan_tag_when_tag_name_is_found(self, mock_tag_cache):
80+
sut = AtlanTag(**{"typeName": "123"})
81+
assert str(sut.type_name) == "123"
8582

86-
# def test_atlan_tag_when_tag_name_is_not_found_then_sentinel_is_returned(
87-
# self, mock_tag_cache
88-
# ):
89-
# mock_tag_cache.get_name_for_id.return_value = None
90-
91-
# sut = AtlanTag(**{"typeName": "123"})
83+
def test_atlan_tag_when_tag_name_is_empty_then_sentinel_is_returned(
84+
self, mock_tag_cache
85+
):
86+
sut = AtlanTag(**{"typeName": ""})
9287

93-
# assert sut.type_name == AtlanTagName.get_deleted_sentinel()
88+
assert sut.type_name == AtlanTagName.get_deleted_sentinel()
9489

9590

9691
class TestAtlanObjectExtraFields:

0 commit comments

Comments
 (0)