Improve type hints in google_travel_time tests (#121171)

This commit is contained in:
epenet 2024-07-04 10:27:55 +02:00 committed by GitHub
parent dd8ba0828a
commit 1f22f0d89b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 23 deletions

View File

@ -1,17 +1,22 @@
"""Fixtures for Google Time Travel tests.""" """Fixtures for Google Time Travel tests."""
from unittest.mock import patch from collections.abc import Generator
from typing import Any
from unittest.mock import MagicMock, patch
from googlemaps.exceptions import ApiError, Timeout, TransportError from googlemaps.exceptions import ApiError, Timeout, TransportError
import pytest import pytest
from homeassistant.components.google_travel_time.const import DOMAIN from homeassistant.components.google_travel_time.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.fixture(name="mock_config") @pytest.fixture(name="mock_config")
async def mock_config_fixture(hass, data, options): async def mock_config_fixture(
hass: HomeAssistant, data: dict[str, Any], options: dict[str, Any]
) -> MockConfigEntry:
"""Mock a Google Travel Time config entry.""" """Mock a Google Travel Time config entry."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -26,7 +31,7 @@ async def mock_config_fixture(hass, data, options):
@pytest.fixture(name="bypass_setup") @pytest.fixture(name="bypass_setup")
def bypass_setup_fixture(): def bypass_setup_fixture() -> Generator[None]:
"""Bypass entry setup.""" """Bypass entry setup."""
with patch( with patch(
"homeassistant.components.google_travel_time.async_setup_entry", "homeassistant.components.google_travel_time.async_setup_entry",
@ -36,7 +41,7 @@ def bypass_setup_fixture():
@pytest.fixture(name="bypass_platform_setup") @pytest.fixture(name="bypass_platform_setup")
def bypass_platform_setup_fixture(): def bypass_platform_setup_fixture() -> Generator[None]:
"""Bypass platform setup.""" """Bypass platform setup."""
with patch( with patch(
"homeassistant.components.google_travel_time.sensor.async_setup_entry", "homeassistant.components.google_travel_time.sensor.async_setup_entry",
@ -46,7 +51,7 @@ def bypass_platform_setup_fixture():
@pytest.fixture(name="validate_config_entry") @pytest.fixture(name="validate_config_entry")
def validate_config_entry_fixture(): def validate_config_entry_fixture() -> Generator[MagicMock]:
"""Return valid config entry.""" """Return valid config entry."""
with ( with (
patch("homeassistant.components.google_travel_time.helpers.Client"), patch("homeassistant.components.google_travel_time.helpers.Client"),
@ -59,24 +64,24 @@ def validate_config_entry_fixture():
@pytest.fixture(name="invalidate_config_entry") @pytest.fixture(name="invalidate_config_entry")
def invalidate_config_entry_fixture(validate_config_entry): def invalidate_config_entry_fixture(validate_config_entry: MagicMock) -> None:
"""Return invalid config entry.""" """Return invalid config entry."""
validate_config_entry.side_effect = ApiError("test") validate_config_entry.side_effect = ApiError("test")
@pytest.fixture(name="invalid_api_key") @pytest.fixture(name="invalid_api_key")
def invalid_api_key_fixture(validate_config_entry): def invalid_api_key_fixture(validate_config_entry: MagicMock) -> None:
"""Throw a REQUEST_DENIED ApiError.""" """Throw a REQUEST_DENIED ApiError."""
validate_config_entry.side_effect = ApiError("REQUEST_DENIED", "Invalid API key.") validate_config_entry.side_effect = ApiError("REQUEST_DENIED", "Invalid API key.")
@pytest.fixture(name="timeout") @pytest.fixture(name="timeout")
def timeout_fixture(validate_config_entry): def timeout_fixture(validate_config_entry: MagicMock) -> None:
"""Throw a Timeout exception.""" """Throw a Timeout exception."""
validate_config_entry.side_effect = Timeout() validate_config_entry.side_effect = Timeout()
@pytest.fixture(name="transport_error") @pytest.fixture(name="transport_error")
def transport_error_fixture(validate_config_entry): def transport_error_fixture(validate_config_entry: MagicMock) -> None:
"""Throw a TransportError exception.""" """Throw a TransportError exception."""
validate_config_entry.side_effect = TransportError("Unknown.") validate_config_entry.side_effect = TransportError("Unknown.")

View File

@ -29,6 +29,8 @@ from homeassistant.data_entry_flow import FlowResultType
from .const import MOCK_CONFIG, RECONFIGURE_CONFIG from .const import MOCK_CONFIG, RECONFIGURE_CONFIG
from tests.common import MockConfigEntry
async def assert_common_reconfigure_steps( async def assert_common_reconfigure_steps(
hass: HomeAssistant, reconfigure_result: config_entries.ConfigFlowResult hass: HomeAssistant, reconfigure_result: config_entries.ConfigFlowResult
@ -194,7 +196,7 @@ async def test_malformed_api_key(hass: HomeAssistant) -> None:
], ],
) )
@pytest.mark.usefixtures("validate_config_entry", "bypass_setup") @pytest.mark.usefixtures("validate_config_entry", "bypass_setup")
async def test_reconfigure(hass: HomeAssistant, mock_config) -> None: async def test_reconfigure(hass: HomeAssistant, mock_config: MockConfigEntry) -> None:
"""Test reconfigure flow.""" """Test reconfigure flow."""
reconfigure_result = await hass.config_entries.flow.async_init( reconfigure_result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -223,7 +225,7 @@ async def test_reconfigure(hass: HomeAssistant, mock_config) -> None:
) )
@pytest.mark.usefixtures("invalidate_config_entry") @pytest.mark.usefixtures("invalidate_config_entry")
async def test_reconfigure_invalid_config_entry( async def test_reconfigure_invalid_config_entry(
hass: HomeAssistant, mock_config hass: HomeAssistant, mock_config: MockConfigEntry
) -> None: ) -> None:
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -259,7 +261,9 @@ async def test_reconfigure_invalid_config_entry(
], ],
) )
@pytest.mark.usefixtures("invalid_api_key") @pytest.mark.usefixtures("invalid_api_key")
async def test_reconfigure_invalid_api_key(hass: HomeAssistant, mock_config) -> None: async def test_reconfigure_invalid_api_key(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -293,7 +297,9 @@ async def test_reconfigure_invalid_api_key(hass: HomeAssistant, mock_config) ->
], ],
) )
@pytest.mark.usefixtures("transport_error") @pytest.mark.usefixtures("transport_error")
async def test_reconfigure_transport_error(hass: HomeAssistant, mock_config) -> None: async def test_reconfigure_transport_error(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -327,7 +333,9 @@ async def test_reconfigure_transport_error(hass: HomeAssistant, mock_config) ->
], ],
) )
@pytest.mark.usefixtures("timeout") @pytest.mark.usefixtures("timeout")
async def test_reconfigure_timeout(hass: HomeAssistant, mock_config) -> None: async def test_reconfigure_timeout(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -361,7 +369,7 @@ async def test_reconfigure_timeout(hass: HomeAssistant, mock_config) -> None:
], ],
) )
@pytest.mark.usefixtures("validate_config_entry") @pytest.mark.usefixtures("validate_config_entry")
async def test_options_flow(hass: HomeAssistant, mock_config) -> None: async def test_options_flow(hass: HomeAssistant, mock_config: MockConfigEntry) -> None:
"""Test options flow.""" """Test options flow."""
result = await hass.config_entries.options.async_init( result = await hass.config_entries.options.async_init(
mock_config.entry_id, data=None mock_config.entry_id, data=None
@ -422,7 +430,9 @@ async def test_options_flow(hass: HomeAssistant, mock_config) -> None:
], ],
) )
@pytest.mark.usefixtures("validate_config_entry") @pytest.mark.usefixtures("validate_config_entry")
async def test_options_flow_departure_time(hass: HomeAssistant, mock_config) -> None: async def test_options_flow_departure_time(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test options flow with departure time.""" """Test options flow with departure time."""
result = await hass.config_entries.options.async_init( result = await hass.config_entries.options.async_init(
mock_config.entry_id, data=None mock_config.entry_id, data=None
@ -492,7 +502,9 @@ async def test_options_flow_departure_time(hass: HomeAssistant, mock_config) ->
], ],
) )
@pytest.mark.usefixtures("validate_config_entry") @pytest.mark.usefixtures("validate_config_entry")
async def test_reset_departure_time(hass: HomeAssistant, mock_config) -> None: async def test_reset_departure_time(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test resetting departure time.""" """Test resetting departure time."""
result = await hass.config_entries.options.async_init( result = await hass.config_entries.options.async_init(
mock_config.entry_id, data=None mock_config.entry_id, data=None
@ -538,7 +550,9 @@ async def test_reset_departure_time(hass: HomeAssistant, mock_config) -> None:
], ],
) )
@pytest.mark.usefixtures("validate_config_entry") @pytest.mark.usefixtures("validate_config_entry")
async def test_reset_arrival_time(hass: HomeAssistant, mock_config) -> None: async def test_reset_arrival_time(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test resetting arrival time.""" """Test resetting arrival time."""
result = await hass.config_entries.options.async_init( result = await hass.config_entries.options.async_init(
mock_config.entry_id, data=None mock_config.entry_id, data=None
@ -582,7 +596,9 @@ async def test_reset_arrival_time(hass: HomeAssistant, mock_config) -> None:
], ],
) )
@pytest.mark.usefixtures("validate_config_entry") @pytest.mark.usefixtures("validate_config_entry")
async def test_reset_options_flow_fields(hass: HomeAssistant, mock_config) -> None: async def test_reset_options_flow_fields(
hass: HomeAssistant, mock_config: MockConfigEntry
) -> None:
"""Test resetting options flow fields that are not time related to None.""" """Test resetting options flow fields that are not time related to None."""
result = await hass.config_entries.options.async_init( result = await hass.config_entries.options.async_init(
mock_config.entry_id, data=None mock_config.entry_id, data=None

View File

@ -1,6 +1,7 @@
"""Test the Google Maps Travel Time sensors.""" """Test the Google Maps Travel Time sensors."""
from unittest.mock import patch from collections.abc import Generator
from unittest.mock import MagicMock, patch
import pytest import pytest
@ -25,7 +26,7 @@ from tests.common import MockConfigEntry
@pytest.fixture(name="mock_update") @pytest.fixture(name="mock_update")
def mock_update_fixture(): def mock_update_fixture() -> Generator[MagicMock]:
"""Mock an update to the sensor.""" """Mock an update to the sensor."""
with ( with (
patch("homeassistant.components.google_travel_time.sensor.Client"), patch("homeassistant.components.google_travel_time.sensor.Client"),
@ -56,7 +57,7 @@ def mock_update_fixture():
@pytest.fixture(name="mock_update_duration") @pytest.fixture(name="mock_update_duration")
def mock_update_duration_fixture(mock_update): def mock_update_duration_fixture(mock_update: MagicMock) -> MagicMock:
"""Mock an update to the sensor returning no duration_in_traffic.""" """Mock an update to the sensor returning no duration_in_traffic."""
mock_update.return_value = { mock_update.return_value = {
"rows": [ "rows": [
@ -77,7 +78,7 @@ def mock_update_duration_fixture(mock_update):
@pytest.fixture(name="mock_update_empty") @pytest.fixture(name="mock_update_empty")
def mock_update_empty_fixture(mock_update): def mock_update_empty_fixture(mock_update: MagicMock) -> MagicMock:
"""Mock an update to the sensor with an empty response.""" """Mock an update to the sensor with an empty response."""
mock_update.return_value = None mock_update.return_value = None
return mock_update return mock_update