Skip to content

Commit 0988b2b

Browse files
authored
Merge pull request #292 from opsmill/lgu-set-generic-rel-with-hfid
Allow useof HFID to create a related node on generic relationship
2 parents 48ffeba + 6901418 commit 0988b2b

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

infrahub_sdk/node.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def __init__(self, branch: str, schema: RelationshipSchemaAPI, data: Any | dict,
187187
if node_data:
188188
self._id = node_data.get("id", None)
189189
self._hfid = node_data.get("hfid", None)
190+
self._kind = node_data.get("kind", None)
190191
self._display_label = node_data.get("display_label", None)
191192
self._typename = node_data.get("__typename", None)
192193

@@ -255,6 +256,8 @@ def _generate_input_data(self, allocate_from_pool: bool = False) -> dict[str, An
255256
data["id"] = self.id
256257
elif self.hfid is not None:
257258
data["hfid"] = self.hfid
259+
if self._kind is not None:
260+
data["kind"] = self._kind
258261

259262
for prop_name in self._properties:
260263
if getattr(self, prop_name) is not None:

infrahub_sdk/testing/schemas/animal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def schema_cat(self) -> NodeSchema:
8080
namespace=NAMESPACE,
8181
include_in_menu=True,
8282
inherit_from=[TESTING_ANIMAL],
83+
human_friendly_id=["owner__name__value", "name__value", "color__value"],
8384
display_labels=["name__value", "breed__value", "color__value"],
8485
order_by=["name__value"],
8586
attributes=[
@@ -108,6 +109,14 @@ def schema_person(self) -> NodeSchema:
108109
identifier="person__animal",
109110
cardinality="many",
110111
direction=RelationshipDirection.INBOUND,
112+
max_count=10,
113+
),
114+
Rel(
115+
name="favorite_animal",
116+
peer=TESTING_ANIMAL,
117+
identifier="favorite_animal",
118+
cardinality="one",
119+
direction=RelationshipDirection.INBOUND,
111120
),
112121
Rel(
113122
name="best_friends",

tests/integration/test_infrahub_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ async def test_count_with_filter(self, client: InfrahubClient, base_dataset):
146146
count = await client.count(kind=TESTING_PERSON, name__values=["Liam Walker", "Ethan Carter"])
147147
assert count == 2
148148

149+
async def test_create_generic_rel_with_hfid(
150+
self, client: InfrahubClient, base_dataset, cat_luna, person_sophia, schema_animal, schema_cat
151+
):
152+
# See https://github.com/opsmill/infrahub-sdk-python/issues/277
153+
assert (
154+
schema_animal.human_friendly_id != schema_cat.human_friendly_id
155+
), "Inherited node schema should have a different hfid than generic one for this test to be relevant"
156+
person_sophia.favorite_animal = {"hfid": cat_luna.hfid, "kind": TESTING_CAT}
157+
await person_sophia.save()
158+
person_sophia = await client.get(kind=TESTING_PERSON, id=person_sophia.id, prefetch_relationships=True)
159+
assert person_sophia.favorite_animal.id == cat_luna.id
160+
149161
# async def test_get_generic_filter_source(self, client: InfrahubClient, base_dataset):
150162
# admin = await client.get(kind="CoreAccount", name__value="admin")
151163

0 commit comments

Comments
 (0)