Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update recorder tests to avoid patching utcnow #93489

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 5 additions & 13 deletions tests/components/recorder/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any, Literal, cast
from unittest.mock import patch, sentinel

from freezegun import freeze_time
from sqlalchemy import create_engine
from sqlalchemy.orm.session import Session

Expand Down Expand Up @@ -282,9 +283,7 @@ def set_state(entity_id, state, **kwargs):
four = three + timedelta(seconds=15 * 5)

states = {mp: [], sns1: [], sns2: [], sns3: [], sns4: []}
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=one
):
with freeze_time(one) as freezer:
states[mp].append(
set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)})
)
Expand All @@ -293,25 +292,18 @@ def set_state(entity_id, state, **kwargs):
states[sns3].append(set_state(sns3, "10", attributes=sns3_attr))
states[sns4].append(set_state(sns4, "10", attributes=sns4_attr))

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=one + timedelta(microseconds=1),
):
freezer.move_to(one + timedelta(microseconds=1))
states[mp].append(
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)})
)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=two
):
freezer.move_to(two)
states[sns1].append(set_state(sns1, "15", attributes=sns1_attr))
states[sns2].append(set_state(sns2, "15", attributes=sns2_attr))
states[sns3].append(set_state(sns3, "15", attributes=sns3_attr))
states[sns4].append(set_state(sns4, "15", attributes=sns4_attr))

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=three
):
freezer.move_to(three)
states[sns1].append(set_state(sns1, "20", attributes=sns1_attr))
states[sns2].append(set_state(sns2, "20", attributes=sns2_attr))
states[sns3].append(set_state(sns3, "20", attributes=sns3_attr))
Expand Down
112 changes: 31 additions & 81 deletions tests/components/recorder/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
from unittest.mock import patch, sentinel

from freezegun import freeze_time
import pytest
from sqlalchemy import text

Expand Down Expand Up @@ -223,25 +224,19 @@ def set_state(state):
point = start + timedelta(seconds=1)
end = point + timedelta(seconds=1)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
with freeze_time(start) as freezer:
set_state("idle")
set_state("YouTube")

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point
):
freezer.move_to(point)
states = [
set_state("idle"),
set_state("Netflix"),
set_state("Plex"),
set_state("YouTube"),
]

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=end
):
freezer.move_to(end)
set_state("Netflix")
set_state("Plex")

Expand Down Expand Up @@ -272,32 +267,23 @@ def set_state(state):
point4 = start + timedelta(seconds=1, microseconds=300)
end = point + timedelta(seconds=1, microseconds=400)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
with freeze_time(start) as freezer:
set_state("idle")
set_state("YouTube")

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point
):
freezer.move_to(point)
states = [set_state("idle")]
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point2
):

freezer.move_to(point2)
states.append(set_state("Netflix"))
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point3
):

freezer.move_to(point3)
states.append(set_state("Plex"))
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point4
):

freezer.move_to(point4)
states.append(set_state("YouTube"))

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=end
):
freezer.move_to(end)
set_state("Netflix")
set_state("Plex")

Expand Down Expand Up @@ -379,21 +365,15 @@ def set_state(state):
start = dt_util.utcnow() - timedelta(minutes=2)
point = start + timedelta(minutes=1)
point2 = point + timedelta(minutes=1, seconds=1)
states = []

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
with freeze_time(start) as freezer:
set_state("1")

states = []
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point
):
freezer.move_to(point)
states.append(set_state("2"))

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point2
):
freezer.move_to(point2)
states.append(set_state("3"))

hist = history.get_last_state_changes(hass, 2, entity_id)
Expand All @@ -415,21 +395,15 @@ def set_state(state):
start = dt_util.utcnow() - timedelta(minutes=2)
point = start + timedelta(minutes=1)
point2 = point + timedelta(minutes=1, seconds=1)
states = []

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
with freeze_time(start) as freezer:
set_state("1")

states = []
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point
):
freezer.move_to(point)
set_state("2")

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point2
):
freezer.move_to(point2)
states.append(set_state("3"))

hist = history.get_last_state_changes(hass, 1, entity_id)
Expand Down Expand Up @@ -457,14 +431,10 @@ def set_state(state):
start = dt_util.utcnow() - timedelta(minutes=2)
point = start + timedelta(minutes=1)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
with freeze_time(start) as freezer:
set_state("1")

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=point
):
freezer.move_to(point)
set_state("2")

hist = history.get_last_state_changes(hass, 2, entity_id)
Expand Down Expand Up @@ -694,29 +664,18 @@ def set_state(state, **kwargs):
points.append(start + timedelta(minutes=i))

states = []
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=start
):
with freeze_time(start) as freezer:
set_state("123", attributes={"attribute": 10.64})

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[0],
):
freezer.move_to(points[0])
# Attributes are different, state not
states.append(set_state("123", attributes={"attribute": 21.42}))

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[1],
):
freezer.move_to(points[1])
# state is different, attributes not
states.append(set_state("32", attributes={"attribute": 21.42}))

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=points[2],
):
freezer.move_to(points[2])
# everything is different
states.append(set_state("412", attributes={"attribute": 54.23}))

Expand Down Expand Up @@ -805,9 +764,7 @@ def set_state(entity_id, state, **kwargs):
four = three + timedelta(seconds=1)

states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []}
with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=one
):
with freeze_time(one) as freezer:
states[mp].append(
set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)})
)
Expand All @@ -821,17 +778,12 @@ def set_state(entity_id, state, **kwargs):
set_state(therm, 20, attributes={"current_temperature": 19.5})
)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow",
return_value=one + timedelta(microseconds=1),
):
freezer.move_to(one + timedelta(microseconds=1))
states[mp].append(
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)})
)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=two
):
freezer.move_to(two)
# This state will be skipped only different in time
set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)})
# This state will be skipped because domain is excluded
Expand All @@ -846,9 +798,7 @@ def set_state(entity_id, state, **kwargs):
set_state(therm2, 20, attributes={"current_temperature": 19})
)

with patch(
"homeassistant.components.recorder.core.dt_util.utcnow", return_value=three
):
freezer.move_to(three)
states[mp].append(
set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)})
)
Expand Down