mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Migrate template tests to use freezegun (#105341)
This commit is contained in:
parent
f567bf6dfe
commit
4d708f1931
@ -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,8 +101,7 @@ 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):
|
||||
freezer.move_to(now)
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
|
@ -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,13 +803,13 @@ 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
|
||||
):
|
||||
freezer.move_to(time_that_will_not_match_right_away)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@ -828,14 +828,14 @@ async def test_if_fires_on_time_change(hass: HomeAssistant, calls) -> None:
|
||||
|
||||
# Trigger once (match template)
|
||||
first_time = start_time.replace(minute=2, second=0)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=first_time):
|
||||
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):
|
||||
freezer.move_to(second_time)
|
||||
async_fire_time_changed(hass, second_time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
@ -843,7 +843,7 @@ async def test_if_fires_on_time_change(hass: HomeAssistant, calls) -> None:
|
||||
|
||||
# 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):
|
||||
freezer.move_to(third_time)
|
||||
async_fire_time_changed(hass, third_time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
@ -851,7 +851,7 @@ async def test_if_fires_on_time_change(hass: HomeAssistant, calls) -> None:
|
||||
|
||||
# Trigger again (match template)
|
||||
forth_time = start_time.replace(minute=8, second=0)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=forth_time):
|
||||
freezer.move_to(forth_time)
|
||||
async_fire_time_changed(hass, forth_time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
Loading…
x
Reference in New Issue
Block a user