Use Generator as return type for fixtures (#122183)

This commit is contained in:
Marc Mueller 2024-07-19 14:55:23 +02:00 committed by GitHub
parent 53c85a5c9b
commit 2f8dfb424b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 23 additions and 23 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Iterable from collections.abc import Generator
from socket import AddressFamily # pylint: disable=no-name-in-module from socket import AddressFamily # pylint: disable=no-name-in-module
from unittest.mock import Mock, create_autospec, patch, seal from unittest.mock import Mock, create_autospec, patch, seal
@ -32,7 +32,7 @@ NEW_DEVICE_LOCATION = "http://198.51.100.7" + "/dmr_description.xml"
@pytest.fixture @pytest.fixture
def domain_data_mock(hass: HomeAssistant) -> Iterable[Mock]: def domain_data_mock(hass: HomeAssistant) -> Mock:
"""Mock the global data used by this component. """Mock the global data used by this component.
This includes network clients and library object factories. Mocking it This includes network clients and library object factories. Mocking it
@ -114,7 +114,7 @@ def config_entry_mock_no_mac() -> MockConfigEntry:
@pytest.fixture @pytest.fixture
def dmr_device_mock(domain_data_mock: Mock) -> Iterable[Mock]: def dmr_device_mock(domain_data_mock: Mock) -> Generator[Mock]:
"""Mock the async_upnp_client DMR device, initially connected.""" """Mock the async_upnp_client DMR device, initially connected."""
with patch( with patch(
"homeassistant.components.dlna_dmr.media_player.DmrDevice", autospec=True "homeassistant.components.dlna_dmr.media_player.DmrDevice", autospec=True
@ -135,7 +135,7 @@ def dmr_device_mock(domain_data_mock: Mock) -> Iterable[Mock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def ssdp_scanner_mock() -> Iterable[Mock]: def ssdp_scanner_mock() -> Generator[Mock]:
"""Mock the SSDP Scanner.""" """Mock the SSDP Scanner."""
with patch("homeassistant.components.ssdp.Scanner", autospec=True) as mock_scanner: with patch("homeassistant.components.ssdp.Scanner", autospec=True) as mock_scanner:
reg_callback = mock_scanner.return_value.async_register_callback reg_callback = mock_scanner.return_value.async_register_callback
@ -144,14 +144,14 @@ def ssdp_scanner_mock() -> Iterable[Mock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def ssdp_server_mock() -> Iterable[Mock]: def ssdp_server_mock() -> Generator[None]:
"""Mock the SSDP Server.""" """Mock the SSDP Server."""
with patch("homeassistant.components.ssdp.Server", autospec=True): with patch("homeassistant.components.ssdp.Server", autospec=True):
yield yield
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def async_get_local_ip_mock() -> Iterable[Mock]: def async_get_local_ip_mock() -> Generator[Mock]:
"""Mock the async_get_local_ip utility function to prevent network access.""" """Mock the async_get_local_ip utility function to prevent network access."""
with patch( with patch(
"homeassistant.components.dlna_dmr.media_player.async_get_local_ip", "homeassistant.components.dlna_dmr.media_player.async_get_local_ip",

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Iterable from collections.abc import Generator
import dataclasses import dataclasses
import logging import logging
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
@ -89,7 +89,7 @@ MOCK_DISCOVERY = ssdp.SsdpServiceInfo(
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_get_mac_address() -> Iterable[Mock]: def mock_get_mac_address() -> Generator[Mock]:
"""Mock the get_mac_address function to prevent network access and assist tests.""" """Mock the get_mac_address function to prevent network access and assist tests."""
with patch( with patch(
"homeassistant.components.dlna_dmr.config_flow.get_mac_address", autospec=True "homeassistant.components.dlna_dmr.config_flow.get_mac_address", autospec=True
@ -99,7 +99,7 @@ def mock_get_mac_address() -> Iterable[Mock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_setup_entry() -> Iterable[Mock]: def mock_setup_entry() -> Generator[Mock]:
"""Mock async_setup_entry.""" """Mock async_setup_entry."""
with patch( with patch(
"homeassistant.components.dlna_dmr.async_setup_entry", return_value=True "homeassistant.components.dlna_dmr.async_setup_entry", return_value=True

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Iterable from collections.abc import Generator
from unittest.mock import ANY, Mock, patch from unittest.mock import ANY, Mock, patch
from async_upnp_client.aiohttp import AiohttpNotifyServer from async_upnp_client.aiohttp import AiohttpNotifyServer
@ -16,7 +16,7 @@ from homeassistant.core import Event, HomeAssistant
@pytest.fixture @pytest.fixture
def aiohttp_notify_servers_mock() -> Iterable[Mock]: def aiohttp_notify_servers_mock() -> Generator[Mock]:
"""Construct mock AiohttpNotifyServer on demand, eliminating network use. """Construct mock AiohttpNotifyServer on demand, eliminating network use.
This fixture provides a list of the constructed servers. This fixture provides a list of the constructed servers.

View File

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import AsyncIterable, Mapping from collections.abc import AsyncGenerator, Mapping
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from typing import Any from typing import Any
@ -95,7 +95,7 @@ async def mock_entity_id(
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
dmr_device_mock: Mock, dmr_device_mock: Mock,
) -> AsyncIterable[str]: ) -> AsyncGenerator[str]:
"""Fixture to set up a mock DlnaDmrEntity in a connected state. """Fixture to set up a mock DlnaDmrEntity in a connected state.
Yields the entity ID. Cleans up the entity after the test is complete. Yields the entity ID. Cleans up the entity after the test is complete.
@ -145,7 +145,7 @@ async def mock_disconnected_entity_id(
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
dmr_device_mock: Mock, dmr_device_mock: Mock,
) -> AsyncIterable[str]: ) -> AsyncGenerator[str]:
"""Fixture to set up a mock DlnaDmrEntity in a disconnected state. """Fixture to set up a mock DlnaDmrEntity in a disconnected state.
Yields the entity ID. Cleans up the entity after the test is complete. Yields the entity ID. Cleans up the entity after the test is complete.

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import AsyncIterable, Iterable from collections.abc import AsyncGenerator, Generator
from typing import Final, cast from typing import Final, cast
from unittest.mock import AsyncMock, MagicMock, Mock, create_autospec, patch, seal from unittest.mock import AsyncMock, MagicMock, Mock, create_autospec, patch, seal
@ -44,7 +44,7 @@ async def setup_media_source(hass: HomeAssistant) -> None:
@pytest.fixture @pytest.fixture
def upnp_factory_mock() -> Iterable[Mock]: def upnp_factory_mock() -> Generator[Mock]:
"""Mock the UpnpFactory class to construct DMS-style UPnP devices.""" """Mock the UpnpFactory class to construct DMS-style UPnP devices."""
with patch( with patch(
"homeassistant.components.dlna_dms.dms.UpnpFactory", "homeassistant.components.dlna_dms.dms.UpnpFactory",
@ -82,7 +82,7 @@ def upnp_factory_mock() -> Iterable[Mock]:
@pytest.fixture(autouse=True, scope="module") @pytest.fixture(autouse=True, scope="module")
def aiohttp_session_requester_mock() -> Iterable[Mock]: def aiohttp_session_requester_mock() -> Generator[Mock]:
"""Mock the AiohttpSessionRequester to prevent network use.""" """Mock the AiohttpSessionRequester to prevent network use."""
with patch( with patch(
"homeassistant.components.dlna_dms.dms.AiohttpSessionRequester", autospec=True "homeassistant.components.dlna_dms.dms.AiohttpSessionRequester", autospec=True
@ -109,7 +109,7 @@ def config_entry_mock() -> MockConfigEntry:
@pytest.fixture @pytest.fixture
def dms_device_mock(upnp_factory_mock: Mock) -> Iterable[Mock]: def dms_device_mock(upnp_factory_mock: Mock) -> Generator[Mock]:
"""Mock the async_upnp_client DMS device, initially connected.""" """Mock the async_upnp_client DMS device, initially connected."""
with patch( with patch(
"homeassistant.components.dlna_dms.dms.DmsDevice", autospec=True "homeassistant.components.dlna_dms.dms.DmsDevice", autospec=True
@ -130,7 +130,7 @@ def dms_device_mock(upnp_factory_mock: Mock) -> Iterable[Mock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def ssdp_scanner_mock() -> Iterable[Mock]: def ssdp_scanner_mock() -> Generator[Mock]:
"""Mock the SSDP Scanner.""" """Mock the SSDP Scanner."""
with patch("homeassistant.components.ssdp.Scanner", autospec=True) as mock_scanner: with patch("homeassistant.components.ssdp.Scanner", autospec=True) as mock_scanner:
reg_callback = mock_scanner.return_value.async_register_callback reg_callback = mock_scanner.return_value.async_register_callback
@ -139,7 +139,7 @@ def ssdp_scanner_mock() -> Iterable[Mock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def ssdp_server_mock() -> Iterable[Mock]: def ssdp_server_mock() -> Generator[None]:
"""Mock the SSDP Server.""" """Mock the SSDP Server."""
with patch("homeassistant.components.ssdp.Server", autospec=True): with patch("homeassistant.components.ssdp.Server", autospec=True):
yield yield
@ -151,7 +151,7 @@ async def device_source_mock(
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
dms_device_mock: Mock, dms_device_mock: Mock,
) -> AsyncIterable[None]: ) -> AsyncGenerator[None]:
"""Fixture to set up a DmsDeviceSource in a connected state and cleanup at completion.""" """Fixture to set up a DmsDeviceSource in a connected state and cleanup at completion."""
config_entry_mock.add_to_hass(hass) config_entry_mock.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry_mock.entry_id) assert await hass.config_entries.async_setup(config_entry_mock.entry_id)

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Iterable from collections.abc import Generator
import dataclasses import dataclasses
import logging import logging
from typing import Final from typing import Final
@ -68,7 +68,7 @@ MOCK_DISCOVERY: Final = ssdp.SsdpServiceInfo(
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_setup_entry() -> Iterable[Mock]: def mock_setup_entry() -> Generator[Mock]:
"""Avoid setting up the entire integration.""" """Avoid setting up the entire integration."""
with patch( with patch(
"homeassistant.components.dlna_dms.async_setup_entry", "homeassistant.components.dlna_dms.async_setup_entry",