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"] == {}