Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Commit

Permalink
Add precommit hook to run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestoLoma committed Dec 22, 2022
1 parent 9fedfa6 commit 651f66f
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .bandit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
exclude = tests,pyatlan/generator
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ repos:
hooks:
- id: black
language_version: python3

- repo: local
hooks:
- id: tests
name: run tests
entry: pytest
language: system
types: [python]
stages: [push]
4 changes: 2 additions & 2 deletions pyatlan/client/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import (
Asset,
Referenceable,
AssetMutationResponse,
AtlasGlossary,
AtlasGlossaryCategory,
AtlasGlossaryTerm,
AssetMutationResponse,
Referenceable,
)
from pyatlan.model.core import AssetResponse, BulkRequest
from pyatlan.model.enums import AtlanDeleteType
Expand Down
11 changes: 6 additions & 5 deletions pyatlan/generator/generate_from_typdefs.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import datetime
import json
import os
from pathlib import Path
from typing import Any

from jinja2 import Environment, PackageLoader
from pathlib import Path
import os
import datetime
from pyatlan.model.typedef import TypeDefResponse, EntityDef

from pyatlan.client.atlan import AtlanClient
from pyatlan.client.typedef import TypeDefClient
from pyatlan.model.core import to_snake_case

from pyatlan.model.typedef import EntityDef, TypeDefResponse

PARENT = Path(__file__).parent
DICT_BY_STRING = dict[str, Any]
Expand Down
2 changes: 1 addition & 1 deletion pyatlan/generator/templates/entity.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,4 @@ Referenceable.update_forward_refs()
AtlasGlossary.update_forward_refs()
{% for entity_def in entity_defs %}
{{entity_def.name}}.Attributes.update_forward_refs()
{% endfor %}
{% endfor %}
12 changes: 8 additions & 4 deletions pyatlan/model/core.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from pydantic import BaseModel, Extra, Field
from typing import TYPE_CHECKING

from pydantic import BaseModel, Extra, Field

if TYPE_CHECKING:
from dataclasses import dataclass
else:
from pydantic.dataclasses import dataclass
from pydantic.generics import GenericModel
from typing import Optional, TypeVar, Generic, Any
from pyatlan.model.enums import EntityStatus, AnnouncementType

from datetime import datetime
from typing import Any, Generic, Optional, TypeVar

from pydantic.generics import GenericModel

from pyatlan.model.enums import AnnouncementType, EntityStatus

CAMEL_CASE_OVERRIDES = {
"IndexTypeEsFields": "IndexTypeESFields",
Expand Down
112 changes: 59 additions & 53 deletions tests/integration/test_entity_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import random
import string
import random

import pytest
import requests
Expand All @@ -14,35 +13,41 @@
from pyatlan.model.enums import AnnouncementType


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def client() -> EntityClient:
return EntityClient(AtlanClient())


@pytest.fixture()
def announcement() -> Announcement:
return Announcement(announcement_title="Important Announcement",
announcement_message='A message'.join(random.choices(string.ascii_lowercase, k=20)),
announcement_type=AnnouncementType.ISSUE)
return Announcement(
announcement_title="Important Announcement",
announcement_message="A message".join(
random.choices(string.ascii_lowercase, k=20) # nosec
),
announcement_type=AnnouncementType.ISSUE,
)


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def atlan_host() -> str:
return get_environment_variable('ATLAN_HOST')
return get_environment_variable("ATLAN_HOST")


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def atlan_api_key() -> str:
return get_environment_variable('ATLAN_API_KEY')
return get_environment_variable("ATLAN_API_KEY")


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def headers(atlan_api_key):
return {
'accept': 'application/json, text/plain, */*',
'content-type': 'application/json',
'authorization': f'Bearer {atlan_api_key}',
return {
"accept": "application/json, text/plain, */*",
"content-type": "application/json",
"authorization": f"Bearer {atlan_api_key}",
}


def get_environment_variable(name) -> str:
ret_value = os.environ[name]
assert ret_value
Expand All @@ -51,21 +56,23 @@ def get_environment_variable(name) -> str:

@pytest.fixture()
def increment_counter():
i = random.randint(0,1000)
i = random.randint(0, 1000)

def increment():
nonlocal i
i += 1
return i

return increment


@pytest.fixture()
def glossary_guids(atlan_host, headers):
return get_guids(atlan_host, headers, "AtlasGlossary")


@pytest.fixture()
def create_glossary(atlan_host, headers, increment_counter):

def create_it():
suffix = increment_counter()
url = f"{atlan_host}/api/meta/entity/bulk"
Expand All @@ -78,20 +85,21 @@ def create_it():
"qualifiedName": "",
"certificateStatus": "DRAFT",
"ownersUsers": [],
"ownerGroups": []
"ownerGroups": [],
},
"typeName": "AtlasGlossary"
"typeName": "AtlasGlossary",
}
]
}
response = requests.request("POST", url, headers=headers, json=payload)
response.raise_for_status()
data = response.json()
guid = list(data['guidAssignments'].values())[0]
guid = list(data["guidAssignments"].values())[0]
return guid

