Skip to content

Commit

Permalink
Fix coordinator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amitfin committed Nov 1, 2024
1 parent 2b02a2e commit 6a897f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
10 changes: 6 additions & 4 deletions custom_components/oref_alert/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .const import (
CONF_ALERT_ACTIVE_DURATION,
CONF_POLL_INTERVAL,
DEFAULT_ALERT_ACTIVE_DURATION,
DEFAULT_POLL_INTERVAL,
DOMAIN,
IST,
Expand Down Expand Up @@ -68,14 +69,17 @@ 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(
CONF_POLL_INTERVAL, DEFAULT_POLL_INTERVAL
)
),
)
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]] = {}
Expand Down Expand Up @@ -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."""
Expand Down
58 changes: 26 additions & 32 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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


Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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]
Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a897f6

Please sign in to comment.