Add type hints for FixtureRequest in tests (#118779)
This commit is contained in:
parent
16fd19f01a
commit
b54a68750b
27 changed files with 67 additions and 36 deletions
|
@ -152,6 +152,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
|
|||
"mqtt_mock_entry": "MqttMockHAClientGenerator",
|
||||
"recorder_db_url": "str",
|
||||
"recorder_mock": "Recorder",
|
||||
"request": "pytest.FixtureRequest",
|
||||
"requests_mock": "Mocker",
|
||||
"snapshot": "SnapshotAssertion",
|
||||
"socket_enabled": "None",
|
||||
|
|
|
@ -66,7 +66,7 @@ async def mock_aioambient(open_api: OpenAPI):
|
|||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture(request) -> MockConfigEntry:
|
||||
def config_entry_fixture(request: pytest.FixtureRequest) -> MockConfigEntry:
|
||||
"""Mock config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=ambient_network.DOMAIN,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""PyTest fixtures and test helpers."""
|
||||
|
||||
from typing import Any
|
||||
from unittest import mock
|
||||
from unittest.mock import AsyncMock, PropertyMock, patch
|
||||
|
||||
|
@ -71,7 +72,7 @@ def config_fixture():
|
|||
|
||||
|
||||
@pytest.fixture(name="feature")
|
||||
def feature_fixture(request):
|
||||
def feature_fixture(request: pytest.FixtureRequest) -> Any:
|
||||
"""Return an entity wrapper from given fixture name."""
|
||||
return request.getfixturevalue(request.param)
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ def blueprint_1():
|
|||
|
||||
|
||||
@pytest.fixture(params=[False, True])
|
||||
def blueprint_2(request):
|
||||
def blueprint_2(request: pytest.FixtureRequest) -> models.Blueprint:
|
||||
"""Blueprint fixture with default inputs."""
|
||||
blueprint = {
|
||||
"blueprint": {
|
||||
|
|
|
@ -81,7 +81,7 @@ def assert_state(actual: State | None, expected: State | None) -> None:
|
|||
)
|
||||
def add_event_call_service(
|
||||
hass: HomeAssistant,
|
||||
request: Any,
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Callable[dict[str, Any], Awaitable[None]]:
|
||||
"""Fixture for calling the add or create event service."""
|
||||
(domain, service_call, data, target) = request.param
|
||||
|
|
|
@ -70,7 +70,7 @@ from . import MockHDMIDevice, assert_key_press_release
|
|||
],
|
||||
ids=["skip_assert_state", "run_assert_state"],
|
||||
)
|
||||
def assert_state_fixture(hass, request):
|
||||
def assert_state_fixture(request: pytest.FixtureRequest):
|
||||
"""Allow for skipping the assert state changes.
|
||||
|
||||
This is broken in this entity, but we still want to test that
|
||||
|
|
|
@ -131,7 +131,7 @@ def mock_get_appliances() -> Generator[None, Any, None]:
|
|||
|
||||
|
||||
@pytest.fixture(name="appliance")
|
||||
def mock_appliance(request) -> Mock:
|
||||
def mock_appliance(request: pytest.FixtureRequest) -> MagicMock:
|
||||
"""Fixture to mock Appliance."""
|
||||
app = "Washer"
|
||||
if hasattr(request, "param") and request.param:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""The tests for the InfluxDB component."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from dataclasses import dataclass
|
||||
import datetime
|
||||
from http import HTTPStatus
|
||||
|
@ -51,7 +52,9 @@ def mock_batch_timeout(hass, monkeypatch):
|
|||
|
||||
|
||||
@pytest.fixture(name="mock_client")
|
||||
def mock_client_fixture(request):
|
||||
def mock_client_fixture(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Generator[MagicMock, None, None]:
|
||||
"""Patch the InfluxDBClient object with mock for version under test."""
|
||||
if request.param == influxdb.API_VERSION_2:
|
||||
client_target = f"{INFLUX_CLIENT_PATH}V2"
|
||||
|
@ -63,7 +66,7 @@ def mock_client_fixture(request):
|
|||
|
||||
|
||||
@pytest.fixture(name="get_mock_call")
|
||||
def get_mock_call_fixture(request):
|
||||
def get_mock_call_fixture(request: pytest.FixtureRequest):
|
||||
"""Get version specific lambda to make write API call mock."""
|
||||
|
||||
def v2_call(body, precision):
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from http import HTTPStatus
|
||||
|
@ -79,7 +80,9 @@ class Table:
|
|||
|
||||
|
||||
@pytest.fixture(name="mock_client")
|
||||
def mock_client_fixture(request):
|
||||
def mock_client_fixture(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Generator[MagicMock, None, None]:
|
||||
"""Patch the InfluxDBClient object with mock for version under test."""
|
||||
if request.param == API_VERSION_2:
|
||||
client_target = f"{INFLUXDB_CLIENT_PATH}V2"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Fixtures for JVC Projector integration."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -15,7 +15,9 @@ from tests.common import MockConfigEntry
|
|||
|
||||
|
||||
@pytest.fixture(name="mock_device")
|
||||
def fixture_mock_device(request) -> Generator[None, AsyncMock, None]:
|
||||
def fixture_mock_device(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Generator[MagicMock, None, None]:
|
||||
"""Return a mocked JVC Projector device."""
|
||||
target = "homeassistant.components.jvc_projector.JvcProjector"
|
||||
if hasattr(request, "param"):
|
||||
|
|
|
@ -111,7 +111,7 @@ class DescrMediaPlayer(SimpleMediaPlayer):
|
|||
|
||||
|
||||
@pytest.fixture(params=[ExtendedMediaPlayer, SimpleMediaPlayer])
|
||||
def player(hass, request):
|
||||
def player(hass: HomeAssistant, request: pytest.FixtureRequest) -> mp.MediaPlayerEntity:
|
||||
"""Return a media player."""
|
||||
return request.param(hass)
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
"""Tests for Met.no config flow."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from typing import Any
|
||||
from unittest.mock import ANY, patch
|
||||
|
||||
import pytest
|
||||
|
@ -17,7 +19,7 @@ from tests.common import MockConfigEntry
|
|||
|
||||
|
||||
@pytest.fixture(name="met_setup", autouse=True)
|
||||
def met_setup_fixture(request):
|
||||
def met_setup_fixture(request: pytest.FixtureRequest) -> Generator[Any]:
|
||||
"""Patch met setup entry."""
|
||||
if "disable_autouse_fixture" in request.keywords:
|
||||
yield
|
||||
|
|
|
@ -4,6 +4,7 @@ import copy
|
|||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
from unittest import mock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
|
@ -182,7 +183,9 @@ async def do_next_cycle(
|
|||
|
||||
|
||||
@pytest.fixture(name="mock_test_state")
|
||||
async def mock_test_state_fixture(hass, request):
|
||||
async def mock_test_state_fixture(
|
||||
hass: HomeAssistant, request: pytest.FixtureRequest
|
||||
) -> Any:
|
||||
"""Mock restore cache."""
|
||||
mock_restore_cache(hass, request.param)
|
||||
return request.param
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""Tests for the Prosegur alarm control panel device."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from pyprosegur.installation import Status
|
||||
|
@ -35,7 +36,7 @@ def mock_auth():
|
|||
|
||||
|
||||
@pytest.fixture(params=list(Status))
|
||||
def mock_status(request):
|
||||
def mock_status(request: pytest.FixtureRequest) -> Generator[None, None, None]:
|
||||
"""Mock the status of the alarm."""
|
||||
|
||||
install = AsyncMock()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""Test data purging."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
import sqlite3
|
||||
|
@ -58,7 +59,7 @@ TEST_EVENT_TYPES = (
|
|||
|
||||
|
||||
@pytest.fixture(name="use_sqlite")
|
||||
def mock_use_sqlite(request):
|
||||
def mock_use_sqlite(request: pytest.FixtureRequest) -> Generator[None, None, None]:
|
||||
"""Pytest fixture to switch purge method."""
|
||||
with patch(
|
||||
"homeassistant.components.recorder.core.Recorder.dialect_name",
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""Test data purging."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
import sqlite3
|
||||
|
@ -54,7 +55,7 @@ def db_schema_32():
|
|||
|
||||
|
||||
@pytest.fixture(name="use_sqlite")
|
||||
def mock_use_sqlite(request):
|
||||
def mock_use_sqlite(request: pytest.FixtureRequest) -> Generator[None, None, None]:
|
||||
"""Pytest fixture to switch purge method."""
|
||||
with patch(
|
||||
"homeassistant.components.recorder.core.Recorder.dialect_name",
|
||||
|
|
|
@ -25,7 +25,7 @@ def override_platforms() -> Generator[None, None, None]:
|
|||
|
||||
|
||||
@pytest.fixture(autouse=True, name="vehicle_type", params=["zoe_40"])
|
||||
def override_vehicle_type(request) -> str:
|
||||
def override_vehicle_type(request: pytest.FixtureRequest) -> str:
|
||||
"""Parametrize vehicle type."""
|
||||
return request.param
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ def override_platforms() -> Generator[None, None, None]:
|
|||
|
||||
|
||||
@pytest.fixture(autouse=True, name="vehicle_type", params=["zoe_40"])
|
||||
def override_vehicle_type(request) -> str:
|
||||
def override_vehicle_type(request: pytest.FixtureRequest) -> str:
|
||||
"""Parametrize vehicle type."""
|
||||
return request.param
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""Tests for ScreenLogic integration service calls."""
|
||||
|
||||
from collections.abc import AsyncGenerator
|
||||
from typing import Any
|
||||
from unittest.mock import DEFAULT, AsyncMock, patch
|
||||
|
||||
|
@ -49,10 +50,10 @@ def dataset_fixture():
|
|||
@pytest.fixture(name="service_fixture")
|
||||
async def setup_screenlogic_services_fixture(
|
||||
hass: HomeAssistant,
|
||||
request,
|
||||
request: pytest.FixtureRequest,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
):
|
||||
) -> AsyncGenerator[dict[str, Any], None]:
|
||||
"""Define the setup for a patched screenlogic integration."""
|
||||
data = (
|
||||
marker.args[0]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Common fixtures and objects for the Switcher integration tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -16,7 +16,7 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_bridge(request):
|
||||
def mock_bridge(request: pytest.FixtureRequest) -> Generator[MagicMock, None, None]:
|
||||
"""Return a mocked SwitcherBridge."""
|
||||
with (
|
||||
patch(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
from collections.abc import Generator
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from Tami4EdgeAPI.device import Device
|
||||
|
@ -37,7 +37,7 @@ def mock_api(mock__get_devices, mock_get_water_quality):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def mock__get_devices(request):
|
||||
def mock__get_devices(request: pytest.FixtureRequest) -> Generator[None, None, None]:
|
||||
"""Fixture to mock _get_devices which makes a call to the API."""
|
||||
|
||||
side_effect = getattr(request, "param", None)
|
||||
|
@ -60,7 +60,9 @@ def mock__get_devices(request):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_get_water_quality(request):
|
||||
def mock_get_water_quality(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Generator[None, None, None]:
|
||||
"""Fixture to mock get_water_quality which makes a call to the API."""
|
||||
|
||||
side_effect = getattr(request, "param", None)
|
||||
|
@ -98,7 +100,9 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_request_otp(request):
|
||||
def mock_request_otp(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Generator[MagicMock, None, None]:
|
||||
"""Mock request_otp."""
|
||||
|
||||
side_effect = getattr(request, "param", None)
|
||||
|
@ -112,7 +116,7 @@ def mock_request_otp(request):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_submit_otp(request):
|
||||
def mock_submit_otp(request: pytest.FixtureRequest) -> Generator[MagicMock, None, None]:
|
||||
"""Mock submit_otp."""
|
||||
|
||||
side_effect = getattr(request, "param", None)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""Tests for Vallox sensor platform."""
|
||||
|
||||
from datetime import datetime, timedelta, tzinfo
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from vallox_websocket_api import MetricData
|
||||
|
@ -12,7 +13,7 @@ from tests.common import MockConfigEntry
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def set_tz(request):
|
||||
def set_tz(request: pytest.FixtureRequest) -> Any:
|
||||
"""Set the default TZ to the one requested."""
|
||||
request.getfixturevalue(request.param)
|
||||
|
||||
|
|
|
@ -56,14 +56,18 @@ async def test_config_flow_abort(hass: HomeAssistant, mock_get_stations) -> None
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mock_fixture, expected_error", # noqa: PT006
|
||||
("mock_fixture", "expected_error"),
|
||||
[
|
||||
("mock_get_stations_500_error", "cannot_connect"),
|
||||
("mock_get_stations_401_error", "invalid_api_key"),
|
||||
],
|
||||
)
|
||||
async def test_config_errors(
|
||||
hass: HomeAssistant, request, expected_error, mock_fixture, mock_get_stations
|
||||
hass: HomeAssistant,
|
||||
request: pytest.FixtureRequest,
|
||||
expected_error: str,
|
||||
mock_fixture: str,
|
||||
mock_get_stations,
|
||||
) -> None:
|
||||
"""Test the config flow for various error scenarios."""
|
||||
mock_get_stations_bad = request.getfixturevalue(mock_fixture)
|
||||
|
|
|
@ -18,7 +18,7 @@ MOCK_SAID4 = "said4"
|
|||
name="region",
|
||||
params=[("EU", Region.EU), ("US", Region.US)],
|
||||
)
|
||||
def fixture_region(request):
|
||||
def fixture_region(request: pytest.FixtureRequest) -> tuple[str, Region]:
|
||||
"""Return a region for input."""
|
||||
return request.param
|
||||
|
||||
|
@ -31,7 +31,7 @@ def fixture_region(request):
|
|||
("Maytag", Brand.Maytag),
|
||||
],
|
||||
)
|
||||
def fixture_brand(request):
|
||||
def fixture_brand(request: pytest.FixtureRequest) -> tuple[str, Brand]:
|
||||
"""Return a brand for input."""
|
||||
return request.param
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""The tests for the Xiaomi vacuum platform."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from datetime import datetime, time, timedelta
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
@ -140,7 +141,9 @@ new_fanspeeds = {
|
|||
|
||||
|
||||
@pytest.fixture(name="mock_mirobo_fanspeeds", params=[old_fanspeeds, new_fanspeeds])
|
||||
def mirobo_old_speeds_fixture(request):
|
||||
def mirobo_old_speeds_fixture(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Generator[MagicMock, None, None]:
|
||||
"""Fixture for testing both types of fanspeeds."""
|
||||
mock_vacuum = MagicMock()
|
||||
mock_vacuum.status().battery = 32
|
||||
|
|
|
@ -385,7 +385,7 @@ def zha_device_restored(hass, zigpy_app_controller, setup_zha):
|
|||
|
||||
|
||||
@pytest.fixture(params=["zha_device_joined", "zha_device_restored"])
|
||||
def zha_device_joined_restored(request):
|
||||
def zha_device_joined_restored(request: pytest.FixtureRequest):
|
||||
"""Join or restore ZHA device."""
|
||||
named_method = request.getfixturevalue(request.param)
|
||||
named_method.name = request.param
|
||||
|
|
|
@ -309,7 +309,7 @@ async def test_ota_sw_version(
|
|||
)
|
||||
async def test_device_restore_availability(
|
||||
hass: HomeAssistant,
|
||||
request,
|
||||
request: pytest.FixtureRequest,
|
||||
device,
|
||||
last_seen_delta,
|
||||
is_available,
|
||||
|
|
Loading…
Add table
Reference in a new issue