Skip to content

Commit 651f66f

Browse files
committed
Add precommit hook to run tests
1 parent 9fedfa6 commit 651f66f

16 files changed

+169
-116
lines changed

.bandit

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bandit]
2+
exclude = tests,pyatlan/generator

.pre-commit-config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ repos:
2424
hooks:
2525
- id: black
2626
language_version: python3
27+
28+
- repo: local
29+
hooks:
30+
- id: tests
31+
name: run tests
32+
entry: pytest
33+
language: system
34+
types: [python]
35+
stages: [push]

pyatlan/client/entity.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
from pyatlan.client.atlan import AtlanClient
2323
from pyatlan.model.assets import (
2424
Asset,
25-
Referenceable,
25+
AssetMutationResponse,
2626
AtlasGlossary,
2727
AtlasGlossaryCategory,
2828
AtlasGlossaryTerm,
29-
AssetMutationResponse,
29+
Referenceable,
3030
)
3131
from pyatlan.model.core import AssetResponse, BulkRequest
3232
from pyatlan.model.enums import AtlanDeleteType

pyatlan/generator/generate_from_typdefs.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import datetime
12
import json
3+
import os
4+
from pathlib import Path
25
from typing import Any
6+
37
from jinja2 import Environment, PackageLoader
4-
from pathlib import Path
5-
import os
6-
import datetime
7-
from pyatlan.model.typedef import TypeDefResponse, EntityDef
8+
89
from pyatlan.client.atlan import AtlanClient
910
from pyatlan.client.typedef import TypeDefClient
1011
from pyatlan.model.core import to_snake_case
11-
12+
from pyatlan.model.typedef import EntityDef, TypeDefResponse
1213

1314
PARENT = Path(__file__).parent
1415
DICT_BY_STRING = dict[str, Any]

pyatlan/generator/templates/entity.jinja2

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,4 @@ Referenceable.update_forward_refs()
243243
AtlasGlossary.update_forward_refs()
244244
{% for entity_def in entity_defs %}
245245
{{entity_def.name}}.Attributes.update_forward_refs()
246-
{% endfor %}
246+
{% endfor %}

pyatlan/model/core.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
from pydantic import BaseModel, Extra, Field
21
from typing import TYPE_CHECKING
32

3+
from pydantic import BaseModel, Extra, Field
4+
45
if TYPE_CHECKING:
56
from dataclasses import dataclass
67
else:
78
from pydantic.dataclasses import dataclass
8-
from pydantic.generics import GenericModel
9-
from typing import Optional, TypeVar, Generic, Any
10-
from pyatlan.model.enums import EntityStatus, AnnouncementType
9+
1110
from datetime import datetime
11+
from typing import Any, Generic, Optional, TypeVar
12+
13+
from pydantic.generics import GenericModel
14+
15+
from pyatlan.model.enums import AnnouncementType, EntityStatus
1216

