Use freezegun in uptimerobot tests (#99046)

pull/99058/head
Erik Montnemery 2023-08-25 16:05:44 +02:00 committed by GitHub
parent 0d3663c52a
commit c827af5826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 12 deletions

View File

@ -1,6 +1,7 @@
"""Test the UptimeRobot init."""
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from pyuptimerobot import UptimeRobotAuthenticationException, UptimeRobotException
@ -12,7 +13,6 @@ from homeassistant.components.uptimerobot.const import (
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.util import dt as dt_util
from .common import (
MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA,
@ -93,7 +93,9 @@ async def test_reauthentication_trigger_key_read_only(
async def test_reauthentication_trigger_after_setup(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test reauthentication trigger."""
mock_config_entry = await setup_uptimerobot_integration(hass)
@ -106,7 +108,8 @@ async def test_reauthentication_trigger_after_setup(
"pyuptimerobot.UptimeRobot.async_get_monitors",
side_effect=UptimeRobotAuthenticationException,
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
flows = hass.config_entries.flow.async_progress()
@ -125,7 +128,10 @@ async def test_reauthentication_trigger_after_setup(
assert flow["context"]["entry_id"] == mock_config_entry.entry_id
async def test_integration_reload(hass: HomeAssistant) -> None:
async def test_integration_reload(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test integration reload."""
mock_entry = await setup_uptimerobot_integration(hass)
@ -134,7 +140,8 @@ async def test_integration_reload(hass: HomeAssistant) -> None:
return_value=mock_uptimerobot_api_response(),
):
assert await hass.config_entries.async_reload(mock_entry.entry_id)
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entry = hass.config_entries.async_get_entry(mock_entry.entry_id)
@ -143,7 +150,9 @@ async def test_integration_reload(hass: HomeAssistant) -> None:
async def test_update_errors(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test errors during updates."""
await setup_uptimerobot_integration(hass)
@ -152,7 +161,8 @@ async def test_update_errors(
"pyuptimerobot.UptimeRobot.async_get_monitors",
side_effect=UptimeRobotException,
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert (
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
@ -163,7 +173,8 @@ async def test_update_errors(
"pyuptimerobot.UptimeRobot.async_get_monitors",
return_value=mock_uptimerobot_api_response(),
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
@ -171,7 +182,8 @@ async def test_update_errors(
"pyuptimerobot.UptimeRobot.async_get_monitors",
return_value=mock_uptimerobot_api_response(key=MockApiResponseKey.ERROR),
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert (
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
@ -181,7 +193,10 @@ async def test_update_errors(
assert "Error fetching uptimerobot data: test error from API" in caplog.text
async def test_device_management(hass: HomeAssistant) -> None:
async def test_device_management(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that we are adding and removing devices for monitors returned from the API."""
mock_entry = await setup_uptimerobot_integration(hass)
dev_reg = dr.async_get(hass)
@ -201,7 +216,8 @@ async def test_device_management(hass: HomeAssistant) -> None:
data=[MOCK_UPTIMEROBOT_MONITOR, {**MOCK_UPTIMEROBOT_MONITOR, "id": 12345}]
),
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
devices = dr.async_entries_for_config_entry(dev_reg, mock_entry.entry_id)
@ -218,7 +234,8 @@ async def test_device_management(hass: HomeAssistant) -> None:
"pyuptimerobot.UptimeRobot.async_get_monitors",
return_value=mock_uptimerobot_api_response(),
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done()