Migrate template tests to use freezegun (#105341)

pull/104998/head
Jan-Philipp Benecke 2023-12-09 09:47:28 +01:00 committed by GitHub
parent f567bf6dfe
commit 4d708f1931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 42 deletions

View File

@ -1,6 +1,7 @@
"""The tests for the Template button platform."""
import datetime as dt
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from homeassistant import setup
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
@ -59,7 +60,9 @@ async def test_missing_required_keys(hass: HomeAssistant) -> None:
assert hass.states.async_all("button") == []
async def test_all_optional_config(hass: HomeAssistant, calls) -> None:
async def test_all_optional_config(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test: including all optional templates is ok."""
with assert_setup_component(1, "template"):
assert await setup.async_setup_component(
@ -98,14 +101,13 @@ async def test_all_optional_config(hass: HomeAssistant, calls) -> None:
)
now = dt.datetime.now(dt.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{CONF_ENTITY_ID: _TEST_OPTIONS_BUTTON},
blocking=True,
)
freezer.move_to(now)
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{CONF_ENTITY_ID: _TEST_OPTIONS_BUTTON},
blocking=True,
)
assert len(calls) == 1
assert calls[0].data["caller"] == _TEST_OPTIONS_BUTTON

View File

@ -1,8 +1,8 @@
"""The tests for the Template automation."""
from datetime import timedelta
from unittest import mock
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
import homeassistant.components.automation as automation
@ -803,56 +803,56 @@ async def test_invalid_for_template_1(hass: HomeAssistant, start_ha, calls) -> N
assert mock_logger.error.called
async def test_if_fires_on_time_change(hass: HomeAssistant, calls) -> None:
async def test_if_fires_on_time_change(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on time changes."""
start_time = dt_util.utcnow() + timedelta(hours=24)
time_that_will_not_match_right_away = start_time.replace(minute=1, second=0)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "template",
"value_template": "{{ utcnow().minute % 2 == 0 }}",
},
"action": {"service": "test.automation"},
}
},
)
await hass.async_block_till_done()
assert len(calls) == 0
freezer.move_to(time_that_will_not_match_right_away)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "template",
"value_template": "{{ utcnow().minute % 2 == 0 }}",
},
"action": {"service": "test.automation"},
}
},
)
await hass.async_block_till_done()
assert len(calls) == 0
# Trigger once (match template)
first_time = start_time.replace(minute=2, second=0)
with patch("homeassistant.util.dt.utcnow", return_value=first_time):
async_fire_time_changed(hass, first_time)
await hass.async_block_till_done()
freezer.move_to(first_time)
async_fire_time_changed(hass, first_time)
await hass.async_block_till_done()
assert len(calls) == 1
# Trigger again (match template)
second_time = start_time.replace(minute=4, second=0)
with patch("homeassistant.util.dt.utcnow", return_value=second_time):
async_fire_time_changed(hass, second_time)
await hass.async_block_till_done()
freezer.move_to(second_time)
async_fire_time_changed(hass, second_time)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert len(calls) == 1
# Trigger again (do not match template)
third_time = start_time.replace(minute=5, second=0)
with patch("homeassistant.util.dt.utcnow", return_value=third_time):
async_fire_time_changed(hass, third_time)
await hass.async_block_till_done()
freezer.move_to(third_time)
async_fire_time_changed(hass, third_time)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert len(calls) == 1
# Trigger again (match template)
forth_time = start_time.replace(minute=8, second=0)
with patch("homeassistant.util.dt.utcnow", return_value=forth_time):
async_fire_time_changed(hass, forth_time)
await hass.async_block_till_done()
freezer.move_to(forth_time)
async_fire_time_changed(hass, forth_time)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert len(calls) == 2