Migrate homeassistant component tests to use freezegun (#105333)

This commit is contained in:
Jan-Philipp Benecke 2023-12-08 21:18:09 +01:00 committed by GitHub
parent 46e75ed94b
commit a7845406a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 264 additions and 269 deletions

View File

@ -2,6 +2,7 @@
from datetime import timedelta from datetime import timedelta
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
import voluptuous as vol import voluptuous as vol
@ -38,17 +39,16 @@ def setup_comp(hass):
mock_component(hass, "group") mock_component(hass, "group")
async def test_if_fires_using_at(hass: HomeAssistant, calls) -> None: async def test_if_fires_using_at(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing at.""" """Test for firing at."""
now = dt_util.now() now = dt_util.now()
trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2) trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2)
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1) time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow",
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -79,7 +79,7 @@ async def test_if_fires_using_at(hass: HomeAssistant, calls) -> None:
("has_date", "has_time"), [(True, True), (True, False), (False, True)] ("has_date", "has_time"), [(True, True), (True, False), (False, True)]
) )
async def test_if_fires_using_at_input_datetime( async def test_if_fires_using_at_input_datetime(
hass: HomeAssistant, calls, has_date, has_time hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, has_date, has_time
) -> None: ) -> None:
"""Test for firing at input_datetime.""" """Test for firing at input_datetime."""
await async_setup_component( await async_setup_component(
@ -107,10 +107,8 @@ async def test_if_fires_using_at_input_datetime(
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1) time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}" some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
with patch(
"homeassistant.util.dt.utcnow", freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away))
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -161,7 +159,9 @@ async def test_if_fires_using_at_input_datetime(
) )
async def test_if_fires_using_multiple_at(hass: HomeAssistant, calls) -> None: async def test_if_fires_using_multiple_at(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing at.""" """Test for firing at."""
now = dt_util.now() now = dt_util.now()
@ -169,10 +169,7 @@ async def test_if_fires_using_multiple_at(hass: HomeAssistant, calls) -> None:
trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2) trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2)
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1) time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
with patch( freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away))
"homeassistant.util.dt.utcnow",
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -203,7 +200,9 @@ async def test_if_fires_using_multiple_at(hass: HomeAssistant, calls) -> None:
assert calls[1].data["some"] == "time - 6" assert calls[1].data["some"] == "time - 6"
async def test_if_not_fires_using_wrong_at(hass: HomeAssistant, calls) -> None: async def test_if_not_fires_using_wrong_at(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""YAML translates time values to total seconds. """YAML translates time values to total seconds.
This should break the before rule. This should break the before rule.
@ -214,9 +213,7 @@ async def test_if_not_fires_using_wrong_at(hass: HomeAssistant, calls) -> None:
year=now.year + 1, hour=1, minute=0, second=0 year=now.year + 1, hour=1, minute=0, second=0
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
with assert_setup_component(1, automation.DOMAIN): with assert_setup_component(1, automation.DOMAIN):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -409,7 +406,9 @@ async def test_untrack_time_change(hass: HomeAssistant) -> None:
assert len(mock_track_time_change.mock_calls) == 3 assert len(mock_track_time_change.mock_calls) == 3
async def test_if_fires_using_at_sensor(hass: HomeAssistant, calls) -> None: async def test_if_fires_using_at_sensor(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing at sensor time.""" """Test for firing at sensor time."""
now = dt_util.now() now = dt_util.now()
@ -424,10 +423,8 @@ async def test_if_fires_using_at_sensor(hass: HomeAssistant, calls) -> None:
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1) time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}" some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
with patch(
"homeassistant.util.dt.utcnow", freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away))
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,

View File

