|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 |
2 | 2 | # Copyright 2022 Atlan Pte. Ltd. |
3 | | -from typing import Generator |
| 3 | +import time |
| 4 | +from typing import Generator, Callable |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | 8 | from pyatlan.cache.classification_cache import ClassificationCache |
8 | 9 | from pyatlan.client.atlan import AtlanClient |
| 10 | +from pyatlan.error import AtlanError |
9 | 11 | from pyatlan.model.enums import AtlanClassificationColor, AtlanTypeCategory |
10 | 12 | from pyatlan.model.typedef import ClassificationDef |
11 | 13 |
|
| 14 | +from retry import retry |
| 15 | +import logging |
| 16 | + |
| 17 | +LOGGER = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
12 | 20 | CLS_NAME = "psdk-classification" |
13 | 21 |
|
14 | 22 |
|
15 | | -class ClassificationTest: |
16 | | - @staticmethod |
17 | | - def create_classification(client: AtlanClient, name: str) -> ClassificationDef: |
| 23 | +@pytest.fixture(scope="session", autouse=True) |
| 24 | +def make_classification( |
| 25 | + client: AtlanClient, |
| 26 | +) -> Generator[Callable[[str], ClassificationDef], None, None]: |
| 27 | + created_names = [] |
| 28 | + |
| 29 | + @retry( |
| 30 | + AtlanError, |
| 31 | + delay=1, |
| 32 | + tries=3, |
| 33 | + max_delay=5, |
| 34 | + backoff=2, |
| 35 | + jitter=(0, 1), |
| 36 | + logger=LOGGER, |
| 37 | + ) |
| 38 | + def _wait_for_successful_purge(name: str): |
| 39 | + client.purge_typedef(name) |
| 40 | + |
| 41 | + def _make_classification(name: str) -> ClassificationDef: |
18 | 42 | classification_def = ClassificationDef.create( |
19 | 43 | name=name, color=AtlanClassificationColor.GREEN |
20 | 44 | ) |
21 | | - response = client.create_typedef(classification_def) |
22 | | - assert response |
23 | | - assert len(response.classification_defs) == 1 |
24 | | - created = response.classification_defs[0] |
25 | | - assert created.category == AtlanTypeCategory.CLASSIFICATION |
26 | | - assert created.name |
27 | | - assert created.guid |
28 | | - assert created.name != name |
29 | | - assert created.display_name == name |
30 | | - return created |
31 | | - |
32 | | - @staticmethod |
33 | | - def delete_classification(client: AtlanClient, cls: ClassificationDef) -> None: |
34 | | - client.purge_typedef(cls.name) |
| 45 | + r = client.create_typedef(classification_def) |
| 46 | + c = r.classification_defs[0] |
| 47 | + created_names.append(c.name) |
| 48 | + return c |
35 | 49 |
|
| 50 | + yield _make_classification |
36 | 51 |
|
37 | | -@pytest.fixture(scope="session") |
38 | | -def client() -> AtlanClient: |
39 | | - return AtlanClient() |
| 52 | + for n in created_names: |
| 53 | + try: |
| 54 | + _wait_for_successful_purge(n) |
| 55 | + except AtlanError as err: |
| 56 | + LOGGER.error(err) |
40 | 57 |
|
41 | 58 |
|
42 | 59 | @pytest.fixture(scope="module") |
43 | | -def classification_def(client: AtlanClient) -> Generator[ClassificationDef, None, None]: |
44 | | - c = ClassificationTest.create_classification(client, CLS_NAME) |
45 | | - yield c |
46 | | - ClassificationTest.delete_classification(client, c) |
| 60 | +def classification_def( |
| 61 | + client: AtlanClient, make_classification: Callable[[str], ClassificationDef] |
| 62 | +) -> ClassificationDef: |
| 63 | + return make_classification(CLS_NAME) |
| 64 | + |
| 65 | + |
| 66 | +def test_classification_def(classification_def: ClassificationDef): |
| 67 | + assert classification_def |
| 68 | + assert classification_def.guid |
| 69 | + assert classification_def.display_name == CLS_NAME |
| 70 | + assert classification_def.name != CLS_NAME |
| 71 | + assert classification_def.options |
| 72 | + assert "color" in classification_def.options.keys() |
| 73 | + assert ( |
| 74 | + classification_def.options.get("color") == AtlanClassificationColor.GREEN.value |
| 75 | + ) |
47 | 76 |
|
48 | 77 |
|
49 | | -@pytest.mark.usefixtures("classification_def") |
50 | | -def test_classification_cache(): |
| 78 | +def test_classification_cache(classification_def: ClassificationDef): |
51 | 79 | cls_id = ClassificationCache.get_id_for_name(CLS_NAME) |
52 | 80 | assert cls_id |
| 81 | + assert cls_id == classification_def.name |
53 | 82 | cls_name = ClassificationCache.get_name_for_id(cls_id) |
54 | 83 | assert cls_name |
55 | 84 | assert cls_name == CLS_NAME |
0 commit comments