Skip to content

Commit 8d97d4a

Browse files
committed
Add integartion tests for metadata
1 parent 2f0ce37 commit 8d97d4a

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

tests/integration/test_infrahub_client.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from infrahub_sdk.constants import InfrahubClientMode
1212
from infrahub_sdk.exceptions import BranchNotFoundError, URLNotFoundError
1313
from infrahub_sdk.node import InfrahubNode
14+
from infrahub_sdk.node.metadata import NodeMetadata, RelationshipMetadata
1415
from infrahub_sdk.playback import JSONPlayback
1516
from infrahub_sdk.recorder import JSONRecorder
1617
from infrahub_sdk.schema import GenericSchema, NodeSchema, ProfileSchemaAPI
@@ -328,6 +329,83 @@ async def create_person_with_tags(clt: InfrahubClient, nbr_tags: int) -> None:
328329
)
329330
assert len(group.members.peers) == 2
330331

332+
async def test_node_metadata_not_fetched_by_default(
333+
self, client: InfrahubClient, base_dataset: None, cat_luna: InfrahubNode
334+
) -> None:
335+
node = await client.get(kind=TESTING_CAT, id=cat_luna.id)
336+
assert node.get_node_metadata() is None
337+
338+
async def test_node_metadata_with_get(
339+
self, client: InfrahubClient, base_dataset: None, cat_luna: InfrahubNode
340+
) -> None:
341+
node = await client.get(kind=TESTING_CAT, id=cat_luna.id, include_metadata=True)
342+
343+
metadata = node.get_node_metadata()
344+
assert isinstance(metadata, NodeMetadata)
345+
assert metadata.created_at is not None
346+
assert metadata.updated_at is not None
347+
assert metadata.created_by is not None
348+
assert metadata.created_by.display_label is not None
349+
assert metadata.updated_by is not None
350+
assert metadata.updated_by.display_label is not None
351+
352+
async def test_node_metadata_with_all(self, client: InfrahubClient, base_dataset: None) -> None:
353+
nodes = await client.all(kind=TESTING_CAT, include_metadata=True)
354+
assert nodes
355+
356+
for node in nodes:
357+
metadata = node.get_node_metadata()
358+
assert isinstance(metadata, NodeMetadata)
359+
assert metadata.created_at is not None
360+
assert metadata.updated_at is not None
361+
assert metadata.created_by is not None
362+
assert metadata.updated_by is not None
363+
364+
async def test_attribute_metadata(self, client: InfrahubClient, base_dataset: None, cat_luna: InfrahubNode) -> None:
365+
node = await client.get(kind=TESTING_CAT, id=cat_luna.id, include_metadata=True)
366+
367+
assert node.name.updated_at is not None
368+
assert node.name.updated_by is not None
369+
assert node.name.updated_by.display_label is not None
370+
assert node.breed.updated_at is not None
371+
assert node.breed.updated_by is not None
372+
373+
async def test_relationship_metadata_cardinality_one(
374+
self, client: InfrahubClient, base_dataset: None, cat_luna: InfrahubNode
375+
) -> None:
376+
node = await client.get(kind=TESTING_CAT, id=cat_luna.id, include_metadata=True)
377+
378+
rel_metadata = node.owner.get_relationship_metadata()
379+
assert isinstance(rel_metadata, RelationshipMetadata)
380+
assert rel_metadata.updated_at is not None
381+
assert rel_metadata.updated_by is not None
382+
assert rel_metadata.updated_by.display_label is not None
383+
384+
async def test_relationship_metadata_cardinality_many(
385+
self, client: InfrahubClient, base_dataset: None, person_ethan: InfrahubNode
386+
) -> None:
387+
# Use include=["animals"] rather than prefetch_relationships=True.
388+
# prefetch_relationships=True recursively fetches each animal's peer relationships,
389+
# which in turn include person_ethan's null favorite_animal cardinality-one relationship.
390+
# The server GraphQL schema marks NestedEdgedTestingAnimal.node_metadata as non-nullable,
391+
# so requesting include_metadata=True on that null edge causes the server to silently
392+
# return empty edges. include=["animals"] fetches only the animals relationship shallowly.
393+
# Also exclude favorite_animal (null cardinality-one inbound) for the same reason.
394+
node = await client.get(
395+
kind=TESTING_PERSON,
396+
id=person_ethan.id,
397+
include_metadata=True,
398+
include=["animals"],
399+
exclude=["favorite_animal"],
400+
)
401+
402+
assert node.animals.peers
403+
for peer in node.animals.peers:
404+
rel_metadata = peer.get_relationship_metadata()
405+
assert isinstance(rel_metadata, RelationshipMetadata)
406+
assert rel_metadata.updated_at is not None
407+
assert rel_metadata.updated_by is not None
408+
331409
@pytest.mark.xfail(reason="https://github.com/opsmill/infrahub-sdk-python/issues/733")
332410
async def test_recorder_with_playback_rewrite_host(
333411
self, base_dataset: None, tmp_path: Path, infrahub_port: int

tests/integration/test_infrahub_client_sync.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from infrahub_sdk.constants import InfrahubClientMode
1111
from infrahub_sdk.exceptions import BranchNotFoundError, URLNotFoundError
1212
from infrahub_sdk.node import InfrahubNodeSync
13+
from infrahub_sdk.node.metadata import NodeMetadata, RelationshipMetadata
1314
from infrahub_sdk.playback import JSONPlayback
1415
from infrahub_sdk.recorder import JSONRecorder
1516
from infrahub_sdk.schema import GenericSchema, NodeSchema, ProfileSchemaAPI
@@ -330,6 +331,68 @@ def create_person_with_tags(clt: InfrahubClientSync, nbr_tags: int) -> None:
330331
)
331332
assert len(group.members.peers) == 2
332333

