Skip to content

Commit 0f109b6

Browse files
committed
Require attribute NumberPool to be mandatory and read-only
1 parent d779c98 commit 0f109b6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

backend/infrahub/core/schema/schema_branch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,14 @@ def validate_attribute_parameters(self) -> None:
10241024
def _validate_number_pool_parameters(
10251025
self, node_schema: NodeSchema, attribute: AttributeSchema, number_pool_parameters: NumberPoolParameters
10261026
) -> None:
1027+
if attribute.optional:
1028+
raise ValidationError(f"{node_schema.kind}.{attribute.name} is a NumberPool it can't be optional")
1029+
1030+
if not attribute.read_only:
1031+
raise ValidationError(
1032+
f"{node_schema.kind}.{attribute.name} is a NumberPool it has to be a read_only attribute"
1033+
)
1034+
10271035
if attribute.inherited:
10281036
generics_with_attribute = []
10291037
for generic_name in node_schema.inherit_from:

backend/tests/unit/core/schema/test_attribute_parameters.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,60 @@ def test_number_pool_invalid_range() -> None:
5858
NodeSchema(**node_schema)
5959

6060

61+
def test_number_pool_optional() -> None:
62+
node_schema_definition: dict[str, Any] = {
63+
"name": "NumberAttribute",
64+
"namespace": "Test",
65+
"attributes": [
66+
{"name": "name", "kind": "Text", "unique": True},
67+
{
68+
"name": "assigned_number",
69+
"kind": "NumberPool",
70+
"optional": True,
71+
"unique": True,
72+
"read_only": True,
73+
"parameters": {"start_range": 10, "end_range": 25},
74+
},
75+
],
76+
}
77+
node_schema = NodeSchema(**node_schema_definition)
78+
79+
schema = SchemaRoot(nodes=[node_schema])
80+
schema_branch = SchemaBranch(cache={})
81+
schema_branch.load_schema(schema=schema)
82+
with pytest.raises(
83+
ValidationError, match="TestNumberAttribute.assigned_number is a NumberPool it can't be optional"
84+
):
85+
schema_branch.process()
86+
87+
88+
def test_number_pool_read_only() -> None:
89+
node_schema_definition: dict[str, Any] = {
90+
"name": "NumberAttribute",
91+
"namespace": "Test",
92+
"attributes": [
93+
{"name": "name", "kind": "Text", "unique": True},
94+
{
95+
"name": "assigned_number",
96+
"kind": "NumberPool",
97+
"optional": False,
98+
"unique": True,
99+
"read_only": False,
100+
"parameters": {"start_range": 10, "end_range": 25},
101+
},
102+
],
103+
}
104+
node_schema = NodeSchema(**node_schema_definition)
105+
106+
schema = SchemaRoot(nodes=[node_schema])
107+
schema_branch = SchemaBranch(cache={})
108+
schema_branch.load_schema(schema=schema)
109+
with pytest.raises(
110+
ValidationError, match="TestNumberAttribute.assigned_number is a NumberPool it has to be a read_only attribute"
111+
):
112+
schema_branch.process()
113+
114+
61115
def test_number_pool_assign_from_generics() -> None:
62116
schema = SchemaRoot(generics=[SNOW_TASK], nodes=[SNOW_INCIDENT, SNOW_REQUEST])
63117
schema_branch = SchemaBranch(cache={})

0 commit comments

Comments
 (0)