From f32cb8545c4fff353037031ff3ba81b4a49746cd Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 19 Jun 2024 23:07:56 +0200 Subject: [PATCH] Use MockHAClientWebSocket.send_json_auto_id in blueprint tests (#119956) --- .../blueprint/test_websocket_api.py | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/tests/components/blueprint/test_websocket_api.py b/tests/components/blueprint/test_websocket_api.py index 4052e7c3316..1f684b451ed 100644 --- a/tests/components/blueprint/test_websocket_api.py +++ b/tests/components/blueprint/test_websocket_api.py @@ -46,11 +46,10 @@ async def test_list_blueprints( ) -> None: """Test listing blueprints.""" client = await hass_ws_client(hass) - await client.send_json({"id": 5, "type": "blueprint/list", "domain": "automation"}) + await client.send_json_auto_id({"type": "blueprint/list", "domain": "automation"}) msg = await client.receive_json() - assert msg["id"] == 5 assert msg["success"] blueprints = msg["result"] assert blueprints == { @@ -80,13 +79,10 @@ async def test_list_blueprints_non_existing_domain( ) -> None: """Test listing blueprints.""" client = await hass_ws_client(hass) - await client.send_json( - {"id": 5, "type": "blueprint/list", "domain": "not_existing"} - ) + await client.send_json_auto_id({"type": "blueprint/list", "domain": "not_existing"}) msg = await client.receive_json() - assert msg["id"] == 5 assert msg["success"] blueprints = msg["result"] assert blueprints == {} @@ -108,9 +104,8 @@ async def test_import_blueprint( ) client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 5, "type": "blueprint/import", "url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml", } @@ -118,7 +113,6 @@ async def test_import_blueprint( msg = await client.receive_json() - assert msg["id"] == 5 assert msg["success"] assert msg["result"] == { "suggested_filename": "balloob/motion_light", @@ -157,9 +151,8 @@ async def test_import_blueprint_update( ) client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 5, "type": "blueprint/import", "url": "https://github.com/in_folder/home-assistant-config/blob/main/blueprints/automation/in_folder_blueprint.yaml", } @@ -167,7 +160,6 @@ async def test_import_blueprint_update( msg = await client.receive_json() - assert msg["id"] == 5 assert msg["success"] assert msg["result"] == { "suggested_filename": "in_folder/in_folder_blueprint", @@ -196,9 +188,8 @@ async def test_save_blueprint( with patch("pathlib.Path.write_text") as write_mock: client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 6, "type": "blueprint/save", "path": "test_save", "yaml": raw_data, @@ -209,7 +200,6 @@ async def test_save_blueprint( msg = await client.receive_json() - assert msg["id"] == 6 assert msg["success"] assert write_mock.mock_calls # There are subtle differences in the dumper quoting @@ -245,9 +235,8 @@ async def test_save_existing_file( """Test saving blueprints.""" client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 7, "type": "blueprint/save", "path": "test_event_service", "yaml": 'blueprint: {name: "name", domain: "automation"}', @@ -258,7 +247,6 @@ async def test_save_existing_file( msg = await client.receive_json() - assert msg["id"] == 7 assert not msg["success"] assert msg["error"] == {"code": "already_exists", "message": "File already exists"} @@ -271,9 +259,8 @@ async def test_save_existing_file_override( client = await hass_ws_client(hass) with patch("pathlib.Path.write_text") as write_mock: - await client.send_json( + await client.send_json_auto_id( { - "id": 7, "type": "blueprint/save", "path": "test_event_service", "yaml": 'blueprint: {name: "name", domain: "automation"}', @@ -285,7 +272,6 @@ async def test_save_existing_file_override( msg = await client.receive_json() - assert msg["id"] == 7 assert msg["success"] assert msg["result"] == {"overrides_existing": True} assert yaml.safe_load(write_mock.mock_calls[0][1][0]) == { @@ -305,9 +291,8 @@ async def test_save_file_error( """Test saving blueprints with OS error.""" with patch("pathlib.Path.write_text", side_effect=OSError): client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 8, "type": "blueprint/save", "path": "test_save", "yaml": "raw_data", @@ -318,7 +303,6 @@ async def test_save_file_error( msg = await client.receive_json() - assert msg["id"] == 8 assert not msg["success"] @@ -329,9 +313,8 @@ async def test_save_invalid_blueprint( """Test saving invalid blueprints.""" client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 8, "type": "blueprint/save", "path": "test_wrong", "yaml": "wrong_blueprint", @@ -342,7 +325,6 @@ async def test_save_invalid_blueprint( msg = await client.receive_json() - assert msg["id"] == 8 assert not msg["success"] assert msg["error"] == { "code": "invalid_format", @@ -358,9 +340,8 @@ async def test_delete_blueprint( with patch("pathlib.Path.unlink", return_value=Mock()) as unlink_mock: client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 9, "type": "blueprint/delete", "path": "test_delete", "domain": "automation", @@ -370,7 +351,6 @@ async def test_delete_blueprint( msg = await client.receive_json() assert unlink_mock.mock_calls - assert msg["id"] == 9 assert msg["success"] @@ -381,9 +361,8 @@ async def test_delete_non_exist_file_blueprint( """Test deleting non existing blueprints.""" client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 9, "type": "blueprint/delete", "path": "none_existing", "domain": "automation", @@ -392,7 +371,6 @@ async def test_delete_non_exist_file_blueprint( msg = await client.receive_json() - assert msg["id"] == 9 assert not msg["success"] @@ -421,9 +399,8 @@ async def test_delete_blueprint_in_use_by_automation( with patch("pathlib.Path.unlink", return_value=Mock()) as unlink_mock: client = await hass_ws_client(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 9, "type": "blueprint/delete", "path": "test_event_service.yaml", "domain": "automation", @@ -433,7 +410,6 @@ async def test_delete_blueprint_in_use_by_automation( msg = await client.receive_json() assert not unlink_mock.mock_calls - assert msg["id"] == 9 assert not msg["success"] assert msg["error"] == { "code": "home_assistant_error",