Apply PR #144 review round 3: lifecycle tests for malformed-option fallback, comment/test hygiene
Add LIFECYCLE tests that wire an entry via make_entry(options=...) and call async_setup_entry, covering the from_options_resilient fallback production actually uses (P2): a fully malformed options dict (horizon_days=True, day_rollover_time='99:99', morning_summary_time='25:00') succeeds with the day-rollover listener at the default 00:00 and the backfill over DEFAULT_HORIZON_DAYS; and a bad sibling (horizon_days=5, morning_summary_time='25:00') keeps horizon_days == 5. Swapping from_options_resilient for from_options in setup now fails these tests. Reword comments/tests describing review history rather than the code: __init__.py setup comment, test_core_settings P2-3/P2-1/P2-5 markers, the moved from_options horizon test, and the AST scanner docstrings. Test hygiene: move freeze_nestquest_clock into tests/conftest.py and use it from test_core_settings and test_timezones; simplify CORE_PKG to a plain import plus Path(mod.__file__).parent; move test_from_options_horizon_validates_and_defaults into test_core_settings. 1575 passed.
This commit is contained in:
+38
-1
@@ -22,6 +22,7 @@ pytest -q``); it is never meant to run against a real HA install.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import datetime
|
||||
import importlib.util
|
||||
import inspect
|
||||
import sys
|
||||
@@ -916,4 +917,40 @@ def make_config_flow_entry():
|
||||
def _factory(domain: str, entry_id: str = "existing"):
|
||||
return SimpleNamespace(domain=domain, entry_id=entry_id)
|
||||
|
||||
return _factory
|
||||
return _factory
|
||||
|
||||
|
||||
def freeze_nestquest_clock(
|
||||
monkeypatch, instant: datetime.datetime
|
||||
) -> tuple[datetime.datetime, list]:
|
||||
"""Freeze the integration's ``datetime`` clock to a fixed aware instant.
|
||||
|
||||
Only ``custom_components.nestquest``'s ``datetime`` binding is patched,
|
||||
so the generation path's ``datetime.datetime.now`` reads this instant
|
||||
(localized to whatever time zone it asks for) while every other module
|
||||
and the test harness keep the real clock. Returns ``(frozen, requested)``
|
||||
where ``frozen`` is the instant normalized to UTC and ``requested`` is
|
||||
the list of ``tzinfo`` objects the code under test passed to ``now`` —
|
||||
so a caller can prove the generation path actually requested the
|
||||
configured HA time zone rather than silently reading UTC.
|
||||
"""
|
||||
import custom_components.nestquest as nestquest
|
||||
|
||||
frozen = instant.astimezone(datetime.timezone.utc)
|
||||
requested: list = []
|
||||
|
||||
class _FrozenDatetime(datetime.datetime):
|
||||
@classmethod
|
||||
def now(cls, tz=None):
|
||||
requested.append(tz)
|
||||
if tz is None:
|
||||
return frozen.replace(tzinfo=None)
|
||||
return frozen.astimezone(tz)
|
||||
|
||||
fake_module = SimpleNamespace(
|
||||
datetime=_FrozenDatetime,
|
||||
timedelta=datetime.timedelta,
|
||||
date=datetime.date,
|
||||
)
|
||||
monkeypatch.setattr(nestquest, "datetime", fake_module)
|
||||
return frozen, requested
|
||||
Reference in New Issue
Block a user