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 history tests to avoid patching utcnow #93487

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
39 changes: 10 additions & 29 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from datetime import timedelta
from http import HTTPStatus
import json
from unittest.mock import patch, sentinel
from unittest.mock import sentinel

from freezegun import freeze_time
import pytest

from homeassistant.components import history
Expand Down Expand Up @@ -245,29 +246,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 @@ -335,9 +325,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 @@ -351,17 +339,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 @@ -376,9 +359,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
37 changes: 9 additions & 28 deletions tests/components/history/test_init_db_schema_30.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
from unittest.mock import patch, sentinel

from freezegun import freeze_time
import pytest

from homeassistant.components import recorder
Expand Down Expand Up @@ -261,29 +262,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 @@ -351,9 +341,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 @@ -367,17 +355,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 @@ -392,9 +375,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