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
co-authored by epenet Erik Montnemery
parent 1759f58fc1
commit 4f6a25b470
8 changed files with 498 additions and 383 deletions
@@ -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
+62 -62
View File
@@ -1,6 +1,6 @@
"""Tests for the diagnostics data provided by the KNX integration."""
from unittest.mock import patch
import pytest
from xknx.io import DEFAULT_MCAST_GRP, DEFAULT_MCAST_PORT
from homeassistant.components.knx.const import (
@@ -29,37 +29,40 @@ from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
@pytest.mark.parametrize("hass_config", [{}])
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
knx: KNXTestKit,
mock_hass_config: None,
) -> None:
"""Test diagnostics."""
await knx.setup_integration({})
with patch("homeassistant.config.async_hass_config_yaml", return_value={}):
# Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0"
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == {
"config_entry_data": {
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
},
"configuration_error": None,
"configuration_yaml": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
# Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0"
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == {
"config_entry_data": {
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
},
"configuration_error": None,
"configuration_yaml": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
@pytest.mark.parametrize("hass_config", [{"knx": {"wrong_key": {}}}])
async def test_diagnostic_config_error(
hass: HomeAssistant,
mock_hass_config: None,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
knx: KNXTestKit,
@@ -67,32 +70,30 @@ async def test_diagnostic_config_error(
"""Test diagnostics."""
await knx.setup_integration({})
with patch(
"homeassistant.config.async_hass_config_yaml",
return_value={"knx": {"wrong_key": {}}},
):
# Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0"
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == {
"config_entry_data": {
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
},
"configuration_error": "extra keys not allowed @ data['knx']['wrong_key']",
"configuration_yaml": {"wrong_key": {}},
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
# Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0"
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == {
"config_entry_data": {
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
},
"configuration_error": "extra keys not allowed @ data['knx']['wrong_key']",
"configuration_yaml": {"wrong_key": {}},
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
@pytest.mark.parametrize("hass_config", [{}])
async def test_diagnostic_redact(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_hass_config: None,
) -> None:
"""Test diagnostics redacting data."""
mock_config_entry: MockConfigEntry = MockConfigEntry(
@@ -114,25 +115,24 @@ async def test_diagnostic_redact(
knx: KNXTestKit = KNXTestKit(hass, mock_config_entry)
await knx.setup_integration({})
with patch("homeassistant.config.async_hass_config_yaml", return_value={}):
# Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0"
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == {
"config_entry_data": {
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
"knxkeys_password": "**REDACTED**",
"user_password": "**REDACTED**",
"device_authentication": "**REDACTED**",
"backbone_key": "**REDACTED**",
},
"configuration_error": None,
"configuration_yaml": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
# Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0"
assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == {
"config_entry_data": {
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
"knxkeys_password": "**REDACTED**",
"user_password": "**REDACTED**",
"device_authentication": "**REDACTED**",
"backbone_key": "**REDACTED**",
},
"configuration_error": None,
"configuration_yaml": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}