Skip to content

Commit

Permalink
one more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtmccarty committed Feb 1, 2025
1 parent 6c11f25 commit a9c68a0
Showing 1 changed file with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from infrahub.core.initialization import create_branch
from infrahub.core.manager import NodeManager
from infrahub.core.node import Node
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.core.validators.uniqueness.model import NodeUniquenessQueryRequest
from infrahub.core.validators.uniqueness.query import NodeUniqueAttributeConstraintQuery
from infrahub.database import InfrahubDatabase
Expand Down Expand Up @@ -425,6 +426,81 @@ async def test_query_relationship_violation_no_attribute(
assert serial_result in expected_result_dicts


async def test_query_relationship_no_violation_same_peer_different_rels(
db: InfrahubDatabase, default_branch: Branch, animal_person_schema: SchemaBranch
):
john = await Node.init(schema="TestPerson", db=db)
await john.new(db=db, name="John", height=175)
await john.save(db=db)
jane = await Node.init(schema="TestPerson", db=db)
await jane.new(db=db, name="Jane", height=165)
await jane.save(db=db)
johns_dog = await Node.init(schema="TestDog", db=db)
await johns_dog.new(db=db, name="J-dog", breed="mixed", owner=john, best_friend=jane)
await johns_dog.save(db=db)
jane_dog = await Node.init(schema="TestDog", db=db)
await jane_dog.new(db=db, name="Jane-dog", breed="mixed", owner=jane, best_friend=jane)
await jane_dog.save(db=db)

branch = await create_branch(db=db, branch_name="branch")
joe = await Node.init(schema="TestPerson", db=db, branch=branch)
await joe.new(db=db, name="Joe", height=175)
await joe.save(db=db)
joes_dog = await Node.init(schema="TestDog", db=db, branch=branch)
await joes_dog.new(db=db, name="Joe-dog", breed="mixed", owner=joe, best_friend=jane)
await joes_dog.save(db=db)

expected_best_friend_result_dicts = [
{
"attr_name": "id",
"node_id": johns_dog.id,
"node_count": 3,
"attr_value": jane.id,
"relationship_identifier": "person__animal_friend",
"deepest_branch_name": default_branch.name,
},
{
"attr_name": "id",
"node_id": jane_dog.id,
"node_count": 3,
"attr_value": jane.id,
"relationship_identifier": "person__animal_friend",
"deepest_branch_name": default_branch.name,
},
{
"attr_name": "id",
"node_id": joes_dog.id,
"node_count": 3,
"attr_value": jane.id,
"relationship_identifier": "person__animal_friend",
"deepest_branch_name": branch.name,
},
]

owner_query = await NodeUniqueAttributeConstraintQuery.init(
db=db,
branch=branch,
query_request=NodeUniquenessQueryRequest(
kind="TestDog", relationship_attribute_paths=[{"identifier": "person__animal", "value": jane.id}]
),
)
owner_query_result = await owner_query.execute(db=db)
assert len(owner_query_result.results) == 0

best_friend_query = await NodeUniqueAttributeConstraintQuery.init(
db=db,
branch=branch,
query_request=NodeUniquenessQueryRequest(
kind="TestDog", relationship_attribute_paths=[{"identifier": "person__animal_friend", "value": jane.id}]
),
)
best_friend_query_result = await best_friend_query.execute(db=db)
assert len(best_friend_query_result.results) == 3
for result in best_friend_query_result.results:
serial_result = dict(result.data)
assert serial_result in expected_best_friend_result_dicts


async def test_query_response_min_count_0_attribute_paths(
db: InfrahubDatabase, car_accord_main, car_prius_main, branch: Branch, default_branch: Branch
):
Expand Down

0 comments on commit a9c68a0

Please sign in to comment.