Skip to content

Commit

Permalink
Use freezegun in tests and add last_fetched to snapshots (#3599)
Browse files Browse the repository at this point in the history
* Use freezegun in tests and add last_fetched to snapshots

* lint

* Remove incorrect workaround

* Also patch the monotonic timer

* Set non UTC system timezone in tests

* Change to a fork of szenius/set-timezone

* Accept review comment

* Revert CI timezone to UTC

---------

Co-authored-by: Erik <[email protected]>
  • Loading branch information
ludeeus and emontnemery authored Apr 11, 2024
1 parent 51addbb commit 13c1c4b
Show file tree
Hide file tree
Showing 35 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-r requirements_base.txt
asynctest==0.13.0
fnv-hash-fast==0.5.0
freezegun==1.4.0
home-assistant-frontend
homeassistant
janus==1.0.0
Expand Down
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _sort_list(entry):
value = data[key]
if key in to_remove:
continue
elif isinstance(value, (str, bool, int, NoneType)):
elif isinstance(value, (str, bool, int, float, NoneType)):
returndata[key] = value
elif isinstance(value, dict):
returndata[key] = recursive_remove_key(
Expand Down
12 changes: 10 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Set up some common test helper things."""
# pytest: disable=protected-access
from . import patch_time # noqa: F401, isort:skip
import asyncio
from collections import OrderedDict
from dataclasses import asdict
Expand All @@ -12,6 +13,7 @@
from unittest.mock import MagicMock, patch

from awesomeversion import AwesomeVersion
import freezegun
from homeassistant import loader
from homeassistant.auth.models import Credentials
from homeassistant.auth.providers.homeassistant import HassAuthProvider
Expand Down Expand Up @@ -76,6 +78,12 @@
asyncio.sleep = lambda _: _sleep(0)


@pytest.fixture(autouse=True)
def time_freezer():
with freezegun.freeze_time("2019-02-26T15:02:39Z"):
yield


@pytest.fixture(autouse=True)
def set_request_context(request: pytest.FixtureRequest):
"""Set request context for every test."""
Expand Down Expand Up @@ -106,7 +114,7 @@ def event_loop():


@pytest.fixture
def hass(event_loop, tmpdir, check_report_issue: None):
def hass(time_freezer, event_loop, tmpdir, check_report_issue: None):
"""Fixture to provide a test instance of Home Assistant."""

def exc_handle(loop, context):
Expand Down Expand Up @@ -265,7 +273,7 @@ async def assert_hacs_data(
),
**(additional or {}),
},
("categories", "config_entry_id", "device_id", "labels", "last_fetched"),
("categories", "config_entry_id", "device_id", "labels"),
)
),
filename,
Expand Down
26 changes: 26 additions & 0 deletions tests/patch_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Patch time related functions.
Copied from Home Assistant Core.
"""
from __future__ import annotations

import datetime
import time

from homeassistant import runner, util
from homeassistant.util import dt as dt_util


def _utcnow() -> datetime.datetime:
"""Make utcnow patchable by freezegun."""
return datetime.datetime.now(tz=datetime.UTC)


def _monotonic() -> float:
"""Make monotonic patchable by freezegun."""
return time.monotonic()


dt_util.utcnow = _utcnow # type: ignore[assignment]
util.utcnow = _utcnow # type: ignore[assignment]
runner.monotonic = _monotonic # type: ignore[assignment]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down Expand Up @@ -42,6 +43,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"full_name": "hacs-test-org/appdaemon-basic",
"id": "1296265",
"installed": true,
"last_fetched": 0.0,
"repository_manifest": {}
}
},
Expand All @@ -22,6 +23,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down Expand Up @@ -41,6 +42,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"releases": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down Expand Up @@ -43,6 +44,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"manifest_name": "Proxy manifest",
Expand Down Expand Up @@ -46,6 +47,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"full_name": "hacs-test-org/integration-basic",
"id": "1296269",
"installed": true,
"last_fetched": 0.0,
"repository_manifest": {}
},
"172733314": {
Expand All @@ -23,6 +24,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"manifest_name": "Proxy manifest",
Expand All @@ -45,6 +46,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"releases": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"manifest_name": "Proxy manifest",
Expand Down Expand Up @@ -46,6 +47,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand All @@ -44,6 +45,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand All @@ -40,6 +41,7 @@
"full_name": "hacs-test-org/plugin-basic",
"id": "1296267",
"installed": true,
"last_fetched": 0.0,
"repository_manifest": {}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"releases": true,
Expand All @@ -42,6 +43,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand All @@ -44,6 +45,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down Expand Up @@ -50,6 +51,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down Expand Up @@ -46,6 +47,7 @@
"full_name": "hacs-test-org/python_script-basic",
"id": "1296262",
"installed": true,
"last_fetched": 0.0,
"repository_manifest": {}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"releases": true,
Expand Down Expand Up @@ -48,6 +49,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down Expand Up @@ -50,6 +51,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"full_name": "hacs/integration",
"id": "172733314",
"installed": true,
"last_fetched": 1551193359.0,
"last_updated": "2023-11-18T21:22:03Z",
"manifest_name": "HACS",
"open_issues": 2,
Expand Down Expand Up @@ -57,6 +58,7 @@
"installed": true,
"installed_commit": "7fd1a60",
"last_commit": "7fd1a60",
"last_fetched": 1551193359.0,
"last_updated": "2011-01-26T19:06:43Z",
"last_version": "1.0.0",
"published_tags": [
Expand Down
Loading

0 comments on commit 13c1c4b

Please sign in to comment.