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
6 changed files with 247 additions and 236 deletions

View File

@@ -923,7 +923,9 @@ async def test_track_template_error_can_recover(
async def test_track_template_time_change(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test tracking template with time change."""
template_error = Template("{{ utcnow().minute % 2 == 0 }}", hass)
@@ -935,17 +937,15 @@ async def test_track_template_time_change(
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
):
unsub = async_track_template(hass, template_error, error_callback)
await hass.async_block_till_done()
assert not calls
freezer.move_to(time_that_will_not_match_right_away)
unsub = async_track_template(hass, template_error, error_callback)
await hass.async_block_till_done()
assert not calls
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
assert calls[0] == (None, None, None)
@@ -3312,84 +3312,89 @@ async def test_track_template_with_time_default(hass: HomeAssistant) -> None:
info.async_remove()
async def test_track_template_with_time_that_leaves_scope(hass: HomeAssistant) -> None:
async def test_track_template_with_time_that_leaves_scope(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test tracking template with time."""
now = dt_util.utcnow()
test_time = datetime(now.year + 1, 5, 24, 11, 59, 1, 500000, tzinfo=dt_util.UTC)
freezer.move_to(test_time)
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
hass.states.async_set("binary_sensor.washing_machine", "on")
specific_runs = []
template_complex = Template(
"""
{% if states.binary_sensor.washing_machine.state == "on" %}
{{ now() }}
{% else %}
{{ states.binary_sensor.washing_machine.last_updated }}
{% endif %}
""",
hass,
)
hass.states.async_set("binary_sensor.washing_machine", "on")
specific_runs = []
template_complex = Template(
"""
{% if states.binary_sensor.washing_machine.state == "on" %}
{{ now() }}
{% else %}
{{ states.binary_sensor.washing_machine.last_updated }}
{% endif %}
""",
hass,
)
def specific_run_callback(
event: EventType[EventStateChangedData] | None,
updates: list[TrackTemplateResult],
) -> None:
specific_runs.append(updates.pop().result)
def specific_run_callback(
event: EventType[EventStateChangedData] | None,
updates: list[TrackTemplateResult],
) -> None:
specific_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": True,
}
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": True,
}
hass.states.async_set("binary_sensor.washing_machine", "off")
await hass.async_block_till_done()
hass.states.async_set("binary_sensor.washing_machine", "off")
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": False,
}
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": False,
}
hass.states.async_set("binary_sensor.washing_machine", "on")
await hass.async_block_till_done()
hass.states.async_set("binary_sensor.washing_machine", "on")
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": True,
}
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": True,
}
# Verify we do not update before the minute rolls over
callback_count_before_time_change = len(specific_runs)
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change
# Verify we do not update before the minute rolls over
callback_count_before_time_change = len(specific_runs)
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change
async_fire_time_changed(hass, test_time + timedelta(seconds=58))
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change
new_time = test_time + timedelta(seconds=58)
freezer.move_to(new_time)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change
# Verify we do update on the next change of minute
async_fire_time_changed(hass, test_time + timedelta(seconds=59))
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change + 1
# Verify we do update on the next change of minute
new_time = test_time + timedelta(seconds=59)
freezer.move_to(new_time)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change + 1
info.async_remove()
async def test_async_track_template_result_multiple_templates_mixing_listeners(
hass: HomeAssistant,
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test tracking multiple templates with mixing listener types."""
@@ -3410,18 +3415,16 @@ async def test_async_track_template_result_multiple_templates_mixing_listeners(
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 11, 59, 55, tzinfo=dt_util.UTC
)
freezer.move_to(time_that_will_not_match_right_away)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
info = async_track_template_result(
hass,
[
TrackTemplate(template_1, None),
TrackTemplate(template_2, None),
],
refresh_listener,
)
info = async_track_template_result(
hass,
[
TrackTemplate(template_1, None),
TrackTemplate(template_2, None),
],
refresh_listener,
)
assert info.listeners == {
"all": False,
@@ -3450,9 +3453,9 @@ async def test_async_track_template_result_multiple_templates_mixing_listeners(
refresh_runs = []
next_time = time_that_will_not_match_right_away + timedelta(hours=25)
with patch("homeassistant.util.dt.utcnow", return_value=next_time):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
freezer.move_to(next_time)
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
assert refresh_runs == [
[
@@ -3787,7 +3790,10 @@ async def test_track_sunset(hass: HomeAssistant) -> None:
assert len(offset_runs) == 1
async def test_async_track_time_change(hass: HomeAssistant) -> None:
async def test_async_track_time_change(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test tracking time change."""
none_runs = []
wildcard_runs = []
@@ -3798,21 +3804,19 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None:
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 11, 59, 55, tzinfo=dt_util.UTC
)
freezer.move_to(time_that_will_not_match_right_away)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_time_change(hass, callback(lambda x: none_runs.append(x)))
unsub_utc = async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), second=[0, 30]
)
unsub_wildcard = async_track_time_change(
hass,
callback(lambda x: wildcard_runs.append(x)),
second="*",
minute="*",
hour="*",
)
unsub = async_track_time_change(hass, callback(lambda x: none_runs.append(x)))
unsub_utc = async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), second=[0, 30]
)
unsub_wildcard = async_track_time_change(
hass,
callback(lambda x: wildcard_runs.append(x)),
second="*",
minute="*",
hour="*",
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999, tzinfo=dt_util.UTC)
@@ -3851,7 +3855,10 @@ async def test_async_track_time_change(hass: HomeAssistant) -> None:
assert len(none_runs) == 3
async def test_periodic_task_minute(hass: HomeAssistant) -> None:
async def test_periodic_task_minute(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test periodic tasks per minute."""
specific_runs = []
@@ -3860,13 +3867,11 @@ async def test_periodic_task_minute(hass: HomeAssistant) -> None:
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 11, 59, 55, tzinfo=dt_util.UTC
)
freezer.move_to(time_that_will_not_match_right_away)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), minute="/5", second=0
)
unsub = async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), minute="/5", second=0
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999, tzinfo=dt_util.UTC)
@@ -3895,7 +3900,10 @@ async def test_periodic_task_minute(hass: HomeAssistant) -> None:
assert len(specific_runs) == 2
async def test_periodic_task_hour(hass: HomeAssistant) -> None:
async def test_periodic_task_hour(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test periodic tasks per hour."""
specific_runs = []
@@ -3904,17 +3912,15 @@ async def test_periodic_task_hour(hass: HomeAssistant) -> None:
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
freezer.move_to(time_that_will_not_match_right_away)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
@@ -3973,71 +3979,77 @@ async def test_periodic_task_wrong_input(hass: HomeAssistant) -> None:
assert len(specific_runs) == 0
async def test_periodic_task_clock_rollback(hass: HomeAssistant) -> None:
async def test_periodic_task_clock_rollback(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test periodic tasks with the time rolling backwards."""
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
freezer.move_to(time_that_will_not_match_right_away)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
new_time = datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
freezer.move_to(new_time)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999, tzinfo=dt_util.UTC)
)
new_time = datetime(now.year + 1, 5, 24, 23, 0, 0, 999999, tzinfo=dt_util.UTC)
freezer.move_to(new_time)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
assert len(specific_runs) == 1
new_time = datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
freezer.move_to(new_time)
async_fire_time_changed(
hass,
datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC),
new_time,
fire_all=True,
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
new_time = datetime(now.year + 1, 5, 24, 0, 0, 0, 999999, tzinfo=dt_util.UTC)
freezer.move_to(new_time)
async_fire_time_changed(
hass,
datetime(now.year + 1, 5, 24, 0, 0, 0, 999999, tzinfo=dt_util.UTC),
new_time,
fire_all=True,
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
)
new_time = datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
freezer.move_to(new_time)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
assert len(specific_runs) == 2
unsub()
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
)
new_time = datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
freezer.move_to(new_time)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
assert len(specific_runs) == 2
async def test_periodic_task_duplicate_time(hass: HomeAssistant) -> None:
async def test_periodic_task_duplicate_time(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test periodic tasks not triggering on duplicate time."""
specific_runs = []
@@ -4046,17 +4058,15 @@ async def test_periodic_task_duplicate_time(hass: HomeAssistant) -> None:
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
freezer.move_to(time_that_will_not_match_right_away)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)