From b4731674f8128b43117e0557733bb38960364019 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Mon, 11 Dec 2023 10:42:16 +0100 Subject: [PATCH] Migrate octoprint tests to use freezegun (#105408) --- tests/components/octoprint/test_sensor.py | 42 +++++++++++------------ 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/tests/components/octoprint/test_sensor.py b/tests/components/octoprint/test_sensor.py index 2ba657c77d5..3d3efd04da0 100644 --- a/tests/components/octoprint/test_sensor.py +++ b/tests/components/octoprint/test_sensor.py @@ -1,6 +1,7 @@ """The tests for Octoptint binary sensor module.""" from datetime import UTC, datetime -from unittest.mock import patch + +from freezegun.api import FrozenDateTimeFactory from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -8,7 +9,7 @@ from homeassistant.helpers import entity_registry as er from . import init_integration -async def test_sensors(hass: HomeAssistant) -> None: +async def test_sensors(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> None: """Test the underlying sensors.""" printer = { "state": { @@ -22,11 +23,8 @@ async def test_sensors(hass: HomeAssistant) -> None: "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000}, "state": "Printing", } - with patch( - "homeassistant.util.dt.utcnow", - return_value=datetime(2020, 2, 20, 9, 10, 13, 543, tzinfo=UTC), - ): - await init_integration(hass, "sensor", printer=printer, job=job) + freezer.move_to(datetime(2020, 2, 20, 9, 10, 13, 543, tzinfo=UTC)) + await init_integration(hass, "sensor", printer=printer, job=job) entity_registry = er.async_get(hass) @@ -80,7 +78,9 @@ async def test_sensors(hass: HomeAssistant) -> None: assert entry.unique_id == "Estimated Finish Time-uuid" -async def test_sensors_no_target_temp(hass: HomeAssistant) -> None: +async def test_sensors_no_target_temp( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test the underlying sensors.""" printer = { "state": { @@ -89,10 +89,8 @@ async def test_sensors_no_target_temp(hass: HomeAssistant) -> None: }, "temperature": {"tool1": {"actual": 18.83136, "target": None}}, } - with patch( - "homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) - ): - await init_integration(hass, "sensor", printer=printer) + freezer.move_to(datetime(2020, 2, 20, 9, 10, 0)) + await init_integration(hass, "sensor", printer=printer) entity_registry = er.async_get(hass) @@ -111,7 +109,9 @@ async def test_sensors_no_target_temp(hass: HomeAssistant) -> None: assert entry.unique_id == "target tool1 temp-uuid" -async def test_sensors_paused(hass: HomeAssistant) -> None: +async def test_sensors_paused( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test the underlying sensors.""" printer = { "state": { @@ -125,10 +125,8 @@ async def test_sensors_paused(hass: HomeAssistant) -> None: "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000}, "state": "Paused", } - with patch( - "homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) - ): - await init_integration(hass, "sensor", printer=printer, job=job) + freezer.move_to(datetime(2020, 2, 20, 9, 10, 0)) + await init_integration(hass, "sensor", printer=printer, job=job) entity_registry = er.async_get(hass) @@ -147,17 +145,17 @@ async def test_sensors_paused(hass: HomeAssistant) -> None: assert entry.unique_id == "Estimated Finish Time-uuid" -async def test_sensors_printer_disconnected(hass: HomeAssistant) -> None: +async def test_sensors_printer_disconnected( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test the underlying sensors.""" job = { "job": {}, "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000}, "state": "Paused", } - with patch( - "homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) - ): - await init_integration(hass, "sensor", printer=None, job=job) + freezer.move_to(datetime(2020, 2, 20, 9, 10, 0)) + await init_integration(hass, "sensor", printer=None, job=job) entity_registry = er.async_get(hass)