Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Jan 3, 2025
1 parent 80e9275 commit 28fa739
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
8 changes: 7 additions & 1 deletion backend/infrahub/core/node/constraints/grouped_uniqueness.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ async def _check_one_schema(
at: Optional[Timestamp] = None,
filters: Optional[list[str]] = None,
) -> None:
"""
Raises a `ValidationError` if database contains at least one node having same `uniqueness_constraint` attributes
than input `node` ones.
"""

schema_branch = self.db.schema.get_schema_branch(name=self.branch.name)
path_groups = node_schema.get_unique_constraint_schema_attribute_paths(schema_branch=schema_branch)

Expand All @@ -158,7 +163,7 @@ async def _check_one_schema(
updated_node=node, node_schema=node_schema, path_groups=[path_group], filters=filters
)

if not query_request:
if query_request.is_empty():
continue

if query_request.has_attributes_only:
Expand All @@ -179,6 +184,7 @@ async def _check_one_schema(
await get_node_query.execute(db=self.db)
node_ids = get_node_query.get_node_ids()

# Node may or may not already exist in the database.
if (node.id in node_ids and len(node_ids) > 1) or (node.id not in node_ids and len(node_ids) > 0):
raise ValidationError(query_request.get_error_message())

Expand Down
2 changes: 1 addition & 1 deletion backend/infrahub/core/validators/uniqueness/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def check_one_schema(
) -> list[NonUniqueNode]:
query_request = await self.build_query_request(schema)

if not query_request:
if query_request.is_empty():
return []

query = await NodeUniqueAttributeConstraintQuery.init(
Expand Down
11 changes: 7 additions & 4 deletions backend/infrahub/core/validators/uniqueness/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class NodeUniquenessQueryRequest(BaseModel):
unique_attribute_paths: set[QueryAttributePath] = Field(default_factory=set)
relationship_attribute_paths: set[QueryRelationshipAttributePath] = Field(default_factory=set)

def is_empty(self) -> bool:
return not self.unique_attribute_paths and not self.relationship_attribute_paths

def __bool__(self) -> bool:
if self.unique_attribute_paths or self.relationship_attribute_paths:
return True
return False
raise ValueError("Test to see if this function is used within CI tests, otherwise will remove it")

def __str__(self) -> str:
return (
Expand All @@ -65,7 +66,9 @@ def has_attributes_only(self) -> bool:
return True

def get_error_message(self) -> str:
uniqueness_constaints = [f"{attr.attribute_name}__{attr.property_name}" for attr in self.unique_attribute_paths]
uniqueness_constaints = sorted(
[f"{attr.attribute_name}__{attr.property_name}" for attr in self.unique_attribute_paths]
)
return f"Violates uniqueness constraint '{uniqueness_constaints}'"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def build(cls, context: DependencyBuilderContext) -> NodeConstraintRunner:
db=context.db,
branch=context.branch,
node_constraints=[
NodeAttributeUniquenessConstraintDependency.build(context=context), # NOTE we probably don't need this Checker, it's redundant with NodeGroupedUniquenessConstraintDependency
NodeGroupedUniquenessConstraintDependency.build(context=context),
],
relationship_manager_constraints=[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pytest

from infrahub.core import registry
Expand Down Expand Up @@ -31,7 +33,7 @@ async def test_uniqueness_constraint_conflict_attribute(
car_accord_main.name.value = "camry"
car_accord_main.get_schema().uniqueness_constraints = [["name__value"]]

with pytest.raises(ValidationError, match="Violates uniqueness constraint 'name'"):
with pytest.raises(ValidationError, match=re.escape("Violates uniqueness constraint '['name__value']'")):
await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main)

async def test_uniqueness_constraint_filters(
Expand Down Expand Up @@ -66,14 +68,34 @@ async def test_uniqueness_constraint_conflict_two_attribute(
car_camry_main: Node,
car_volt_main: Node,
):
car_accord_main.name.value = "camry"
car_accord_main.color.value = "#123456"
car_accord_main.name.value = car_camry_main.name.value
car_accord_main.get_schema().uniqueness_constraints = [
["name__value", "color__value"],
["nbr_seats__value", "name__value"],
]

with pytest.raises(ValidationError, match="Violates uniqueness constraint 'name-color'"):
with pytest.raises(
ValidationError, match=re.escape("Violates uniqueness constraint '['color__value', 'name__value']'")
):
await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main)

async def test_uniqueness_constraint_conflict_multiple_constraints(
self,
db: InfrahubDatabase,
default_branch: Branch,
car_accord_main: Node,
car_camry_main: Node,
car_volt_main: Node,
):
car_accord_main.nbr_seats.value = car_camry_main.nbr_seats.value
car_accord_main.color.value = car_camry_main.color.value
car_accord_main.get_schema().uniqueness_constraints = [
["name__value", "color__value"],
["nbr_seats__value", "color__value"],
]

with pytest.raises(
ValidationError, match=re.escape("Violates uniqueness constraint '['color__value', 'nbr_seats__value']'")
):
await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main)

async def test_uniqueness_constraint_no_conflict_attribute_enum(
Expand Down Expand Up @@ -108,7 +130,7 @@ async def test_uniqueness_constraint_conflict_attribute_enum(
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'"):
with pytest.raises(ValidationError, match="Violates uniqueness constraint ['name__value', 'nbr_seats__value']"):
await self.__call_system_under_test(db=db, branch=default_branch, node=car_accord_main)

async def test_uniqueness_constraint_no_conflict_one_relationship(
Expand Down

0 comments on commit 28fa739

Please sign in to comment.