From 5617a738c0847e37a5c81bc5ace4cd5da3ab4f08 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 25 Aug 2023 16:04:51 +0200 Subject: [PATCH] Use freezegun in airly tests (#99028) --- tests/components/airly/test_init.py | 80 ++++++++++++++--------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/tests/components/airly/test_init.py b/tests/components/airly/test_init.py index f360beb8c51..9b69607e6aa 100644 --- a/tests/components/airly/test_init.py +++ b/tests/components/airly/test_init.py @@ -1,7 +1,7 @@ """Test init of Airly integration.""" from typing import Any -from unittest.mock import patch +from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM @@ -11,7 +11,6 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er -from homeassistant.util.dt import utcnow from . import API_POINT_URL, init_integration @@ -99,7 +98,9 @@ async def test_config_with_turned_off_station( async def test_update_interval( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + freezer: FrozenDateTimeFactory, ) -> None: """Test correct update interval when the number of configured instances changes.""" REMAINING_REQUESTS = 15 @@ -135,50 +136,47 @@ async def test_update_interval( assert entry.state is ConfigEntryState.LOADED update_interval = set_update_interval(instances, REMAINING_REQUESTS) - future = utcnow() + update_interval - with patch("homeassistant.util.dt.utcnow") as mock_utcnow: - mock_utcnow.return_value = future - async_fire_time_changed(hass, future) - await hass.async_block_till_done() + freezer.tick(update_interval) + async_fire_time_changed(hass) + await hass.async_block_till_done() - # call_count should increase by one because we have one instance configured - assert aioclient_mock.call_count == 2 + # call_count should increase by one because we have one instance configured + assert aioclient_mock.call_count == 2 - # Now we add the second Airly instance - entry = MockConfigEntry( - domain=DOMAIN, - title="Work", - unique_id="66.66-111.11", - data={ - "api_key": "foo", - "latitude": 66.66, - "longitude": 111.11, - "name": "Work", - }, - ) + # Now we add the second Airly instance + entry = MockConfigEntry( + domain=DOMAIN, + title="Work", + unique_id="66.66-111.11", + data={ + "api_key": "foo", + "latitude": 66.66, + "longitude": 111.11, + "name": "Work", + }, + ) - aioclient_mock.get( - "https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000", - text=load_fixture("valid_station.json", "airly"), - headers=HEADERS, - ) - entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - instances = 2 + aioclient_mock.get( + "https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000", + text=load_fixture("valid_station.json", "airly"), + headers=HEADERS, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + instances = 2 - assert aioclient_mock.call_count == 3 - assert len(hass.config_entries.async_entries(DOMAIN)) == 2 - assert entry.state is ConfigEntryState.LOADED + assert aioclient_mock.call_count == 3 + assert len(hass.config_entries.async_entries(DOMAIN)) == 2 + assert entry.state is ConfigEntryState.LOADED - update_interval = set_update_interval(instances, REMAINING_REQUESTS) - future = utcnow() + update_interval - mock_utcnow.return_value = future - async_fire_time_changed(hass, future) - await hass.async_block_till_done() + update_interval = set_update_interval(instances, REMAINING_REQUESTS) + freezer.tick(update_interval) + async_fire_time_changed(hass) + await hass.async_block_till_done() - # call_count should increase by two because we have two instances configured - assert aioclient_mock.call_count == 5 + # call_count should increase by two because we have two instances configured + assert aioclient_mock.call_count == 5 async def test_unload_entry(