Update history tests to avoid patching utcnow (#93487)

This commit is contained in:
J. Nick Koston 2023-05-24 16:09:26 -05:00 committed by GitHub
parent 45b1ad36b1
commit 40a47c72f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 57 deletions

View File

@ -3,8 +3,9 @@
from datetime import timedelta from datetime import timedelta
from http import HTTPStatus from http import HTTPStatus
import json import json
from unittest.mock import patch, sentinel from unittest.mock import sentinel
from freezegun import freeze_time
import pytest import pytest
from homeassistant.components import history from homeassistant.components import history
@ -245,29 +246,18 @@ def test_get_significant_states_only(hass_history) -> None:
points.append(start + timedelta(minutes=i)) points.append(start + timedelta(minutes=i))
states = [] states = []
with patch( with freeze_time(start) as freezer:
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
set_state("123", attributes={"attribute": 10.64}) set_state("123", attributes={"attribute": 10.64})
with patch( freezer.move_to(points[0])
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[0],
):
# Attributes are different, state not # Attributes are different, state not
states.append(set_state("123", attributes={"attribute": 21.42})) states.append(set_state("123", attributes={"attribute": 21.42}))
with patch( freezer.move_to(points[1])
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[1],
):
# state is different, attributes not # state is different, attributes not
states.append(set_state("32", attributes={"attribute": 21.42})) states.append(set_state("32", attributes={"attribute": 21.42}))
with patch( freezer.move_to(points[2])
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[2],
):
# everything is different # everything is different
states.append(set_state("412", attributes={"attribute": 54.23})) states.append(set_state("412", attributes={"attribute": 54.23}))
@ -335,9 +325,7 @@ def record_states(hass):
four = three + timedelta(seconds=1) four = three + timedelta(seconds=1)
states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []} states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []}
with patch( with freeze_time(one) as freezer:
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=one
):
states[mp].append( states[mp].append(
set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)})
) )
@ -351,17 +339,12 @@ def record_states(hass):
set_state(therm, 20, attributes={"current_temperature": 19.5}) set_state(therm, 20, attributes={"current_temperature": 19.5})
) )
with patch( freezer.move_to(one + timedelta(microseconds=1))
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=one + timedelta(microseconds=1),
):
states[mp].append( states[mp].append(
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)})
) )
with patch( freezer.move_to(two)
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=two
):
# This state will be skipped only different in time # This state will be skipped only different in time
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)}) set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)})
# This state will be skipped because domain is excluded # This state will be skipped because domain is excluded
@ -376,9 +359,7 @@ def record_states(hass):
set_state(therm2, 20, attributes={"current_temperature": 19}) set_state(therm2, 20, attributes={"current_temperature": 19})
) )
with patch( freezer.move_to(three)
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=three
):
states[mp].append( states[mp].append(
set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)}) set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)})
) )

View File

@ -7,6 +7,7 @@ from http import HTTPStatus
import json import json
from unittest.mock import patch, sentinel from unittest.mock import patch, sentinel
from freezegun import freeze_time
import pytest import pytest
from homeassistant.components import recorder from homeassistant.components import recorder
@ -261,29 +262,18 @@ def test_get_significant_states_only(legacy_hass_history) -> None:
points.append(start + timedelta(minutes=i)) points.append(start + timedelta(minutes=i))
states = [] states = []
with patch( with freeze_time(start) as freezer:
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
set_state("123", attributes={"attribute": 10.64}) set_state("123", attributes={"attribute": 10.64})
with patch( freezer.move_to(points[0])
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[0],
):
# Attributes are different, state not # Attributes are different, state not
states.append(set_state("123", attributes={"attribute": 21.42})) states.append(set_state("123", attributes={"attribute": 21.42}))
with patch( freezer.move_to(points[1])
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[1],
):
# state is different, attributes not # state is different, attributes not
states.append(set_state("32", attributes={"attribute": 21.42})) states.append(set_state("32", attributes={"attribute": 21.42}))
with patch( freezer.move_to(points[2])
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[2],
):
# everything is different # everything is different
states.append(set_state("412", attributes={"attribute": 54.23})) states.append(set_state("412", attributes={"attribute": 54.23}))
@ -351,9 +341,7 @@ def record_states(hass):
four = three + timedelta(seconds=1) four = three + timedelta(seconds=1)
states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []} states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []}
with patch( with freeze_time(one) as freezer:
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=one
):
states[mp].append( states[mp].append(
set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)})
) )
@ -367,17 +355,12 @@ def record_states(hass):
set_state(therm, 20, attributes={"current_temperature": 19.5}) set_state(therm, 20, attributes={"current_temperature": 19.5})
) )
with patch( freezer.move_to(one + timedelta(microseconds=1))
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=one + timedelta(microseconds=1),
):
states[mp].append( states[mp].append(
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)})
) )
with patch( freezer.move_to(two)
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=two
):
# This state will be skipped only different in time # This state will be skipped only different in time
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)}) set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)})
# This state will be skipped because domain is excluded # This state will be skipped because domain is excluded
@ -392,9 +375,7 @@ def record_states(hass):
set_state(therm2, 20, attributes={"current_temperature": 19}) set_state(therm2, 20, attributes={"current_temperature": 19})
) )
with patch( freezer.move_to(three)
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=three
):
states[mp].append( states[mp].append(
set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)}) set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)})
) )