|
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, patch |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
|
9 | 11 |
|
10 | 12 | if TYPE_CHECKING: |
11 | 13 | from infrahub_sdk.client import InfrahubClient |
| 14 | + from infrahub_sdk.node import InfrahubNode |
12 | 15 |
|
13 | 16 |
|
14 | 17 | @pytest.fixture |
@@ -263,3 +266,105 @@ async def test_parameters_non_dict(client_with_schema_01: InfrahubClient, locati |
263 | 266 | obj = ObjectFile(location="some/path", content=location_with_non_dict_parameters) |
264 | 267 | with pytest.raises(ValidationError): |
265 | 268 | await obj.validate_format(client=client_with_schema_01) |
| 269 | + |
| 270 | + |
| 271 | +@dataclass |
| 272 | +class HfidLoadTestCase: |
| 273 | + """Test case for HFID normalization in object loading.""" |
| 274 | + |
| 275 | + name: str |
| 276 | + data: list[dict[str, Any]] |
| 277 | + expected_primary_tag: str | list[str] | None |
| 278 | + expected_tags: list[str] | list[list[str]] | None |
| 279 | + |
| 280 | + |
| 281 | +HFID_NORMALIZATION_TEST_CASES = [ |
| 282 | + HfidLoadTestCase( |
| 283 | + name="cardinality_one_string_hfid_normalized", |
| 284 | + data=[{"name": "Mexico", "type": "Country", "primary_tag": "Important"}], |
| 285 | + expected_primary_tag=["Important"], |
| 286 | + expected_tags=None, |
| 287 | + ), |
| 288 | + HfidLoadTestCase( |
| 289 | + name="cardinality_one_list_hfid_unchanged", |
| 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_uuid_unchanged", |
| 296 | + data=[{"name": "Mexico", "type": "Country", "primary_tag": "550e8400-e29b-41d4-a716-446655440000"}], |
| 297 | + expected_primary_tag="550e8400-e29b-41d4-a716-446655440000", |
| 298 | + expected_tags=None, |
| 299 | + ), |
| 300 | + HfidLoadTestCase( |
| 301 | + name="cardinality_many_string_hfids_normalized", |
| 302 | + data=[{"name": "Mexico", "type": "Country", "tags": ["Important", "Active"]}], |
| 303 | + expected_primary_tag=None, |
| 304 | + expected_tags=[["Important"], ["Active"]], |
| 305 | + ), |
| 306 | + HfidLoadTestCase( |
| 307 | + name="cardinality_many_list_hfids_unchanged", |
| 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_mixed_hfids_normalized", |
| 314 | + data=[{"name": "Mexico", "type": "Country", "tags": ["Important", ["namespace", "name"]]}], |
| 315 | + expected_primary_tag=None, |
| 316 | + expected_tags=[["Important"], ["namespace", "name"]], |
| 317 | + ), |
| 318 | + HfidLoadTestCase( |
| 319 | + name="cardinality_many_uuids_unchanged", |
| 320 | + data=[ |
| 321 | + { |
| 322 | + "name": "Mexico", |
| 323 | + "type": "Country", |
| 324 | + "tags": ["550e8400-e29b-41d4-a716-446655440000", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"], |
| 325 | + } |
| 326 | + ], |
| 327 | + expected_primary_tag=None, |
| 328 | + expected_tags=["550e8400-e29b-41d4-a716-446655440000", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"], |
| 329 | + ), |
| 330 | +] |
| 331 | + |
| 332 | + |
| 333 | +@pytest.mark.parametrize("test_case", HFID_NORMALIZATION_TEST_CASES, ids=lambda tc: tc.name) |
| 334 | +async def test_hfid_normalization_in_object_loading( |
| 335 | + client_with_schema_01: InfrahubClient, test_case: HfidLoadTestCase |
| 336 | +) -> None: |
| 337 | + """Test that HFIDs are normalized correctly based on cardinality and format.""" |
| 338 | + |
| 339 | + root_location = {"apiVersion": "infrahub.app/v1", "kind": "Object", "spec": {"kind": "BuiltinLocation", "data": []}} |
| 340 | + location = { |
| 341 | + "apiVersion": root_location["apiVersion"], |
| 342 | + "kind": root_location["kind"], |
| 343 | + "spec": {"kind": root_location["spec"]["kind"], "data": test_case.data}, |
| 344 | + } |
| 345 | + |
| 346 | + obj = ObjectFile(location="some/path", content=location) |
| 347 | + await obj.validate_format(client=client_with_schema_01) |
| 348 | + |
| 349 | + create_calls: list[dict[str, Any]] = [] |
| 350 | + |
| 351 | + async def mock_create( |
| 352 | + kind: str, |
| 353 | + branch: str | None = None, |
| 354 | + data: dict | None = None, |
| 355 | + **kwargs: Any, # noqa: ANN401 |
| 356 | + ) -> InfrahubNode: |
| 357 | + create_calls.append({"kind": kind, "data": data}) |
| 358 | + original_create = client_with_schema_01.__class__.create |
| 359 | + return await original_create(client_with_schema_01, kind=kind, branch=branch, data=data, **kwargs) |
| 360 | + |
| 361 | + client_with_schema_01.create = mock_create |
| 362 | + |
| 363 | + with patch("infrahub_sdk.node.InfrahubNode.save", new_callable=AsyncMock): |
| 364 | + await obj.process(client=client_with_schema_01) |
| 365 | + |
| 366 | + assert len(create_calls) == 1 |
| 367 | + if test_case.expected_primary_tag is not None: |
| 368 | + assert create_calls[0]["data"]["primary_tag"] == test_case.expected_primary_tag |
| 369 | + if test_case.expected_tags is not None: |
| 370 | + assert create_calls[0]["data"]["tags"] == test_case.expected_tags |
0 commit comments