feat: autoayuno-api (FastAPI) — alimentos (OFF) y recetas

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Carlos Narro
2026-07-01 19:35:02 +02:00
commit 150a323a37
9 changed files with 369 additions and 0 deletions

46
tests/test_api.py Normal file
View File

@@ -0,0 +1,46 @@
import os
import sys
# BD en memoria para tests, antes de importar la app.
os.environ["DATABASE_URL"] = "sqlite://"
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
import off # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
from main import app # noqa: E402
client = TestClient(app)
def test_health():
assert client.get("/health").json() == {"status": "ok"}
def test_add_and_search_food():
r = client.post("/foods", json={"name": "Mi salsa", "kcalPer100g": 120})
assert r.status_code == 200
assert r.json()["kcalPer100g"] == 120
res = client.get("/foods", params={"q": "salsa"}).json()
assert any(f["name"] == "Mi salsa" for f in res)
def test_search_imports_from_off(monkeypatch):
monkeypatch.setattr(
off, "search",
lambda q, limit=12: [{"name": "Cosa OFF", "kcal_100g": 90, "off_id": "111", "barcode": "111"}],
)
res = client.get("/foods", params={"q": "cosarara"}).json()
assert any(f["name"] == "Cosa OFF" for f in res)
def test_publish_and_get_recipe():
r = client.post("/recipes", json={
"name": "Ensalada",
"servings": 2,
"items": [{"food": {"name": "Lechuga", "kcalPer100g": 15}, "grams": 100}],
})
assert r.status_code == 200
rid = r.json()["id"]
got = client.get(f"/recipes/{rid}").json()
assert got["name"] == "Ensalada"
assert got["servings"] == 2