Skip to content

Commit

Permalink
test_http_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
amitfin committed Oct 27, 2024
1 parent 48a4ecc commit ddde975
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion tests/test_coordinator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The tests for the coordinator file."""

from datetime import timedelta
from http import HTTPStatus

import homeassistant.util.dt as dt_util
import pytest
Expand All @@ -20,7 +21,11 @@
DOMAIN,
IST,
)
from custom_components.oref_alert.coordinator import OrefAlertDataUpdateCoordinator
from custom_components.oref_alert.coordinator import (
OREF_ALERTS_URL,
OREF_HISTORY_URL,
OrefAlertDataUpdateCoordinator,
)

from .utils import load_json_fixture, mock_urls

Expand Down Expand Up @@ -200,3 +205,37 @@ async def test_synthetic_alert(
await hass.async_block_till_done(wait_background_tasks=True)
assert len(coordinator.data.alerts) == 0
await coordinator.async_shutdown()


async def test_http_cache(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test HTTP caching."""
freezer.move_to("2023-10-07 06:30:00+03:00")
mock_urls(
aioclient_mock, "single_alert_real_time.json", "single_alert_history.json"
)
coordinator = OrefAlertDataUpdateCoordinator(hass, DEFAULT_CONFIG_ENTRY)
await coordinator.async_config_entry_first_refresh()
coordinator.async_add_listener(lambda: None)
assert len(coordinator.data.alerts) == 2

aioclient_mock.clear_requests()
aioclient_mock.get(OREF_ALERTS_URL, status=HTTPStatus.NOT_MODIFIED)
aioclient_mock.get(OREF_HISTORY_URL, status=HTTPStatus.NOT_MODIFIED)
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(coordinator.data.alerts) == 2

aioclient_mock.clear_requests()
aioclient_mock.get(OREF_ALERTS_URL, text="")
aioclient_mock.get(OREF_HISTORY_URL, status=HTTPStatus.NOT_MODIFIED)
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(coordinator.data.alerts) == 1

await coordinator.async_shutdown()

0 comments on commit ddde975

Please sign in to comment.