diff --git a/backend/infrahub/core/schema/schema_branch.py b/backend/infrahub/core/schema/schema_branch.py index 97eeb7a7c5..ef219c8766 100644 --- a/backend/infrahub/core/schema/schema_branch.py +++ b/backend/infrahub/core/schema/schema_branch.py @@ -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" diff --git a/backend/tests/unit/core/schema/__init__.py b/backend/tests/unit/core/schema/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/backend/tests/unit/core/schema/schema_branch/__init__.py b/backend/tests/unit/core/schema/schema_branch/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/backend/tests/unit/core/schema/schema_branch/test_schema_uniqueness_constraints.py b/backend/tests/unit/core/schema/schema_branch/test_schema_uniqueness_constraints.py new file mode 100644 index 0000000000..b44524a696 --- /dev/null +++ b/backend/tests/unit/core/schema/schema_branch/test_schema_uniqueness_constraints.py @@ -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) diff --git a/changelog/4677.fixed.md b/changelog/4677.fixed.md new file mode 100644 index 0000000000..6753f02c70 --- /dev/null +++ b/changelog/4677.fixed.md @@ -0,0 +1 @@ +Report user-friendly error for invalid uniqueness_constraints when loading schemas