From 6a897f6ede13f7c640e6981f7b68104d3c4b4591 Mon Sep 17 00:00:00 2001 From: Amit Finkelstein Date: Fri, 1 Nov 2024 09:47:31 +0000 Subject: [PATCH] Fix coordinator tests --- custom_components/oref_alert/coordinator.py | 10 ++-- tests/test_coordinator.py | 58 +++++++++------------ 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/custom_components/oref_alert/coordinator.py b/custom_components/oref_alert/coordinator.py index d01cced..621731b 100644 --- a/custom_components/oref_alert/coordinator.py +++ b/custom_components/oref_alert/coordinator.py @@ -18,6 +18,7 @@ from .const import ( CONF_ALERT_ACTIVE_DURATION, CONF_POLL_INTERVAL, + DEFAULT_ALERT_ACTIVE_DURATION, DEFAULT_POLL_INTERVAL, DOMAIN, IST, @@ -68,6 +69,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: super().__init__( hass, LOGGER, + config_entry=config_entry, name=DOMAIN, update_interval=timedelta( seconds=config_entry.options.get( @@ -75,7 +77,9 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: ) ), ) - self._config_entry: ConfigEntry = config_entry + self._active_duration = config_entry.options.get( + CONF_ALERT_ACTIVE_DURATION, DEFAULT_ALERT_ACTIVE_DURATION + ) self._http_client = async_get_clientsession(hass) self._http_cache = {} self._synthetic_alerts: dict[int, dict[str, Any]] = {} @@ -173,9 +177,7 @@ def _current_to_history_format( def _active_alerts(self, alerts: list[Any]) -> list[Any]: """Return the list of active alerts.""" - return self._recent_alerts( - alerts, self._config_entry.options[CONF_ALERT_ACTIVE_DURATION] - ) + return self._recent_alerts(alerts, self._active_duration) def _recent_alerts(self, alerts: list[Any], active_duration: int) -> list[Any]: """Return the list of recent alerts, assuming the input is sorted.""" diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index 4d1ecc0..735dd13 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -6,6 +6,7 @@ import homeassistant.util.dt as dt_util import pytest from freezegun.api import FrozenDateTimeFactory +from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from pytest_homeassistant_custom_component.common import ( @@ -29,16 +30,25 @@ from .utils import load_json_fixture, mock_urls -DEFAULT_CONFIG_ENTRY = MockConfigEntry( - domain=DOMAIN, options={CONF_ALERT_ACTIVE_DURATION: DEFAULT_ALERT_ACTIVE_DURATION} -) + +def create_coordinator( + hass: HomeAssistant, options: dict | None = None +) -> OrefAlertDataUpdateCoordinator: + """Create a test coordinator.""" + config = MockConfigEntry( + domain=DOMAIN, + options={ + CONF_ALERT_ACTIVE_DURATION: DEFAULT_ALERT_ACTIVE_DURATION, + **(options if options else {}), + }, + ) + config.mock_state(hass, ConfigEntryState.SETUP_IN_PROGRESS) + return OrefAlertDataUpdateCoordinator(hass, config) async def test_init(hass: HomeAssistant) -> None: """Test initializing the coordinator.""" - await OrefAlertDataUpdateCoordinator( - hass, DEFAULT_CONFIG_ENTRY - ).async_config_entry_first_refresh() + await create_coordinator(hass).async_config_entry_first_refresh() # No pending refresh since there are no listeners @@ -54,16 +64,7 @@ def update() -> None: nonlocal updates updates += 1 - coordinator = OrefAlertDataUpdateCoordinator( - hass, - MockConfigEntry( - domain=DOMAIN, - options={ - CONF_POLL_INTERVAL: 100, - CONF_ALERT_ACTIVE_DURATION: DEFAULT_ALERT_ACTIVE_DURATION, - }, - ), - ) + coordinator = create_coordinator(hass, {CONF_POLL_INTERVAL: 100}) coordinator.async_add_listener(update) for _ in range(10): freezer.tick(timedelta(seconds=50)) @@ -81,7 +82,7 @@ async def test_server_down_during_init( ) -> None: """Test errors on HTTP requests during initialization.""" mock_urls(aioclient_mock, None, None, exc=Exception("dummy log for testing")) - coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY) + coordinator = create_coordinator(hass) with pytest.raises(ConfigEntryNotReady): await coordinator.async_config_entry_first_refresh() assert "dummy log for testing" in caplog.text @@ -97,7 +98,7 @@ async def test_alerts_processing( mock_urls( aioclient_mock, "multi_alerts_real_time.json", "multi_alerts_history.json" ) - coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY) + coordinator = create_coordinator(hass) await coordinator.async_config_entry_first_refresh() assert coordinator.data.alerts == load_json_fixture("combined_alerts.json") assert coordinator.data.active_alerts == load_json_fixture("combined_alerts.json") @@ -111,10 +112,7 @@ async def test_active_alerts( """Test active alerts logic.""" freezer.move_to("2023-10-07 06:30:00+03:00") mock_urls(aioclient_mock, None, "multi_alerts_history.json") - coordinator = OrefAlertDataUpdateCoordinator( - hass, - MockConfigEntry(domain=DOMAIN, options={CONF_ALERT_ACTIVE_DURATION: 1}), - ) + coordinator = create_coordinator(hass, {CONF_ALERT_ACTIVE_DURATION: 1}) await coordinator.async_config_entry_first_refresh() inactive_alert, active_alert = load_json_fixture("multi_alerts_history.json") assert coordinator.data.alerts == [active_alert, inactive_alert] @@ -129,7 +127,7 @@ async def test_real_time_timestamp( """Test real time timestamp.""" freezer.move_to("2023-10-07 06:30:00+03:00") mock_urls(aioclient_mock, "single_alert_real_time.json", None) - coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY) + coordinator = create_coordinator(hass) await coordinator.async_config_entry_first_refresh() coordinator.async_add_listener(lambda: None) for _ in range(25): @@ -152,7 +150,7 @@ async def test_real_time_in_history( mock_urls( aioclient_mock, "single_alert_real_time.json", "history_same_as_real_time.json" ) - coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY) + coordinator = create_coordinator(hass) await coordinator.async_config_entry_first_refresh() assert len(coordinator.data.alerts) == 1 @@ -169,7 +167,7 @@ async def test_area_name_typo( "single_alert_real_time_typo.json", "single_alert_history_typo.json", ) - coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY) + coordinator = create_coordinator(hass) await coordinator.async_config_entry_first_refresh() assert len(coordinator.data.alerts) == 1 assert coordinator.data.alerts[0]["data"] == "ביר הדאג\u0027" @@ -180,12 +178,8 @@ async def test_synthetic_alert( freezer: FrozenDateTimeFactory, ) -> None: """Test synthetic alert.""" - coordinator = OrefAlertDataUpdateCoordinator( - hass, - MockConfigEntry( - domain=DOMAIN, - options={CONF_POLL_INTERVAL: 1, CONF_ALERT_ACTIVE_DURATION: 100}, - ), + coordinator = create_coordinator( + hass, {CONF_POLL_INTERVAL: 1, CONF_ALERT_ACTIVE_DURATION: 100} ) await coordinator.async_config_entry_first_refresh() coordinator.async_add_listener(lambda: None) @@ -217,7 +211,7 @@ async def test_http_cache( mock_urls( aioclient_mock, "single_alert_real_time.json", "single_alert_history.json" ) - coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY) + coordinator = create_coordinator(hass) await coordinator.async_config_entry_first_refresh() coordinator.async_add_listener(lambda: None) assert len(coordinator.data.alerts) == 2