patch time tests since time can tick to match during the test

This commit is contained in:
J. Nick Koston
2020-07-20 19:55:19 +00:00
parent ff7a443d9f
commit fa57afc142
2 changed files with 202 additions and 126 deletions

View File

@@ -46,7 +46,11 @@ async def test_if_fires_using_at(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=5, minute=0, second=0)) now = dt_util.utcnow()
async_fire_time_changed(
hass, now.replace(year=now.year + 1, hour=5, minute=0, second=0)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1

View File

@@ -1,4 +1,5 @@
"""The tests for the time_pattern automation.""" """The tests for the time_pattern automation."""
from asynctest.mock import patch
import pytest import pytest
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
@@ -23,6 +24,13 @@ def setup_comp(hass):
async def test_if_fires_when_hour_matches(hass, calls): async def test_if_fires_when_hour_matches(hass, calls):
"""Test for firing if hour is matching.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -39,20 +47,27 @@ async def test_if_fires_when_hour_matches(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0)) async_fire_time_changed(hass, now.replace(year=now.year + 2, hour=0))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
await common.async_turn_off(hass) await common.async_turn_off(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0)) async_fire_time_changed(hass, now.replace(year=now.year + 1, hour=0))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
async def test_if_fires_when_minute_matches(hass, calls): async def test_if_fires_when_minute_matches(hass, calls):
"""Test for firing if minutes are matching.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -69,7 +84,7 @@ async def test_if_fires_when_minute_matches(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(minute=0)) async_fire_time_changed(hass, now.replace(year=now.year + 2, minute=0))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@@ -77,6 +92,13 @@ async def test_if_fires_when_minute_matches(hass, calls):
async def test_if_fires_when_second_matches(hass, calls): async def test_if_fires_when_second_matches(hass, calls):
"""Test for firing if seconds are matching.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -93,7 +115,7 @@ async def test_if_fires_when_second_matches(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(second=0)) async_fire_time_changed(hass, now.replace(year=now.year + 2, second=0))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@@ -101,6 +123,13 @@ async def test_if_fires_when_second_matches(hass, calls):
async def test_if_fires_when_all_matches(hass, calls): async def test_if_fires_when_all_matches(hass, calls):
"""Test for firing if everything matches.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -117,7 +146,9 @@ async def test_if_fires_when_all_matches(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=2, second=3)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=1, minute=2, second=3)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@@ -125,6 +156,13 @@ async def test_if_fires_when_all_matches(hass, calls):
async def test_if_fires_periodic_seconds(hass, calls): async def test_if_fires_periodic_seconds(hass, calls):
"""Test for firing periodically every second.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -134,21 +172,31 @@ async def test_if_fires_periodic_seconds(hass, calls):
"platform": "time_pattern", "platform": "time_pattern",
"hours": "*", "hours": "*",
"minutes": "*", "minutes": "*",
"seconds": "/2", "seconds": "/10",
}, },
"action": {"service": "test.automation"}, "action": {"service": "test.automation"},
} }
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0, minute=0, second=2)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=0, minute=0, second=10)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) >= 1
async def test_if_fires_periodic_minutes(hass, calls): async def test_if_fires_periodic_minutes(hass, calls):
"""Test for firing periodically every minute.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -165,7 +213,9 @@ async def test_if_fires_periodic_minutes(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0, minute=2, second=0)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=0, minute=2, second=0)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@@ -173,6 +223,13 @@ async def test_if_fires_periodic_minutes(hass, calls):
async def test_if_fires_periodic_hours(hass, calls): async def test_if_fires_periodic_hours(hass, calls):
"""Test for firing periodically every hour.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -189,7 +246,9 @@ async def test_if_fires_periodic_hours(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=2, minute=0, second=0)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=2, minute=0, second=0)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@@ -197,6 +256,13 @@ async def test_if_fires_periodic_hours(hass, calls):
async def test_default_values(hass, calls): async def test_default_values(hass, calls):
"""Test for firing at 2 minutes every hour.""" """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
):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@@ -208,17 +274,23 @@ async def test_default_values(hass, calls):
}, },
) )
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=2, second=0)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=1, minute=2, second=0)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=2, second=1)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=1, minute=2, second=1)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=2, minute=2, second=0)) async_fire_time_changed(
hass, now.replace(year=now.year + 2, hour=2, minute=2, second=0)
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(calls) == 2