Add type hints for TTS test fixtures (#118704)

This commit is contained in:
epenet 2024-06-03 17:43:18 +02:00 committed by GitHub
parent 32d4431f9b
commit f178467b0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 72 additions and 42 deletions

View file

@ -130,6 +130,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"hass_supervisor_access_token": "str",
"hass_supervisor_user": "MockUser",
"hass_ws_client": "WebSocketGenerator",
"init_tts_cache_dir_side_effect": "Any",
"issue_registry": "IssueRegistry",
"legacy_auth": "LegacyApiPasswordAuthProvider",
"local_auth": "HassAuthProvider",
@ -141,6 +142,9 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"mock_get_source_ip": "_patch",
"mock_hass_config": "None",
"mock_hass_config_yaml": "None",
"mock_tts_cache_dir": "Path",
"mock_tts_get_cache_files": "MagicMock",
"mock_tts_init_cache_dir": "MagicMock",
"mock_zeroconf": "MagicMock",
"mqtt_client_mock": "MqttMockPahoClient",
"mqtt_mock": "MqttMockHAClient",
@ -153,6 +157,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"stub_blueprint_populate": "None",
"tmp_path": "Path",
"tmpdir": "py.path.local",
"tts_mutagen_mock": "MagicMock",
"unused_tcp_port_factory": "Callable[[], int]",
"unused_udp_port_factory": "Callable[[], int]",
}

View file

