Migrate octoprint tests to use freezegun (#105408)

This commit is contained in:
Jan-Philipp Benecke 2023-12-11 10:42:16 +01:00 committed by GitHub
parent 3e3f9cf092
commit b4731674f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
"""The tests for Octoptint binary sensor module.""" """The tests for Octoptint binary sensor module."""
from datetime import UTC, datetime from datetime import UTC, datetime
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -8,7 +9,7 @@ from homeassistant.helpers import entity_registry as er
from . import init_integration from . import init_integration
async def test_sensors(hass: HomeAssistant) -> None: async def test_sensors(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> None:
"""Test the underlying sensors.""" """Test the underlying sensors."""
printer = { printer = {
"state": { "state": {
@ -22,11 +23,8 @@ async def test_sensors(hass: HomeAssistant) -> None:
"progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000}, "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000},
"state": "Printing", "state": "Printing",
} }
with patch( freezer.move_to(datetime(2020, 2, 20, 9, 10, 13, 543, tzinfo=UTC))
"homeassistant.util.dt.utcnow", await init_integration(hass, "sensor", printer=printer, job=job)
return_value=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) 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" 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.""" """Test the underlying sensors."""
printer = { printer = {
"state": { "state": {
@ -89,10 +89,8 @@ async def test_sensors_no_target_temp(hass: HomeAssistant) -> None:
}, },
"temperature": {"tool1": {"actual": 18.83136, "target": None}}, "temperature": {"tool1": {"actual": 18.83136, "target": None}},
} }
with patch( freezer.move_to(datetime(2020, 2, 20, 9, 10, 0))
"homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) await init_integration(hass, "sensor", printer=printer)
):
await init_integration(hass, "sensor", printer=printer)
entity_registry = er.async_get(hass) 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" 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.""" """Test the underlying sensors."""
printer = { printer = {
"state": { "state": {
@ -125,10 +125,8 @@ async def test_sensors_paused(hass: HomeAssistant) -> None:
"progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000}, "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000},
"state": "Paused", "state": "Paused",
} }
with patch( freezer.move_to(datetime(2020, 2, 20, 9, 10, 0))
"homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) await init_integration(hass, "sensor", printer=printer, job=job)
):
await init_integration(hass, "sensor", printer=printer, job=job)
entity_registry = er.async_get(hass) 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" 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.""" """Test the underlying sensors."""
job = { job = {
"job": {}, "job": {},
"progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000}, "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000},
"state": "Paused", "state": "Paused",
} }
with patch( freezer.move_to(datetime(2020, 2, 20, 9, 10, 0))
"homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) await init_integration(hass, "sensor", printer=None, job=job)
):
await init_integration(hass, "sensor", printer=None, job=job)
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)