Skip to content

Commit

Permalink
Use freezegun in DST tests (home-assistant#58939)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored and natekspencer committed Nov 4, 2021
1 parent 682d7d1 commit 715fdbc
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 162 deletions.
2 changes: 2 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-r requirements_test_pre_commit.txt
codecov==2.1.12
coverage==6.1.1
freezegun==1.1.0
jsonpickle==1.4.1
mock-open==1.4.0
mypy==0.910
Expand All @@ -18,6 +19,7 @@ pipdeptree==2.1.0
pylint-strict-informational==0.1
pytest-aiohttp==0.3.0
pytest-cov==2.12.1
pytest-freezegun==0.4.2
pytest-socket==0.4.1
pytest-test-groups==1.0.3
pytest-sugar==0.9.4
Expand Down
7 changes: 5 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,12 @@ def async_fire_mqtt_message(hass, topic, payload, qos=0, retain=False):

@ha.callback
def async_fire_time_changed(
hass: HomeAssistant, datetime_: datetime, fire_all: bool = False
hass: HomeAssistant, datetime_: datetime = None, fire_all: bool = False
) -> None:
"""Fire a time changes event."""
"""Fire a time changed event."""
if datetime_ is None:
datetime_ = date_util.utcnow()

hass.bus.async_fire(EVENT_TIME_CHANGED, {"now": date_util.as_utc(datetime_)})

for task in list(hass.loop._scheduled):
Expand Down
49 changes: 48 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest.mock import MagicMock, patch

from aiohttp.test_utils import make_mocked_request
import freezegun
import multidict
import pytest
import pytest_socket
Expand Down Expand Up @@ -63,15 +64,24 @@ def pytest_configure(config):


def pytest_runtest_setup():
"""Throw if tests attempt to open sockets.
"""Prepare pytest_socket and freezegun.
pytest_socket:
Throw if tests attempt to open sockets.
allow_unix_socket is set to True because it's needed by asyncio.
Important: socket_allow_hosts must be called before disable_socket, otherwise all
destinations will be allowed.
freezegun:
Modified to include https://github.com/spulec/freezegun/pull/424
"""
pytest_socket.socket_allow_hosts(["127.0.0.1"])
disable_socket(allow_unix_socket=True)

freezegun.api.datetime_to_fakedatetime = ha_datetime_to_fakedatetime
freezegun.api.FakeDatetime = HAFakeDatetime


@pytest.fixture
def socket_disabled(pytestconfig):
Expand Down Expand Up @@ -126,6 +136,43 @@ def __new__(cls, *args, **kwargs):
socket.socket = GuardedSocket


def ha_datetime_to_fakedatetime(datetime):
"""Convert datetime to FakeDatetime.
Modified to include https://github.com/spulec/freezegun/pull/424.
"""
return freezegun.api.FakeDatetime(
datetime.year,
datetime.month,
datetime.day,
datetime.hour,
datetime.minute,
datetime.second,
datetime.microsecond,
datetime.tzinfo,
fold=datetime.fold,
)


class HAFakeDatetime(freezegun.api.FakeDatetime):
"""Modified to include https://github.com/spulec/freezegun/pull/424."""

@classmethod
def now(cls, tz=None):
"""Return frozen now."""
now = cls._time_to_freeze() or freezegun.api.real_datetime.now()
if tz:
result = tz.fromutc(now.replace(tzinfo=tz))
else:
result = now

# Add the _tz_offset only if it's non-zero to preserve fold
if cls._tz_offset():
result += cls._tz_offset()

return ha_datetime_to_fakedatetime(result)


def check_real(func):
"""Force a function to require a keyword _test_real to be passed in."""

Expand Down
Loading

0 comments on commit 715fdbc

Please sign in to comment.