|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import TYPE_CHECKING, Any |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from infrahub_sdk.exceptions import ValidationError |
8 | | -from infrahub_sdk.spec.object import ObjectFile, RelationshipDataFormat, get_relationship_info |
| 10 | +from infrahub_sdk.node.related_node import RelatedNode |
| 11 | +from infrahub_sdk.spec.object import ( |
| 12 | + ObjectFile, |
| 13 | + RelationshipDataFormat, |
| 14 | + get_relationship_info, |
| 15 | + normalize_hfid_reference, |
| 16 | +) |
9 | 17 |
|
10 | 18 | if TYPE_CHECKING: |
11 | 19 | from infrahub_sdk.client import InfrahubClient |
| 20 | + from infrahub_sdk.node import InfrahubNode |
12 | 21 |
|
13 | 22 |
|
14 | 23 | @pytest.fixture |
@@ -263,3 +272,258 @@ async def test_parameters_non_dict(client_with_schema_01: InfrahubClient, locati |
263 | 272 | obj = ObjectFile(location="some/path", content=location_with_non_dict_parameters) |
264 | 273 | with pytest.raises(ValidationError): |
265 | 274 | await obj.validate_format(client=client_with_schema_01) |
| 275 | + |
| 276 | + |
| 277 | +@dataclass |
| 278 | +class HfidLoadTestCase: |
| 279 | + """Test case for HFID normalization in object loading.""" |
| 280 | + |
| 281 | + name: str |
| 282 | + data: list[dict[str, Any]] |
| 283 | + expected_primary_tag: str | list[str] | None |
| 284 | + expected_tags: list[str] | list[list[str]] | None |
| 285 | + |
| 286 | + |
| 287 | +HFID_NORMALIZATION_TEST_CASES = [ |
| 288 | + HfidLoadTestCase( |
| 289 | + name="cardinality_one_string_hfid_normalized", |
| 290 | + data=[{"name": "Mexico", "type": "Country", "primary_tag": "Important"}], |
| 291 | + expected_primary_tag=["Important"], |
| 292 | + expected_tags=None, |
| 293 | + ), |
| 294 | + HfidLoadTestCase( |
| 295 | + name="cardinality_one_list_hfid_unchanged", |
| 296 | + data=[{"name": "Mexico", "type": "Country", "primary_tag": ["Important"]}], |
| 297 | + expected_primary_tag=["Important"], |
| 298 | + expected_tags=None, |
| 299 | + ), |
| 300 | + HfidLoadTestCase( |
| 301 | + name="cardinality_one_uuid_unchanged", |
| 302 | + data=[{"name": "Mexico", "type": "Country", "primary_tag": "550e8400-e29b-41d4-a716-446655440000"}], |
| 303 | + expected_primary_tag="550e8400-e29b-41d4-a716-446655440000", |
| 304 | + expected_tags=None, |
| 305 | + ), |
| 306 | + HfidLoadTestCase( |
| 307 | + name="cardinality_many_string_hfids_normalized", |
| 308 | + data=[{"name": "Mexico", "type": "Country", "tags": ["Important", "Active"]}], |
| 309 | + expected_primary_tag=None, |
| 310 | + expected_tags=[["Important"], ["Active"]], |
| 311 | + ), |
| 312 | + HfidLoadTestCase( |
| 313 | + name="cardinality_many_list_hfids_unchanged", |
| 314 | + data=[{"name": "Mexico", "type": "Country", "tags": [["Important"], ["Active"]]}], |
| 315 | + expected_primary_tag=None, |
| 316 | + expected_tags=[["Important"], ["Active"]], |
| 317 | + ), |
| 318 | + HfidLoadTestCase( |
| 319 | + name="cardinality_many_mixed_hfids_normalized", |
| 320 | + data=[{"name": "Mexico", "type": "Country", "tags": ["Important", ["namespace", "name"]]}], |
| 321 | + expected_primary_tag=None, |
| 322 | + expected_tags=[["Important"], ["namespace", "name"]], |
| 323 | + ), |
| 324 | + HfidLoadTestCase( |
| 325 | + name="cardinality_many_uuids_unchanged", |
| 326 | + data=[ |
| 327 | + { |
| 328 | + "name": "Mexico", |
| 329 | + "type": "Country", |
| 330 | + "tags": ["550e8400-e29b-41d4-a716-446655440000", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"], |
| 331 | + } |
| 332 | + ], |
| 333 | + expected_primary_tag=None, |
| 334 | + expected_tags=["550e8400-e29b-41d4-a716-446655440000", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"], |
| 335 | + ), |
| 336 | +] |
| 337 | + |
| 338 | + |
| 339 | +@pytest.mark.parametrize("test_case", HFID_NORMALIZATION_TEST_CASES, ids=lambda tc: tc.name) |
| 340 | +async def test_hfid_normalization_in_object_loading( |
| 341 | + client_with_schema_01: InfrahubClient, test_case: HfidLoadTestCase |
| 342 | +) -> None: |
| 343 | + """Test that HFIDs are normalized correctly based on cardinality and format.""" |
| 344 | + |
| 345 | + root_location = {"apiVersion": "infrahub.app/v1", "kind": "Object", "spec": {"kind": "BuiltinLocation", "data": []}} |
| 346 | + location = { |
| 347 | + "apiVersion": root_location["apiVersion"], |
| 348 | + "kind": root_location["kind"], |
| 349 | + "spec": {"kind": root_location["spec"]["kind"], "data": test_case.data}, |
| 350 | + } |
| 351 | + |
| 352 | + obj = ObjectFile(location="some/path", content=location) |
| 353 | + await obj.validate_format(client=client_with_schema_01) |
| 354 | + |
| 355 | + create_calls: list[dict[str, Any]] = [] |
| 356 | + |
| 357 | + async def mock_create( |
| 358 | + kind: str, |
| 359 | + branch: str | None = None, |
| 360 | + data: dict | None = None, |
| 361 | + **kwargs: Any, # noqa: ANN401 |
| 362 | + ) -> InfrahubNode: |
| 363 | + create_calls.append({"kind": kind, "data": data}) |
| 364 | + original_create = client_with_schema_01.__class__.create |
| 365 | + return await original_create(client_with_schema_01, kind=kind, branch=branch, data=data, **kwargs) |
| 366 | + |
| 367 | + client_with_schema_01.create = mock_create |
| 368 | + |
| 369 | + with patch("infrahub_sdk.node.InfrahubNode.save", new_callable=AsyncMock): |
| 370 | + await obj.process(client=client_with_schema_01) |
| 371 | + |
| 372 | + assert len(create_calls) == 1 |
| 373 | + if test_case.expected_primary_tag is not None: |
| 374 | + assert create_calls[0]["data"]["primary_tag"] == test_case.expected_primary_tag |
| 375 | + if test_case.expected_tags is not None: |
| 376 | + assert create_calls[0]["data"]["tags"] == test_case.expected_tags |
| 377 | + |
| 378 | + |
| 379 | +@dataclass |
| 380 | +class GraphQLPayloadTestCase: |
| 381 | + """Test case for verifying data format that leads to correct GraphQL payload. |
| 382 | +
|
| 383 | + The RelatedNode interprets data as follows: |
| 384 | + - list → stored as hfid → GraphQL: {"hfid": [...]} |
| 385 | + - string → stored as id → GraphQL: {"id": "..."} |
| 386 | + """ |
| 387 | + |
| 388 | + name: str |
| 389 | + peer_has_hfid: bool |
| 390 | + input_value: str | list[str] |
| 391 | + expected_output_type: str # "list" for hfid, "string" for id |
| 392 | + expected_output_value: str | list[str] |
| 393 | + |
| 394 | + |
| 395 | +GRAPHQL_PAYLOAD_TEST_CASES = [ |
| 396 | + # Peer HAS HFID - non-UUID string should become list (hfid) |
| 397 | + GraphQLPayloadTestCase( |
| 398 | + name="hfid_defined_string_becomes_list", |
| 399 | + peer_has_hfid=True, |
| 400 | + input_value="Important", |
| 401 | + expected_output_type="list", |
| 402 | + expected_output_value=["Important"], |
| 403 | + ), |
| 404 | + # Peer HAS HFID - UUID string should stay as string (id) |
| 405 | + GraphQLPayloadTestCase( |
| 406 | + name="hfid_defined_uuid_stays_string", |
| 407 | + peer_has_hfid=True, |
| 408 | + input_value="550e8400-e29b-41d4-a716-446655440000", |
| 409 | + expected_output_type="string", |
| 410 | + expected_output_value="550e8400-e29b-41d4-a716-446655440000", |
| 411 | + ), |
| 412 | + # Peer HAS HFID - list stays as list (hfid) |
| 413 | + GraphQLPayloadTestCase( |
| 414 | + name="hfid_defined_list_stays_list", |
| 415 | + peer_has_hfid=True, |
| 416 | + input_value=["namespace", "name"], |
| 417 | + expected_output_type="list", |
| 418 | + expected_output_value=["namespace", "name"], |
| 419 | + ), |
| 420 | + # Peer has NO HFID - non-UUID string stays as string (id lookup) |
| 421 | + GraphQLPayloadTestCase( |
| 422 | + name="no_hfid_string_stays_string", |
| 423 | + peer_has_hfid=False, |
| 424 | + input_value="some-string-value", |
| 425 | + expected_output_type="string", |
| 426 | + expected_output_value="some-string-value", |
| 427 | + ), |
| 428 | + # Peer has NO HFID - UUID stays as string (id) |
| 429 | + GraphQLPayloadTestCase( |
| 430 | + name="no_hfid_uuid_stays_string", |
| 431 | + peer_has_hfid=False, |
| 432 | + input_value="550e8400-e29b-41d4-a716-446655440000", |
| 433 | + expected_output_type="string", |
| 434 | + expected_output_value="550e8400-e29b-41d4-a716-446655440000", |
| 435 | + ), |
| 436 | +] |
| 437 | + |
| 438 | + |
| 439 | +@pytest.mark.parametrize("test_case", GRAPHQL_PAYLOAD_TEST_CASES, ids=lambda tc: tc.name) |
| 440 | +def test_graphql_payload_format(test_case: GraphQLPayloadTestCase) -> None: |
| 441 | + """Test that relationship data is formatted correctly for GraphQL payload. |
| 442 | +
|
| 443 | + The RelatedNode class interprets: |
| 444 | + - list input → {"hfid": [...]} in GraphQL |
| 445 | + - string input → {"id": "..."} in GraphQL |
| 446 | +
|
| 447 | + This test verifies the normalization produces the correct format. |
| 448 | + """ |
| 449 | + if test_case.peer_has_hfid: |
| 450 | + # When peer has HFID, use normalization |
| 451 | + processed_value = normalize_hfid_reference(test_case.input_value) |
| 452 | + else: |
| 453 | + # When peer has no HFID, pass value as-is (no normalization) |
| 454 | + processed_value = test_case.input_value |
| 455 | + |
| 456 | + # Verify the output type matches expected |
| 457 | + if test_case.expected_output_type == "list": |
| 458 | + assert isinstance(processed_value, list), ( |
| 459 | + f"Expected list output for hfid, got {type(processed_value).__name__}: {processed_value}" |
| 460 | + ) |
| 461 | + else: |
| 462 | + assert isinstance(processed_value, str), ( |
| 463 | + f"Expected string output for id, got {type(processed_value).__name__}: {processed_value}" |
| 464 | + ) |
| 465 | + |
| 466 | + # Verify the actual value |
| 467 | + assert processed_value == test_case.expected_output_value, ( |
| 468 | + f"Expected {test_case.expected_output_value}, got {processed_value}" |
| 469 | + ) |
| 470 | + |
| 471 | + |
| 472 | +@dataclass |
| 473 | +class RelatedNodePayloadTestCase: |
| 474 | + """Test case for verifying the actual GraphQL payload structure from RelatedNode.""" |
| 475 | + |
| 476 | + name: str |
| 477 | + input_data: str | list[str] |
| 478 | + expected_payload: dict[str, Any] |
| 479 | + |
| 480 | + |
| 481 | +RELATED_NODE_PAYLOAD_TEST_CASES = [ |
| 482 | + # String (UUID) → {"id": "uuid"} |
| 483 | + RelatedNodePayloadTestCase( |
| 484 | + name="uuid_string_becomes_id_payload", |
| 485 | + input_data="550e8400-e29b-41d4-a716-446655440000", |
| 486 | + expected_payload={"id": "550e8400-e29b-41d4-a716-446655440000"}, |
| 487 | + ), |
| 488 | + # List (HFID) → {"hfid": [...]} |
| 489 | + RelatedNodePayloadTestCase( |
| 490 | + name="list_becomes_hfid_payload", |
| 491 | + input_data=["Important"], |
| 492 | + expected_payload={"hfid": ["Important"]}, |
| 493 | + ), |
| 494 | + # Multi-component HFID list → {"hfid": [...]} |
| 495 | + RelatedNodePayloadTestCase( |
| 496 | + name="multi_component_hfid_payload", |
| 497 | + input_data=["namespace", "name"], |
| 498 | + expected_payload={"hfid": ["namespace", "name"]}, |
| 499 | + ), |
| 500 | +] |
| 501 | + |
| 502 | + |
| 503 | +@pytest.mark.parametrize("test_case", RELATED_NODE_PAYLOAD_TEST_CASES, ids=lambda tc: tc.name) |
| 504 | +def test_related_node_graphql_payload(test_case: RelatedNodePayloadTestCase) -> None: |
| 505 | + """Test that RelatedNode produces the correct GraphQL payload structure. |
| 506 | +
|
| 507 | + This test verifies the actual {"id": ...} or {"hfid": ...} payload |
| 508 | + that gets sent in GraphQL mutations. |
| 509 | + """ |
| 510 | + # Create mock dependencies |
| 511 | + mock_client = MagicMock() |
| 512 | + mock_schema = MagicMock() |
| 513 | + |
| 514 | + # Create RelatedNode with the input data |
| 515 | + related_node = RelatedNode( |
| 516 | + schema=mock_schema, |
| 517 | + name="test_rel", |
| 518 | + branch="main", |
| 519 | + client=mock_client, |
| 520 | + data=test_case.input_data, |
| 521 | + ) |
| 522 | + |
| 523 | + # Generate the input data that would go into GraphQL mutation |
| 524 | + payload = related_node._generate_input_data() |
| 525 | + |
| 526 | + # Verify the payload structure |
| 527 | + assert payload == test_case.expected_payload, ( |
| 528 | + f"Expected payload {test_case.expected_payload}, got {payload}" |
| 529 | + ) |
0 commit comments