78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
"""Tests for §4.6 offline / disconnected degradation without unhandled errors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.migrations import discover_migrations
|
|
from app.db.session import Base
|
|
from app.models.datasource import DataSource
|
|
from app.services.source_freshness import compute_domain_freshness
|
|
|
|
|
|
class _AsyncSQLiteSession:
|
|
def __init__(self, session: Session):
|
|
self._session = session
|
|
|
|
def add(self, instance) -> None:
|
|
self._session.add(instance)
|
|
|
|
async def execute(self, statement, params=None):
|
|
return (
|
|
self._session.execute(statement, params)
|
|
if params is not None
|
|
else self._session.execute(statement)
|
|
)
|
|
|
|
async def commit(self) -> None:
|
|
self._session.commit()
|
|
|
|
|
|
class _AsyncConnShim:
|
|
def __init__(self, conn):
|
|
self._conn = conn
|
|
self.dialect = conn.dialect
|
|
|
|
async def execute(self, stmt, params=None):
|
|
return (
|
|
self._conn.execute(stmt, params)
|
|
if params is not None
|
|
else self._conn.execute(stmt)
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_offline_mode_marks_domains_offline_and_never_raises(monkeypatch) -> None:
|
|
engine = create_engine("sqlite:///:memory:")
|
|
with engine.begin() as raw_conn:
|
|
shim = _AsyncConnShim(raw_conn)
|
|
for step in discover_migrations():
|
|
if step.version in {"0009", "0010", "0011"}:
|
|
await step.upgrade(shim)
|
|
Base.metadata.create_all(raw_conn, checkfirst=True)
|
|
|
|
monkeypatch.setenv("PLANET_OFFLINE_MODE", "1")
|
|
with Session(engine) as sync_sess:
|
|
db = _AsyncSQLiteSession(sync_sess)
|
|
db.add(
|
|
DataSource(
|
|
name="USGS Earthquakes",
|
|
source="usgs_earthquakes",
|
|
module="L2",
|
|
collector_class="USGSEarthquakeCollector",
|
|
is_active=True,
|
|
last_status="offline",
|
|
)
|
|
)
|
|
await db.commit()
|
|
|
|
summary = await compute_domain_freshness(db, now=datetime.now(UTC))
|
|
assert summary["offline_mode"] is True
|
|
dis = next(d for d in summary["domains"] if d["domain"] == "disasters")
|
|
assert dis["status"] == "offline"
|
|
assert dis["is_blind_spot"] is True
|