@ -1,7 +1,7 @@
"""The tests for the time_pattern automation.""" """The tests for the time_pattern automation."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
import voluptuous as vol import voluptuous as vol
@ -27,15 +27,15 @@ def setup_comp(hass):
mock_component(hass, "group") mock_component(hass, "group")
async def test_if_fires_when_hour_matches(hass: HomeAssistant, calls) -> None: async def test_if_fires_when_hour_matches(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing if hour is matching.""" """Test for firing if hour is matching."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, hour=3 year=now.year + 1, hour=3
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -72,15 +72,15 @@ async def test_if_fires_when_hour_matches(hass: HomeAssistant, calls) -> None:
assert calls[0].data["id"] == 0 assert calls[0].data["id"] == 0
async def test_if_fires_when_minute_matches(hass: HomeAssistant, calls) -> None: async def test_if_fires_when_minute_matches(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing if minutes are matching.""" """Test for firing if minutes are matching."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, minute=30 year=now.year + 1, minute=30
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -103,15 +103,15 @@ async def test_if_fires_when_minute_matches(hass: HomeAssistant, calls) -> None:
assert len(calls) == 1 assert len(calls) == 1
async def test_if_fires_when_second_matches(hass: HomeAssistant, calls) -> None: async def test_if_fires_when_second_matches(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing if seconds are matching.""" """Test for firing if seconds are matching."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, second=30 year=now.year + 1, second=30
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -135,16 +135,14 @@ async def test_if_fires_when_second_matches(hass: HomeAssistant, calls) -> None:
async def test_if_fires_when_second_as_string_matches( async def test_if_fires_when_second_as_string_matches(
hass: HomeAssistant, calls hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None: ) -> None:
"""Test for firing if seconds are matching.""" """Test for firing if seconds are matching."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, second=15 year=now.year + 1, second=15
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -169,15 +167,15 @@ async def test_if_fires_when_second_as_string_matches(
assert len(calls) == 1 assert len(calls) == 1
async def test_if_fires_when_all_matches(hass: HomeAssistant, calls) -> None: async def test_if_fires_when_all_matches(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing if everything matches.""" """Test for firing if everything matches."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, hour=4 year=now.year + 1, hour=4
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -202,15 +200,15 @@ async def test_if_fires_when_all_matches(hass: HomeAssistant, calls) -> None:
assert len(calls) == 1 assert len(calls) == 1
async def test_if_fires_periodic_seconds(hass: HomeAssistant, calls) -> None: async def test_if_fires_periodic_seconds(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing periodically every second.""" """Test for firing periodically every second."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, second=1 year=now.year + 1, second=1
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -235,16 +233,16 @@ async def test_if_fires_periodic_seconds(hass: HomeAssistant, calls) -> None:
assert len(calls) >= 1 assert len(calls) >= 1
async def test_if_fires_periodic_minutes(hass: HomeAssistant, calls) -> None: async def test_if_fires_periodic_minutes(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing periodically every minute.""" """Test for firing periodically every minute."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, minute=1 year=now.year + 1, minute=1
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -269,15 +267,15 @@ async def test_if_fires_periodic_minutes(hass: HomeAssistant, calls) -> None:
assert len(calls) == 1 assert len(calls) == 1
async def test_if_fires_periodic_hours(hass: HomeAssistant, calls) -> None: async def test_if_fires_periodic_hours(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing periodically every hour.""" """Test for firing periodically every hour."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, hour=1 year=now.year + 1, hour=1
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -302,15 +300,15 @@ async def test_if_fires_periodic_hours(hass: HomeAssistant, calls) -> None:
assert len(calls) == 1 assert len(calls) == 1
async def test_default_values(hass: HomeAssistant, calls) -> None: async def test_default_values(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing at 2 minutes every hour.""" """Test for firing at 2 minutes every hour."""
now = dt_util.utcnow() now = dt_util.utcnow()
time_that_will_not_match_right_away = dt_util.utcnow().replace( time_that_will_not_match_right_away = dt_util.utcnow().replace(
year=now.year + 1, minute=1 year=now.year + 1, minute=1
) )
with patch( freezer.move_to(time_that_will_not_match_right_away)
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,