Allow parameterizing YAML config in tests (#87981)

* Add fixture to parameterize yaml config

* Apply to more tests

* Re-add @fixture label

* Add fixtures to patch yaml content and targets

* Typo

* Improve docstr

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update references to mock_yaml_configuration

* Apply new fixtures

* Apply to check_config tests

* Follow up comments

* Rename fixtures, update docstr

* Split paths

* Patch load_yaml_config_file instead

* sort

* Fix tests

* improve docst

* Rename fixtures

* sorting

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Improve docstr

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Improve docstr

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Improve docstr

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Improve docstr

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Improve docstr

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Improve docstr

Co-authored-by: Erik Montnemery <erik@montnemery.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Jan Bouwhuis 2023-02-20 16:57:12 +01:00 committed by GitHub
parent 1759f58fc1
commit 4f6a25b470
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 498 additions and 383 deletions

View file

@ -1,29 +1,39 @@
"""The tests for the Event automation."""
from unittest.mock import AsyncMock, patch
from unittest.mock import patch
import pytest
import homeassistant.components.automation as automation
from homeassistant.core import CoreState
from homeassistant.core import CoreState, HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component
from tests.common import async_mock_service
async def test_if_fires_on_hass_start(hass):
@pytest.mark.parametrize(
"hass_config",
[
{
automation.DOMAIN: {
"alias": "hello",
"trigger": {"platform": "homeassistant", "event": "start"},
"action": {
"service": "test.automation",
"data_template": {"id": "{{ trigger.id}}"},
},
}
}
],
)
async def test_if_fires_on_hass_start(
hass: HomeAssistant, mock_hass_config: None, hass_config: ConfigType | None
) -> None:
"""Test the firing when Home Assistant starts."""
calls = async_mock_service(hass, "test", "automation")
hass.state = CoreState.not_running
config = {
automation.DOMAIN: {
"alias": "hello",
"trigger": {"platform": "homeassistant", "event": "start"},
"action": {
"service": "test.automation",
"data_template": {"id": "{{ trigger.id}}"},
},
}
}
assert await async_setup_component(hass, automation.DOMAIN, config)
assert await async_setup_component(hass, automation.DOMAIN, hass_config)
assert automation.is_on(hass, "automation.hello")
assert len(calls) == 0
@ -32,13 +42,9 @@ async def test_if_fires_on_hass_start(hass):
assert automation.is_on(hass, "automation.hello")
assert len(calls) == 1
with patch(
"homeassistant.config.async_hass_config_yaml",
AsyncMock(return_value=config),
):
await hass.services.async_call(
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True
)
await hass.services.async_call(
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True
)
assert automation.is_on(hass, "automation.hello")
assert len(calls) == 1