Update tests that track time to account for microsecond precision (#38044)

This commit is contained in:
J. Nick Koston 2020-07-21 11:22:55 -10:00 committed by GitHub
parent fa0e12ffe8
commit 4015991622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 62 deletions

View File

@ -30,6 +30,15 @@ def setup_comp(hass):
async def test_if_fires_using_at(hass, calls): async def test_if_fires_using_at(hass, calls):
"""Test for firing at.""" """Test for firing at."""
now = dt_util.utcnow()
time_that_will_not_match_right_away = now.replace(
year=now.year + 1, hour=4, minute=59, second=0
)
with patch(
"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,
@ -62,6 +71,15 @@ async def test_if_not_fires_using_wrong_at(hass, calls):
This should break the before rule. This should break the before rule.
""" """
now = dt_util.utcnow()
time_that_will_not_match_right_away = now.replace(
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
):
with assert_setup_component(0, automation.DOMAIN): with assert_setup_component(0, automation.DOMAIN):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -78,7 +96,9 @@ async def test_if_not_fires_using_wrong_at(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=0, second=5)) async_fire_time_changed(
hass, now.replace(year=now.year + 1, hour=1, minute=0, second=5)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0

View File

@ -755,17 +755,17 @@ async def test_async_track_time_change(hass):
hass, lambda x: specific_runs.append(1), second=[0, 30] hass, lambda x: specific_runs.append(1), second=[0, 30]
) )
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
assert len(wildcard_runs) == 1 assert len(wildcard_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 15)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 15, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
assert len(wildcard_runs) == 2 assert len(wildcard_runs) == 2
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 30)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
assert len(wildcard_runs) == 3 assert len(wildcard_runs) == 3
@ -773,7 +773,7 @@ async def test_async_track_time_change(hass):
unsub() unsub()
unsub_utc() unsub_utc()
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 30)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
assert len(wildcard_runs) == 3 assert len(wildcard_runs) == 3
@ -789,21 +789,21 @@ async def test_periodic_task_minute(hass):
hass, lambda x: specific_runs.append(1), minute="/5", second=0 hass, lambda x: specific_runs.append(1), minute="/5", second=0
) )
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 3, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 3, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 5, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
unsub() unsub()
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 5, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
@ -818,23 +818,23 @@ async def test_periodic_task_hour(hass):
hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0 hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0
) )
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 23, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 0, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 1, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 1, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
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, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 3 assert len(specific_runs) == 3
@ -856,7 +856,7 @@ async def test_periodic_task_wrong_input(hass):
hass, lambda x: specific_runs.append(1), hour="/two" hass, lambda x: specific_runs.append(1), hour="/two"
) )
async_fire_time_changed(hass, datetime(now.year + 1, 5, 2, 0, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 2, 0, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 0 assert len(specific_runs) == 0
@ -871,31 +871,33 @@ async def test_periodic_task_clock_rollback(hass):
hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0 hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0
) )
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 23, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed( async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0), fire_all=True hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999), fire_all=True
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 0, 0, 0), fire_all=True) async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 0, 0, 0, 999999), fire_all=True
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 3 assert len(specific_runs) == 3
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, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 4 assert len(specific_runs) == 4
unsub() 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, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 4 assert len(specific_runs) == 4
@ -910,15 +912,15 @@ async def test_periodic_task_duplicate_time(hass):
hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0 hass, lambda x: specific_runs.append(1), hour="/2", minute=0, second=0
) )
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 0, 0, 0)) async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
@ -944,25 +946,25 @@ async def test_periodic_task_entering_dst(hass):
) )
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 25, 1, 50, 0)) hass, timezone.localize(datetime(now.year + 1, 3, 25, 1, 50, 0, 999999))
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 0 assert len(specific_runs) == 0
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 25, 3, 50, 0)) hass, timezone.localize(datetime(now.year + 1, 3, 25, 3, 50, 0, 999999))
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 0 assert len(specific_runs) == 0
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 26, 1, 50, 0)) hass, timezone.localize(datetime(now.year + 1, 3, 26, 1, 50, 0, 999999))
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 0 assert len(specific_runs) == 0
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 26, 2, 50, 0)) hass, timezone.localize(datetime(now.year + 1, 3, 26, 2, 50, 0, 999999))
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
@ -990,31 +992,46 @@ async def test_periodic_task_leaving_dst(hass):
) )
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 10, 28, 2, 5, 0), is_dst=False) hass,
timezone.localize(
datetime(now.year + 1, 10, 28, 2, 5, 0, 999999), is_dst=False
),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 0 assert len(specific_runs) == 0
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 10, 28, 2, 55, 0), is_dst=False) hass,
timezone.localize(
datetime(now.year + 1, 10, 28, 2, 55, 0, 999999), is_dst=False
),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 1 assert len(specific_runs) == 1
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 2, 10, 28, 2, 45, 0), is_dst=True) hass,
timezone.localize(
datetime(now.year + 2, 10, 28, 2, 45, 0, 999999), is_dst=True
),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 2, 10, 28, 2, 55, 0), is_dst=True) hass,
timezone.localize(
datetime(now.year + 2, 10, 28, 2, 55, 0, 999999), is_dst=True
),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2
async_fire_time_changed( async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 2, 10, 28, 2, 55, 0), is_dst=True) hass,
timezone.localize(
datetime(now.year + 2, 10, 28, 2, 55, 0, 999999), is_dst=True
),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == 2 assert len(specific_runs) == 2