1317
CAMEL_CASE_OVERRIDES = {
1418
"IndexTypeEsFields": "IndexTypeESFields",
+59-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import random
33
import string
4-
import random
54

65
import pytest
76
import requests
@@ -14,35 +13,41 @@
1413
from pyatlan.model.enums import AnnouncementType
1514

1615

17-
@pytest.fixture(scope='module')
16+
@pytest.fixture(scope="module")
1817
def client() -> EntityClient:
1918
return EntityClient(AtlanClient())
2019

2120

2221
@pytest.fixture()
2322
def announcement() -> Announcement:
24-
return Announcement(announcement_title="Important Announcement",
25-
announcement_message='A message'.join(random.choices(string.ascii_lowercase, k=20)),
26-
announcement_type=AnnouncementType.ISSUE)
23+
return Announcement(
24+
announcement_title="Important Announcement",
25+
announcement_message="A message".join(
26+
random.choices(string.ascii_lowercase, k=20) # nosec
27+
),
28+
announcement_type=AnnouncementType.ISSUE,
29+
)
2730

2831

29-
@pytest.fixture(scope='session')
32+
@pytest.fixture(scope="session")
3033
def atlan_host() -> str:
31-
return get_environment_variable('ATLAN_HOST')
34+
return get_environment_variable("ATLAN_HOST")
3235

3336

34-
@pytest.fixture(scope='session')
37+
@pytest.fixture(scope="session")
3538
def atlan_api_key() -> str:
36-
return get_environment_variable('ATLAN_API_KEY')
39+
return get_environment_variable("ATLAN_API_KEY")
40+
3741

38-
@pytest.fixture(scope='session')
42+
@pytest.fixture(scope="session")
3943
def headers(atlan_api_key):
40-
return {
41-
'accept': 'application/json, text/plain, */*',
42-
'content-type': 'application/json',
43-
'authorization': f'Bearer {atlan_api_key}',
44+
return {
45+
"accept": "application/json, text/plain, */*",
46+
"content-type": "application/json",
47+
"authorization": f"Bearer {atlan_api_key}",
4448
}
4549

50+
4651
def get_environment_variable(name) -> str:
4752
ret_value = os.environ[name]
4853
assert ret_value
@@ -51,21 +56,23 @@ def get_environment_variable(name) -> str:
5156

5257
@pytest.fixture()
5358
def increment_counter():
54-
i = random.randint(0,1000)
59+
i = random.randint(0, 1000)
60+
5561
def increment():
5662
nonlocal i
5763
i += 1
5864
return i
65+
5966
return increment
6067

6168

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

73+
6674
@pytest.fixture()
6775
def create_glossary(atlan_host, headers, increment_counter):
68-
6976
def create_it():
7077
suffix = increment_counter()
7178
url = f"{atlan_host}/api/meta/entity/bulk"
@@ -78,20 +85,21 @@ def create_it():
7885
"qualifiedName": "",
7986
"certificateStatus": "DRAFT",
8087
"ownersUsers": [],
81-
"ownerGroups": []
88+
"ownerGroups": [],
8289
},
83-
"typeName": "AtlasGlossary"
90+
"typeName": "AtlasGlossary",
8491
}
8592
]
8693
}
8794
response = requests.request("POST", url, headers=headers, json=payload)
8895
response.raise_for_status()
8996
data = response.json()
90-
guid = list(data['guidAssignments'].values())[0]
97+
guid = list(data["guidAssignments"].values())[0]
9198
return guid
9299

93100
return create_it
94101

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

@@ -102,34 +110,16 @@ def get_guids(atlan_host, headers, type_name):
102110
"query": {
103111
"bool": {
104112
"must": [
105-
{
106-
"term": {
107-
"__state": "ACTIVE"
108-
}
109-
},
110-
{
111-
"prefix": {
112-
"name.keyword": {
113-
"value": "Integration"
114-
}
115-
}
116-
}
113+
{"term": {"__state": "ACTIVE"}},
114+
{"prefix": {"name.keyword": {"value": "Integration"}}},
117115
]
118116
}
119117
},
120118
"post_filter": {
121-
"bool": {
122-
"filter": {
123-
"term": {
124-
"__typeName.keyword": type_name
125-
}
126-
}
127-
}
128-
}
119+
"bool": {"filter": {"term": {"__typeName.keyword": type_name}}}
120+
},
129121
},
130-
"attributes": [
131-
"connectorName"
132-
]
122+
"attributes": ["connectorName"],
133123
}
134124

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

139+
149140
def delete_assets(atlan_host, headers, type_name):
150141
for guid in get_guids(atlan_host, headers, type_name):
151142
delete_asset(atlan_host, headers, guid)
143+
144+
152145
@pytest.fixture(autouse=True, scope="module")
153146
def cleanup_categories(atlan_host, headers, atlan_api_key):
154147
delete_assets(atlan_host, headers, "AtlasGlossaryCategory")
155148
yield
156149
delete_assets(atlan_host, headers, "AtlasGlossaryCategory")
157150

151+
158152
@pytest.fixture(autouse=True, scope="module")
159153
def cleanup_glossaries(atlan_host, headers, atlan_api_key):
160154
delete_assets(atlan_host, headers, "AtlasGlossary")
161155
yield
162156
delete_assets(atlan_host, headers, "AtlasGlossary")
163157

164158

165-
166159
def test_get_glossary_by_guid_good_guid(create_glossary, client: EntityClient):
167160
glossary = client.get_entity_by_guid(create_glossary(), AtlasGlossary)
168161
assert isinstance(glossary, AtlasGlossary)
@@ -171,7 +164,10 @@ def test_get_glossary_by_guid_good_guid(create_glossary, client: EntityClient):
171164
def test_get_glossary_by_guid_bad_guid(client: EntityClient):
172165
with pytest.raises(AtlanServiceException) as ex_info:
173166
client.get_entity_by_guid("76d54dd6-925b-499b-a455-6", AtlasGlossary)
174-
assert 'Given instance guid 76d54dd6-925b-499b-a455-6 is invalid/not found' in ex_info.value.args[0]
167+
assert (
168+
"Given instance guid 76d54dd6-925b-499b-a455-6 is invalid/not found"
169+
in ex_info.value.args[0]
170+
)
175171

