From 75743ed947602cbb0d80b56ab3033de2e3be47de Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 25 Aug 2023 16:02:25 +0200 Subject: [PATCH] Use freezegun in here_travel_time tests (#99032) --- .../here_travel_time/test_sensor.py | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/tests/components/here_travel_time/test_sensor.py b/tests/components/here_travel_time/test_sensor.py index 91439a11d95..28228788cf5 100644 --- a/tests/components/here_travel_time/test_sensor.py +++ b/tests/components/here_travel_time/test_sensor.py @@ -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