Migrate unifi tests to use freezegun (#105343)

This commit is contained in:
Jan-Philipp Benecke 2023-12-09 12:55:40 +01:00 committed by GitHub
parent 1eaf208450
commit b5785003a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 18 deletions

View File

@ -1,9 +1,8 @@
"""The tests for the UniFi Network device tracker platform.""" """The tests for the UniFi Network device tracker platform."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
from aiounifi.models.message import MessageKey from aiounifi.models.message import MessageKey
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory, freeze_time
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
@ -72,7 +71,7 @@ async def test_tracked_wireless_clients(
# Change time to mark client as away # Change time to mark client as away
new_time = dt_util.utcnow() + controller.option_detection_time new_time = dt_util.utcnow() + controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -293,6 +292,7 @@ async def test_tracked_wireless_clients_event_source(
async def test_tracked_devices( async def test_tracked_devices(
hass: HomeAssistant, hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
mock_unifi_websocket, mock_unifi_websocket,
mock_device_registry, mock_device_registry,
) -> None: ) -> None:
@ -351,9 +351,9 @@ async def test_tracked_devices(
# Change of time can mark device not_home outside of expected reporting interval # Change of time can mark device not_home outside of expected reporting interval
new_time = dt_util.utcnow() + timedelta(seconds=90) new_time = dt_util.utcnow() + timedelta(seconds=90)
with patch("homeassistant.util.dt.utcnow", return_value=new_time): freezer.move_to(new_time)
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("device_tracker.device_1").state == STATE_NOT_HOME assert hass.states.get("device_tracker.device_1").state == STATE_NOT_HOME
assert hass.states.get("device_tracker.device_2").state == STATE_HOME assert hass.states.get("device_tracker.device_2").state == STATE_HOME
@ -712,7 +712,7 @@ async def test_option_ssid_filter(
await hass.async_block_till_done() await hass.async_block_till_done()
new_time = dt_util.utcnow() + controller.option_detection_time new_time = dt_util.utcnow() + controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -740,7 +740,7 @@ async def test_option_ssid_filter(
# Time pass to mark client as away # Time pass to mark client as away
new_time += controller.option_detection_time new_time += controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -759,7 +759,7 @@ async def test_option_ssid_filter(
await hass.async_block_till_done() await hass.async_block_till_done()
new_time += controller.option_detection_time new_time += controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -808,7 +808,7 @@ async def test_wireless_client_go_wired_issue(
# Pass time # Pass time
new_time = dt_util.utcnow() + controller.option_detection_time new_time = dt_util.utcnow() + controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -877,7 +877,7 @@ async def test_option_ignore_wired_bug(
# pass time # pass time
new_time = dt_util.utcnow() + controller.option_detection_time new_time = dt_util.utcnow() + controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -4,6 +4,7 @@ from datetime import datetime, timedelta
from unittest.mock import patch from unittest.mock import patch
from aiounifi.models.message import MessageKey from aiounifi.models.message import MessageKey
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
@ -429,6 +430,7 @@ async def test_bandwidth_sensors(
async def test_uptime_sensors( async def test_uptime_sensors(
hass: HomeAssistant, hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
mock_unifi_websocket, mock_unifi_websocket,
entity_registry_enabled_by_default: None, entity_registry_enabled_by_default: None,
initial_uptime, initial_uptime,
@ -450,13 +452,13 @@ async def test_uptime_sensors(
} }
now = datetime(2021, 1, 1, 1, 1, 0, tzinfo=dt_util.UTC) now = datetime(2021, 1, 1, 1, 1, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.now", return_value=now): freezer.move_to(now)
config_entry = await setup_unifi_integration( config_entry = await setup_unifi_integration(
hass, hass,
aioclient_mock, aioclient_mock,
options=options, options=options,
clients_response=[uptime_client], clients_response=[uptime_client],
) )
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1 assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1