Use freezegun in tomorrowio tests (#99044)
This commit is contained in:
parent
f18a277cac
commit
f99743bedb
1 changed files with 38 additions and 44 deletions
|
@ -1,6 +1,7 @@
|
|||
"""Tests for Tomorrow.io init."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
|
||||
from homeassistant.components.tomorrowio.config_flow import (
|
||||
_get_config_schema,
|
||||
|
@ -11,7 +12,6 @@ from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
|
|||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import MIN_CONFIG
|
||||
|
||||
|
@ -42,10 +42,9 @@ async def test_load_and_unload(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
async def test_update_intervals(
|
||||
hass: HomeAssistant, tomorrowio_config_entry_update
|
||||
hass: HomeAssistant, freezer: FrozenDateTimeFactory, tomorrowio_config_entry_update
|
||||
) -> None:
|
||||
"""Test coordinator update intervals."""
|
||||
now = dt_util.utcnow()
|
||||
data = _get_config_schema(hass, SOURCE_USER)(MIN_CONFIG)
|
||||
data[CONF_NAME] = "test"
|
||||
config_entry = MockConfigEntry(
|
||||
|
@ -56,63 +55,58 @@ async def test_update_intervals(
|
|||
version=1,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
with patch("homeassistant.helpers.update_coordinator.utcnow", return_value=now):
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 1
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 1
|
||||
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
|
||||
# Before the update interval, no updates yet
|
||||
future = now + timedelta(minutes=30)
|
||||
with patch("homeassistant.helpers.update_coordinator.utcnow", return_value=future):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 0
|
||||
freezer.tick(timedelta(minutes=30))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 0
|
||||
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
|
||||
# On the update interval, we get a new update
|
||||
future = now + timedelta(minutes=32)
|
||||
with patch("homeassistant.helpers.update_coordinator.utcnow", return_value=future):
|
||||
async_fire_time_changed(hass, now + timedelta(minutes=32))
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 1
|
||||
freezer.tick(timedelta(minutes=2))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 1
|
||||
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
|
||||
# Adding a second config entry should cause the update interval to double
|
||||
config_entry_2 = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=data,
|
||||
options={CONF_TIMESTEP: 1},
|
||||
unique_id=f"{_get_unique_id(hass, data)}_1",
|
||||
version=1,
|
||||
)
|
||||
config_entry_2.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry_2.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.data[CONF_API_KEY] == config_entry_2.data[CONF_API_KEY]
|
||||
# We should get an immediate call once the new config entry is setup for a
|
||||
# partial update
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 1
|
||||
# Adding a second config entry should cause the update interval to double
|
||||
config_entry_2 = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=data,
|
||||
options={CONF_TIMESTEP: 1},
|
||||
unique_id=f"{_get_unique_id(hass, data)}_1",
|
||||
version=1,
|
||||
)
|
||||
config_entry_2.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry_2.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.data[CONF_API_KEY] == config_entry_2.data[CONF_API_KEY]
|
||||
# We should get an immediate call once the new config entry is setup for a
|
||||
# partial update
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 1
|
||||
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
|
||||
# We should get no new calls on our old interval
|
||||
future = now + timedelta(minutes=64)
|
||||
with patch("homeassistant.helpers.update_coordinator.utcnow", return_value=future):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 0
|
||||
freezer.tick(timedelta(minutes=32))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 0
|
||||
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
|
||||
# We should get two calls on our new interval, one for each entry
|
||||
future = now + timedelta(minutes=96)
|
||||
with patch("homeassistant.helpers.update_coordinator.utcnow", return_value=future):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 2
|
||||
freezer.tick(timedelta(minutes=32))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
assert len(tomorrowio_config_entry_update.call_args_list) == 2
|
||||
|
||||
tomorrowio_config_entry_update.reset_mock()
|
||||
|
|
Loading…
Add table
Reference in a new issue