Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog/174.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve error message when a single node is passed to a cardinality-many relationship.
12 changes: 10 additions & 2 deletions infrahub_sdk/node/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ def __init__(
RelatedNode(name=name, client=self.client, branch=self.branch, schema=schema, data=item)
)
else:
raise ValueError(f"Unexpected format for {name} found a {type(data)}, {data}")
raise ValueError(
f"Relationship '{name}' expects a list of nodes (cardinality many), "
f"but received a single {type(data).__name__}. "
f"Wrap the value in a list, e.g. {name}=[value]."
)

def __getitem__(self, item: int) -> RelatedNode:
return self.peers[item] # type: ignore[return-value]
Expand Down Expand Up @@ -296,7 +300,11 @@ def __init__(
RelatedNodeSync(name=name, client=self.client, branch=self.branch, schema=schema, data=item)
)
else:
raise ValueError(f"Unexpected format for {name} found a {type(data)}, {data}")
raise ValueError(
f"Relationship '{name}' expects a list of nodes (cardinality many), "
f"but received a single {type(data).__name__}. "
f"Wrap the value in a list, e.g. {name}=[value]."
)

def __getitem__(self, item: int) -> RelatedNodeSync:
return self.peers[item] # type: ignore[return-value]
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/sdk/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,36 @@ async def test_init_node_data_graphql(
assert node.primary_tag.typename == "BuiltinTag"


@pytest.mark.parametrize("client_type", client_types)
async def test_cardinality_many_requires_list(
client: InfrahubClient, location_schema: NodeSchemaAPI, client_type: str
) -> None:
data = {
"name": {"value": "JFK1"},
"tags": {"id": "pppppppp"},
}
with pytest.raises(ValueError, match=r"expects a list of nodes"):
if client_type == "standard":
InfrahubNode(client=client, schema=location_schema, data=data)
else:
InfrahubNodeSync(client=client, schema=location_schema, data=data)


@pytest.mark.parametrize("client_type", client_types)
async def test_cardinality_many_accepts_list(
client: InfrahubClient, location_schema: NodeSchemaAPI, client_type: str
) -> None:
data = {
"name": {"value": "JFK1"},
"tags": [{"id": "aaaaaa"}, {"id": "bbbb"}],
}
if client_type == "standard":
node = InfrahubNode(client=client, schema=location_schema, data=data)
else:
node = InfrahubNodeSync(client=client, schema=location_schema, data=data)
assert len(node.tags.peers) == 2


@pytest.mark.parametrize("client_type", client_types)
async def test_query_data_no_filters_property(
clients: BothClients, location_schema: NodeSchemaAPI, client_type: str
Expand Down