Use freezegun in here_travel_time tests (#99032)

pull/99058/head
Erik Montnemery 2023-08-25 16:02:25 +02:00 committed by GitHub
parent 676f59fded
commit 75743ed947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 19 deletions

View File

@ -2,6 +2,7 @@
from datetime import timedelta
from unittest.mock import MagicMock, patch
from freezegun.api import FrozenDateTimeFactory
from here_routing import (
HERERoutingError,
HERERoutingTooManyRequestsError,
@ -64,7 +65,6 @@ from homeassistant.const import (
)
from homeassistant.core import CoreState, HomeAssistant, State
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from .conftest import RESPONSE, TRANSIT_RESPONSE
from .const import (
@ -662,7 +662,9 @@ async def test_transit_errors(
async def test_routing_rate_limit(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that rate limiting is applied when encountering HTTP 429."""
with patch(
@ -689,9 +691,8 @@ async def test_routing_rate_limit(
"Rate limit for this service has been reached"
),
):
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=DEFAULT_SCAN_INTERVAL + 1)
)
freezer.tick(timedelta(seconds=DEFAULT_SCAN_INTERVAL + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_distance").state == "unavailable"
@ -701,18 +702,17 @@ async def test_routing_rate_limit(
"here_routing.HERERoutingApi.route",
return_value=RESPONSE,
):
async_fire_time_changed(
hass,
utcnow()
+ timedelta(seconds=DEFAULT_SCAN_INTERVAL * BACKOFF_MULTIPLIER + 1),
)
freezer.tick(timedelta(seconds=DEFAULT_SCAN_INTERVAL * BACKOFF_MULTIPLIER + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_distance").state == "13.682"
assert "Resetting update interval to" in caplog.text
async def test_transit_rate_limit(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that rate limiting is applied when encountering HTTP 429."""
with patch(
@ -747,9 +747,8 @@ async def test_transit_rate_limit(
"Rate limit for this service has been reached"
),
):
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=DEFAULT_SCAN_INTERVAL + 1)
)
freezer.tick(timedelta(seconds=DEFAULT_SCAN_INTERVAL + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_distance").state == "unavailable"
@ -759,11 +758,8 @@ async def test_transit_rate_limit(
"here_transit.HERETransitApi.route",
return_value=TRANSIT_RESPONSE,
):
async_fire_time_changed(
hass,
utcnow()
+ timedelta(seconds=DEFAULT_SCAN_INTERVAL * BACKOFF_MULTIPLIER + 1),
)
freezer.tick(timedelta(seconds=DEFAULT_SCAN_INTERVAL * BACKOFF_MULTIPLIER + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get("sensor.test_distance").state == "1.883"
assert "Resetting update interval to" in caplog.text