central/tests/test_preview_hook.py

120 lines
3.8 KiB
Python
Raw Normal View History

feat(2-G.5): preview_for_settings framework hook + NWIS opt-in Adds an optional async hook on SourceAdapter so any adapter can surface a settings-driven preview on its /adapters/<name> edit page. The framework renders the result generically as a table — no adapter-name branches in GUI templates or route code. Framework changes: - src/central/adapter.py: new async preview_for_settings(self, settings) on the base class, default returns None. Adapters opt in by overriding; non-overriding adapters render unchanged. - src/central/gui/routes.py: GET /adapters/{name} instantiates the adapter with a no-op _PreviewConfigStore stub and a /dev/null cursor path (GUI has no live ConfigStore), constructs settings_obj via the schema, and calls preview_for_settings inside a try/except. Result lands in template context as preview_rows / preview_error. - src/central/gui/templates/_adapter_preview.html: new partial. Generic table with columns derived from the first dict's keys; error banner mirrors the existing last_error article style. - src/central/gui/templates/adapters_edit.html: one-line include between the Region fieldset and Save/Cancel. NWIS opt-in: - New NWIS_MONITORING_LOCATIONS_URL constant and _PREVIEW_LIMIT cap of 50. - preview_for_settings returns None when region is None, otherwise one-shot fetches monitoring-locations within the bbox via a fresh aiohttp session. Must work even when adapter is not started -- the GUI process never calls startup(). Returns list[dict] with the contract column order: site_id, name, site_type, state. Errors propagate so the framework can render the operator-visible banner. - HTTP call factored into _fetch_preview_text so tests mock cleanly. Tests (7 new): - tests/test_preview_hook.py: default returns None; partial renders list with correct headers/rows/count; partial renders error banner; partial renders empty when both context values are None. - tests/test_nwis.py adds TestNWISPreview: returns None without region, returns rows with correct column order, propagates HTTP errors. Verification: - 457/457 full suite green (was 450; +7 new tests). - Live /adapters/nwis preview returns 50 rows with the contract keys against the current production Iowa bbox. - /adapters/eonet preview_for_settings returns None via base default -- proves framework is duck-typed, no NWIS-specific code in framework.
2026-05-19 17:34:35 +00:00
"""Tests for the SourceAdapter.preview_for_settings hook + framework rendering."""
from collections.abc import AsyncIterator
from pathlib import Path
import jinja2
import pytest
from pydantic import BaseModel
from central.adapter import SourceAdapter
from central.models import Event
TEMPLATES_DIR = Path(__file__).resolve().parents[1] / "src" / "central" / "gui" / "templates"
class _StubSettings(BaseModel):
pass
class _StubAdapter(SourceAdapter):
"""Minimal SourceAdapter subclass that does NOT override preview_for_settings."""
name = "stub"
display_name = "Stub Adapter"
description = "Test fixture"
settings_schema = _StubSettings
default_cadence_s = 60
def __init__(self) -> None:
pass
async def poll(self) -> AsyncIterator[Event]: # pragma: no cover - never invoked
if False:
yield # type: ignore[unreachable]
return
async def apply_config(self, new_config) -> None: # pragma: no cover
pass
def subject_for(self, event: Event) -> str: # pragma: no cover
return "stub"
@pytest.mark.asyncio
async def test_default_returns_none():
"""The base SourceAdapter's preview_for_settings default returns None.
Any adapter that does not override the hook gets a no-op preview, so the
framework renders the page without a preview block (no crash, no opt-out
boilerplate required in each adapter).
"""
adapter = _StubAdapter()
result = await adapter.preview_for_settings(_StubSettings())
assert result is None
def _render_partial(**context) -> str:
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(str(TEMPLATES_DIR)),
autoescape=jinja2.select_autoescape(["html"]),
)
tmpl = env.get_template("_adapter_preview.html")
return tmpl.render(**context)
def test_partial_renders_list():
"""Given list[dict] with insertion-ordered keys, partial renders a table.
Framework is duck-typed: columns come from the first dict's keys. No
adapter-name branches, no per-adapter Jinja.
"""
rows = [
{"a": "x1", "b": "y1"},
{"a": "x2", "b": "y2"},
]
out = _render_partial(preview_rows=rows, preview_error=None)
# Header row uses first dict's keys in order.
assert "<th>a</th>" in out
assert "<th>b</th>" in out
# Body rows carry every value.
for cell in ("x1", "y1", "x2", "y2"):
assert cell in out
# Row count surfaces in the legend.
assert "Preview (2 rows)" in out
# No error banner when preview_error is None.
assert "Preview Unavailable" not in out
def test_partial_renders_error():
"""When preview_error is set, partial renders the error banner and no table."""
out = _render_partial(preview_rows=None, preview_error="Preview unavailable: boom")
assert "Preview unavailable: boom" in out
# No table when an error is set.
assert "<table" not in out
assert "<th>" not in out
def test_partial_renders_nothing_when_both_none():
"""No preview_rows and no preview_error -> partial renders no preview section.
Lets adapters that don't opt in (e.g. EONET, GDACS) render normally without
any preview-related markup on the page.
"""
out = _render_partial(preview_rows=None, preview_error=None).strip()
assert "<table" not in out
assert "Preview Unavailable" not in out
# Either empty or only whitespace/newlines from the template.
assert "Preview (" not in out
def test_partial_renders_empty_list():
"""Empty list -> legend with (0 rows), no table.
Distinct from None (which renders nothing at all). Lets adapters signal
'query ran, matched zero rows' separately from 'preview not meaningful'.
"""
out = _render_partial(preview_rows=[], preview_error=None)
assert "Preview (0 rows)" in out
assert "<table" not in out
assert "<th>" not in out