Skip to content

Commit 2624713

Browse files
committed
Update create convenience method on AtlasGlossaryTerm.Attributes
1 parent e45ce9c commit 2624713

4 files changed

Lines changed: 164 additions & 5 deletions

File tree

pyatlan/generator/templates/entity.jinja2

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ from pyatlan.model.structs import (
5252
)
5353
from pyatlan.utils import next_id
5454

55+
def validate_single_required_field(field_names: list[str], values: list[Any]):
56+
indexes = [idx for idx, value in enumerate(values) if value is not None]
57+
if not indexes:
58+
raise ValueError(
59+
f"One of the following parameters are required: {', '.join(field_names)}"
60+
)
61+
if len(indexes) > 1:
62+
names = [field_names[idx] for idx in indexes]
63+
raise ValueError(
64+
f"Only one of the following parameters are allowed: {', '.join(names)}"
65+
)
66+
5567
def validate_required_fields(field_names:list[str], values:list[Any]):
5668
for field_name, value in zip(field_names, values):
5769
if value is None:
@@ -547,12 +559,27 @@ class {{ entity_def.name }}({{super_classes[0]}} {%- if "Asset" in super_classes
547559
cls,
548560
*,
549561
name: StrictStr,
550-
anchor: AtlasGlossary,
562+
anchor: Optional[AtlasGlossary] = None,
563+
glossary_qualified_name: Optional[StrictStr] = None,
564+
glossary_guid: Optional[StrictStr] = None,
551565
categories: Optional[list[AtlasGlossaryCategory]] = None,
552566
) -> AtlasGlossaryTerm.Attributes:
553-
validate_required_fields(["name", "anchor"], [name, anchor])
567+
validate_required_fields(["name"], [name])
568+
validate_single_required_field(
569+
["anchor", "glossary_qualified_name", "glossary_guid"],
570+
[anchor, glossary_qualified_name, glossary_guid],
571+
)
572+
if glossary_qualified_name:
573+
anchor = AtlasGlossary()
574+
anchor.qualified_name = glossary_qualified_name
575+
if glossary_guid:
576+
anchor = AtlasGlossary()
577+
anchor.guid = glossary_guid
554578
return AtlasGlossaryTerm.Attributes(
555-
name=name, anchor=anchor, categories=categories, qualified_name=next_id()
579+
name=name,
580+
anchor=anchor,
581+
categories=categories,
582+
qualified_name=next_id(),
556583
)
557584
{%- endif %}
558585
attributes: '{{entity_def.name}}.Attributes' = Field(

pyatlan/model/assets.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@
5959
from pyatlan.utils import next_id
6060

6161

62+
def validate_single_required_field(field_names: list[str], values: list[Any]):
63+
indexes = [idx for idx, value in enumerate(values) if value is not None]
64+
if not indexes:
65+
raise ValueError(
66+
f"One of the following parameters are required: {', '.join(field_names)}"
67+
)
68+
if len(indexes) > 1:
69+
names = [field_names[idx] for idx in indexes]
70+
raise ValueError(
71+
f"Only one of the following parameters are allowed: {', '.join(names)}"
72+
)
73+
74+
6275
def validate_required_fields(field_names: list[str], values: list[Any]):
6376
for field_name, value in zip(field_names, values):
6477
if value is None:
@@ -2210,10 +2223,22 @@ def create(
22102223
cls,
22112224
*,
22122225
name: StrictStr,
2213-
anchor: AtlasGlossary,
2226+
anchor: Optional[AtlasGlossary] = None,
2227+
glossary_qualified_name: Optional[StrictStr] = None,
2228+
glossary_guid: Optional[StrictStr] = None,
22142229
categories: Optional[list[AtlasGlossaryCategory]] = None,
22152230
) -> AtlasGlossaryTerm.Attributes:
2216-
validate_required_fields(["name", "anchor"], [name, anchor])
2231+
validate_required_fields(["name"], [name])
2232+
validate_single_required_field(
2233+
["anchor", "glossary_qualified_name", "glossary_guid"],
2234+
[anchor, glossary_qualified_name, glossary_guid],
2235+
)
2236+
if glossary_qualified_name:
2237+
anchor = AtlasGlossary()
2238+
anchor.qualified_name = glossary_qualified_name
2239+
if glossary_guid:
2240+
anchor = AtlasGlossary()
2241+
anchor.guid = glossary_guid
22172242
return AtlasGlossaryTerm.Attributes(
22182243
name=name,
22192244
anchor=anchor,

tests/unit/test_glossary_term.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import pytest
2+
3+
from pyatlan.model.assets import AtlasGlossary, AtlasGlossaryTerm
4+
5+
6+
@pytest.mark.parametrize(
7+
"name, anchor, glossary_qualified_name, glossary_guid, message",
8+
[
9+
(None, None, None, "1234", "name is required"),
10+
(
11+
"Glossary",
12+
None,
13+
None,
14+
None,
15+
"One of the following parameters are required: anchor, glossary_qualified_name, glossary_guid",
16+
),
17+
(
18+
"Glossary",
19+
AtlasGlossary(),
20+
"qname",
21+
None,
22+
"Only one of the following parameters are allowed: anchor, glossary_qualified_name",
23+
),
24+
(
25+
"Glossary",
26+
AtlasGlossary(),
27+
None,
28+
"123",
29+
"Only one of the following parameters are allowed: anchor, glossary_guid",
30+
),
31+
(
32+
"Glossary",
33+
None,
34+
"qname",
35+
"123",
36+
"Only one of the following parameters are allowed: glossary_qualified_name, glossary_guid",
37+
),
38+
],
39+
)
40+
def test_create_atttributes_without_required_parameters_raises_value_error(
41+
name, anchor, glossary_qualified_name, glossary_guid, message
42+
):
43+
with pytest.raises(ValueError, match=message):
44+
AtlasGlossaryTerm.Attributes.create(
45+
name=name,
46+
anchor=anchor,
47+
glossary_qualified_name=glossary_qualified_name,
48+
glossary_guid=glossary_guid,
49+
)
50+
51+
52+
@pytest.mark.parametrize(
53+
"name, anchor, glossary_qualified_name, glossary_guid",
54+
[
55+
("Glossary", AtlasGlossary(), None, None),
56+
("Glossary", None, "glossary/qualifiedName", None),
57+
("Glossary", None, None, "123"),
58+
],
59+
)
60+
def test_create_atttributes_with_required_parameters(
61+
name, anchor, glossary_qualified_name, glossary_guid
62+
):
63+
sut = AtlasGlossaryTerm.Attributes.create(
64+
name=name,
65+
anchor=anchor,
66+
glossary_qualified_name=glossary_qualified_name,
67+
glossary_guid=glossary_guid,
68+
)
69+
70+
if anchor:
71+
assert anchor == sut.anchor
72+
if glossary_qualified_name:
73+
assert sut.anchor.qualified_name == glossary_qualified_name
74+
if glossary_guid:
75+
assert sut.anchor.guid == glossary_guid

tests/unit/test_model.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
Schema,
3838
Table,
3939
View,
40+
validate_single_required_field,
4041
)
4142
from pyatlan.model.core import Announcement, AssetResponse
4243
from pyatlan.model.enums import (
@@ -1320,3 +1321,34 @@ def test_attributes(clazz, property_name, attribute_value):
13201321
local_ns,
13211322
)
13221323
assert attribute_value == local_ns["ret_value"]
1324+
1325+
1326+
@pytest.mark.parametrize(
1327+
"names, values, message",
1328+
[
1329+
(
1330+
("one", "two"),
1331+
(None, None),
1332+
"One of the following parameters are required: one, two",
1333+
),
1334+
(
1335+
("one", "two"),
1336+
(1, 2),
1337+
"Only one of the following parameters are allowed: one, two",
1338+
),
1339+
(
1340+
("one", "two", "three"),
1341+
(1, None, 3),
1342+
"Only one of the following parameters are allowed: one, three",
1343+
),
1344+
],
1345+
)
1346+
def test_validate_single_required_field_with_bad_values_raises_value_error(
1347+
names, values, message
1348+
):
1349+
with pytest.raises(ValueError, match=message):
1350+
validate_single_required_field(names, values)
1351+
1352+
1353+
def test_validate_single_required_field_with_only_one_field_does_not_raise_value_error():
1354+
validate_single_required_field(["One", "Two", "Three"], [None, None, 3])

0 commit comments

Comments
 (0)