10e510682c
Statische HTML/CSS/JS: homepage, productlijnen, recepten, zoekbare winkelvinder.
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
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")
|