Skip to content
Merged
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
56 changes: 34 additions & 22 deletions tests/unit/sdk/test_hierarchical_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync
from infrahub_sdk.schema import NodeSchema
from infrahub_sdk.schema import NodeSchema, NodeSchemaAPI

if TYPE_CHECKING:
from infrahub_sdk import InfrahubClient, InfrahubClientSync
Expand All @@ -16,7 +16,7 @@


@pytest.fixture
async def hierarchical_schema():
async def hierarchical_schema() -> NodeSchemaAPI:
"""Schema for a hierarchical location node with hierarchy support."""
data = {
"name": "Location",
Expand All @@ -43,7 +43,7 @@ async def hierarchical_schema():
},
],
}
schema_api = NodeSchema(**data).convert_api() # type: ignore
schema_api = NodeSchema(**data).convert_api()
# Set hierarchy field manually since it's not part of NodeSchema but only NodeSchemaAPI
# This field would normally be set by the backend
schema_api.hierarchy = "InfraLocation"
Expand All @@ -53,7 +53,7 @@ async def hierarchical_schema():
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_hierarchical_node_has_hierarchy_support(
client: InfrahubClient, client_sync: InfrahubClientSync, hierarchical_schema, client_type
):
) -> None:
"""Test that hierarchical nodes are properly detected and support parent/children/ancestors/descendants."""
if client_type == "standard":
node = InfrahubNode(client=client, schema=hierarchical_schema)
Expand All @@ -68,7 +68,7 @@ async def test_hierarchical_node_has_hierarchy_support(
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_hierarchical_node_has_all_hierarchical_fields(
client: InfrahubClient, client_sync: InfrahubClientSync, hierarchical_schema, client_type
):
) -> None:
"""Test that hierarchical nodes have parent, children, ancestors and descendants attributes."""
if client_type == "standard":
node = InfrahubNode(client=client, schema=hierarchical_schema)
Expand Down Expand Up @@ -111,7 +111,7 @@ async def test_hierarchical_node_has_all_hierarchical_fields(
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_hierarchical_node_with_parent_data(
client: InfrahubClient, client_sync: InfrahubClientSync, hierarchical_schema, client_type
):
) -> None:
"""Test that hierarchical nodes can be initialized with parent data."""
data = {
"id": "location-1",
Expand All @@ -134,7 +134,7 @@ async def test_hierarchical_node_with_parent_data(
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_hierarchical_node_with_children_data(
client: InfrahubClient, client_sync: InfrahubClientSync, hierarchical_schema, client_type
):
) -> None:
"""Test that hierarchical nodes can be initialized with children data."""
data = {
"id": "location-1",
Expand Down Expand Up @@ -165,7 +165,7 @@ async def test_hierarchical_node_with_children_data(
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_hierarchical_node_with_ancestors_data(
client: InfrahubClient, client_sync: InfrahubClientSync, hierarchical_schema, client_type
):
) -> None:
"""Test that hierarchical nodes can be initialized with ancestors data."""
data = {
"id": "location-1",
Expand Down Expand Up @@ -196,7 +196,7 @@ async def test_hierarchical_node_with_ancestors_data(
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_hierarchical_node_with_descendants_data(
client: InfrahubClient, client_sync: InfrahubClientSync, hierarchical_schema, client_type
):
) -> None:
"""Test that hierarchical nodes can be initialized with descendants data."""
data = {
"id": "location-1",
Expand Down Expand Up @@ -230,7 +230,7 @@ async def test_hierarchical_node_with_descendants_data(
@pytest.mark.parametrize("client_type", ["standard", "sync"])
async def test_non_hierarchical_node_no_hierarchical_fields(
client: InfrahubClient, client_sync: InfrahubClientSync, location_schema, client_type
):
) -> None:
"""Test that non-hierarchical nodes don't have parent/children/ancestors/descendants."""
if client_type == "standard":
node = InfrahubNode(client=client, schema=location_schema)
Expand All @@ -254,7 +254,7 @@ async def test_non_hierarchical_node_no_hierarchical_fields(
_ = node.descendants


async def test_hierarchical_node_query_generation_includes_parent(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_query_generation_includes_parent(client: InfrahubClient, hierarchical_schema) -> None:
"""Test that query generation includes parent when requested."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -276,7 +276,9 @@ async def test_hierarchical_node_query_generation_includes_parent(client: Infrah
assert "...on InfraLocation" in query_data["parent"]["node"]


async def test_hierarchical_node_query_generation_includes_children(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_query_generation_includes_children(
client: InfrahubClient, hierarchical_schema
) -> None:
"""Test that query generation includes children when requested."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -299,7 +301,9 @@ async def test_hierarchical_node_query_generation_includes_children(client: Infr
assert "...on InfraLocation" in query_data["children"]["edges"]["node"]


async def test_hierarchical_node_query_generation_includes_ancestors(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_query_generation_includes_ancestors(
client: InfrahubClient, hierarchical_schema
) -> None:
"""Test that query generation includes ancestors when requested."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -322,7 +326,9 @@ async def test_hierarchical_node_query_generation_includes_ancestors(client: Inf
assert "...on InfraLocation" in query_data["ancestors"]["edges"]["node"]


async def test_hierarchical_node_query_generation_includes_descendants(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_query_generation_includes_descendants(
client: InfrahubClient, hierarchical_schema
) -> None:
"""Test that query generation includes descendants when requested."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -345,7 +351,9 @@ async def test_hierarchical_node_query_generation_includes_descendants(client: I
assert "...on InfraLocation" in query_data["descendants"]["edges"]["node"]


async def test_hierarchical_node_query_generation_prefetch_relationships(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_query_generation_prefetch_relationships(
client: InfrahubClient, hierarchical_schema
) -> None:
"""Test that query generation includes all hierarchical fields with prefetch_relationships=True."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -366,7 +374,7 @@ async def test_hierarchical_node_query_generation_prefetch_relationships(client:
assert "descendants" in query_data


async def test_hierarchical_node_query_generation_exclude(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_query_generation_exclude(client: InfrahubClient, hierarchical_schema) -> None:
"""Test that query generation respects exclude for hierarchical fields."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -387,7 +395,9 @@ async def test_hierarchical_node_query_generation_exclude(client: InfrahubClient
assert "descendants" in query_data


def test_hierarchical_node_sync_query_generation_includes_parent(client_sync: InfrahubClientSync, hierarchical_schema):
def test_hierarchical_node_sync_query_generation_includes_parent(
client_sync: InfrahubClientSync, hierarchical_schema
) -> None:
"""Test that sync query generation includes parent when requested."""
# Set schema in cache to avoid HTTP request
cache_data = {
Expand All @@ -411,7 +421,7 @@ def test_hierarchical_node_sync_query_generation_includes_parent(client_sync: In

def test_hierarchical_node_sync_query_generation_includes_children(
client_sync: InfrahubClientSync, hierarchical_schema
):
) -> None:
"""Test that sync query generation includes children when requested."""
# Set schema in cache to avoid HTTP request
cache_data = {
Expand All @@ -436,7 +446,7 @@ def test_hierarchical_node_sync_query_generation_includes_children(

def test_hierarchical_node_sync_query_generation_includes_ancestors(
client_sync: InfrahubClientSync, hierarchical_schema
):
) -> None:
"""Test that sync query generation includes ancestors when requested."""
# Set schema in cache to avoid HTTP request
cache_data = {
Expand All @@ -461,7 +471,7 @@ def test_hierarchical_node_sync_query_generation_includes_ancestors(

def test_hierarchical_node_sync_query_generation_includes_descendants(
client_sync: InfrahubClientSync, hierarchical_schema
):
) -> None:
"""Test that sync query generation includes descendants when requested."""
# Set schema in cache to avoid HTTP request
cache_data = {
Expand All @@ -484,7 +494,9 @@ def test_hierarchical_node_sync_query_generation_includes_descendants(
assert "...on InfraLocation" in query_data["descendants"]["edges"]["node"]


async def test_hierarchical_node_no_infinite_recursion_with_children(client: InfrahubClient, hierarchical_schema):
async def test_hierarchical_node_no_infinite_recursion_with_children(
client: InfrahubClient, hierarchical_schema
) -> None:
"""Test that including children does not cause infinite recursion."""
# Pre-populate schema cache to avoid fetching from server
cache_data = {
Expand All @@ -510,7 +522,7 @@ async def test_hierarchical_node_no_infinite_recursion_with_children(client: Inf

def test_hierarchical_node_sync_no_infinite_recursion_with_children(
client_sync: InfrahubClientSync, hierarchical_schema
):
) -> None:
"""Test that including children does not cause infinite recursion in sync mode."""
# Set schema in cache to avoid HTTP request
cache_data = {
Expand Down