Migrate gree tests to use freezegun (#105340)

This commit is contained in:
Jan-Philipp Benecke 2023-12-09 10:19:42 +01:00 committed by GitHub
parent cc85e89cf2
commit 2d02cdcd0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 31 deletions

View File

@ -1,7 +1,7 @@
"""Tests for gree component.""" """Tests for gree component."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from homeassistant.components.climate import DOMAIN from homeassistant.components.climate import DOMAIN
@ -24,7 +24,7 @@ def mock_now():
async def test_discovery_after_setup( async def test_discovery_after_setup(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Test gree devices don't change after multiple discoveries.""" """Test gree devices don't change after multiple discoveries."""
mock_device_1 = build_device_mock( mock_device_1 = build_device_mock(
@ -58,7 +58,7 @@ async def test_discovery_after_setup(
device.side_effect = [mock_device_1, mock_device_2] device.side_effect = [mock_device_1, mock_device_2]
next_update = mock_now + timedelta(minutes=6) next_update = mock_now + timedelta(minutes=6)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -2,6 +2,7 @@
from datetime import timedelta from datetime import timedelta
from unittest.mock import DEFAULT as DEFAULT_MOCK, AsyncMock, patch from unittest.mock import DEFAULT as DEFAULT_MOCK, AsyncMock, patch
from freezegun.api import FrozenDateTimeFactory
from greeclimate.device import HorizontalSwing, VerticalSwing from greeclimate.device import HorizontalSwing, VerticalSwing
from greeclimate.exceptions import DeviceNotBoundError, DeviceTimeoutError from greeclimate.exceptions import DeviceNotBoundError, DeviceTimeoutError
import pytest import pytest
@ -115,7 +116,7 @@ async def test_discovery_setup_connection_error(
async def test_discovery_after_setup( async def test_discovery_after_setup(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Test gree devices don't change after multiple discoveries.""" """Test gree devices don't change after multiple discoveries."""
MockDevice1 = build_device_mock( MockDevice1 = build_device_mock(
@ -142,7 +143,7 @@ async def test_discovery_after_setup(
device.side_effect = [MockDevice1, MockDevice2] device.side_effect = [MockDevice1, MockDevice2]
next_update = mock_now + timedelta(minutes=6) next_update = mock_now + timedelta(minutes=6)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -151,7 +152,7 @@ async def test_discovery_after_setup(
async def test_discovery_add_device_after_setup( async def test_discovery_add_device_after_setup(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Test gree devices can be added after initial setup.""" """Test gree devices can be added after initial setup."""
MockDevice1 = build_device_mock( MockDevice1 = build_device_mock(
@ -178,7 +179,7 @@ async def test_discovery_add_device_after_setup(
device.side_effect = [MockDevice2] device.side_effect = [MockDevice2]
next_update = mock_now + timedelta(minutes=6) next_update = mock_now + timedelta(minutes=6)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -187,7 +188,7 @@ async def test_discovery_add_device_after_setup(
async def test_discovery_device_bind_after_setup( async def test_discovery_device_bind_after_setup(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Test gree devices can be added after a late device bind.""" """Test gree devices can be added after a late device bind."""
MockDevice1 = build_device_mock( MockDevice1 = build_device_mock(
@ -212,7 +213,7 @@ async def test_discovery_device_bind_after_setup(
MockDevice1.update_state.side_effect = None MockDevice1.update_state.side_effect = None
next_update = mock_now + timedelta(minutes=5) next_update = mock_now + timedelta(minutes=5)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -220,7 +221,9 @@ async def test_discovery_device_bind_after_setup(
assert state.state != STATE_UNAVAILABLE assert state.state != STATE_UNAVAILABLE
async def test_update_connection_failure(hass: HomeAssistant, device, mock_now) -> None: async def test_update_connection_failure(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, device, mock_now
) -> None:
"""Testing update hvac connection failure exception.""" """Testing update hvac connection failure exception."""
device().update_state.side_effect = [ device().update_state.side_effect = [
DEFAULT_MOCK, DEFAULT_MOCK,
@ -231,7 +234,7 @@ async def test_update_connection_failure(hass: HomeAssistant, device, mock_now)
await async_setup_gree(hass) await async_setup_gree(hass)
next_update = mock_now + timedelta(minutes=5) next_update = mock_now + timedelta(minutes=5)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -241,12 +244,12 @@ async def test_update_connection_failure(hass: HomeAssistant, device, mock_now)
assert state.state != STATE_UNAVAILABLE assert state.state != STATE_UNAVAILABLE
next_update = mock_now + timedelta(minutes=10) next_update = mock_now + timedelta(minutes=10)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
next_update = mock_now + timedelta(minutes=15) next_update = mock_now + timedelta(minutes=15)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -257,7 +260,7 @@ async def test_update_connection_failure(hass: HomeAssistant, device, mock_now)
async def test_update_connection_failure_recovery( async def test_update_connection_failure_recovery(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Testing update hvac connection failure recovery.""" """Testing update hvac connection failure recovery."""
device().update_state.side_effect = [ device().update_state.side_effect = [
@ -270,7 +273,7 @@ async def test_update_connection_failure_recovery(
# First update becomes unavailable # First update becomes unavailable
next_update = mock_now + timedelta(minutes=5) next_update = mock_now + timedelta(minutes=5)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -280,7 +283,7 @@ async def test_update_connection_failure_recovery(
# Second update restores the connection # Second update restores the connection
next_update = mock_now + timedelta(minutes=10) next_update = mock_now + timedelta(minutes=10)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -290,7 +293,7 @@ async def test_update_connection_failure_recovery(
async def test_update_unhandled_exception( async def test_update_unhandled_exception(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Testing update hvac connection unhandled response exception.""" """Testing update hvac connection unhandled response exception."""
device().update_state.side_effect = [DEFAULT_MOCK, Exception] device().update_state.side_effect = [DEFAULT_MOCK, Exception]
@ -302,7 +305,7 @@ async def test_update_unhandled_exception(
assert state.state != STATE_UNAVAILABLE assert state.state != STATE_UNAVAILABLE
next_update = mock_now + timedelta(minutes=10) next_update = mock_now + timedelta(minutes=10)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -312,14 +315,14 @@ async def test_update_unhandled_exception(
async def test_send_command_device_timeout( async def test_send_command_device_timeout(
hass: HomeAssistant, discovery, device, mock_now hass: HomeAssistant, freezer: FrozenDateTimeFactory, discovery, device, mock_now
) -> None: ) -> None:
"""Test for sending power on command to the device with a device timeout.""" """Test for sending power on command to the device with a device timeout."""
await async_setup_gree(hass) await async_setup_gree(hass)
# First update to make the device available # First update to make the device available
next_update = mock_now + timedelta(minutes=5) next_update = mock_now + timedelta(minutes=5)
with patch("homeassistant.util.dt.utcnow", return_value=next_update): freezer.move_to(next_update)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()