Skip to content

Commit e36f067

Browse files
committed
[tests] Added unit and integration tests for custom connector type
1 parent 11e04da commit e36f067

File tree

4 files changed

+210
-4
lines changed

4 files changed

+210
-4
lines changed

tests/integration/connection_test.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# Copyright 2022 Atlan Pte. Ltd.
3+
from typing import Generator
4+
35
import pytest
46

57
from pyatlan.client.atlan import AtlanClient
68
from pyatlan.model.assets import Connection
7-
from pyatlan.model.enums import AtlanConnectorType
8-
from tests.integration.client import TestId
9+
from pyatlan.model.enums import AtlanConnectionCategory, AtlanConnectorType
10+
from tests.integration.client import TestId, delete_asset
911

1012
MODULE_NAME = TestId.make_unique("CONN")
1113

@@ -24,6 +26,29 @@ def create_connection(
2426
)
2527

2628

29+
@pytest.fixture(scope="module")
30+
def custom_connection(client: AtlanClient) -> Generator[Connection, None, None]:
31+
CUSTOM_CONNECTOR_TYPE = AtlanConnectorType.CREATE_CUSTOM(
32+
name=f"{MODULE_NAME}_NAME",
33+
value=f"{MODULE_NAME}_type",
34+
category=AtlanConnectionCategory.API,
35+
)
36+
result = create_connection(
37+
client=client, name=MODULE_NAME, connector_type=CUSTOM_CONNECTOR_TYPE
38+
)
39+
yield result
40+
# TODO: proper connection delete workflow
41+
delete_asset(client, guid=result.guid, asset_type=Connection)
42+
43+
44+
def test_custom_connection(custom_connection: Connection):
45+
assert custom_connection.name == MODULE_NAME
46+
assert custom_connection.connector_name == f"{MODULE_NAME}_type"
47+
assert custom_connection.qualified_name
48+
assert f"default/{MODULE_NAME}_type" in custom_connection.qualified_name
49+
assert AtlanConnectorType[f"{MODULE_NAME}_NAME"].value == f"{MODULE_NAME}_type"
50+
51+
2752
def test_invalid_connection(client: AtlanClient):
2853
with pytest.raises(
2954
ValueError, match="One of admin_user, admin_groups or admin_roles is required"

tests/unit/model/a_d_l_s_object_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_create():
134134
),
135135
(
136136
ADLS_OBJECT_NAME,
137-
"default/adls-invalid/production",
137+
"default/adls/invalid/production",
138138
"abc",
139139
ADLS_CONTAINER_NAME,
140140
ADLS_CONTAINER_QUALIFIED_NAME,

tests/unit/model/connection_test.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pyatlan.client.atlan import AtlanClient
77
from pyatlan.client.token import TokenClient
88
from pyatlan.model.assets import Connection
9-
from pyatlan.model.enums import AtlanConnectorType
9+
from pyatlan.model.enums import AtlanConnectionCategory, AtlanConnectorType
1010
from tests.unit.model.constants import CONNECTION_NAME, CONNECTION_QUALIFIED_NAME
1111

1212

@@ -174,6 +174,69 @@ def test_create(
174174
assert sut.admin_roles == set(admin_roles)
175175

176176

177+
@pytest.mark.parametrize(
178+
"name, connector_type, admin_users, admin_groups, admin_roles",
179+
[
180+
(
181+
CONNECTION_NAME,
182+
AtlanConnectorType.CREATE_CUSTOM(
183+
name="FOO", value="foo", category=AtlanConnectionCategory.BI
184+
),
185+
["ernest"],
186+
[],
187+
[],
188+
),
189+
(
190+
CONNECTION_NAME,
191+
AtlanConnectorType.CREATE_CUSTOM(
192+
name="BAR", value="bar", category=AtlanConnectionCategory.API
193+
),
194+
[],
195+
["ernest"],
196+
[],
197+
),
198+
(
199+
CONNECTION_NAME,
200+
AtlanConnectorType.CREATE_CUSTOM(
201+
name="BAZ", value="baz", category=AtlanConnectionCategory.WAREHOUSE
202+
),
203+
[],
204+
[],
205+
["ernest"],
206+
),
207+
],
208+
)
209+
def test_creator_with_custom_type(
210+
name: str,
211+
connector_type: AtlanConnectorType,
212+
admin_users: List[str],
213+
admin_groups: List[str],
214+
admin_roles: List[str],
215+
mock_role_cache,
216+
mock_user_cache,
217+
mock_group_cache,
218+
):
219+
mock_role_cache.validate_idstrs
220+
mock_user_cache.validate_names
221+
mock_group_cache.validate_aliases
222+
223+
sut = Connection.create(
224+
name=name,
225+
connector_type=connector_type,
226+
admin_users=admin_users,
227+
admin_groups=admin_groups,
228+
admin_roles=admin_roles,
229+
)
230+
231+
assert sut.name == name
232+
assert sut.qualified_name
233+
assert sut.qualified_name[:20] == connector_type.to_qualified_name()[:20]
234+
assert sut.connector_name == connector_type.value
235+
assert sut.admin_users == set(admin_users)
236+
assert sut.admin_groups == set(admin_groups)
237+
assert sut.admin_roles == set(admin_roles)
238+
239+
177240
@pytest.mark.parametrize(
178241
"qualified_name, name, message",
179242
[

tests/unit/test_utils.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from pyatlan.errors import InvalidRequestError
8+
from pyatlan.model.enums import AtlanConnectionCategory, AtlanConnectorType
89
from pyatlan.model.utils import construct_object_key
910
from pyatlan.utils import (
1011
ComparisonCategory,
@@ -292,3 +293,120 @@ def test_validate_type_with_valid_values(name, the_type, value):
292293
def test_contruct_object_key(prefix, name, expected_key):
293294
key = construct_object_key(prefix, name)
294295
assert key == expected_key
296+
297+
298+
@pytest.mark.parametrize(
299+
"custom_connectors",
300+
[
301+
[
302+
AtlanConnectorType.CREATE_CUSTOM(
303+
name="FOO", value="foo", category=AtlanConnectionCategory.BI
304+
),
305+
AtlanConnectorType.CREATE_CUSTOM(
306+
name="BAR", value="bar", category=AtlanConnectionCategory.API
307+
),
308+
AtlanConnectorType.CREATE_CUSTOM(
309+
name="BAZ", value="baz", category=AtlanConnectionCategory.WAREHOUSE
310+
),
311+
]
312+
],
313+
)
314+
def test_atlan_connector_type_create_custom(custom_connectors):
315+
for custom_connector in custom_connectors:
316+
assert custom_connector and custom_connector.category
317+
assert custom_connector.value in AtlanConnectorType.get_values()
318+
assert custom_connector.strip() in AtlanConnectorType.get_values()
319+
assert custom_connector.name in AtlanConnectorType.get_names()
320+
assert (
321+
custom_connector.name,
322+
custom_connector.value,
323+
) in AtlanConnectorType.get_items()
324+
325+
326+
@pytest.mark.parametrize(
327+
"custom_asset_qns",
328+
[
329+
[
330+
"default/c1/1234567890/asset/name",
331+
"default/c2/1234567890/asset/name",
332+
"default/c3/1234567890/asset/name",
333+
# Duplicate custom connector names
334+
"default/c1/1234567890/asset/name",
335+
"default/c2/1234567890/asset/name",
336+
"default/c3/1234567890/asset/name",
337+
# Duplicate custom connector names
338+
"default/c1/1234567890/asset/name",
339+
"default/c2/1234567890/asset/name",
340+
"default/c3/1234567890/asset/name",
341+
]
342+
],
343+
)
344+
def test_atlan_connector_type_custom_asset_qn(custom_asset_qns):
345+
# Calculate the unique custom QNs
346+
unique_custom_qns = set(custom_asset_qns)
347+
348+
# Calculate the initial lengths of predefined values, names, and items
349+
len_get_values = len(AtlanConnectorType.get_values())
350+
len_get_names = len(AtlanConnectorType.get_names())
351+
len_get_items = len(AtlanConnectorType.get_items())
352+
353+
# Check each custom QN
354+
for custom_qn in custom_asset_qns:
355+
custom_connector = AtlanConnectorType._get_connector_type_from_qualified_name(
356+
custom_qn
357+
)
358+
custom_connector.category = AtlanConnectionCategory.CUSTOM
359+
assert custom_connector.value in AtlanConnectorType.get_values()
360+
assert custom_connector.strip() in AtlanConnectorType.get_values()
361+
assert (
362+
custom_connector.name,
363+
custom_connector.value,
364+
) in AtlanConnectorType.get_items()
365+
366+
# Ensure the value is only added once (i.e no duplicates)
367+
assert len(AtlanConnectorType.get_values()) == len_get_values + len(
368+
unique_custom_qns
369+
)
370+
assert len(AtlanConnectorType.get_names()) == len_get_names + len(unique_custom_qns)
371+
assert len(AtlanConnectorType.get_items()) == len_get_items + len(unique_custom_qns)
372+
373+
374+
@pytest.mark.parametrize(
375+
"custom_connection_qns",
376+
[
377+
[
378+
"default/cm1/1234567890",
379+
"default/cm2/1234567890",
380+
"default/cm3/1234567890",
381+
# Duplicate custom connector names
382+
"default/cm1/1234567890",
383+
"default/cm2/1234567890",
384+
"default/cm3/1234567890",
385+
# Duplicate custom connector names
386+
"default/cm1/1234567890",
387+
"default/cm2/1234567890",
388+
"default/cm3/1234567890",
389+
]
390+
],
391+
)
392+
def test_atlan_connector_type_custom_connection_qn(custom_connection_qns):
393+
# Calculate the unique custom QNs
394+
unique_custom_qns = set(custom_connection_qns)
395+
396+
# Calculate the initial lengths of predefined values, names, and items
397+
len_get_values = len(AtlanConnectorType.get_values())
398+
len_get_names = len(AtlanConnectorType.get_names())
399+
len_get_items = len(AtlanConnectorType.get_items())
400+
401+
# Check each custom QN
402+
for custom_qn in custom_connection_qns:
403+
custom_connector_name = AtlanConnectorType.get_connector_name(custom_qn)
404+
assert custom_connector_name in AtlanConnectorType.get_values()
405+
assert custom_connector_name.strip() in AtlanConnectorType.get_values()
406+
407+
# Ensure the value is only added once (i.e no duplicates)
408+
assert len(AtlanConnectorType.get_values()) == len_get_values + len(
409+
unique_custom_qns
410+
)
411+
assert len(AtlanConnectorType.get_names()) == len_get_names + len(unique_custom_qns)
412+
assert len(AtlanConnectorType.get_items()) == len_get_items + len(unique_custom_qns)

0 commit comments

Comments
 (0)