|
3 | 3 | from collections.abc import Generator
|
4 | 4 | from unittest.mock import MagicMock, patch
|
5 | 5 |
|
| 6 | +from googlemaps.exceptions import ApiError, Timeout, TransportError |
6 | 7 | import pytest
|
7 | 8 |
|
8 | 9 | from homeassistant.components.google_travel_time.config_flow import default_options
|
|
13 | 14 | UNITS_IMPERIAL,
|
14 | 15 | UNITS_METRIC,
|
15 | 16 | )
|
| 17 | +from homeassistant.components.google_travel_time.sensor import SCAN_INTERVAL |
16 | 18 | from homeassistant.core import HomeAssistant
|
| 19 | +from homeassistant.util import dt as dt_util |
17 | 20 | from homeassistant.util.unit_system import (
|
18 | 21 | METRIC_SYSTEM,
|
19 | 22 | US_CUSTOMARY_SYSTEM,
|
|
22 | 25 |
|
23 | 26 | from .const import MOCK_CONFIG
|
24 | 27 |
|
25 |
| -from tests.common import MockConfigEntry |
| 28 | +from tests.common import MockConfigEntry, async_fire_time_changed |
26 | 29 |
|
27 | 30 |
|
28 | 31 | @pytest.fixture(name="mock_update")
|
@@ -240,3 +243,25 @@ async def test_sensor_unit_system(
|
240 | 243 |
|
241 | 244 | distance_matrix_mock.assert_called_once()
|
242 | 245 | assert distance_matrix_mock.call_args.kwargs["units"] == expected_unit_option
|
| 246 | + |
| 247 | + |
| 248 | +@pytest.mark.parametrize( |
| 249 | + ("exception"), |
| 250 | + [(ApiError), (TransportError), (Timeout)], |
| 251 | +) |
| 252 | +@pytest.mark.parametrize( |
| 253 | + ("data", "options"), |
| 254 | + [(MOCK_CONFIG, {})], |
| 255 | +) |
| 256 | +async def test_sensor_exception( |
| 257 | + hass: HomeAssistant, |
| 258 | + caplog: pytest.LogCaptureFixture, |
| 259 | + mock_update: MagicMock, |
| 260 | + mock_config: MagicMock, |
| 261 | + exception: Exception, |
| 262 | +) -> None: |
| 263 | + """Test that exception gets caught.""" |
| 264 | + mock_update.side_effect = exception("Errormessage") |
| 265 | + async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) |
| 266 | + await hass.async_block_till_done() |
| 267 | + assert "Error getting travel time" in caplog.text |
0 commit comments