Use freezegun in version tests (#99047)

pull/99058/head
Erik Montnemery 2023-08-25 16:01:48 +02:00 committed by GitHub
parent f99743bedb
commit 81aa35a9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -4,6 +4,8 @@ from __future__ import annotations
from typing import Any, Final
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from homeassistant import config_entries
from homeassistant.components.version.const import (
DEFAULT_CONFIGURATION,
@ -14,7 +16,6 @@ from homeassistant.components.version.const import (
)
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed
@ -37,6 +38,7 @@ TEST_DEFAULT_IMPORT_CONFIG: Final = {
async def mock_get_version_update(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
version: str = MOCK_VERSION,
data: dict[str, Any] = MOCK_VERSION_DATA,
side_effect: Exception = None,
@ -47,9 +49,8 @@ async def mock_get_version_update(
return_value=(version, data),
side_effect=side_effect,
):
async_fire_time_changed(
hass, dt_util.utcnow() + UPDATE_COORDINATOR_UPDATE_INTERVAL
)
freezer.tick(UPDATE_COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()

View File

@ -1,6 +1,7 @@
"""The test for the version sensor platform."""
from __future__ import annotations
from freezegun.api import FrozenDateTimeFactory
from pyhaversion.exceptions import HaVersionException
import pytest
@ -19,15 +20,19 @@ async def test_version_sensor(hass: HomeAssistant) -> None:
assert "channel" not in state.attributes
async def test_update(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None:
async def test_update(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test updates."""
await setup_version_integration(hass)
assert hass.states.get("sensor.local_installation").state == MOCK_VERSION
await mock_get_version_update(hass, version="1970.1.1")
await mock_get_version_update(hass, freezer, version="1970.1.1")
assert hass.states.get("sensor.local_installation").state == "1970.1.1"
assert "Error fetching version data" not in caplog.text
await mock_get_version_update(hass, side_effect=HaVersionException)
await mock_get_version_update(hass, freezer, side_effect=HaVersionException)
assert hass.states.get("sensor.local_installation").state == "unavailable"
assert "Error fetching version data" in caplog.text