Skip to content

Commit db9186a

Browse files
committed
[tests] Updated unit tests as per changes
1 parent 4ec945c commit db9186a

17 files changed

+832
-662
lines changed

pyatlan/client/atlan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def __init__(self, **data):
222222
adapter = HTTPAdapter(max_retries=self.retry)
223223
session.mount(HTTPS_PREFIX, adapter)
224224
session.mount(HTTP_PREFIX, adapter)
225-
self._current_client_tls.client = None
225+
AtlanClient.set_current_client(self)
226226

227227
@property
228228
def cache_key(self) -> int:

tests/integration/admin_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pytest
77
from pydantic.v1 import StrictStr
88

9-
from pyatlan.cache.role_cache import RoleCache
109
from pyatlan.client.atlan import AtlanClient
1110
from pyatlan.model.group import AtlanGroup, CreateGroupResponse, GroupRequest
1211
from pyatlan.model.keycloak_events import AdminEventRequest, KeycloakEventRequest
@@ -35,7 +34,7 @@ def delete_group(client: AtlanClient, guid: str) -> None:
3534

3635

3736
def test_retrieve_roles(client: AtlanClient):
38-
admin_role_guid = RoleCache.get_id_for_name("$admin")
37+
admin_role_guid = client.role_cache.get_id_for_name("$admin")
3938
assert admin_role_guid
4039

4140

tests/integration/connection_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright 2022 Atlan Pte. Ltd.
33
import pytest
44

5-
from pyatlan.cache.role_cache import RoleCache
65
from pyatlan.client.atlan import AtlanClient
76
from pyatlan.model.assets import Connection
87
from pyatlan.model.enums import AtlanConnectorType
@@ -14,7 +13,7 @@
1413
def create_connection(
1514
client: AtlanClient, name: str, connector_type: AtlanConnectorType
1615
) -> Connection:
17-
admin_role_guid = str(RoleCache.get_id_for_name("$admin"))
16+
admin_role_guid = str(client.role_cache.get_id_for_name("$admin"))
1817
to_create = Connection.create(
1918
name=name, connector_type=connector_type, admin_roles=[admin_role_guid]
2019
)

tests/integration/custom_metadata_test.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import pytest
88

