search_schools_typesense returns URNs, which forces the caller to hydrate from the 25,000-row in-memory frame. Every field a suggestion needs is already in the Typesense document, so this returns documents and the caller needs no pandas at all — the difference between a query that can run per keystroke and one that cannot. Never raises. Typesense unreachable or erroring gives an empty list, because a dropdown that quietly stops appearing is the right failure for a keystroke path. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015mWQnpye9F299NVRCCSRvj
72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
"""Tests for school autosuggest (spec 2026-08-26)."""
|
|
|
|
from backend import data_loader
|
|
|
|
|
|
class _FakeDocs:
|
|
def __init__(self, hits, explode=False):
|
|
self._hits = hits
|
|
self._explode = explode
|
|
self.last_params = None
|
|
|
|
def search(self, params):
|
|
self.last_params = params
|
|
if self._explode:
|
|
raise RuntimeError("typesense is down")
|
|
return {"hits": [{"document": d} for d in self._hits]}
|
|
|
|
|
|
class _FakeClient:
|
|
def __init__(self, hits, explode=False):
|
|
self.docs = _FakeDocs(hits, explode)
|
|
self.collections = {"schools": type("C", (), {"documents": self.docs})()}
|
|
|
|
|
|
_HIT = {
|
|
"urn": 100010, "school_name": "Brecknock Primary School",
|
|
"local_authority": "Camden", "postcode": "NW1 1AA",
|
|
"phase": "Primary", "school_type": "Community school",
|
|
}
|
|
|
|
|
|
def _use(monkeypatch, client):
|
|
monkeypatch.setattr(data_loader, "_get_typesense_client", lambda: client)
|
|
|
|
|
|
def test_returns_the_fields_a_suggestion_needs(monkeypatch):
|
|
# Local authority is not decoration: there are many schools called
|
|
# "St Mary's", and a list without it cannot be chosen between.
|
|
_use(monkeypatch, _FakeClient([_HIT]))
|
|
out = data_loader.suggest_schools_typesense("breck")
|
|
assert out == [{
|
|
"urn": 100010, "school_name": "Brecknock Primary School",
|
|
"local_authority": "Camden", "postcode": "NW1 1AA",
|
|
"phase": "Primary", "school_type": "Community school",
|
|
}]
|
|
|
|
|
|
def test_a_missing_optional_field_becomes_an_empty_string(monkeypatch):
|
|
# phase and school_type are optional in the Typesense schema. A missing
|
|
# key must not KeyError in the keystroke path.
|
|
_use(monkeypatch, _FakeClient([{"urn": 1, "school_name": "X",
|
|
"local_authority": "Y", "postcode": "Z"}]))
|
|
out = data_loader.suggest_schools_typesense("x")
|
|
assert out[0]["phase"] == "" and out[0]["school_type"] == ""
|
|
|
|
|
|
def test_typesense_unavailable_gives_no_suggestions_rather_than_raising(monkeypatch):
|
|
_use(monkeypatch, None)
|
|
assert data_loader.suggest_schools_typesense("anything") == []
|
|
|
|
|
|
def test_a_typesense_error_gives_no_suggestions_rather_than_raising(monkeypatch):
|
|
_use(monkeypatch, _FakeClient([], explode=True))
|
|
assert data_loader.suggest_schools_typesense("anything") == []
|
|
|
|
|
|
def test_the_limit_is_passed_through_and_clamped(monkeypatch):
|
|
client = _FakeClient([])
|
|
_use(monkeypatch, client)
|
|
data_loader.suggest_schools_typesense("x", limit=500)
|
|
assert client.docs.last_params["per_page"] == 20
|