Use MockHAClientWebSocket.send_json_auto_id in blueprint tests (#119956)

This commit is contained in:
Erik Montnemery 2024-06-19 23:07:56 +02:00 committed by GitHub
parent 42b62ec427
commit f32cb8545c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -46,11 +46,10 @@ async def test_list_blueprints(
) -> None: ) -> None:
"""Test listing blueprints.""" """Test listing blueprints."""
client = await hass_ws_client(hass) 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() msg = await client.receive_json()
assert msg["id"] == 5
assert msg["success"] assert msg["success"]
blueprints = msg["result"] blueprints = msg["result"]
assert blueprints == { assert blueprints == {
@ -80,13 +79,10 @@ async def test_list_blueprints_non_existing_domain(
) -> None: ) -> None:
"""Test listing blueprints.""" """Test listing blueprints."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id({"type": "blueprint/list", "domain": "not_existing"})
{"id": 5, "type": "blueprint/list", "domain": "not_existing"}
)
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 5
assert msg["success"] assert msg["success"]
blueprints = msg["result"] blueprints = msg["result"]
assert blueprints == {} assert blueprints == {}
@ -108,9 +104,8 @@ async def test_import_blueprint(
) )
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "blueprint/import", "type": "blueprint/import",
"url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml", "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() msg = await client.receive_json()
assert msg["id"] == 5
assert msg["success"] assert msg["success"]
assert msg["result"] == { assert msg["result"] == {
"suggested_filename": "balloob/motion_light", "suggested_filename": "balloob/motion_light",
@ -157,9 +151,8 @@ async def test_import_blueprint_update(
) )
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "blueprint/import", "type": "blueprint/import",
"url": "https://github.com/in_folder/home-assistant-config/blob/main/blueprints/automation/in_folder_blueprint.yaml", "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() msg = await client.receive_json()
assert msg["id"] == 5
assert msg["success"] assert msg["success"]
assert msg["result"] == { assert msg["result"] == {
"suggested_filename": "in_folder/in_folder_blueprint", "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: with patch("pathlib.Path.write_text") as write_mock:
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 6,
"type": "blueprint/save", "type": "blueprint/save",
"path": "test_save", "path": "test_save",
"yaml": raw_data, "yaml": raw_data,
@ -209,7 +200,6 @@ async def test_save_blueprint(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 6
assert msg["success"] assert msg["success"]
assert write_mock.mock_calls assert write_mock.mock_calls
# There are subtle differences in the dumper quoting # There are subtle differences in the dumper quoting
@ -245,9 +235,8 @@ async def test_save_existing_file(
"""Test saving blueprints.""" """Test saving blueprints."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 7,
"type": "blueprint/save", "type": "blueprint/save",
"path": "test_event_service", "path": "test_event_service",
"yaml": 'blueprint: {name: "name", domain: "automation"}', "yaml": 'blueprint: {name: "name", domain: "automation"}',
@ -258,7 +247,6 @@ async def test_save_existing_file(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 7
assert not msg["success"] assert not msg["success"]
assert msg["error"] == {"code": "already_exists", "message": "File already exists"} 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) client = await hass_ws_client(hass)
with patch("pathlib.Path.write_text") as write_mock: with patch("pathlib.Path.write_text") as write_mock:
await client.send_json( await client.send_json_auto_id(
{ {
"id": 7,
"type": "blueprint/save", "type": "blueprint/save",
"path": "test_event_service", "path": "test_event_service",
"yaml": 'blueprint: {name: "name", domain: "automation"}', "yaml": 'blueprint: {name: "name", domain: "automation"}',
@ -285,7 +272,6 @@ async def test_save_existing_file_override(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 7
assert msg["success"] assert msg["success"]
assert msg["result"] == {"overrides_existing": True} assert msg["result"] == {"overrides_existing": True}
assert yaml.safe_load(write_mock.mock_calls[0][1][0]) == { 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.""" """Test saving blueprints with OS error."""
with patch("pathlib.Path.write_text", side_effect=OSError): with patch("pathlib.Path.write_text", side_effect=OSError):
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 8,
"type": "blueprint/save", "type": "blueprint/save",
"path": "test_save", "path": "test_save",
"yaml": "raw_data", "yaml": "raw_data",
@ -318,7 +303,6 @@ async def test_save_file_error(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 8
assert not msg["success"] assert not msg["success"]
@ -329,9 +313,8 @@ async def test_save_invalid_blueprint(
"""Test saving invalid blueprints.""" """Test saving invalid blueprints."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 8,
"type": "blueprint/save", "type": "blueprint/save",
"path": "test_wrong", "path": "test_wrong",
"yaml": "wrong_blueprint", "yaml": "wrong_blueprint",
@ -342,7 +325,6 @@ async def test_save_invalid_blueprint(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 8
assert not msg["success"] assert not msg["success"]
assert msg["error"] == { assert msg["error"] == {
"code": "invalid_format", "code": "invalid_format",
@ -358,9 +340,8 @@ async def test_delete_blueprint(
with patch("pathlib.Path.unlink", return_value=Mock()) as unlink_mock: with patch("pathlib.Path.unlink", return_value=Mock()) as unlink_mock:
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 9,
"type": "blueprint/delete", "type": "blueprint/delete",
"path": "test_delete", "path": "test_delete",
"domain": "automation", "domain": "automation",
@ -370,7 +351,6 @@ async def test_delete_blueprint(
msg = await client.receive_json() msg = await client.receive_json()
assert unlink_mock.mock_calls assert unlink_mock.mock_calls
assert msg["id"] == 9
assert msg["success"] assert msg["success"]
@ -381,9 +361,8 @@ async def test_delete_non_exist_file_blueprint(
"""Test deleting non existing blueprints.""" """Test deleting non existing blueprints."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 9,
"type": "blueprint/delete", "type": "blueprint/delete",
"path": "none_existing", "path": "none_existing",
"domain": "automation", "domain": "automation",
@ -392,7 +371,6 @@ async def test_delete_non_exist_file_blueprint(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["id"] == 9
assert not msg["success"] 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: with patch("pathlib.Path.unlink", return_value=Mock()) as unlink_mock:
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 9,
"type": "blueprint/delete", "type": "blueprint/delete",
"path": "test_event_service.yaml", "path": "test_event_service.yaml",
"domain": "automation", "domain": "automation",
@ -433,7 +410,6 @@ async def test_delete_blueprint_in_use_by_automation(
msg = await client.receive_json() msg = await client.receive_json()
assert not unlink_mock.mock_calls assert not unlink_mock.mock_calls
assert msg["id"] == 9
assert not msg["success"] assert not msg["success"]
assert msg["error"] == { assert msg["error"] == {
"code": "home_assistant_error", "code": "home_assistant_error",