9-
from pyatlan.cache.custom_metadata_cache import CustomMetadataCache
109
from pyatlan.client.atlan import AtlanClient
1110
from pyatlan.model.assets import (
1211
Asset,
@@ -623,7 +622,7 @@ def test_search_by_any_accountable(
623622
term: AtlasGlossaryTerm,
624623
):
625624
attributes = ["name", "anchor"]
626-
cm_attributes = CustomMetadataCache.get_attributes_for_search_results(
625+
cm_attributes = client.custom_metadata_cache.get_attributes_for_search_results(
627626
set_name=CM_RACI
628627
)
629628
assert cm_attributes
@@ -724,7 +723,7 @@ def test_remove_term_cm_ipr(
724723
def test_remove_attribute(client: AtlanClient, cm_raci: CustomMetadataDef):
725724
global _removal_epoch
726725
cm_name = CM_RACI
727-
existing = CustomMetadataCache.get_custom_metadata_def(name=cm_name)
726+
existing = client.custom_metadata_cache.get_custom_metadata_def(name=cm_name)
728727
existing_attrs = existing.attribute_defs
729728
updated_attrs = []
730729
for existing_attr in existing_attrs:
@@ -758,15 +757,15 @@ def test_remove_attribute(client: AtlanClient, cm_raci: CustomMetadataDef):
758757
@pytest.mark.order(after="test_remove_attribute")
759758
def test_retrieve_structures(client: AtlanClient, cm_raci: CustomMetadataDef):
760759
global _removal_epoch
761-
custom_attributes = CustomMetadataCache.get_all_custom_attributes()
760+
custom_attributes = client.custom_metadata_cache.get_all_custom_attributes()
762761
assert custom_attributes
763762
assert len(custom_attributes) >= 3
764763
assert CM_RACI in custom_attributes.keys()
765764
assert CM_IPR in custom_attributes.keys()
766765
assert CM_QUALITY in custom_attributes.keys()
767766
extra = _validate_raci_structure(custom_attributes.get(CM_RACI), 4)
768767
assert not extra
769-
custom_attributes = CustomMetadataCache.get_all_custom_attributes(
768+
custom_attributes = client.custom_metadata_cache.get_all_custom_attributes(
770769
include_deleted=True
771770
)
772771
assert custom_attributes
@@ -785,7 +784,7 @@ def test_retrieve_structures(client: AtlanClient, cm_raci: CustomMetadataDef):
785784

786785
@pytest.mark.order(after="test_retrieve_structures")
787786
def test_recreate_attribute(client: AtlanClient, cm_raci: CustomMetadataDef):
788-
existing = CustomMetadataCache.get_custom_metadata_def(name=CM_RACI)
787+
existing = client.custom_metadata_cache.get_custom_metadata_def(name=CM_RACI)
789788
existing_attrs = existing.attribute_defs
790789
updated_attrs = []
791790
for existing_attr in existing_attrs:
@@ -819,7 +818,7 @@ def test_recreate_attribute(client: AtlanClient, cm_raci: CustomMetadataDef):
819818
def test_retrieve_structure_without_archived(
820819
client: AtlanClient, cm_raci: CustomMetadataDef
821820
):
822-
custom_attributes = CustomMetadataCache.get_all_custom_attributes()
821+
custom_attributes = client.custom_metadata_cache.get_all_custom_attributes()
823822
assert custom_attributes
824823
assert len(custom_attributes) >= 3
825824
assert CM_RACI in custom_attributes.keys()
@@ -838,7 +837,7 @@ def test_retrieve_structure_without_archived(
838837
def test_retrieve_structure_with_archived(
839838
client: AtlanClient, cm_raci: CustomMetadataDef
840839
):
841-
custom_attributes = CustomMetadataCache.get_all_custom_attributes(
840+
custom_attributes = client.custom_metadata_cache.get_all_custom_attributes(
842841
include_deleted=True
843842
)
844843
assert custom_attributes

tests/integration/test_open_lineage.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44

5-
from pyatlan.cache.role_cache import RoleCache
65
from pyatlan.client.atlan import AtlanClient
76
from pyatlan.model.assets import Asset, Connection, Process, SparkJob
87
from pyatlan.model.audit import AuditSearchRequest
@@ -17,7 +16,7 @@
1716

1817
@pytest.fixture(scope="module")
1918
def connection(client: AtlanClient):
20-
admin_role_guid = RoleCache.get_id_for_name("$admin")
19+
admin_role_guid = client.role_cache.get_id_for_name("$admin")
2120
assert admin_role_guid
2221
response = client.open_lineage.create_connection(
2322
name=MODULE_NAME, admin_roles=[admin_role_guid]

tests/integration/test_sql_assets.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pytest
77

8-
from pyatlan.cache.role_cache import RoleCache
98
from pyatlan.client.atlan import AtlanClient
109
from pyatlan.model.assets import (
1110
Asset,
@@ -76,7 +75,7 @@ def test_create(
7675
client: AtlanClient,
7776
upsert: Callable[[Asset], AssetMutationResponse],
7877
):
79-
role = RoleCache.get_id_for_name("$admin")
78+
role = client.role_cache.get_id_for_name("$admin")
8079
assert role
8180
connection_name = TestId.make_unique("INT")
8281
c = Connection.create(

tests/unit/model/badge_test.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from unittest.mock import MagicMock
2+
13
import pytest
24

5+
from pyatlan.client.atlan import AtlanClient
36
from pyatlan.model.assets import Badge
47
from pyatlan.model.enums import BadgeComparisonOperator, BadgeConditionColor
58
from pyatlan.model.structs import BadgeCondition
@@ -23,6 +26,33 @@
2326
BADGE_METADATA_ATTRIBUTE = f"{CM_ID}.{CM_ATTR_ID}"
2427

2528

29+
@pytest.fixture(autouse=True)
30+
def set_env(monkeypatch):
31+
monkeypatch.setenv("ATLAN_BASE_URL", "https://test.atlan.com")
32+
monkeypatch.setenv("ATLAN_API_KEY", "test-api-key")
33+
34+
35+
@pytest.fixture()
36+
def client():
37+
return AtlanClient()
38+
39+
40+
@pytest.fixture()
41+
def current_client(client, monkeypatch):
42+
monkeypatch.setattr(
43+
AtlanClient,
44+
"get_current_client",
45+
lambda: client,
46+
)
47+
48+
49+
@pytest.fixture()
50+
def mock_cm_cache(current_client, monkeypatch):
51+
mock_cache = MagicMock()
52+
monkeypatch.setattr(AtlanClient, "custom_metadata_cache", mock_cache)
53+
return mock_cache
54+
55+
2656
@pytest.mark.parametrize(
2757
"name, cm_name, cm_attribute, badge_conditions, message",
2858
[
@@ -47,9 +77,9 @@ def test_create_when_required_parameters_are_missing_raises_value_error(
4777
)
4878

4979

50-
def test_create(mock_custom_metadata_cache):
51-
mock_custom_metadata_cache.get_attr_id_for_name.return_value = CM_ATTR_ID
52-
mock_custom_metadata_cache.get_id_for_name.return_value = CM_ID
80+
def test_create(mock_cm_cache):
81+
mock_cm_cache.get_attr_id_for_name.return_value = CM_ATTR_ID
82+
mock_cm_cache.get_id_for_name.return_value = CM_ID
5383

5484
badge = Badge.create(
5585
name=BADGE_NAME,

tests/unit/test_atlan_tag_name.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pydantic.v1 import parse_obj_as
55

66
import pyatlan.cache.atlan_tag_cache
7+
from pyatlan.client.atlan import AtlanClient
78
from pyatlan.model.assets import Purpose
89
from pyatlan.model.constants import DELETED_
910
from pyatlan.model.core import AtlanTagName
@@ -13,8 +14,30 @@
1314
GOOD_ATLAN_TAG_NAME = "PII"
1415

1516

16-
def test_init_with_bad_atlan_tag_name_raises_value_error(monkeypatch):
17-
def get_id_for_name(_):
17+
@pytest.fixture(autouse=True)
18+
def set_env(monkeypatch):
19+
monkeypatch.setenv("ATLAN_BASE_URL", "https://test.atlan.com")
20+
monkeypatch.setenv("ATLAN_API_KEY", "test-api-key")
21+
22+
23+
@pytest.fixture()
24+
def client():
25+
return AtlanClient()
26+
27+
28+
@pytest.fixture()
29+
def current_client(client, monkeypatch):
30+
monkeypatch.setattr(
31+
AtlanClient,
32+
"get_current_client",
33+
lambda: client,
34+
)
35+
36+
37+
def test_init_with_bad_atlan_tag_name_raises_value_error(
38+
current_client: AtlanClient, monkeypatch
39+
):
40+
def get_id_for_name(_, __):
1841
return None
1942

2043
monkeypatch.setattr(
@@ -29,8 +52,8 @@ def get_id_for_name(_):
2952

3053

3154
@pytest.fixture()
32-
def good_atlan_tag(monkeypatch):
33-
def get_id_for_name(value):
55+
def good_atlan_tag(current_client: AtlanClient, monkeypatch):
56+
def get_id_for_name(_, value):
3457
return ATLAN_TAG_ID
3558

3659
monkeypatch.setattr(
@@ -41,8 +64,8 @@ def get_id_for_name(value):
4164
return AtlanTagName(GOOD_ATLAN_TAG_NAME)
4265

4366

44-
def test_init_with_good_name(monkeypatch):
45-
def get_id_for_name(value):
67+
def test_init_with_good_name(current_client: AtlanClient, monkeypatch):
68+
def get_id_for_name(_, value):
4669
assert value == GOOD_ATLAN_TAG_NAME
4770
return GOOD_ATLAN_TAG_NAME
4871

@@ -60,13 +83,15 @@ def get_id_for_name(value):
6083

6184

6285
def test_convert_to_display_text_when_atlan_tag_passed_returns_same_atlan_tag(
63-
monkeypatch, good_atlan_tag
86+
good_atlan_tag,
6487
):
6588
assert good_atlan_tag is AtlanTagName._convert_to_display_text(good_atlan_tag)
6689

6790

68-
def test_convert_to_display_text_when_bad_string(monkeypatch):
69-
def get_name_for_id(_):
91+
def test_convert_to_display_text_when_bad_string(
92+
current_client: AtlanClient, monkeypatch
93+
):
94+
def get_name_for_id(_, __):
7095
return None
7196

7297
monkeypatch.setattr(
@@ -81,11 +106,11 @@ def get_name_for_id(_):
81106
)
82107

83108

84-
def test_convert_to_display_text_when_id(monkeypatch):
85-
def get_name_for_id(value):
109+
def test_convert_to_display_text_when_id(current_client: AtlanClient, monkeypatch):
110+
def get_name_for_id(_, __):
86111
return GOOD_ATLAN_TAG_NAME
87112

88-
def get_id_for_name(value):
113+
def get_id_for_name(_, value):
89114
assert value == GOOD_ATLAN_TAG_NAME
90115
return GOOD_ATLAN_TAG_NAME
91116

@@ -105,15 +130,15 @@ def get_id_for_name(value):
105130
assert str(sut) == GOOD_ATLAN_TAG_NAME
106131

107132

108-
def test_json_encode_atlan_tag(monkeypatch, good_atlan_tag):
133+
def test_json_encode_atlan_tag(good_atlan_tag):
109134
assert AtlanTagName.json_encode_atlan_tag(good_atlan_tag) == ATLAN_TAG_ID
110135

111136

112-
def test_asset_tag_name_field_deserialization(monkeypatch):
113-
def get_name_for_id(_):
137+
def test_asset_tag_name_field_deserialization(current_client: AtlanClient, monkeypatch):
138+
def get_name_for_id(_, __):
114139
return None
115140

116-
def get_id_for_name(_):
141+
def get_id_for_name(_, __):
117142
return None
118143

119144
monkeypatch.setattr(

0 commit comments

Comments
 (0)