1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

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
8 changed files with 498 additions and 383 deletions

View File

@@ -44,6 +44,7 @@ from homeassistant.components.websocket_api.auth import (
TYPE_AUTH_REQUIRED,
)
from homeassistant.components.websocket_api.http import URL
from homeassistant.config import YAML_CONFIG_FILE
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import HASSIO_USER_NAME
from homeassistant.core import CoreState, HomeAssistant
@@ -90,6 +91,7 @@ from .common import ( # noqa: E402, isort:skip
get_test_home_assistant,
init_recorder_component,
mock_storage,
patch_yaml_files,
)
from .test_util.aiohttp import ( # noqa: E402, isort:skip
AiohttpClientMocker,
@@ -917,6 +919,66 @@ async def _mqtt_mock_entry(
yield _setup_mqtt_entry
@pytest.fixture
def hass_config() -> ConfigType | None:
"""Fixture to parametrize the content of main configuration using mock_hass_config.
To set a configuration, tests can be marked with:
@pytest.mark.parametrize("hass_config", [{integration: {...}}])
Add the `mock_hass_config: None` fixture to the test.
"""
return None
@pytest.fixture
def mock_hass_config(
hass: HomeAssistant, hass_config: ConfigType | None
) -> Generator[None, None, None]:
"""Fixture to mock the content of main configuration.
Patches homeassistant.config.load_yaml_config_file with `hass_config` parameterized as content.
"""
with patch("homeassistant.config.load_yaml_config_file", return_value=hass_config):
yield
@pytest.fixture
def hass_config_yaml() -> str | None:
"""Fixture to parametrize the content of configuration.yaml file.
To set yaml content, tests can be marked with:
@pytest.mark.parametrize("hass_config_yaml", ["..."])
Add the `mock_hass_config_yaml: None` fixture to the test.
"""
return None
@pytest.fixture
def hass_config_yaml_files(hass_config_yaml: str | None) -> dict[str, str] | None:
"""Fixture to parametrize multiple yaml configuration files.
To set the YAML files to patch, tests can be marked with:
@pytest.mark.parametrize(
"hass_config_yaml_files", [{"configuration.yaml": "..."}]
)
Add the `mock_hass_config_yaml: None` fixture to the test.
"""
return None if hass_config_yaml is None else {YAML_CONFIG_FILE: hass_config_yaml}
@pytest.fixture
def mock_hass_config_yaml(
hass: HomeAssistant, hass_config_yaml_files: dict[str, str] | None
) -> Generator[None, None, None]:
"""Fixture to mock the content of the yaml configuration files.
Patches yaml configuration files using the `hass_config_yaml`
and `hass_config_yaml_files` fixtures.
"""
with patch_yaml_files(hass_config_yaml_files):
yield
@pytest.fixture
async def mqtt_mock_entry_no_yaml_config(
hass: HomeAssistant,