Migrate non-component tests to use freezegun/freezer (#105142)

This commit is contained in:
Jan-Philipp Benecke 2023-12-07 22:58:09 +01:00 committed by GitHub
parent 00e87b9dff
commit d1aa690c24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 247 additions and 236 deletions

View file

@ -9,6 +9,7 @@ from types import MappingProxyType
from unittest import mock
from unittest.mock import AsyncMock, MagicMock, patch
from freezegun import freeze_time
import pytest
import voluptuous as vol
@ -1346,13 +1347,13 @@ async def test_wait_template_with_utcnow(hass: HomeAssistant) -> None:
try:
non_matching_time = start_time.replace(hour=3)
with patch("homeassistant.util.dt.utcnow", return_value=non_matching_time):
with freeze_time(non_matching_time):
hass.async_create_task(script_obj.async_run(context=Context()))
await asyncio.wait_for(wait_started_flag.wait(), 1)
assert script_obj.is_running
match_time = start_time.replace(hour=12)
with patch("homeassistant.util.dt.utcnow", return_value=match_time):
with freeze_time(match_time):
async_fire_time_changed(hass, match_time)
except (AssertionError, asyncio.TimeoutError):
await script_obj.async_stop()
@ -1378,15 +1379,13 @@ async def test_wait_template_with_utcnow_no_match(hass: HomeAssistant) -> None:
try:
non_matching_time = start_time.replace(hour=3)
with patch("homeassistant.util.dt.utcnow", return_value=non_matching_time):
with freeze_time(non_matching_time):
hass.async_create_task(script_obj.async_run(context=Context()))
await asyncio.wait_for(wait_started_flag.wait(), 1)
assert script_obj.is_running
second_non_matching_time = start_time.replace(hour=4)
with patch(
"homeassistant.util.dt.utcnow", return_value=second_non_matching_time
):
with freeze_time(second_non_matching_time):
async_fire_time_changed(hass, second_non_matching_time)
async with asyncio.timeout(0.1):