mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Improve script config tests (#84433)
This commit is contained in:
parent
cb13418bab
commit
2c02abfdd4
@ -1380,7 +1380,7 @@ async def test_automation_not_trigger_on_bootstrap(hass):
|
||||
async def test_automation_bad_config_validation(
|
||||
hass: HomeAssistant, caplog, broken_config, problem, details
|
||||
):
|
||||
"""Test bad trigger configuration which can be detected during validation."""
|
||||
"""Test bad automation configuration which can be detected during validation."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
|
@ -1,11 +1,13 @@
|
||||
"""Tests for config/script."""
|
||||
from http import HTTPStatus
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import config
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
|
||||
@ -17,6 +19,89 @@ async def setup_script(hass, script_config, stub_blueprint_populate): # noqa: F
|
||||
assert await async_setup_component(hass, "script", {"script": script_config})
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_config", ({},))
|
||||
async def test_get_script_config(hass: HomeAssistant, hass_client, hass_config_store):
|
||||
"""Test getting script config."""
|
||||
with patch.object(config, "SECTIONS", ["script"]):
|
||||
await async_setup_component(hass, "config", {})
|
||||
|
||||
client = await hass_client()
|
||||
|
||||
hass_config_store["scripts.yaml"] = {
|
||||
"sun": {"alias": "Sun"},
|
||||
"moon": {"alias": "Moon"},
|
||||
}
|
||||
|
||||
resp = await client.get("/api/config/script/config/moon")
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
|
||||
assert result == {"alias": "Moon"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_config", ({},))
|
||||
async def test_update_script_config(
|
||||
hass: HomeAssistant, hass_client, hass_config_store
|
||||
):
|
||||
"""Test updating script config."""
|
||||
with patch.object(config, "SECTIONS", ["script"]):
|
||||
await async_setup_component(hass, "config", {})
|
||||
|
||||
assert sorted(hass.states.async_entity_ids("script")) == []
|
||||
|
||||
client = await hass_client()
|
||||
|
||||
orig_data = {"sun": {"alias": "Sun"}, "moon": {"alias": "Moon"}}
|
||||
hass_config_store["scripts.yaml"] = orig_data
|
||||
|
||||
resp = await client.post(
|
||||
"/api/config/script/config/moon",
|
||||
data=json.dumps({"alias": "Moon updated", "sequence": []}),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert sorted(hass.states.async_entity_ids("script")) == ["script.moon"]
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
assert result == {"result": "ok"}
|
||||
|
||||
new_data = hass_config_store["scripts.yaml"]
|
||||
assert list(new_data["moon"]) == ["alias", "sequence"]
|
||||
assert new_data["moon"] == {"alias": "Moon updated", "sequence": []}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_config", ({},))
|
||||
async def test_update_remove_key_script_config(
|
||||
hass: HomeAssistant, hass_client, hass_config_store
|
||||
):
|
||||
"""Test updating script config while removing a key."""
|
||||
with patch.object(config, "SECTIONS", ["script"]):
|
||||
await async_setup_component(hass, "config", {})
|
||||
|
||||
assert sorted(hass.states.async_entity_ids("script")) == []
|
||||
|
||||
client = await hass_client()
|
||||
|
||||
orig_data = {"sun": {"key": "value"}, "moon": {"key": "value"}}
|
||||
hass_config_store["scripts.yaml"] = orig_data
|
||||
|
||||
resp = await client.post(
|
||||
"/api/config/script/config/moon",
|
||||
data=json.dumps({"sequence": []}),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert sorted(hass.states.async_entity_ids("script")) == ["script.moon"]
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
result = await resp.json()
|
||||
assert result == {"result": "ok"}
|
||||
|
||||
new_data = hass_config_store["scripts.yaml"]
|
||||
assert list(new_data["moon"]) == ["sequence"]
|
||||
assert new_data["moon"] == {"sequence": []}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script_config",
|
||||
(
|
||||
@ -26,7 +111,7 @@ async def setup_script(hass, script_config, stub_blueprint_populate): # noqa: F
|
||||
},
|
||||
),
|
||||
)
|
||||
async def test_delete_script(hass, hass_client, hass_config_store):
|
||||
async def test_delete_script(hass: HomeAssistant, hass_client, hass_config_store):
|
||||
"""Test deleting a script."""
|
||||
with patch.object(config, "SECTIONS", ["script"]):
|
||||
await async_setup_component(hass, "config", {})
|
||||
|
Loading…
x
Reference in New Issue
Block a user