From c9cc2eb7c8357ab556f41a4386b0ddcf86e1e454 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 23 Mar 2022 09:30:01 +0100 Subject: [PATCH] Fix flaky datetime test (#68524) --- tests/conftest.py | 5 +++++ tests/util/test_init.py | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9f19415cafa..92f19f087a6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -797,6 +797,8 @@ def enable_nightly_purge(): @pytest.fixture def hass_recorder(enable_nightly_purge, enable_statistics, hass_storage): """Home Assistant fixture with in-memory recorder.""" + original_tz = dt_util.DEFAULT_TIME_ZONE + hass = get_test_home_assistant() stats = recorder.Recorder.async_periodic_statistics if enable_statistics 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 hass.stop() + # Restore timezone, it is set when creating the hass object + dt_util.DEFAULT_TIME_ZONE = original_tz + @pytest.fixture async def recorder_mock(enable_nightly_purge, enable_statistics, hass): diff --git a/tests/util/test_init.py b/tests/util/test_init.py index d6508b40c37..c1eb878ab95 100644 --- a/tests/util/test_init.py +++ b/tests/util/test_init.py @@ -67,9 +67,13 @@ def test_repr_helper(): assert util.repr_helper(5) == "5" assert util.repr_helper(True) == "True" 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():