Spaces:
Running
Running
Create test_known_dishes.py
Browse files- test_known_dishes.py +96 -0
test_known_dishes.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# test_known_dishes.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
from typing import Optional
|
| 7 |
+
|
| 8 |
+
# Wichtig: Script im Projekt-Root starten (wo core/ liegt)
|
| 9 |
+
# z.B. python test_known_dishes.py "lebkuchen" 30
|
| 10 |
+
|
| 11 |
+
def _env(name: str) -> str:
|
| 12 |
+
return os.getenv(name, "").strip()
|
| 13 |
+
|
| 14 |
+
def _print_header(title: str) -> None:
|
| 15 |
+
print("\n" + "=" * 80)
|
| 16 |
+
print(title)
|
| 17 |
+
print("=" * 80)
|
| 18 |
+
|
| 19 |
+
def main() -> int:
|
| 20 |
+
# CLI args
|
| 21 |
+
q: str = sys.argv[1].strip() if len(sys.argv) >= 2 else "lebkuchen"
|
| 22 |
+
grams: Optional[float] = None
|
| 23 |
+
if len(sys.argv) >= 3:
|
| 24 |
+
try:
|
| 25 |
+
grams = float(sys.argv[2])
|
| 26 |
+
except Exception:
|
| 27 |
+
grams = None
|
| 28 |
+
|
| 29 |
+
_print_header("CONFIG (ENV)")
|
| 30 |
+
print("KNOWN_DISHES_SHEET_ID:", bool(_env("KNOWN_DISHES_SHEET_ID")), "(gesetzt)" if _env("KNOWN_DISHES_SHEET_ID") else "(leer)")
|
| 31 |
+
print("AGENT_SHEET_ID:", bool(_env("AGENT_SHEET_ID")), "(gesetzt)" if _env("AGENT_SHEET_ID") else "(leer)")
|
| 32 |
+
print("KNOWN_DISHES_TAB:", _env("KNOWN_DISHES_TAB") or "known_dishes (default)")
|
| 33 |
+
print("KNOWN_DISHES_CSV_FALLBACK:", _env("KNOWN_DISHES_CSV_FALLBACK") or "(leer)")
|
| 34 |
+
print("CARBS_DB_TTL_S:", _env("CARBS_DB_TTL_S") or "600 (default)")
|
| 35 |
+
|
| 36 |
+
# Import after printing env (damit du sofort siehst ob env leer ist)
|
| 37 |
+
_print_header("IMPORTS")
|
| 38 |
+
try:
|
| 39 |
+
from core.tools import carbs_db
|
| 40 |
+
print("OK: core.tools.carbs_db importiert")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print("FEHLER: Import core.tools.carbs_db fehlgeschlagen:", repr(e))
|
| 43 |
+
return 2
|
| 44 |
+
|
| 45 |
+
# Force reload
|
| 46 |
+
_print_header("RELOAD_DB(force=True)")
|
| 47 |
+
try:
|
| 48 |
+
res = carbs_db.reload_db(force=True)
|
| 49 |
+
print(res)
|
| 50 |
+
if not res.get("ok"):
|
| 51 |
+
print("\n❌ DB konnte nicht geladen werden.")
|
| 52 |
+
print(" -> Prüfe Sheet-ID, Tab-Name, Service Account, Rechte auf Sheet")
|
| 53 |
+
return 3
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print("FEHLER beim reload_db:", repr(e))
|
| 56 |
+
return 3
|
| 57 |
+
|
| 58 |
+
# Search
|
| 59 |
+
_print_header(f"SEARCH_FOOD(q={q!r})")
|
| 60 |
+
try:
|
| 61 |
+
sres = carbs_db.search_food(q, limit=10)
|
| 62 |
+
print("ok:", sres.get("ok"))
|
| 63 |
+
print("query_norm:", sres.get("query"))
|
| 64 |
+
hits = sres.get("hits") or []
|
| 65 |
+
print("hits:", len(hits))
|
| 66 |
+
for i, h in enumerate(hits, 1):
|
| 67 |
+
print(f"{i:02d}. {h.get('name')} | {h.get('kh_per_100g')} g/100g | score={h.get('score')} | aliases={len(h.get('aliases') or [])}")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print("FEHLER bei search_food:", repr(e))
|
| 70 |
+
return 4
|
| 71 |
+
|
| 72 |
+
# Estimate 100g
|
| 73 |
+
_print_header(f"ESTIMATE_CARBS(food={q!r}, grams=100)")
|
| 74 |
+
try:
|
| 75 |
+
eres_100 = carbs_db.estimate_carbs(q, 100.0)
|
| 76 |
+
print(eres_100)
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print("FEHLER bei estimate_carbs(100g):", repr(e))
|
| 79 |
+
return 5
|
| 80 |
+
|
| 81 |
+
# Estimate custom grams (optional)
|
| 82 |
+
if grams is not None:
|
| 83 |
+
_print_header(f"ESTIMATE_CARBS(food={q!r}, grams={grams})")
|
| 84 |
+
try:
|
| 85 |
+
eres = carbs_db.estimate_carbs(q, grams)
|
| 86 |
+
print(eres)
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print("FEHLER bei estimate_carbs(custom):", repr(e))
|
| 89 |
+
return 6
|
| 90 |
+
|
| 91 |
+
print("\n✅ Test fertig.")
|
| 92 |
+
return 0
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
raise SystemExit(main())
|