Skip to content

Commit

Permalink
Merge pull request #4690 from opsmill/pog-schema-constraint-IFC-795
Browse files Browse the repository at this point in the history
Return friendly error for invalid uniqueness_constraint
  • Loading branch information
ogenstad authored Oct 23, 2024
2 parents 29e35aa + 49d44f8 commit 29d713d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
8 changes: 7 additions & 1 deletion backend/infrahub/core/schema/schema_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,13 @@ def sync_uniqueness_constraints_and_unique_attributes(self) -> None:
if len(constraint_paths) > 1:
continue
constraint_path = constraint_paths[0]
schema_attribute_path = node_schema.parse_schema_path(path=constraint_path, schema=self)
try:
schema_attribute_path = node_schema.parse_schema_path(path=constraint_path, schema=self)
except AttributePathParsingError as exc:
raise ValueError(
f"{node_schema.kind}: Requested unique constraint not found within node. (`{constraint_path}`)"
) from exc

if (
schema_attribute_path.is_type_attribute
and schema_attribute_path.attribute_property_name == "value"
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest

from infrahub.core.schema import AttributeSchema, NodeSchema, SchemaRoot
from infrahub.core.schema.schema_branch import SchemaBranch


@pytest.mark.parametrize(
"schema_root,expected_error",
[
pytest.param(
SchemaRoot(
nodes=[
NodeSchema(
name="Person",
namespace="Testing",
uniqueness_constraints=[["first_name__value"]],
attributes=[
AttributeSchema(
name="name",
kind="Text",
),
AttributeSchema(
name="description",
kind="Text",
optional=True,
),
],
),
],
),
"TestingPerson: Requested unique constraint not found within node. (`first_name__value`)",
id="missing_all",
),
pytest.param(
SchemaRoot(
nodes=[
NodeSchema(
name="Person",
namespace="Testing",
uniqueness_constraints=[
["first_name__value", "last_name__value"],
["origin__value", "family__value"],
],
attributes=[
AttributeSchema(
name="first_name",
kind="Text",
),
AttributeSchema(
name="last_name",
kind="Text",
),
AttributeSchema(
name="origin",
kind="Text",
),
],
),
],
),
"TestingPerson.uniqueness_constraints: family__value is invalid on schema TestingPerson",
id="missing_single",
),
],
)
async def test_schema_protected_generics(schema_root: SchemaRoot, expected_error: str):
schema = SchemaBranch(cache={}, name="test")
schema.load_schema(schema=schema_root)

with pytest.raises(ValueError) as exc:
schema.process_validate()

assert expected_error == str(exc.value)
1 change: 1 addition & 0 deletions changelog/4677.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Report user-friendly error for invalid uniqueness_constraints when loading schemas

0 comments on commit 29d713d

Please sign in to comment.