Fix flaky datetime test (#68524)

This commit is contained in:
Marc Mueller 2022-03-23 09:30:01 +01:00 committed by GitHub
parent 0c45241d43
commit c9cc2eb7c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -797,6 +797,8 @@ def enable_nightly_purge():
@pytest.fixture @pytest.fixture
def hass_recorder(enable_nightly_purge, enable_statistics, hass_storage): def hass_recorder(enable_nightly_purge, enable_statistics, hass_storage):
"""Home Assistant fixture with in-memory recorder.""" """Home Assistant fixture with in-memory recorder."""
original_tz = dt_util.DEFAULT_TIME_ZONE
hass = get_test_home_assistant() hass = get_test_home_assistant()
stats = recorder.Recorder.async_periodic_statistics if enable_statistics else None stats = recorder.Recorder.async_periodic_statistics if enable_statistics else None
nightly = recorder.Recorder.async_nightly_tasks if enable_nightly_purge else None nightly = recorder.Recorder.async_nightly_tasks if enable_nightly_purge else None
@ -821,6 +823,9 @@ def hass_recorder(enable_nightly_purge, enable_statistics, hass_storage):
yield setup_recorder yield setup_recorder
hass.stop() hass.stop()
# Restore timezone, it is set when creating the hass object
dt_util.DEFAULT_TIME_ZONE = original_tz
@pytest.fixture @pytest.fixture
async def recorder_mock(enable_nightly_purge, enable_statistics, hass): async def recorder_mock(enable_nightly_purge, enable_statistics, hass):

View file

@ -67,9 +67,13 @@ def test_repr_helper():
assert util.repr_helper(5) == "5" assert util.repr_helper(5) == "5"
assert util.repr_helper(True) == "True" assert util.repr_helper(True) == "True"
assert util.repr_helper({"test": 1}) == "test=1" assert util.repr_helper({"test": 1}) == "test=1"
assert (
util.repr_helper(datetime(1986, 7, 9, 12, 0, 0)) == "1986-07-09T12:00:00+00:00" tz = dt_util.get_time_zone("Europe/Copenhagen")
) with patch("homeassistant.util.dt.DEFAULT_TIME_ZONE", tz):
assert (
util.repr_helper(datetime(1986, 7, 9, 12, 0, 0))
== "1986-07-09T12:00:00+02:00"
)
def test_convert(): def test_convert():