core/tests/components/google_travel_time/conftest.py

88 lines
2.7 KiB
Python

"""Fixtures for Google Time Travel tests."""
from collections.abc import Generator
from typing import Any
from unittest.mock import MagicMock, patch
from googlemaps.exceptions import ApiError, Timeout, TransportError
import pytest
from homeassistant.components.google_travel_time.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.fixture(name="mock_config")
async def mock_config_fixture(
hass: HomeAssistant, data: dict[str, Any], options: dict[str, Any]
) -> MockConfigEntry:
"""Mock a Google Travel Time config entry."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data=data,
options=options,
entry_id="test",
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry
@pytest.fixture(name="bypass_setup")
def bypass_setup_fixture() -> Generator[None]:
"""Bypass entry setup."""
with patch(
"homeassistant.components.google_travel_time.async_setup_entry",
return_value=True,
):
yield
@pytest.fixture(name="bypass_platform_setup")
def bypass_platform_setup_fixture() -> Generator[None]:
"""Bypass platform setup."""
with patch(
"homeassistant.components.google_travel_time.sensor.async_setup_entry",
return_value=True,
):
yield
@pytest.fixture(name="validate_config_entry")
def validate_config_entry_fixture() -> Generator[MagicMock]:
"""Return valid config entry."""
with (
patch("homeassistant.components.google_travel_time.helpers.Client"),
patch(
"homeassistant.components.google_travel_time.helpers.distance_matrix"
) as distance_matrix_mock,
):
distance_matrix_mock.return_value = None
yield distance_matrix_mock
@pytest.fixture(name="invalidate_config_entry")
def invalidate_config_entry_fixture(validate_config_entry: MagicMock) -> None:
"""Return invalid config entry."""
validate_config_entry.side_effect = ApiError("test")
@pytest.fixture(name="invalid_api_key")
def invalid_api_key_fixture(validate_config_entry: MagicMock) -> None:
"""Throw a REQUEST_DENIED ApiError."""
validate_config_entry.side_effect = ApiError("REQUEST_DENIED", "Invalid API key.")
@pytest.fixture(name="timeout")
def timeout_fixture(validate_config_entry: MagicMock) -> None:
"""Throw a Timeout exception."""
validate_config_entry.side_effect = Timeout()
@pytest.fixture(name="transport_error")
def transport_error_fixture(validate_config_entry: MagicMock) -> None:
"""Throw a TransportError exception."""
validate_config_entry.side_effect = TransportError("Unknown.")