From b868f13591c260a8d81aa8a00f54c904a50c6777 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Jul 2020 16:04:36 -1000 Subject: [PATCH] Ensure all track time change tests mock a specific start time (#38178) * Ensure all track time change tests mock a specific start time * make sure tests calling async_track_utc_time_change fire time in utc --- tests/helpers/test_event.py | 173 +++++++++++++++++++++++++----------- 1 file changed, 123 insertions(+), 50 deletions(-) diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 669b2ab25c8..0c16e624fc3 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -50,7 +50,7 @@ async def test_track_point_in_time(hass): runs = [] async_track_point_in_utc_time( - hass, callback(lambda x: runs.append(1)), birthday_paulus + hass, callback(lambda x: runs.append(x)), birthday_paulus ) async_fire_time_changed(hass, before_birthday) @@ -67,7 +67,7 @@ async def test_track_point_in_time(hass): assert len(runs) == 1 async_track_point_in_utc_time( - hass, callback(lambda x: runs.append(1)), birthday_paulus + hass, callback(lambda x: runs.append(x)), birthday_paulus ) async_fire_time_changed(hass, after_birthday) @@ -75,7 +75,7 @@ async def test_track_point_in_time(hass): assert len(runs) == 2 unsub = async_track_point_in_time( - hass, callback(lambda x: runs.append(1)), birthday_paulus + hass, callback(lambda x: runs.append(x)), birthday_paulus ) unsub() @@ -539,7 +539,7 @@ async def test_track_time_interval(hass): utc_now = dt_util.utcnow() unsub = async_track_time_interval( - hass, lambda x: specific_runs.append(1), timedelta(seconds=10) + hass, lambda x: specific_runs.append(x), timedelta(seconds=10) ) async_fire_time_changed(hass, utc_now + timedelta(seconds=5)) @@ -750,22 +750,33 @@ async def test_async_track_time_change(hass): now = dt_util.utcnow() - unsub = async_track_time_change(hass, lambda x: wildcard_runs.append(1)) - unsub_utc = async_track_utc_time_change( - hass, lambda x: specific_runs.append(1), second=[0, 30] - ) + time_that_will_not_match_right_away = datetime(now.year + 1, 5, 24, 11, 59, 55) - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999)) + with patch( + "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away + ): + unsub = async_track_time_change(hass, lambda x: wildcard_runs.append(x)) + unsub_utc = async_track_utc_time_change( + hass, lambda x: specific_runs.append(x), second=[0, 30] + ) + + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 1 assert len(wildcard_runs) == 1 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 15, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 0, 15, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 1 assert len(wildcard_runs) == 2 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 assert len(wildcard_runs) == 3 @@ -773,7 +784,9 @@ async def test_async_track_time_change(hass): unsub() unsub_utc() - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 assert len(wildcard_runs) == 3 @@ -785,25 +798,38 @@ async def test_periodic_task_minute(hass): now = dt_util.utcnow() - unsub = async_track_utc_time_change( - hass, lambda x: specific_runs.append(1), minute="/5", second=0 + time_that_will_not_match_right_away = datetime(now.year + 1, 5, 24, 11, 59, 55) + + with patch( + "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away + ): + unsub = async_track_utc_time_change( + hass, 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) ) - - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999)) await hass.async_block_till_done() assert len(specific_runs) == 1 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 3, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 3, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 1 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 unsub() - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 @@ -814,33 +840,50 @@ async def test_periodic_task_hour(hass): now = dt_util.utcnow() - unsub = async_track_utc_time_change( - hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0 + time_that_will_not_match_right_away = datetime(now.year + 1, 5, 24, 21, 59, 55) + + with patch( + "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away + ): + unsub = async_track_utc_time_change( + hass, 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) ) - - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999)) 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)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 1 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 1, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 1, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 3 unsub() - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 2, 0, 0, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 3 @@ -853,10 +896,12 @@ async def test_periodic_task_wrong_input(hass): with pytest.raises(ValueError): async_track_utc_time_change( - hass, lambda x: specific_runs.append(1), hour="/two" + hass, lambda x: specific_runs.append(x), hour="/two" ) - async_fire_time_changed(hass, datetime(now.year + 1, 5, 2, 0, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 2, 0, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 0 @@ -867,37 +912,54 @@ async def test_periodic_task_clock_rollback(hass): now = dt_util.utcnow() - unsub = async_track_utc_time_change( - hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0 + time_that_will_not_match_right_away = datetime(now.year + 1, 5, 24, 21, 59, 55) + + with patch( + "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away + ): + unsub = async_track_utc_time_change( + hass, 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) ) - - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999)) - 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)) await hass.async_block_till_done() assert len(specific_runs) == 1 async_fire_time_changed( - hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999), fire_all=True + hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999, tzinfo=dt_util.UTC) + ) + await hass.async_block_till_done() + assert len(specific_runs) == 1 + + async_fire_time_changed( + hass, + datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC), + fire_all=True, ) await hass.async_block_till_done() assert len(specific_runs) == 2 async_fire_time_changed( - hass, datetime(now.year + 1, 5, 24, 0, 0, 0, 999999), fire_all=True + hass, + datetime(now.year + 1, 5, 24, 0, 0, 0, 999999, tzinfo=dt_util.UTC), + fire_all=True, ) await hass.async_block_till_done() assert len(specific_runs) == 3 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 4 unsub() - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 4 @@ -908,19 +970,30 @@ async def test_periodic_task_duplicate_time(hass): now = dt_util.utcnow() - unsub = async_track_utc_time_change( - hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0 + time_that_will_not_match_right_away = datetime(now.year + 1, 5, 24, 21, 59, 55) + + with patch( + "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away + ): + unsub = async_track_utc_time_change( + hass, 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) ) - - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999)) await hass.async_block_till_done() assert len(specific_runs) == 1 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 1 - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999, tzinfo=dt_util.UTC) + ) await hass.async_block_till_done() assert len(specific_runs) == 2 @@ -942,7 +1015,7 @@ async def test_periodic_task_entering_dst(hass): "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away ): unsub = async_track_time_change( - hass, lambda x: specific_runs.append(1), hour=2, minute=30, second=0 + hass, lambda x: specific_runs.append(x), hour=2, minute=30, second=0 ) async_fire_time_changed( @@ -988,7 +1061,7 @@ async def test_periodic_task_leaving_dst(hass): "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away ): unsub = async_track_time_change( - hass, lambda x: specific_runs.append(1), hour=2, minute=30, second=0 + hass, lambda x: specific_runs.append(x), hour=2, minute=30, second=0 ) async_fire_time_changed(