← back to Paul Conrad Archive
tests/test_search_index.py
63 lines
"""q= must also match repository, collection_name and publication text (seed defect: 'wichita' returned 0)."""
import time
from fastapi.testclient import TestClient
from conrad import db
from conrad.models import CartoonRecord
def _client(monkeypatch):
for k in ("CONRAD_BASIC_USER", "CONRAD_BASIC_PASS", "CONRAD_REQUIRE_AUTH"):
monkeypatch.delenv(k, raising=False)
from conrad.web.app import create_app
return TestClient(create_app())
def _seed(conn):
db.upsert_source(conn, "wichita", "Wichita State")
db.upsert_source(conn, "other", "Other")
db.save_record(conn, CartoonRecord(canonical_id="w:1", identifier="1", title="Untitled drawing", year=1970,
repository="Wichita State University Libraries Special Collections",
collection_name="Paul Conrad Cartoons MS 87-10"), "wichita")
db.save_record(conn, CartoonRecord(canonical_id="o:1", identifier="1", title="Nixon at sea", year=1973,
publication="The Daily Iowan", repository="Elsewhere",
collection_name="Box drawings"), "other")
conn.commit()
db.refresh_index(conn)
def test_q_matches_repository_collection_publication(tmpdb, monkeypatch):
_seed(tmpdb)
c = _client(monkeypatch)
get = lambda q: c.get("/api/search", params={"q": q, "granularity": "all"}).json()["total"]
assert get("wichita") == 1 # repository text
assert get("ms 87-10") == 1 # collection_name text
assert get("daily iowan") == 1 # publication text
assert get("NIXON") == 1 # title still works, case-insensitive
assert get("nothing-like-this") == 0
def test_index_migration_backfills_old_index(tmpdb, monkeypatch):
_seed(tmpdb)
tmpdb.execute("UPDATE cartoon_index SET search_text=NULL")
tmpdb.commit()
c = _client(monkeypatch) # create_app must notice NULL search_text and rebuild
assert c.get("/api/search", params={"q": "wichita", "granularity": "all"}).json()["total"] == 1
def test_search_is_fast(tmpdb, monkeypatch):
_seed(tmpdb)
rows = [(f"x:{i}", f"{i}") for i in range(3000)]
for cid, ident in rows:
db.save_record(tmpdb, CartoonRecord(canonical_id=cid, identifier=ident, title=f"t{ident}", year=1960,
repository="Huntington Library", collection_name="Conrad papers"), "other")
tmpdb.commit()
db.refresh_index(tmpdb)
c = _client(monkeypatch)
c.get("/api/search", params={"q": "huntington"})
t = time.perf_counter()
r = c.get("/api/search", params={"q": "huntington", "granularity": "all"}).json()
assert r["total"] == 3000
assert time.perf_counter() - t < 0.1