Migrate roku tests to use freezegun (#105418)

This commit is contained in:
Jan-Philipp Benecke 2023-12-09 23:47:19 +01:00 committed by GitHub
parent 64a5271a51
commit a8148cea65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from rokuecp import RokuConnectionError, RokuConnectionTimeoutError, RokuError from rokuecp import RokuConnectionError, RokuConnectionTimeoutError, RokuError
@ -153,6 +154,7 @@ async def test_availability(
hass: HomeAssistant, hass: HomeAssistant,
mock_roku: MagicMock, mock_roku: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
error: RokuError, error: RokuError,
) -> None: ) -> None:
"""Test entity availability.""" """Test entity availability."""
@ -160,23 +162,22 @@ async def test_availability(
future = now + timedelta(minutes=1) future = now + timedelta(minutes=1)
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
with patch("homeassistant.util.dt.utcnow", return_value=now): freezer.move_to(now)
await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
with patch("homeassistant.util.dt.utcnow", return_value=future): freezer.move_to(future)
mock_roku.update.side_effect = error mock_roku.update.side_effect = error
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(MAIN_ENTITY_ID).state == STATE_UNAVAILABLE assert hass.states.get(MAIN_ENTITY_ID).state == STATE_UNAVAILABLE
future += timedelta(minutes=1) future += timedelta(minutes=1)
freezer.move_to(future)
with patch("homeassistant.util.dt.utcnow", return_value=future): mock_roku.update.side_effect = None
mock_roku.update.side_effect = None async_fire_time_changed(hass, future)
async_fire_time_changed(hass, future) await hass.async_block_till_done()
await hass.async_block_till_done() assert hass.states.get(MAIN_ENTITY_ID).state == STATE_IDLE
assert hass.states.get(MAIN_ENTITY_ID).state == STATE_IDLE
async def test_supported_features( async def test_supported_features(