Skip to content

Commit

Permalink
Use freezegun in tests and add last_fetched to snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Apr 10, 2024
1 parent 3f09f5f commit d1e663d
Show file tree
Hide file tree
Showing 35 changed files with 91 additions and 2 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
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""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
from glob import iglob
import json
import logging
import os
import freezegun
import shutil
from typing import Any, Generator
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -76,6 +78,11 @@
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 @@ -265,7 +272,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."""
from __future__ import annotations

import datetime
import freezegun

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

class CustomFakeDatetime(freezegun.api.FakeDatetime): # type: ignore[name-defined]
"""Modified to workaround tz problem."""

@classmethod
def _tz_offset(cls):
return datetime.timedelta(hours=0)


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


dt_util.utcnow = _utcnow # type: ignore[assignment]
util.utcnow = _utcnow # type: ignore[assignment]
freezegun.api.FakeDatetime = CustomFakeDatetime

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
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
Loading

0 comments on commit d1e663d

Please sign in to comment.