334+
def test_node_metadata_not_fetched_by_default(
335+
self, client_sync: InfrahubClientSync, base_dataset: None, cat_luna: InfrahubNode
336+
) -> None:
337+
node = client_sync.get(kind=TESTING_CAT, id=cat_luna.id)
338+
assert node.get_node_metadata() is None
339+
340+
def test_node_metadata_with_get(
341+
self, client_sync: InfrahubClientSync, base_dataset: None, cat_luna: InfrahubNode
342+
) -> None:
343+
node = client_sync.get(kind=TESTING_CAT, id=cat_luna.id, include_metadata=True)
344+
345+
metadata = node.get_node_metadata()
346+
assert isinstance(metadata, NodeMetadata)
347+
assert metadata.created_at is not None
348+
assert metadata.updated_at is not None
349+
assert metadata.created_by is not None
350+
assert metadata.created_by.display_label is not None
351+
assert metadata.updated_by is not None
352+
assert metadata.updated_by.display_label is not None
353+
354+
def test_attribute_metadata(
355+
self, client_sync: InfrahubClientSync, base_dataset: None, cat_luna: InfrahubNode
356+
) -> None:
357+
node = client_sync.get(kind=TESTING_CAT, id=cat_luna.id, include_metadata=True)
358+
359+
assert node.name.updated_at is not None
360+
assert node.name.updated_by is not None
361+
assert node.name.updated_by.display_label is not None
362+
assert node.breed.updated_at is not None
363+
assert node.breed.updated_by is not None
364+
365+
def test_relationship_metadata_cardinality_one(
366+
self, client_sync: InfrahubClientSync, base_dataset: None, cat_luna: InfrahubNode
367+
) -> None:
368+
node = client_sync.get(kind=TESTING_CAT, id=cat_luna.id, include_metadata=True)
369+
370+
rel_metadata = node.owner.get_relationship_metadata()
371+
assert isinstance(rel_metadata, RelationshipMetadata)
372+
assert rel_metadata.updated_at is not None
373+
assert rel_metadata.updated_by is not None
374+
assert rel_metadata.updated_by.display_label is not None
375+
376+
def test_relationship_metadata_cardinality_many(
377+
self, client_sync: InfrahubClientSync, base_dataset: None, person_ethan: InfrahubNode
378+
) -> None:
379+
# Use include=["animals"] rather than prefetch_relationships=True — see async counterpart
380+
# in test_infrahub_client.py for full explanation.
381+
node = client_sync.get(
382+
kind=TESTING_PERSON,
383+
id=person_ethan.id,
384+
include_metadata=True,
385+
include=["animals"],
386+
exclude=["favorite_animal"],
387+
)
388+
389+
assert node.animals.peers
390+
for peer in node.animals.peers:
391+
rel_metadata = peer.get_relationship_metadata()
392+
assert isinstance(rel_metadata, RelationshipMetadata)
393+
assert rel_metadata.updated_at is not None
394+
assert rel_metadata.updated_by is not None
395+
333396
@pytest.mark.xfail(reason="https://github.com/opsmill/infrahub-sdk-python/issues/733")
334397
def test_recorder_with_playback_rewrite_host(self, base_dataset: None, tmp_path: Path, infrahub_port: int) -> None:
335398
# Create a fresh client for recording to ensure clean state (no cached schema)

0 commit comments

Comments
 (0)