mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Migrate google related tests to use freezegun (#105552)
* Migrate google related tests to use freezegun * retrigger CI * Fix google tests * Add timezone to config_entry_token_expiry * Separate config_entry_token_expiry from token_expiry * Also test token refresh for offset-naive tokens * retrigger CI * Apply code review suggestion Co-authored-by: Allen Porter <allen.porter@gmail.com> * Run ruff-format --------- Co-authored-by: Allen Porter <allen.porter@gmail.com>
This commit is contained in:
parent
f8f31627ce
commit
0ae0331c5c
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
from collections.abc import Awaitable, Callable, Generator
|
from collections.abc import Awaitable, Callable, Generator
|
||||||
import datetime
|
import datetime
|
||||||
import http
|
import http
|
||||||
|
import time
|
||||||
from typing import Any, TypeVar
|
from typing import Any, TypeVar
|
||||||
from unittest.mock import Mock, mock_open, patch
|
from unittest.mock import Mock, mock_open, patch
|
||||||
|
|
||||||
@ -189,9 +190,9 @@ def creds(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def config_entry_token_expiry(token_expiry: datetime.datetime) -> float:
|
def config_entry_token_expiry() -> float:
|
||||||
"""Fixture for token expiration value stored in the config entry."""
|
"""Fixture for token expiration value stored in the config entry."""
|
||||||
return token_expiry.timestamp()
|
return time.time() + 86400
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -9,6 +9,7 @@ from unittest.mock import patch
|
|||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ClientError
|
from aiohttp.client_exceptions import ClientError
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
from gcal_sync.auth import API_BASE_URL
|
from gcal_sync.auth import API_BASE_URL
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -578,11 +579,13 @@ async def test_scan_calendar_error(
|
|||||||
|
|
||||||
|
|
||||||
async def test_future_event_update_behavior(
|
async def test_future_event_update_behavior(
|
||||||
hass: HomeAssistant, mock_events_list_items, component_setup
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_events_list_items,
|
||||||
|
component_setup,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test an future event that becomes active."""
|
"""Test an future event that becomes active."""
|
||||||
now = dt_util.now()
|
now = dt_util.now()
|
||||||
now_utc = dt_util.utcnow()
|
|
||||||
one_hour_from_now = now + datetime.timedelta(minutes=60)
|
one_hour_from_now = now + datetime.timedelta(minutes=60)
|
||||||
end_event = one_hour_from_now + datetime.timedelta(minutes=90)
|
end_event = one_hour_from_now + datetime.timedelta(minutes=90)
|
||||||
event = {
|
event = {
|
||||||
@ -600,12 +603,9 @@ async def test_future_event_update_behavior(
|
|||||||
|
|
||||||
# Advance time until event has started
|
# Advance time until event has started
|
||||||
now += datetime.timedelta(minutes=60)
|
now += datetime.timedelta(minutes=60)
|
||||||
now_utc += datetime.timedelta(minutes=60)
|
freezer.move_to(now)
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=now_utc), patch(
|
async_fire_time_changed(hass, now)
|
||||||
"homeassistant.util.dt.now", return_value=now
|
await hass.async_block_till_done()
|
||||||
):
|
|
||||||
async_fire_time_changed(hass, now)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
# Event has started
|
# Event has started
|
||||||
state = hass.states.get(TEST_ENTITY)
|
state = hass.states.get(TEST_ENTITY)
|
||||||
@ -613,11 +613,13 @@ async def test_future_event_update_behavior(
|
|||||||
|
|
||||||
|
|
||||||
async def test_future_event_offset_update_behavior(
|
async def test_future_event_offset_update_behavior(
|
||||||
hass: HomeAssistant, mock_events_list_items, component_setup
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_events_list_items,
|
||||||
|
component_setup,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test an future event that becomes active."""
|
"""Test an future event that becomes active."""
|
||||||
now = dt_util.now()
|
now = dt_util.now()
|
||||||
now_utc = dt_util.utcnow()
|
|
||||||
one_hour_from_now = now + datetime.timedelta(minutes=60)
|
one_hour_from_now = now + datetime.timedelta(minutes=60)
|
||||||
end_event = one_hour_from_now + datetime.timedelta(minutes=90)
|
end_event = one_hour_from_now + datetime.timedelta(minutes=90)
|
||||||
event_summary = "Test Event in Progress"
|
event_summary = "Test Event in Progress"
|
||||||
@ -638,12 +640,9 @@ async def test_future_event_offset_update_behavior(
|
|||||||
|
|
||||||
# Advance time until event has started
|
# Advance time until event has started
|
||||||
now += datetime.timedelta(minutes=45)
|
now += datetime.timedelta(minutes=45)
|
||||||
now_utc += datetime.timedelta(minutes=45)
|
freezer.move_to(now)
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=now_utc), patch(
|
async_fire_time_changed(hass, now)
|
||||||
"homeassistant.util.dt.now", return_value=now
|
await hass.async_block_till_done()
|
||||||
):
|
|
||||||
async_fire_time_changed(hass, now)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
# Event has not started, but the offset was reached
|
# Event has not started, but the offset was reached
|
||||||
state = hass.states.get(TEST_ENTITY)
|
state = hass.states.get(TEST_ENTITY)
|
||||||
|
@ -9,6 +9,7 @@ from typing import Any
|
|||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ClientError
|
from aiohttp.client_exceptions import ClientError
|
||||||
|
from freezegun import freeze_time
|
||||||
from oauth2client.client import (
|
from oauth2client.client import (
|
||||||
DeviceFlowInfo,
|
DeviceFlowInfo,
|
||||||
FlowExchangeError,
|
FlowExchangeError,
|
||||||
@ -130,7 +131,7 @@ async def primary_calendar(
|
|||||||
|
|
||||||
async def fire_alarm(hass, point_in_time):
|
async def fire_alarm(hass, point_in_time):
|
||||||
"""Fire an alarm and wait for callbacks to run."""
|
"""Fire an alarm and wait for callbacks to run."""
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=point_in_time):
|
with freeze_time(point_in_time):
|
||||||
async_fire_time_changed(hass, point_in_time)
|
async_fire_time_changed(hass, point_in_time)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
@ -699,7 +699,11 @@ async def test_add_event_location(
|
|||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"config_entry_token_expiry",
|
"config_entry_token_expiry",
|
||||||
[datetime.datetime.max.replace(tzinfo=UTC).timestamp() + 1],
|
[
|
||||||
|
(datetime.datetime.max.replace(tzinfo=UTC).timestamp() + 1),
|
||||||
|
(utcnow().replace(tzinfo=None).timestamp()),
|
||||||
|
],
|
||||||
|
ids=["max_timestamp", "timestamp_naive"],
|
||||||
)
|
)
|
||||||
async def test_invalid_token_expiry_in_config_entry(
|
async def test_invalid_token_expiry_in_config_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from unittest.mock import ANY, patch
|
from unittest.mock import ANY, patch
|
||||||
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import (
|
from homeassistant.components import (
|
||||||
@ -76,7 +77,7 @@ from homeassistant.core import (
|
|||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
State,
|
State,
|
||||||
)
|
)
|
||||||
from homeassistant.util import color
|
from homeassistant.util import color, dt as dt_util
|
||||||
from homeassistant.util.unit_conversion import TemperatureConverter
|
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||||
|
|
||||||
from . import BASIC_CONFIG, MockConfig
|
from . import BASIC_CONFIG, MockConfig
|
||||||
@ -3389,7 +3390,9 @@ async def test_humidity_setting_sensor_data(
|
|||||||
assert err.value.code == const.ERR_NOT_SUPPORTED
|
assert err.value.code == const.ERR_NOT_SUPPORTED
|
||||||
|
|
||||||
|
|
||||||
async def test_transport_control(hass: HomeAssistant) -> None:
|
async def test_transport_control(
|
||||||
|
hass: HomeAssistant, freezer: FrozenDateTimeFactory
|
||||||
|
) -> None:
|
||||||
"""Test the TransportControlTrait."""
|
"""Test the TransportControlTrait."""
|
||||||
assert helpers.get_google_type(media_player.DOMAIN, None) is not None
|
assert helpers.get_google_type(media_player.DOMAIN, None) is not None
|
||||||
|
|
||||||
@ -3398,7 +3401,7 @@ async def test_transport_control(hass: HomeAssistant) -> None:
|
|||||||
media_player.DOMAIN, feature, None, None
|
media_player.DOMAIN, feature, None, None
|
||||||
)
|
)
|
||||||
|
|
||||||
now = datetime(2020, 1, 1)
|
now = datetime(2020, 1, 1, tzinfo=dt_util.UTC)
|
||||||
|
|
||||||
trt = trait.TransportControlTrait(
|
trt = trait.TransportControlTrait(
|
||||||
hass,
|
hass,
|
||||||
@ -3429,13 +3432,13 @@ async def test_transport_control(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Patch to avoid time ticking over during the command failing the test
|
# Patch to avoid time ticking over during the command failing the test
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
freezer.move_to(now)
|
||||||
await trt.execute(
|
await trt.execute(
|
||||||
trait.COMMAND_MEDIA_SEEK_RELATIVE,
|
trait.COMMAND_MEDIA_SEEK_RELATIVE,
|
||||||
BASIC_DATA,
|
BASIC_DATA,
|
||||||
{"relativePositionMs": 10000},
|
{"relativePositionMs": 10000},
|
||||||
{},
|
{},
|
||||||
)
|
)
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].data == {
|
assert calls[0].data == {
|
||||||
ATTR_ENTITY_ID: "media_player.bla",
|
ATTR_ENTITY_ID: "media_player.bla",
|
||||||
|
@ -8,6 +8,7 @@ from http import HTTPStatus
|
|||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from freezegun import freeze_time
|
||||||
from google_nest_sdm.event import EventMessage
|
from google_nest_sdm.event import EventMessage
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -173,7 +174,7 @@ async def async_get_image(hass, width=None, height=None):
|
|||||||
|
|
||||||
async def fire_alarm(hass, point_in_time):
|
async def fire_alarm(hass, point_in_time):
|
||||||
"""Fire an alarm and wait for callbacks to run."""
|
"""Fire an alarm and wait for callbacks to run."""
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=point_in_time):
|
with freeze_time(point_in_time):
|
||||||
async_fire_time_changed(hass, point_in_time)
|
async_fire_time_changed(hass, point_in_time)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user