From 5794b619e2309828eb43e5aa4e30f310be6d4a6e Mon Sep 17 00:00:00 2001 From: Nico Tonnhofer Date: Sun, 15 Oct 2023 23:40:15 +0200 Subject: [PATCH] IOT: update_thing() now behaves correctly with merge=True (#6918) --- moto/iot/models.py | 3 ++- tests/test_iot/test_iot_things.py | 34 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/moto/iot/models.py b/moto/iot/models.py index 1ebce5eaa..9ef417831 100644 --- a/moto/iot/models.py +++ b/moto/iot/models.py @@ -883,7 +883,8 @@ class IoTBackend(BaseBackend): if not do_merge: thing.attributes = attributes else: - thing.attributes = {k: v for k, v in attributes.items() if v} + thing.attributes.update(attributes) + thing.attributes = {k: v for k, v in thing.attributes.items() if v} def create_keys_and_certificate( self, set_as_active: bool diff --git a/tests/test_iot/test_iot_things.py b/tests/test_iot/test_iot_things.py index 773bdab3e..ddaffdc66 100644 --- a/tests/test_iot/test_iot_things.py +++ b/tests/test_iot/test_iot_things.py @@ -52,7 +52,32 @@ def test_update_thing(): assert len(res["things"]) == 1 assert res["things"][0]["thingName"] is not None assert res["things"][0]["thingArn"] is not None - assert res["things"][0]["attributes"]["k1"] == "v1" + assert res["things"][0]["attributes"] == {"k1": "v1"} + + client.update_thing(thingName=name, attributePayload={"attributes": {"k2": "v2"}}) + res = client.list_things() + assert len(res["things"]) == 1 + assert res["things"][0]["thingName"] is not None + assert res["things"][0]["thingArn"] is not None + assert res["things"][0]["attributes"] == {"k2": "v2"} + + client.update_thing( + thingName=name, attributePayload={"attributes": {"k1": "v1"}, "merge": True} + ) + res = client.list_things() + assert len(res["things"]) == 1 + assert res["things"][0]["thingName"] is not None + assert res["things"][0]["thingArn"] is not None + assert res["things"][0]["attributes"] == {"k1": "v1", "k2": "v2"} + + client.update_thing( + thingName=name, attributePayload={"attributes": {"k1": "v1.1"}, "merge": True} + ) + res = client.list_things() + assert len(res["things"]) == 1 + assert res["things"][0]["thingName"] is not None + assert res["things"][0]["thingArn"] is not None + assert res["things"][0]["attributes"] == {"k1": "v1.1", "k2": "v2"} client.update_thing( thingName=name, attributePayload={"attributes": {"k1": ""}, "merge": True} @@ -61,6 +86,13 @@ def test_update_thing(): assert len(res["things"]) == 1 assert res["things"][0]["thingName"] is not None assert res["things"][0]["thingArn"] is not None + assert res["things"][0]["attributes"] == {"k2": "v2"} + + client.update_thing(thingName=name, attributePayload={"attributes": {"k2": ""}}) + res = client.list_things() + assert len(res["things"]) == 1 + assert res["things"][0]["thingName"] is not None + assert res["things"][0]["thingArn"] is not None assert res["things"][0]["attributes"] == {}