@ -3,6 +3,7 @@
from __future__ import annotations
from collections.abc import AsyncIterable, Generator
from pathlib import Path
from typing import Any
from unittest.mock import AsyncMock
@ -34,7 +35,7 @@ _TRANSCRIPT = "test transcript"
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -1,6 +1,7 @@
"""Fixtures for cloud tests."""
from collections.abc import AsyncGenerator, Callable, Coroutine
from pathlib import Path
from typing import Any
from unittest.mock import DEFAULT, MagicMock, PropertyMock, patch
@ -180,13 +181,13 @@ def set_cloud_prefs_fixture(
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None:
"""Mock writing tags."""

View file

@ -3,6 +3,7 @@
from __future__ import annotations
from collections.abc import Callable, Generator
from pathlib import Path
from typing import TYPE_CHECKING, Any
from unittest.mock import MagicMock, patch
@ -59,7 +60,7 @@ def stub_blueprint_populate_fixture() -> Generator[None, None, None]:
# TTS test fixtures
@pytest.fixture(name="mock_tts_get_cache_files")
def mock_tts_get_cache_files_fixture():
def mock_tts_get_cache_files_fixture() -> Generator[MagicMock, None, None]:
"""Mock the list TTS cache function."""
from tests.components.tts.common import mock_tts_get_cache_files_fixture_helper
@ -88,8 +89,11 @@ def init_tts_cache_dir_side_effect_fixture() -> Any:
@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
):
tmp_path: Path,
mock_tts_init_cache_dir: MagicMock,
mock_tts_get_cache_files: MagicMock,
request: pytest.FixtureRequest,
) -> Generator[Path, None, None]:
"""Mock the TTS cache dir with empty dir."""
from tests.components.tts.common import mock_tts_cache_dir_fixture_helper
@ -99,7 +103,7 @@ def mock_tts_cache_dir_fixture(
@pytest.fixture(name="tts_mutagen_mock")
def tts_mutagen_mock_fixture():
def tts_mutagen_mock_fixture() -> Generator[MagicMock, None, None]:
"""Mock writing tags."""
from tests.components.tts.common import tts_mutagen_mock_fixture_helper

View file

@ -5,6 +5,7 @@ from __future__ import annotations
import asyncio
from asyncio import Event
from collections.abc import Awaitable, Callable
from pathlib import Path
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
@ -57,7 +58,7 @@ async def load_homeassistant(hass) -> None:
@pytest.fixture(autouse=True)
def mock_tts(mock_tts_cache_dir):
def mock_tts(mock_tts_cache_dir: Path) -> None:
"""Auto mock the tts cache."""

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Generator
from http import HTTPStatus
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
@ -28,12 +29,12 @@ from tests.typing import ClientSessionGenerator
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None:
"""Mock writing tags."""
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -2,6 +2,7 @@
from http import HTTPStatus
import io
from pathlib import Path
from unittest.mock import patch
import wave
@ -33,7 +34,7 @@ def get_empty_wav() -> bytes:
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -1,6 +1,7 @@
"""Tests for Microsoft text-to-speech."""
from http import HTTPStatus
from pathlib import Path
from unittest.mock import patch
from pycsspeechtts import pycsspeechtts
@ -24,7 +25,7 @@ from tests.typing import ClientSessionGenerator
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Generator
from http import HTTPStatus
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
@ -41,7 +42,7 @@ SUPPORT_LANGUAGES = ["de_CH", "de_DE", "en_GB", "en_US"]
TEST_DOMAIN = "test"
def mock_tts_get_cache_files_fixture_helper():
def mock_tts_get_cache_files_fixture_helper() -> Generator[MagicMock, None, None]:
"""Mock the list TTS cache function."""
with patch(
"homeassistant.components.tts._get_cache_files", return_value={}
@ -66,8 +67,11 @@ def init_tts_cache_dir_side_effect_fixture_helper() -> Any:
def mock_tts_cache_dir_fixture_helper(
tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request
):
tmp_path: Path,
mock_tts_init_cache_dir: MagicMock,
mock_tts_get_cache_files: MagicMock,
request: pytest.FixtureRequest,
) -> Generator[Path, None, None]:
"""Mock the TTS cache dir with empty dir."""
mock_tts_init_cache_dir.return_value = str(tmp_path)
@ -88,7 +92,7 @@ def mock_tts_cache_dir_fixture_helper(
pytest.fail("Test failed, see log for details")
def tts_mutagen_mock_fixture_helper():
def tts_mutagen_mock_fixture_helper() -> Generator[MagicMock, None, None]:
"""Mock writing tags."""
with patch(
"homeassistant.components.tts.SpeechManager.write_tags",

View file

@ -4,6 +4,8 @@ From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-info
"""
from collections.abc import Generator
from pathlib import Path
from unittest.mock import MagicMock
import pytest
@ -37,13 +39,13 @@ def pytest_runtest_makereport(item, call):
@pytest.fixture(autouse=True, name="mock_tts_cache_dir")
def mock_tts_cache_dir_fixture_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_fixture_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None:
"""Mock writing tags."""

View file

@ -2,6 +2,7 @@
import asyncio
from http import HTTPStatus
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
@ -187,7 +188,7 @@ async def test_setup_component_no_access_cache_folder(
)
async def test_service(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -248,7 +249,7 @@ async def test_service(
)
async def test_service_default_language(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -309,7 +310,7 @@ async def test_service_default_language(
)
async def test_service_default_special_language(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -366,7 +367,7 @@ async def test_service_default_special_language(
)
async def test_service_language(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -423,7 +424,7 @@ async def test_service_language(
)
async def test_service_wrong_language(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -477,7 +478,7 @@ async def test_service_wrong_language(
)
async def test_service_options(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -561,7 +562,7 @@ class MockEntityWithDefaults(MockTTSEntity):
)
async def test_service_default_options(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -629,7 +630,7 @@ async def test_service_default_options(
)
async def test_merge_default_service_options(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -696,7 +697,7 @@ async def test_merge_default_service_options(
)
async def test_service_wrong_options(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -752,7 +753,7 @@ async def test_service_wrong_options(
)
async def test_service_clear_cache(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -814,7 +815,7 @@ async def test_service_clear_cache(
async def test_service_receive_voice(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -886,7 +887,7 @@ async def test_service_receive_voice(
async def test_service_receive_voice_german(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -994,7 +995,7 @@ async def test_web_view_wrong_filename(
)
async def test_service_without_cache(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
setup: str,
tts_service: str,
service_data: dict[str, Any],
@ -1042,7 +1043,7 @@ class MockEntityBoom(MockTTSEntity):
@pytest.mark.parametrize("mock_provider", [MockProviderBoom(DEFAULT_LANG)])
async def test_setup_legacy_cache_dir(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
mock_provider: MockProvider,
) -> None:
"""Set up a TTS platform with cache and call service without cache."""
@ -1078,7 +1079,7 @@ async def test_setup_legacy_cache_dir(
@pytest.mark.parametrize("mock_tts_entity", [MockEntityBoom(DEFAULT_LANG)])
async def test_setup_cache_dir(
hass: HomeAssistant,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
mock_tts_entity: MockTTSEntity,
) -> None:
"""Set up a TTS platform with cache and call service without cache."""
@ -1185,7 +1186,7 @@ async def test_service_get_tts_error(
async def test_load_cache_legacy_retrieve_without_mem_cache(
hass: HomeAssistant,
mock_provider: MockProvider,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
hass_client: ClientSessionGenerator,
) -> None:
"""Set up component and load cache and get without mem cache."""
@ -1211,7 +1212,7 @@ async def test_load_cache_legacy_retrieve_without_mem_cache(
async def test_load_cache_retrieve_without_mem_cache(
hass: HomeAssistant,
mock_tts_entity: MockTTSEntity,
mock_tts_cache_dir,
mock_tts_cache_dir: Path,
hass_client: ClientSessionGenerator,
) -> None:
"""Set up component and load cache and get without mem cache."""

View file

@ -2,6 +2,8 @@
from __future__ import annotations
from pathlib import Path
import pytest
from homeassistant.components.media_player import (
@ -139,7 +141,7 @@ async def test_platform_setup_with_error(
async def test_service_without_cache_config(
hass: HomeAssistant, mock_tts_cache_dir, mock_tts
hass: HomeAssistant, mock_tts_cache_dir: Path, mock_tts
) -> None:
"""Set up a TTS platform without cache."""
calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)

View file

@ -1,6 +1,8 @@
"""The tests for the VoiceRSS speech platform."""
from http import HTTPStatus
from pathlib import Path
from unittest.mock import MagicMock
import pytest
@ -29,12 +31,12 @@ FORM_DATA = {
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None:
"""Mock writing tags."""
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -2,6 +2,7 @@
import asyncio
import io
from pathlib import Path
import time
from unittest.mock import AsyncMock, Mock, patch
import wave
@ -18,7 +19,7 @@ _MEDIA_ID = "12345"
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -1,6 +1,7 @@
"""Common fixtures for the Wyoming tests."""
from collections.abc import Generator
from pathlib import Path
from unittest.mock import AsyncMock, patch
import pytest
@ -18,7 +19,7 @@ from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir

View file

@ -1,6 +1,8 @@
"""The tests for the Yandex SpeechKit speech platform."""
from http import HTTPStatus
from pathlib import Path
from unittest.mock import MagicMock
import pytest
@ -22,12 +24,12 @@ URL = "https://tts.voicetech.yandex.net/generate?"
@pytest.fixture(autouse=True)
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock):
def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None:
"""Mock writing tags."""
@pytest.fixture(autouse=True)
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path:
"""Mock the TTS cache dir with empty dir."""
return mock_tts_cache_dir