From 6901418fa3e09273de942632275c15fd3ae7210e Mon Sep 17 00:00:00 2001 From: Lucas Guillermou Date: Mon, 3 Mar 2025 11:24:59 +0100 Subject: [PATCH] Allow useof HFID to create a related node on generic relationship --- infrahub_sdk/node.py | 3 +++ infrahub_sdk/testing/schemas/animal.py | 9 +++++++++ tests/integration/test_infrahub_client.py | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/infrahub_sdk/node.py b/infrahub_sdk/node.py index f34a7b5a..fde6eb93 100644 --- a/infrahub_sdk/node.py +++ b/infrahub_sdk/node.py @@ -187,6 +187,7 @@ def __init__(self, branch: str, schema: RelationshipSchemaAPI, data: Any | dict, if node_data: self._id = node_data.get("id", None) self._hfid = node_data.get("hfid", None) + self._kind = node_data.get("kind", None) self._display_label = node_data.get("display_label", None) self._typename = node_data.get("__typename", None) @@ -255,6 +256,8 @@ def _generate_input_data(self, allocate_from_pool: bool = False) -> dict[str, An data["id"] = self.id elif self.hfid is not None: data["hfid"] = self.hfid + if self._kind is not None: + data["kind"] = self._kind for prop_name in self._properties: if getattr(self, prop_name) is not None: diff --git a/infrahub_sdk/testing/schemas/animal.py b/infrahub_sdk/testing/schemas/animal.py index 1210a31a..b0e288d3 100644 --- a/infrahub_sdk/testing/schemas/animal.py +++ b/infrahub_sdk/testing/schemas/animal.py @@ -80,6 +80,7 @@ def schema_cat(self) -> NodeSchema: namespace=NAMESPACE, include_in_menu=True, inherit_from=[TESTING_ANIMAL], + human_friendly_id=["owner__name__value", "name__value", "color__value"], display_labels=["name__value", "breed__value", "color__value"], order_by=["name__value"], attributes=[ @@ -108,6 +109,14 @@ def schema_person(self) -> NodeSchema: identifier="person__animal", cardinality="many", direction=RelationshipDirection.INBOUND, + max_count=10, + ), + Rel( + name="favorite_animal", + peer=TESTING_ANIMAL, + identifier="favorite_animal", + cardinality="one", + direction=RelationshipDirection.INBOUND, ), Rel( name="best_friends", diff --git a/tests/integration/test_infrahub_client.py b/tests/integration/test_infrahub_client.py index 817205ec..41ff5a14 100644 --- a/tests/integration/test_infrahub_client.py +++ b/tests/integration/test_infrahub_client.py @@ -146,6 +146,18 @@ async def test_count_with_filter(self, client: InfrahubClient, base_dataset): count = await client.count(kind=TESTING_PERSON, name__values=["Liam Walker", "Ethan Carter"]) assert count == 2 + async def test_create_generic_rel_with_hfid( + self, client: InfrahubClient, base_dataset, cat_luna, person_sophia, schema_animal, schema_cat + ): + # See https://github.com/opsmill/infrahub-sdk-python/issues/277 + assert ( + schema_animal.human_friendly_id != schema_cat.human_friendly_id + ), "Inherited node schema should have a different hfid than generic one for this test to be relevant" + person_sophia.favorite_animal = {"hfid": cat_luna.hfid, "kind": TESTING_CAT} + await person_sophia.save() + person_sophia = await client.get(kind=TESTING_PERSON, id=person_sophia.id, prefetch_relationships=True) + assert person_sophia.favorite_animal.id == cat_luna.id + # async def test_get_generic_filter_source(self, client: InfrahubClient, base_dataset): # admin = await client.get(kind="CoreAccount", name__value="admin")