mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Migrate homeassistant component tests to use freezegun (#105333)
This commit is contained in:
parent
46e75ed94b
commit
a7845406a5
@ -2,6 +2,7 @@
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
@ -38,17 +39,16 @@ def setup_comp(hass):
|
||||
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."""
|
||||
now = dt_util.now()
|
||||
|
||||
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)
|
||||
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=dt_util.as_utc(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,
|
||||
@ -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)]
|
||||
)
|
||||
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:
|
||||
"""Test for firing at input_datetime."""
|
||||
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)
|
||||
|
||||
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
||||
):
|
||||
|
||||
freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away))
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
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."""
|
||||
|
||||
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)
|
||||
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
|
||||
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
||||
):
|
||||
freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away))
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
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"
|
||||
|
||||
|
||||
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.
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
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)
|
||||
with assert_setup_component(1, automation.DOMAIN):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
@ -409,7 +406,9 @@ async def test_untrack_time_change(hass: HomeAssistant) -> None:
|
||||
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."""
|
||||
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)
|
||||
|
||||
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
||||
):
|
||||
|
||||
freezer.move_to(dt_util.as_utc(time_that_will_not_match_right_away))
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
|
@ -1,7 +1,7 @@
|
||||
"""The tests for the time_pattern automation."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
@ -27,15 +27,15 @@ def setup_comp(hass):
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, hour=3
|
||||
)
|
||||
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,
|
||||
@ -72,15 +72,15 @@ async def test_if_fires_when_hour_matches(hass: HomeAssistant, calls) -> None:
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, minute=30
|
||||
)
|
||||
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,
|
||||
@ -103,15 +103,15 @@ async def test_if_fires_when_minute_matches(hass: HomeAssistant, calls) -> None:
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, second=30
|
||||
)
|
||||
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,
|
||||
@ -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(
|
||||
hass: HomeAssistant, calls
|
||||
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
|
||||
) -> None:
|
||||
"""Test for firing if seconds are matching."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, second=15
|
||||
)
|
||||
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,
|
||||
@ -169,15 +167,15 @@ async def test_if_fires_when_second_as_string_matches(
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, hour=4
|
||||
)
|
||||
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,
|
||||
@ -202,15 +200,15 @@ async def test_if_fires_when_all_matches(hass: HomeAssistant, calls) -> None:
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, second=1
|
||||
)
|
||||
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,
|
||||
@ -235,16 +233,16 @@ async def test_if_fires_periodic_seconds(hass: HomeAssistant, calls) -> None:
|
||||
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."""
|
||||
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, minute=1
|
||||
)
|
||||
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,
|
||||
@ -269,15 +267,15 @@ async def test_if_fires_periodic_minutes(hass: HomeAssistant, calls) -> None:
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, hour=1
|
||||
)
|
||||
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,
|
||||
@ -302,15 +300,15 @@ async def test_if_fires_periodic_hours(hass: HomeAssistant, calls) -> None:
|
||||
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."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, minute=1
|
||||
)
|
||||
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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user