Use freezegun in cert_expiry tests (#105125)

This commit is contained in:
Jan-Philipp Benecke 2023-12-07 21:55:37 +01:00 committed by GitHub
parent 4c4ad9404f
commit 8a4b761c78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View file

@ -2,6 +2,8 @@
from datetime import timedelta
from unittest.mock import patch
from freezegun import freeze_time
from homeassistant.components.cert_expiry.const import DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntryState
@ -73,8 +75,8 @@ async def test_update_unique_id(hass: HomeAssistant) -> None:
assert entry.unique_id == f"{HOST}:{PORT}"
@patch("homeassistant.util.dt.utcnow", return_value=static_datetime())
async def test_unload_config_entry(mock_now, hass: HomeAssistant) -> None:
@freeze_time(static_datetime())
async def test_unload_config_entry(hass: HomeAssistant) -> None:
"""Test unloading a config entry."""
assert hass.state is CoreState.running

View file

@ -4,6 +4,8 @@ import socket
import ssl
from unittest.mock import patch
from freezegun import freeze_time
from homeassistant.components.cert_expiry.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import CoreState, HomeAssistant
@ -15,8 +17,8 @@ from .helpers import future_timestamp, static_datetime
from tests.common import MockConfigEntry, async_fire_time_changed
@patch("homeassistant.util.dt.utcnow", return_value=static_datetime())
async def test_async_setup_entry(mock_now, hass: HomeAssistant) -> None:
@freeze_time(static_datetime())
async def test_async_setup_entry(hass: HomeAssistant) -> None:
"""Test async_setup_entry."""
assert hass.state is CoreState.running
@ -82,7 +84,7 @@ async def test_update_sensor(hass: HomeAssistant) -> None:
starting_time = static_datetime()
timestamp = future_timestamp(100)
with patch("homeassistant.util.dt.utcnow", return_value=starting_time), patch(
with freeze_time(starting_time), patch(
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
return_value=timestamp,
):
@ -98,7 +100,7 @@ async def test_update_sensor(hass: HomeAssistant) -> None:
assert state.attributes.get("is_valid")
next_update = starting_time + timedelta(hours=24)
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
with freeze_time(next_update), patch(
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
return_value=timestamp,
):
@ -126,7 +128,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None:
starting_time = static_datetime()
timestamp = future_timestamp(100)
with patch("homeassistant.util.dt.utcnow", return_value=starting_time), patch(
with freeze_time(starting_time), patch(
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
return_value=timestamp,
):
@ -143,7 +145,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None:
next_update = starting_time + timedelta(hours=24)
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
with freeze_time(next_update), patch(
"homeassistant.components.cert_expiry.helper.get_cert",
side_effect=socket.gaierror,
):
@ -155,7 +157,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None:
state = hass.states.get("sensor.example_com_cert_expiry")
assert state.state == STATE_UNAVAILABLE
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
with freeze_time(next_update), patch(
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
return_value=timestamp,
):
@ -171,7 +173,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None:
next_update = starting_time + timedelta(hours=72)
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
with freeze_time(next_update), patch(
"homeassistant.components.cert_expiry.helper.get_cert",
side_effect=ssl.SSLError("something bad"),
):
@ -186,7 +188,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None:
next_update = starting_time + timedelta(hours=96)
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
with freeze_time(next_update), patch(
"homeassistant.components.cert_expiry.helper.get_cert", side_effect=Exception()
):
async_fire_time_changed(hass, utcnow() + timedelta(hours=96))