return create_it


def get_guids(atlan_host, headers, type_name):
url = f"{atlan_host}/api/meta/search/indexsearch"

Expand All @@ -102,34 +110,16 @@ def get_guids(atlan_host, headers, type_name):
"query": {
"bool": {
"must": [
{
"term": {
"__state": "ACTIVE"
}
},
{
"prefix": {
"name.keyword": {
"value": "Integration"
}
}
}
{"term": {"__state": "ACTIVE"}},
{"prefix": {"name.keyword": {"value": "Integration"}}},
]
}
},
"post_filter": {
"bool": {
"filter": {
"term": {
"__typeName.keyword": type_name
}
}
}
}
"bool": {"filter": {"term": {"__typeName.keyword": type_name}}}
},
},
"attributes": [
"connectorName"
]
"attributes": ["connectorName"],
}

response = requests.request("POST", url, headers=headers, json=payload)
Expand All @@ -146,23 +136,26 @@ def delete_asset(atlan_host, headers, guid):
response = requests.delete(url, headers=headers)
response.raise_for_status()


def delete_assets(atlan_host, headers, type_name):
for guid in get_guids(atlan_host, headers, type_name):
delete_asset(atlan_host, headers, guid)


@pytest.fixture(autouse=True, scope="module")
def cleanup_categories(atlan_host, headers, atlan_api_key):
delete_assets(atlan_host, headers, "AtlasGlossaryCategory")
yield
delete_assets(atlan_host, headers, "AtlasGlossaryCategory")


@pytest.fixture(autouse=True, scope="module")
def cleanup_glossaries(atlan_host, headers, atlan_api_key):
delete_assets(atlan_host, headers, "AtlasGlossary")
yield
delete_assets(atlan_host, headers, "AtlasGlossary")



def test_get_glossary_by_guid_good_guid(create_glossary, client: EntityClient):
glossary = client.get_entity_by_guid(create_glossary(), AtlasGlossary)
assert isinstance(glossary, AtlasGlossary)
Expand All @@ -171,7 +164,10 @@ def test_get_glossary_by_guid_good_guid(create_glossary, client: EntityClient):
def test_get_glossary_by_guid_bad_guid(client: EntityClient):
with pytest.raises(AtlanServiceException) as ex_info:
client.get_entity_by_guid("76d54dd6-925b-499b-a455-6", AtlasGlossary)
assert 'Given instance guid 76d54dd6-925b-499b-a455-6 is invalid/not found' in ex_info.value.args[0]
assert (
"Given instance guid 76d54dd6-925b-499b-a455-6 is invalid/not found"
in ex_info.value.args[0]
)


