From fa57afc142a11e1c09fbf04b18f750f29fef8322 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 20 Jul 2020 19:55:19 +0000 Subject: [PATCH] patch time tests since time can tick to match during the test --- tests/components/automation/test_time.py | 6 +- .../automation/test_time_pattern.py | 322 +++++++++++------- 2 files changed, 202 insertions(+), 126 deletions(-) diff --git a/tests/components/automation/test_time.py b/tests/components/automation/test_time.py index 0ba85467fcd..81f1657e0a2 100644 --- a/tests/components/automation/test_time.py +++ b/tests/components/automation/test_time.py @@ -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() assert len(calls) == 1 diff --git a/tests/components/automation/test_time_pattern.py b/tests/components/automation/test_time_pattern.py index 01aa32f318f..b5141f088e4 100644 --- a/tests/components/automation/test_time_pattern.py +++ b/tests/components/automation/test_time_pattern.py @@ -1,4 +1,5 @@ """The tests for the time_pattern automation.""" +from asynctest.mock import patch import pytest import homeassistant.components.automation as automation @@ -23,53 +24,67 @@ def setup_comp(hass): async def test_if_fires_when_hour_matches(hass, calls): """Test for firing if hour is matching.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": 0, - "minutes": "*", - "seconds": "*", - }, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": 0, + "minutes": "*", + "seconds": "*", + }, + "action": {"service": "test.automation"}, + } + }, + ) - 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() assert len(calls) == 1 await common.async_turn_off(hass) 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() assert len(calls) == 1 async def test_if_fires_when_minute_matches(hass, calls): """Test for firing if minutes are matching.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": "*", - "minutes": 0, - "seconds": "*", - }, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": "*", + "minutes": 0, + "seconds": "*", + }, + "action": {"service": "test.automation"}, + } + }, + ) - 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() assert len(calls) == 1 @@ -77,23 +92,30 @@ async def test_if_fires_when_minute_matches(hass, calls): async def test_if_fires_when_second_matches(hass, calls): """Test for firing if seconds are matching.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": "*", - "minutes": "*", - "seconds": 0, - }, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": "*", + "minutes": "*", + "seconds": 0, + }, + "action": {"service": "test.automation"}, + } + }, + ) - 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() assert len(calls) == 1 @@ -101,23 +123,32 @@ async def test_if_fires_when_second_matches(hass, calls): async def test_if_fires_when_all_matches(hass, calls): """Test for firing if everything matches.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": 1, - "minutes": 2, - "seconds": 3, - }, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": 1, + "minutes": 2, + "seconds": 3, + }, + "action": {"service": "test.automation"}, + } + }, + ) - 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() assert len(calls) == 1 @@ -125,47 +156,66 @@ async def test_if_fires_when_all_matches(hass, calls): async def test_if_fires_periodic_seconds(hass, calls): """Test for firing periodically every second.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": "*", - "minutes": "*", - "seconds": "/2", - }, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": "*", + "minutes": "*", + "seconds": "/10", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + async_fire_time_changed( + hass, now.replace(year=now.year + 2, hour=0, minute=0, second=10) ) - async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0, minute=0, second=2)) - await hass.async_block_till_done() - assert len(calls) == 1 + assert len(calls) >= 1 async def test_if_fires_periodic_minutes(hass, calls): """Test for firing periodically every minute.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": "*", - "minutes": "/2", - "seconds": "*", - }, - "action": {"service": "test.automation"}, - } - }, - ) - async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0, minute=2, second=0)) + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": "*", + "minutes": "/2", + "seconds": "*", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + async_fire_time_changed( + hass, now.replace(year=now.year + 2, hour=0, minute=2, second=0) + ) await hass.async_block_till_done() assert len(calls) == 1 @@ -173,23 +223,32 @@ async def test_if_fires_periodic_minutes(hass, calls): async def test_if_fires_periodic_hours(hass, calls): """Test for firing periodically every hour.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": { - "platform": "time_pattern", - "hours": "/2", - "minutes": "*", - "seconds": "*", - }, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": "/2", + "minutes": "*", + "seconds": "*", + }, + "action": {"service": "test.automation"}, + } + }, + ) - 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() assert len(calls) == 1 @@ -197,28 +256,41 @@ async def test_if_fires_periodic_hours(hass, calls): async def test_default_values(hass, calls): """Test for firing at 2 minutes every hour.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "time_pattern", "minutes": "2"}, - "action": {"service": "test.automation"}, - } - }, + 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( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": {"platform": "time_pattern", "minutes": "2"}, + "action": {"service": "test.automation"}, + } + }, + ) + + async_fire_time_changed( + hass, now.replace(year=now.year + 2, hour=1, minute=2, second=0) ) - async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=2, second=0)) + await hass.async_block_till_done() + assert len(calls) == 1 + + async_fire_time_changed( + hass, now.replace(year=now.year + 2, hour=1, minute=2, second=1) + ) await hass.async_block_till_done() assert len(calls) == 1 - async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=2, second=1)) - - await hass.async_block_till_done() - 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() assert len(calls) == 2