176172

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

183179

184-
def test_update_glossary_with_changes(create_glossary, client: EntityClient, announcement):
180+
def test_update_glossary_with_changes(
181+
create_glossary, client: EntityClient, announcement
182+
):
185183
glossary = client.get_entity_by_guid(create_glossary(), AtlasGlossary)
186184
glossary.set_announcement(announcement)
187185
response = client.upsert(glossary)
@@ -194,18 +192,20 @@ def test_update_glossary_with_changes(create_glossary, client: EntityClient, ann
194192
assert glossary.attributes.announcement_title == announcement.announcement_title
195193

196194

197-
198195
def test_purge_glossary(create_glossary, client: EntityClient):
199196
response = client.purge_entity_by_guid(create_glossary())
200197
assert len(response.mutated_entities.DELETE) == 1
201198
assert not response.mutated_entities.UPDATE
202199
assert not response.mutated_entities.CREATE
203200

204201

205-
206202
def test_create_glossary(client: EntityClient, increment_counter):
207203
glossary = AtlasGlossary(
208-
attributes=AtlasGlossary.Attributes(name=f"Integration Test Glossary {increment_counter()}", user_description="This a test glossary"))
204+
attributes=AtlasGlossary.Attributes(
205+
name=f"Integration Test Glossary {increment_counter()}",
206+
user_description="This a test glossary",
207+
)
208+
)
209209
response = client.upsert(glossary)
210210
assert not response.mutated_entities.UPDATE
211211
assert len(response.mutated_entities.CREATE) == 1
@@ -217,18 +217,24 @@ def test_create_glossary(client: EntityClient, increment_counter):
217217
def test_create_glossary_category(client: EntityClient, increment_counter):
218218
suffix = increment_counter()
219219
glossary = AtlasGlossary(
220-
attributes=AtlasGlossary.Attributes(name=f"Integration Test Glossary {suffix}", user_description="This a test glossary"))
220+
attributes=AtlasGlossary.Attributes(
221+
name=f"Integration Test Glossary {suffix}",
222+
user_description="This a test glossary",
223+
)
224+
)
221225
response = client.upsert(glossary)
222226
glossary = response.mutated_entities.CREATE[0]
223227
category = AtlasGlossaryCategory(
224-
attributes=AtlasGlossaryCategory.Attributes(name=f"Integration Test Glossary Category {suffix}",
225-
user_description="This is a test glossary category",
226-
anchor=glossary)
228+
attributes=AtlasGlossaryCategory.Attributes(
229+
name=f"Integration Test Glossary Category {suffix}",
230+
user_description="This is a test glossary category",
231+
anchor=glossary,
232+
)
227233
)
228234
response = client.upsert(category)
229235
assert response.mutated_entities.UPDATE
230236
assert len(response.mutated_entities.UPDATE) == 1
231-
assert isinstance( response.mutated_entities.UPDATE[0], AtlasGlossary)
237+
assert isinstance(response.mutated_entities.UPDATE[0], AtlasGlossary)
232238
assert response.mutated_entities.CREATE
233239
assert len(response.mutated_entities.CREATE) == 1
234240
assert isinstance(response.mutated_entities.CREATE[0], AtlasGlossaryCategory)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"guidAssignments": {}
3-
}
3+
}

tests/unit/data/asset_mutated_response_update.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
]
4646
},
4747
"guidAssignments": {}
48-
}
48+
}

tests/unit/data/glossary.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@
140140
}
141141
},
142142
"labels": []
143-
}
143+
}

tests/unit/data/glossary_category.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,4 @@
112112
}
113113
},
114114
"labels": []
115-
}
115+
}

tests/unit/data/glossary_term.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,4 @@
165165
}
166166
},
167167
"labels": []
168-
}
168+
}

tests/unit/data/glossary_term2.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
"relationshipAttributes": {
1010
"typeName": "AtlasGlossaryTermAnchor"
1111
}
12-
}
12+
}

tests/unit/data/typedefs.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)