|
| 1 | +import logging |
| 2 | +from typing import Callable, List, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | + |
| 7 | +from pyatlan.client.atlan import AtlanClient |
| 8 | +from pyatlan.model.assets import Asset, Connection |
| 9 | +from pyatlan.model.enums import AtlanConnectorType |
| 10 | +from pyatlan.model.response import AssetMutationResponse |
| 11 | +from pyatlan.test_utils import TestId |
| 12 | +from pyatlan.test_utils.base_vcr import BaseVCR |
| 13 | + |
| 14 | +LOGGER = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class TestBaseVCR(BaseVCR): |
| 18 | + @pytest.mark.vcr() |
| 19 | + def test_sample_get(self): |
| 20 | + response = requests.get("https://www.google.com/") |
| 21 | + assert response.status_code == 201 |
| 22 | + |
| 23 | + |
| 24 | +class TestConnection(BaseVCR): |
| 25 | + connection: Optional[Connection] = None |
| 26 | + |
| 27 | + @pytest.fixture(scope="module") |
| 28 | + def client(self) -> AtlanClient: |
| 29 | + return AtlanClient() |
| 30 | + |
| 31 | + @pytest.fixture(scope="module") |
| 32 | + def upsert(self, client: AtlanClient): |
| 33 | + guids: List[str] = [] |
| 34 | + |
| 35 | + def _upsert(asset: Asset) -> AssetMutationResponse: |
| 36 | + _response = client.asset.save(asset) |
| 37 | + if ( |
| 38 | + _response |
| 39 | + and _response.mutated_entities |
| 40 | + and _response.mutated_entities.CREATE |
| 41 | + ): |
| 42 | + guids.append(_response.mutated_entities.CREATE[0].guid) |
| 43 | + return _response |
| 44 | + |
| 45 | + yield _upsert |
| 46 | + |
| 47 | + # for guid in reversed(guids): |
| 48 | + # response = client.asset.purge_by_guid(guid) |
| 49 | + # if ( |
| 50 | + # not response |
| 51 | + # or not response.mutated_entities |
| 52 | + # or not response.mutated_entities.DELETE |
| 53 | + # ): |
| 54 | + # LOGGER.error(f"Failed to remove asset with GUID {guid}.") |
| 55 | + |
| 56 | + @pytest.mark.vcr(cassette_name="TestConnectionCreate.json") |
| 57 | + def test_create( |
| 58 | + self, |
| 59 | + client: AtlanClient, |
| 60 | + upsert: Callable[[Asset], AssetMutationResponse], |
| 61 | + ): |
| 62 | + role = client.role_cache.get_id_for_name("$admin") |
| 63 | + assert role |
| 64 | + connection_name = TestId.make_unique("INT") |
| 65 | + c = Connection.create( |
| 66 | + name=connection_name, |
| 67 | + connector_type=AtlanConnectorType.SNOWFLAKE, |
| 68 | + admin_roles=[role], |
| 69 | + ) |
| 70 | + assert c.guid |
| 71 | + response = upsert(c) |
| 72 | + assert response.mutated_entities |
| 73 | + assert response.mutated_entities.CREATE |
| 74 | + assert len(response.mutated_entities.CREATE) == 1 |
| 75 | + assert isinstance(response.mutated_entities.CREATE[0], Connection) |
| 76 | + assert response.guid_assignments |
| 77 | + c = response.mutated_entities.CREATE[0] |
| 78 | + c = client.asset.get_by_guid(c.guid, Connection, ignore_relationships=False) |
| 79 | + assert isinstance(c, Connection) |
| 80 | + TestConnection.connection = c |
| 81 | + |
| 82 | + # @pytest.mark.order(after="test_create") |
| 83 | + # @pytest.mark.vcr() |
| 84 | + # def test_create_for_modification( |
| 85 | + # self, client: AtlanClient, upsert: Callable[[Asset], AssetMutationResponse] |
| 86 | + # ): |
| 87 | + # assert TestConnection.connection |
| 88 | + # assert TestConnection.connection.name |
| 89 | + # connection = TestConnection.connection |
| 90 | + # description = f"{connection.description} more stuff" |
| 91 | + # connection = Connection.create_for_modification( |
| 92 | + # qualified_name=TestConnection.connection.qualified_name or "", |
| 93 | + # name=TestConnection.connection.name, |
| 94 | + # ) |
| 95 | + # connection.description = description |
| 96 | + # response = upsert(connection) |
| 97 | + # verify_asset_updated(response, Connection) |
| 98 | + |
| 99 | + # @pytest.mark.order(after="test_create") |
| 100 | + # @pytest.mark.vcr() |
| 101 | + # def test_trim_to_required( |
| 102 | + # self, client: AtlanClient, upsert: Callable[[Asset], AssetMutationResponse] |
| 103 | + # ): |
| 104 | + # assert TestConnection.connection |
| 105 | + # connection = TestConnection.connection.trim_to_required() |
| 106 | + # response = upsert(connection) |
| 107 | + # assert response.mutated_entities is None |
0 commit comments