Skip to content

Commit

Permalink
Improve TTS test fixtures (#93517)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed May 25, 2023
1 parent e2daffc commit 3fc0c9a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 73 deletions.
59 changes: 50 additions & 9 deletions tests/components/conftest.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand Down Expand Up @@ -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()
60 changes: 60 additions & 0 deletions tests/components/tts/common.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +17,7 @@
TextToSpeechEntity,
TtsAudioType,
Voice,
_get_cache_files,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
Expand All @@ -34,6 +38,62 @@
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:
Expand Down
64 changes: 0 additions & 64 deletions tests/components/tts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 3fc0c9a

Please sign in to comment.