def test_update_glossary_when_no_changes(create_glossary, client: EntityClient):
Expand All @@ -181,7 +177,9 @@ def test_update_glossary_when_no_changes(create_glossary, client: EntityClient):
assert not response.mutated_entities


def test_update_glossary_with_changes(create_glossary, client: EntityClient, announcement):
def test_update_glossary_with_changes(
create_glossary, client: EntityClient, announcement
):
glossary = client.get_entity_by_guid(create_glossary(), AtlasGlossary)
glossary.set_announcement(announcement)
response = client.upsert(glossary)
Expand All @@ -194,18 +192,20 @@ def test_update_glossary_with_changes(create_glossary, client: EntityClient, ann
assert glossary.attributes.announcement_title == announcement.announcement_title



def test_purge_glossary(create_glossary, client: EntityClient):
response = client.purge_entity_by_guid(create_glossary())
assert len(response.mutated_entities.DELETE) == 1
assert not response.mutated_entities.UPDATE
assert not response.mutated_entities.CREATE



def test_create_glossary(client: EntityClient, increment_counter):
glossary = AtlasGlossary(
attributes=AtlasGlossary.Attributes(name=f"Integration Test Glossary {increment_counter()}", user_description="This a test glossary"))
attributes=AtlasGlossary.Attributes(
name=f"Integration Test Glossary {increment_counter()}",
user_description="This a test glossary",
)
)
response = client.upsert(glossary)
assert not response.mutated_entities.UPDATE
assert len(response.mutated_entities.CREATE) == 1
Expand All @@ -217,18 +217,24 @@ def test_create_glossary(client: EntityClient, increment_counter):
def test_create_glossary_category(client: EntityClient, increment_counter):
suffix = increment_counter()
glossary = AtlasGlossary(
attributes=AtlasGlossary.Attributes(name=f"Integration Test Glossary {suffix}", user_description="This a test glossary"))
attributes=AtlasGlossary.Attributes(
name=f"Integration Test Glossary {suffix}",
user_description="This a test glossary",
)
)
response = client.upsert(glossary)
glossary = response.mutated_entities.CREATE[0]
category = AtlasGlossaryCategory(
attributes=AtlasGlossaryCategory.Attributes(name=f"Integration Test Glossary Category {suffix}",
user_description="This is a test glossary category",
anchor=glossary)
attributes=AtlasGlossaryCategory.Attributes(
name=f"Integration Test Glossary Category {suffix}",
user_description="This is a test glossary category",
anchor=glossary,
)
)
response = client.upsert(category)
assert response.mutated_entities.UPDATE
assert len(response.mutated_entities.UPDATE) == 1
assert isinstance( response.mutated_entities.UPDATE[0], AtlasGlossary)
assert isinstance(response.mutated_entities.UPDATE[0], AtlasGlossary)
assert response.mutated_entities.CREATE
assert len(response.mutated_entities.CREATE) == 1
assert isinstance(response.mutated_entities.CREATE[0], AtlasGlossaryCategory)
2 changes: 1 addition & 1 deletion tests/unit/data/asset_mutated_response_empty.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"guidAssignments": {}
}
}
2 changes: 1 addition & 1 deletion tests/unit/data/asset_mutated_response_update.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
]
},
"guidAssignments": {}
}
}
2 changes: 1 addition & 1 deletion tests/unit/data/glossary.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@
}
},
"labels": []
}
}
2 changes: 1 addition & 1 deletion tests/unit/data/glossary_category.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@
}
},
"labels": []
}
}
2 changes: 1 addition & 1 deletion tests/unit/data/glossary_term.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@
}
},
"labels": []
}
}
2 changes: 1 addition & 1 deletion tests/unit/data/glossary_term2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"relationshipAttributes": {
"typeName": "AtlasGlossaryTermAnchor"
}
}
}
2 changes: 1 addition & 1 deletion tests/unit/data/typedefs.json

Large diffs are not rendered by default.

Loading

0 comments on commit 651f66f

Please sign in to comment.