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
|
||||
import datetime
|
||||
import http
|
||||
import time
|
||||
from typing import Any, TypeVar
|
||||
from unittest.mock import Mock, mock_open, patch
|
||||
|
||||
@ -189,9 +190,9 @@ def creds(
|
||||
|
||||
|
||||
@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."""
|
||||
return token_expiry.timestamp()
|
||||
return time.time() + 86400
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -9,6 +9,7 @@ from unittest.mock import patch
|
||||
import urllib
|
||||
|
||||
from aiohttp.client_exceptions import ClientError
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from gcal_sync.auth import API_BASE_URL
|
||||
import pytest
|
||||
|
||||
@ -578,11 +579,13 @@ async def test_scan_calendar_error(
|
||||
|
||||
|
||||
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:
|
||||
"""Test an future event that becomes active."""
|
||||
now = dt_util.now()
|
||||
now_utc = dt_util.utcnow()
|
||||
one_hour_from_now = now + datetime.timedelta(minutes=60)
|
||||
end_event = one_hour_from_now + datetime.timedelta(minutes=90)
|
||||
event = {
|
||||
@ -600,10 +603,7 @@ async def test_future_event_update_behavior(
|
||||
|
||||
# Advance time until event has started
|
||||
now += datetime.timedelta(minutes=60)
|
||||
now_utc += datetime.timedelta(minutes=60)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now_utc), patch(
|
||||
"homeassistant.util.dt.now", return_value=now
|
||||
):
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -613,11 +613,13 @@ async def test_future_event_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:
|
||||
"""Test an future event that becomes active."""
|
||||
now = dt_util.now()
|
||||
now_utc = dt_util.utcnow()
|
||||
one_hour_from_now = now + datetime.timedelta(minutes=60)
|
||||
end_event = one_hour_from_now + datetime.timedelta(minutes=90)
|
||||
event_summary = "Test Event in Progress"
|
||||
@ -638,10 +640,7 @@ async def test_future_event_offset_update_behavior(
|
||||
|
||||
# Advance time until event has started
|
||||
now += datetime.timedelta(minutes=45)
|
||||
now_utc += datetime.timedelta(minutes=45)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now_utc), patch(
|
||||
"homeassistant.util.dt.now", return_value=now
|
||||
):
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -9,6 +9,7 @@ from typing import Any
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from aiohttp.client_exceptions import ClientError
|
||||
from freezegun import freeze_time
|
||||
from oauth2client.client import (
|
||||
DeviceFlowInfo,
|
||||
FlowExchangeError,
|
||||
@ -130,7 +131,7 @@ async def primary_calendar(
|
||||
|
||||
async def fire_alarm(hass, point_in_time):
|
||||
"""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)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -699,7 +699,11 @@ async def test_add_event_location(
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"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(
|
||||
hass: HomeAssistant,
|
||||
|
@ -2,6 +2,7 @@
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import ANY, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import (
|
||||
@ -76,7 +77,7 @@ from homeassistant.core import (
|
||||
HomeAssistant,
|
||||
State,
|
||||
)
|
||||
from homeassistant.util import color
|
||||
from homeassistant.util import color, dt as dt_util
|
||||
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||
|
||||
from . import BASIC_CONFIG, MockConfig
|
||||
@ -3389,7 +3390,9 @@ async def test_humidity_setting_sensor_data(
|
||||
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."""
|
||||
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
|
||||
)
|
||||
|
||||
now = datetime(2020, 1, 1)
|
||||
now = datetime(2020, 1, 1, tzinfo=dt_util.UTC)
|
||||
|
||||
trt = trait.TransportControlTrait(
|
||||
hass,
|
||||
@ -3429,7 +3432,7 @@ async def test_transport_control(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
# 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(
|
||||
trait.COMMAND_MEDIA_SEEK_RELATIVE,
|
||||
BASIC_DATA,
|
||||
|
@ -8,6 +8,7 @@ from http import HTTPStatus
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import aiohttp
|
||||
from freezegun import freeze_time
|
||||
from google_nest_sdm.event import EventMessage
|
||||
import pytest
|
||||
|
||||
@ -173,7 +174,7 @@ async def async_get_image(hass, width=None, height=None):
|
||||
|
||||
async def fire_alarm(hass, point_in_time):
|
||||
"""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)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user