1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00

Improve TTS test fixtures (#93517)

This commit is contained in:
Erik Montnemery 2023-05-25 11:59:20 +02:00 committed by GitHub
parent e2daffc117
commit 3fc0c9a325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 110 additions and 73 deletions

View File

@ -1,17 +1,10 @@
"""Fixtures for component testing."""
from collections.abc import Generator
from unittest.mock import patch
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from tests.components.tts.conftest import ( # noqa: F401, pylint: disable=unused-import
init_tts_cache_dir_side_effect_fixture,
mock_tts_cache_dir_fixture,
mock_tts_get_cache_files_fixture,
mock_tts_init_cache_dir_fixture,
tts_mutagen_mock_fixture,
)
@pytest.fixture(scope="session", autouse=True)
def patch_zeroconf_multiple_catcher() -> Generator[None, None, None]:
@ -40,3 +33,51 @@ def entity_registry_enabled_by_default() -> Generator[None, None, None]:
return_value=True,
):
yield
@pytest.fixture(name="mock_tts_get_cache_files")
def mock_tts_get_cache_files_fixture():
"""Mock the list TTS cache function."""
from tests.components.tts.common import mock_tts_get_cache_files_fixture_helper
yield from mock_tts_get_cache_files_fixture_helper()
@pytest.fixture(name="mock_tts_init_cache_dir")
def mock_tts_init_cache_dir_fixture(
init_tts_cache_dir_side_effect: Any,
) -> Generator[MagicMock, None, None]:
"""Mock the TTS cache dir in memory."""
from tests.components.tts.common import mock_tts_init_cache_dir_fixture_helper
yield from mock_tts_init_cache_dir_fixture_helper(init_tts_cache_dir_side_effect)
@pytest.fixture(name="init_tts_cache_dir_side_effect")
def init_tts_cache_dir_side_effect_fixture() -> Any:
"""Return the cache dir."""
from tests.components.tts.common import (
init_tts_cache_dir_side_effect_fixture_helper,
)
return init_tts_cache_dir_side_effect_fixture_helper()
@pytest.fixture(name="mock_tts_cache_dir")
def mock_tts_cache_dir_fixture(
tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request
):
"""Mock the TTS cache dir with empty dir."""
from tests.components.tts.common import mock_tts_cache_dir_fixture_helper
yield from mock_tts_cache_dir_fixture_helper(
tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request
)
@pytest.fixture(name="tts_mutagen_mock")
def tts_mutagen_mock_fixture():
"""Mock writing tags."""
from tests.components.tts.common import tts_mutagen_mock_fixture_helper
yield from tts_mutagen_mock_fixture_helper()

View File

@ -1,8 +1,11 @@
"""Provide common tests tools for tts."""
from __future__ import annotations
from collections.abc import Generator
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
import voluptuous as vol
from homeassistant.components import media_source
@ -14,6 +17,7 @@ from homeassistant.components.tts import (
TextToSpeechEntity,
TtsAudioType,
Voice,
_get_cache_files,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
@ -34,6 +38,62 @@ SUPPORT_LANGUAGES = ["de_CH", "de_DE", "en_GB", "en_US"]
TEST_DOMAIN = "test"
def mock_tts_get_cache_files_fixture_helper():
"""Mock the list TTS cache function."""
with patch(
"homeassistant.components.tts._get_cache_files", return_value={}
) as mock_cache_files:
yield mock_cache_files
def mock_tts_init_cache_dir_fixture_helper(
init_tts_cache_dir_side_effect: Any,
) -> Generator[MagicMock, None, None]:
"""Mock the TTS cache dir in memory."""
with patch(
"homeassistant.components.tts._init_tts_cache_dir",
side_effect=init_tts_cache_dir_side_effect,
) as mock_cache_dir:
yield mock_cache_dir
def init_tts_cache_dir_side_effect_fixture_helper() -> Any:
"""Return the cache dir."""
return None
def mock_tts_cache_dir_fixture_helper(
tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request
):
"""Mock the TTS cache dir with empty dir."""
mock_tts_init_cache_dir.return_value = str(tmp_path)
# Restore original get cache files behavior, we're working with a real dir.
mock_tts_get_cache_files.side_effect = _get_cache_files
yield tmp_path
if not hasattr(request.node, "rep_call") or request.node.rep_call.passed:
return
# Print contents of dir if failed
print("Content of dir for", request.node.nodeid) # noqa: T201
for fil in tmp_path.iterdir():
print(fil.relative_to(tmp_path)) # noqa: T201
# To show the log.
pytest.fail("Test failed, see log for details")
def tts_mutagen_mock_fixture_helper():
"""Mock writing tags."""
with patch(
"homeassistant.components.tts.SpeechManager.write_tags",
side_effect=lambda *args: args[1],
) as mock_write_tags:
yield mock_write_tags
async def get_media_source_url(hass: HomeAssistant, media_content_id: str) -> str:
"""Get the media source url."""
if media_source.DOMAIN not in hass.config.components:

View File

@ -3,12 +3,9 @@
From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
"""
from collections.abc import Generator
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.tts import _get_cache_files
from homeassistant.config import async_process_ha_core_config
from homeassistant.config_entries import ConfigFlow
from homeassistant.core import HomeAssistant
@ -38,73 +35,12 @@ def pytest_runtest_makereport(item, call):
setattr(item, f"rep_{rep.when}", rep)
@pytest.fixture(name="mock_tts_get_cache_files")
def mock_tts_get_cache_files_fixture():
"""Mock the list TTS cache function."""
with patch(
"homeassistant.components.tts._get_cache_files", return_value={}
) as mock_cache_files:
yield mock_cache_files
@pytest.fixture(name="mock_tts_init_cache_dir")
def mock_tts_init_cache_dir_fixture(
init_tts_cache_dir_side_effect: Any,
) -> Generator[MagicMock, None, None]:
"""Mock the TTS cache dir in memory."""
with patch(
"homeassistant.components.tts._init_tts_cache_dir",
side_effect=init_tts_cache_dir_side_effect,
) as mock_cache_dir:
yield mock_cache_dir
@pytest.fixture(name="init_tts_cache_dir_side_effect")
def init_tts_cache_dir_side_effect_fixture() -> Any:
"""Return the cache dir."""
return None
@pytest.fixture(name="mock_tts_cache_dir")
def mock_tts_cache_dir_fixture(
tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request
):
"""Mock the TTS cache dir with empty dir."""
mock_tts_init_cache_dir.return_value = str(tmp_path)
# Restore original get cache files behavior, we're working with a real dir.
mock_tts_get_cache_files.side_effect = _get_cache_files
yield tmp_path
if not hasattr(request.node, "rep_call") or request.node.rep_call.passed:
return
# Print contents of dir if failed
print("Content of dir for", request.node.nodeid) # noqa: T201
for fil in tmp_path.iterdir():
print(fil.relative_to(tmp_path)) # noqa: T201
# To show the log.
pytest.fail("Test failed, see log for details")
@pytest.fixture(autouse=True, name="mock_tts_cache_dir")
def mock_tts_cache_dir_fixture_autouse(mock_tts_cache_dir):
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
@pytest.fixture(name="tts_mutagen_mock")
def tts_mutagen_mock_fixture():
"""Mock writing tags."""
with patch(
"homeassistant.components.tts.SpeechManager.write_tags",
side_effect=lambda *args: args[1],
) as mock_write_tags:
yield mock_write_tags
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
"""Mock writing tags."""