Files
cucinafood/tests/test_search.py
T
mo 10e510682c Eerste merksite voor Cucina Food.
Statische HTML/CSS/JS: homepage, productlijnen, recepten, zoekbare winkelvinder.
2026-08-29 14:34:18 +02:00

51 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import unicodedata
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def norm(s: str) -> str:
s = unicodedata.normalize("NFD", s or "")
s = "".join(c for c in s if unicodedata.category(c) != "Mn")
return s.lower().replace("'", "").replace("’", "")
def search(stores, q, limit=40):
q = norm(q).strip()
if len(q) < 2:
return []
hits = []
for s in stores:
blob = norm(" ".join([s.get("city", ""), s.get("address", ""), str(s.get("id", "")), s.get("label", "")]))
if q in blob:
hits.append(s)
if len(hits) >= limit:
break
return hits
def test_amsterdam_not_empty():
stores = json.loads((ROOT / "data/stores.json").read_text())
hits = search(stores, "amsterdam")
assert len(hits) >= 5, hits[:3]
assert all("amster" in norm(h["city"]) or "amster" in norm(h["label"]) for h in hits[:5])
def test_empty_query():
stores = json.loads((ROOT / "data/stores.json").read_text())
assert search(stores, "") == []
assert search(stores, "a") == []
def test_unknown_city():
stores = json.loads((ROOT / "data/stores.json").read_text())
assert search(stores, "xyzzyfoo") == []
if __name__ == "__main__":
test_amsterdam_not_empty()
test_empty_query()
test_unknown_city()
print("ok")