From 2dc6f35cbee0d4897f73b309867240336f64bcd9 Mon Sep 17 00:00:00 2001 From: Benoit Kohler Date: Mon, 19 Aug 2024 20:24:54 +0200 Subject: [PATCH] Fixes SDK error on save after include (#4123) * proposal fix for 4116 --- python_sdk/infrahub_sdk/node.py | 4 +-- python_sdk/tests/unit/sdk/test_node.py | 45 ++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/python_sdk/infrahub_sdk/node.py b/python_sdk/infrahub_sdk/node.py index 8b7c8d5f86..510453cd0f 100644 --- a/python_sdk/infrahub_sdk/node.py +++ b/python_sdk/infrahub_sdk/node.py @@ -479,7 +479,7 @@ def __init__( super().__init__(name=name, schema=schema, branch=branch) self.initialized = data is not None - self._has_update = data is not None + self._has_update = False if data is None: return @@ -574,7 +574,7 @@ def __init__( super().__init__(name=name, schema=schema, branch=branch) self.initialized = data is not None - self._has_update = data is not None + self._has_update = False if data is None: return diff --git a/python_sdk/tests/unit/sdk/test_node.py b/python_sdk/tests/unit/sdk/test_node.py index e0de21a3e4..5749052d2e 100644 --- a/python_sdk/tests/unit/sdk/test_node.py +++ b/python_sdk/tests/unit/sdk/test_node.py @@ -1073,7 +1073,9 @@ async def test_create_input_data_with_relationships_03(clients, rfile_schema, cl @pytest.mark.parametrize("client_type", client_types) -async def test_create_input_data_with_relationships_03_for_update(clients, rfile_schema, client_type): +async def test_create_input_data_with_relationships_03_for_update_include_unmodified( + clients, rfile_schema, client_type +): data = { "name": {"value": "rfile01", "is_protected": True, "source": "ffffffff"}, "template_path": {"value": "mytemplate.j2"}, @@ -1088,8 +1090,13 @@ async def test_create_input_data_with_relationships_03_for_update(clients, rfile node = InfrahubNodeSync(client=clients.sync, schema=rfile_schema, data=data) node.template_path.value = "my-changed-template.j2" - assert node._generate_input_data(exclude_unmodified=True)["data"] == { + assert node._generate_input_data(exclude_unmodified=False)["data"] == { "data": { + "name": { + "is_protected": True, + "source": "ffffffff", + "value": "rfile01", + }, "query": { "id": "qqqqqqqq", "_relation__is_protected": True, @@ -1103,6 +1110,38 @@ async def test_create_input_data_with_relationships_03_for_update(clients, rfile } +@pytest.mark.parametrize("client_type", client_types) +async def test_create_input_data_with_relationships_03_for_update_exclude_unmodified( + clients, rfile_schema, client_type +): + data = { + "name": {"value": "rfile01", "is_protected": True, "source": "ffffffff"}, + "template_path": {"value": "mytemplate.j2"}, + "query": {"id": "qqqqqqqq", "source": "ffffffff", "owner": "ffffffff", "is_protected": True}, + "repository": {"id": "rrrrrrrr", "source": "ffffffff", "owner": "ffffffff"}, + "tags": [{"id": "t1t1t1t1"}, "t2t2t2t2"], + } + + if client_type == "standard": + node = InfrahubNode(client=clients.standard, schema=rfile_schema, data=data) + else: + node = InfrahubNodeSync(client=clients.sync, schema=rfile_schema, data=data) + + node.template_path.value = "my-changed-template.j2" + assert node._generate_input_data(exclude_unmodified=True)["data"] == { + "data": { + "query": { + "id": "qqqqqqqq", + "_relation__is_protected": True, + "_relation__owner": "ffffffff", + "_relation__source": "ffffffff", + }, + "template_path": {"value": "my-changed-template.j2"}, + "repository": {"id": "rrrrrrrr", "_relation__owner": "ffffffff", "_relation__source": "ffffffff"}, + } + } + + @pytest.mark.parametrize("client_type", client_types) async def test_create_input_data_with_IPHost_attribute(client, ipaddress_schema, client_type): data = {"address": {"value": ipaddress.ip_interface("1.1.1.1/24"), "is_protected": True}} @@ -1426,7 +1465,7 @@ async def test_relationships_excluded_input_data(client, location_schema, client else: node = InfrahubNodeSync(client=client, schema=location_schema, data=data) - assert node.tags.has_update is True + assert node.tags.has_update is False @pytest.mark.parametrize("client_type", client_types)