From baa33b91f082345a9100daf96cec98b10c903df6 Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Wed, 11 Dec 2024 23:42:52 +0100 Subject: [PATCH 1/3] Fix uniqueness constraint with enum attributes The actual value of the enum member was not checked. Instead a string representation of the member was used, which was causing the database query to never match any nodes. --- backend/infrahub/core/node/constraints/grouped_uniqueness.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/infrahub/core/node/constraints/grouped_uniqueness.py b/backend/infrahub/core/node/constraints/grouped_uniqueness.py index 76f6d64c98..8558f3272b 100644 --- a/backend/infrahub/core/node/constraints/grouped_uniqueness.py +++ b/backend/infrahub/core/node/constraints/grouped_uniqueness.py @@ -97,7 +97,10 @@ async def _get_node_attribute_path_values( attribute_field = getattr(updated_node, attribute_name) attribute_value = getattr(attribute_field, schema_attribute_path.attribute_property_name or "value") node_value_combination.append( - SchemaAttributePathValue.from_schema_attribute_path(schema_attribute_path, value=attribute_value) + SchemaAttributePathValue.from_schema_attribute_path( + schema_attribute_path, + value=attribute_value.value if attribute_field.is_enum else attribute_value, + ) ) return node_value_combination From fb26b6fa7c7187375c9962b10d50f71581fe6dfe Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Thu, 12 Dec 2024 14:54:01 +0100 Subject: [PATCH 2/3] Add tests to validate fix --- .../test_node_grouped_uniqueness.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/backend/tests/unit/core/constraint_validators/test_node_grouped_uniqueness.py b/backend/tests/unit/core/constraint_validators/test_node_grouped_uniqueness.py index 0ba788198c..bbd2cbeed0 100644 --- a/backend/tests/unit/core/constraint_validators/test_node_grouped_uniqueness.py +++ b/backend/tests/unit/core/constraint_validators/test_node_grouped_uniqueness.py @@ -75,6 +75,41 @@ async def test_uniqueness_constraint_conflict_two_attribute( with pytest.raises(ValidationError, match="Violates uniqueness constraint 'name-color'"): await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main) + async def test_uniqueness_constraint_no_conflict_attribute_enum( + self, + db: InfrahubDatabase, + default_branch: Branch, + car_accord_main: Node, + car_camry_main: Node, + car_volt_main: Node, + ): + car_schema = registry.schema.get("TestCar", branch=default_branch, duplicate=False) + car_schema.uniqueness_constraints = [["nbr_seats__value", "name__value"]] + attr = car_schema.get_attribute("nbr_seats") + attr.optional = False + attr.enum = [2, 4, 5, 7] + + await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main) + + async def test_uniqueness_constraint_conflict_attribute_enum( + self, + db: InfrahubDatabase, + default_branch: Branch, + car_accord_main: Node, + car_camry_main: Node, + car_volt_main: Node, + ): + car_schema = registry.schema.get("TestCar", branch=default_branch, duplicate=False) + attr = car_schema.get_attribute("nbr_seats") + attr.optional = False + attr.enum = [2, 4, 5, 7] + + car_accord_main.name.value = "camry" + car_accord_main.get_schema().uniqueness_constraints = [["nbr_seats__value", "name__value"]] + + with pytest.raises(ValidationError, match="Violates uniqueness constraint 'nbr_seats-name'"): + await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main) + async def test_uniqueness_constraint_no_conflict_one_relationship( self, db: InfrahubDatabase, default_branch: Branch, car_person_generics_data_simple ): From 5acf443df1f89ed6d988c840c49100971cb4770a Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Thu, 12 Dec 2024 15:06:43 +0100 Subject: [PATCH 3/3] Add changelog entry --- changelog/5132.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/5132.fixed.md diff --git a/changelog/5132.fixed.md b/changelog/5132.fixed.md new file mode 100644 index 0000000000..692ac80f43 --- /dev/null +++ b/changelog/5132.fixed.md @@ -0,0 +1 @@ +Fix uniqueness constraint check with enum based attributes \ No newline at end of file