Use freezegun in airly tests (#99028)

This commit is contained in:
Erik Montnemery 2023-08-25 16:04:51 +02:00 committed by GitHub
parent dd39e8fe64
commit 5617a738c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
"""Test init of Airly integration.""" """Test init of Airly integration."""
from typing import Any from typing import Any
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM 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.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er 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 from . import API_POINT_URL, init_integration
@ -99,7 +98,9 @@ async def test_config_with_turned_off_station(
async def test_update_interval( async def test_update_interval(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test correct update interval when the number of configured instances changes.""" """Test correct update interval when the number of configured instances changes."""
REMAINING_REQUESTS = 15 REMAINING_REQUESTS = 15
@ -135,50 +136,47 @@ async def test_update_interval(
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
update_interval = set_update_interval(instances, REMAINING_REQUESTS) update_interval = set_update_interval(instances, REMAINING_REQUESTS)
future = utcnow() + update_interval freezer.tick(update_interval)
with patch("homeassistant.util.dt.utcnow") as mock_utcnow: async_fire_time_changed(hass)
mock_utcnow.return_value = future await hass.async_block_till_done()
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
# call_count should increase by one because we have one instance configured # call_count should increase by one because we have one instance configured
assert aioclient_mock.call_count == 2 assert aioclient_mock.call_count == 2
# Now we add the second Airly instance # Now we add the second Airly instance
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
title="Work", title="Work",
unique_id="66.66-111.11", unique_id="66.66-111.11",
data={ data={
"api_key": "foo", "api_key": "foo",
"latitude": 66.66, "latitude": 66.66,
"longitude": 111.11, "longitude": 111.11,
"name": "Work", "name": "Work",
}, },
) )
aioclient_mock.get( aioclient_mock.get(
"https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000", "https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000",
text=load_fixture("valid_station.json", "airly"), text=load_fixture("valid_station.json", "airly"),
headers=HEADERS, headers=HEADERS,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
instances = 2 instances = 2
assert aioclient_mock.call_count == 3 assert aioclient_mock.call_count == 3
assert len(hass.config_entries.async_entries(DOMAIN)) == 2 assert len(hass.config_entries.async_entries(DOMAIN)) == 2
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
update_interval = set_update_interval(instances, REMAINING_REQUESTS) update_interval = set_update_interval(instances, REMAINING_REQUESTS)
future = utcnow() + update_interval freezer.tick(update_interval)
mock_utcnow.return_value = future async_fire_time_changed(hass)
async_fire_time_changed(hass, future) await hass.async_block_till_done()
await hass.async_block_till_done()
# call_count should increase by two because we have two instances configured # call_count should increase by two because we have two instances configured
assert aioclient_mock.call_count == 5 assert aioclient_mock.call_count == 5
async def test_unload_entry( async def test_unload_entry(