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."""
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(