Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IFC-993 Fix uniqueness constraint with enum attributes #5195

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/infrahub/core/node/constraints/grouped_uniqueness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
ajtmccarty marked this conversation as resolved.
Show resolved Hide resolved
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
):
Expand Down
1 change: 1 addition & 0 deletions changelog/5132.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix uniqueness constraint check with enum based attributes
Loading