Migrate mqtt tests to use freezegun (#105414)

This commit is contained in:
Jan-Philipp Benecke 2023-12-10 09:25:16 +01:00 committed by GitHub
parent 1cc47c0553
commit ff85d0c290
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 41 deletions

View file

@ -8,6 +8,7 @@ from pathlib import Path
from typing import Any from typing import Any
from unittest.mock import ANY, MagicMock, patch from unittest.mock import ANY, MagicMock, patch
from freezegun import freeze_time
import pytest import pytest
import voluptuous as vol import voluptuous as vol
import yaml import yaml
@ -31,6 +32,7 @@ from homeassistant.generated.mqtt import MQTT
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_mqtt_message from tests.common import MockConfigEntry, async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
@ -1320,9 +1322,8 @@ async def help_test_entity_debug_info_max_messages(
"subscriptions" "subscriptions"
] ]
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: with freeze_time(start_dt):
dt_utcnow.return_value = start_dt
for i in range(0, debug_info.STORED_MESSAGES + 1): for i in range(0, debug_info.STORED_MESSAGES + 1):
async_fire_mqtt_message(hass, "test-topic", f"{i}") async_fire_mqtt_message(hass, "test-topic", f"{i}")
@ -1396,7 +1397,7 @@ async def help_test_entity_debug_info_message(
debug_info_data = debug_info.info_for_device(hass, device.id) debug_info_data = debug_info.info_for_device(hass, device.id)
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
if state_topic is not None: if state_topic is not None:
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1 assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
@ -1404,8 +1405,7 @@ async def help_test_entity_debug_info_message(
"subscriptions" "subscriptions"
] ]
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: with freeze_time(start_dt):
dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, str(state_topic), state_payload) async_fire_mqtt_message(hass, str(state_topic), state_payload)
debug_info_data = debug_info.info_for_device(hass, device.id) debug_info_data = debug_info.info_for_device(hass, device.id)
@ -1426,8 +1426,7 @@ async def help_test_entity_debug_info_message(
expected_transmissions = [] expected_transmissions = []
if service: if service:
# Trigger an outgoing MQTT message # Trigger an outgoing MQTT message
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: with freeze_time(start_dt):
dt_utcnow.return_value = start_dt
if service: if service:
service_data = {ATTR_ENTITY_ID: f"{domain}.beer_test"} service_data = {ATTR_ENTITY_ID: f"{domain}.beer_test"}
if service_parameters: if service_parameters:

View file

@ -8,6 +8,7 @@ import ssl
from typing import Any, TypedDict from typing import Any, TypedDict
from unittest.mock import ANY, MagicMock, call, mock_open, patch from unittest.mock import ANY, MagicMock, call, mock_open, patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
import voluptuous as vol import voluptuous as vol
@ -40,6 +41,7 @@ from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import async_get_platforms from homeassistant.helpers.entity_platform import async_get_platforms
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from .test_common import help_all_subscribe_calls from .test_common import help_all_subscribe_calls
@ -3256,6 +3258,7 @@ async def test_debug_info_wildcard(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test debug info.""" """Test debug info."""
await mqtt_mock_entry() await mqtt_mock_entry()
@ -3279,9 +3282,8 @@ async def test_debug_info_wildcard(
"subscriptions" "subscriptions"
] ]
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: freezer.move_to(start_dt)
dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, "sensor/abc", "123") async_fire_mqtt_message(hass, "sensor/abc", "123")
debug_info_data = debug_info.info_for_device(hass, device.id) debug_info_data = debug_info.info_for_device(hass, device.id)
@ -3304,6 +3306,7 @@ async def test_debug_info_filter_same(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test debug info removes messages with same timestamp.""" """Test debug info removes messages with same timestamp."""
await mqtt_mock_entry() await mqtt_mock_entry()
@ -3327,13 +3330,12 @@ async def test_debug_info_filter_same(
"subscriptions" "subscriptions"
] ]
dt1 = datetime(2019, 1, 1, 0, 0, 0) dt1 = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
dt2 = datetime(2019, 1, 1, 0, 0, 1) dt2 = datetime(2019, 1, 1, 0, 0, 1, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: freezer.move_to(dt1)
dt_utcnow.return_value = dt1
async_fire_mqtt_message(hass, "sensor/abc", "123") async_fire_mqtt_message(hass, "sensor/abc", "123")
async_fire_mqtt_message(hass, "sensor/abc", "123") async_fire_mqtt_message(hass, "sensor/abc", "123")
dt_utcnow.return_value = dt2 freezer.move_to(dt2)
async_fire_mqtt_message(hass, "sensor/abc", "123") async_fire_mqtt_message(hass, "sensor/abc", "123")
debug_info_data = debug_info.info_for_device(hass, device.id) debug_info_data = debug_info.info_for_device(hass, device.id)
@ -3364,6 +3366,7 @@ async def test_debug_info_same_topic(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test debug info.""" """Test debug info."""
await mqtt_mock_entry() await mqtt_mock_entry()
@ -3388,9 +3391,8 @@ async def test_debug_info_same_topic(
"subscriptions" "subscriptions"
] ]
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: freezer.move_to(start_dt)
dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False) async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
debug_info_data = debug_info.info_for_device(hass, device.id) debug_info_data = debug_info.info_for_device(hass, device.id)
@ -3408,9 +3410,8 @@ async def test_debug_info_same_topic(
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data) async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
await hass.async_block_till_done() await hass.async_block_till_done()
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: freezer.move_to(start_dt)
dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False) async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
@ -3418,6 +3419,7 @@ async def test_debug_info_qos_retain(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_mock_entry: MqttMockHAClientGenerator,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test debug info.""" """Test debug info."""
await mqtt_mock_entry() await mqtt_mock_entry()
@ -3441,9 +3443,8 @@ async def test_debug_info_qos_retain(
"subscriptions" "subscriptions"
] ]
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: freezer.move_to(start_dt)
dt_utcnow.return_value = start_dt
# simulate the first message was replayed from the broker with retained flag # simulate the first message was replayed from the broker with retained flag
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=True) async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=True)
# simulate an update message # simulate an update message