nutrivision-ai / tests /test_database.py
NAVANEETH Y
NutriVision AI — deploy to Hugging Face Space (model via Git LFS)
c0afe44
Raw History Blame Contribute Delete
1.96 kB
"""Tests for the persistence layer (SQLite-backed)."""
from nutrivision.database import crud
def test_create_and_fetch(db_session, sample_result):
analysis = crud.create_analysis(
db_session, result=sample_result, image_name="meal.jpg",
school_name="GHS Test", district="Mysuru", category="primary",
)
db_session.commit()
assert analysis.id is not None
fetched = crud.get_analysis(db_session, analysis.id)
assert fetched is not None
assert fetched.image_name == "meal.jpg"
assert len(fetched.items) == 2
assert fetched.total_calories == 272.5
def test_list_and_delete(db_session, sample_result):
a = crud.create_analysis(db_session, result=sample_result, school_name="S1")
db_session.commit()
rows = crud.list_analyses(db_session)
assert len(rows) >= 1
assert crud.delete_analysis(db_session, a.id) is True
db_session.commit()
assert crud.get_analysis(db_session, a.id) is None
def test_statistics(db_session, sample_result):
crud.create_analysis(db_session, result=sample_result, school_name="GHS A")
compliant = {**sample_result, "compliance": {
**sample_result["compliance"], "status": "Compliant",
"overall_compliant": True,
}}
crud.create_analysis(db_session, result=compliant, school_name="GHS A")
db_session.commit()
stats = crud.get_statistics(db_session)
assert stats["total_analyses"] == 2
assert stats["compliant_analyses"] == 1
assert stats["compliance_rate"] == 50.0
assert any(s["school_name"] == "GHS A" for s in stats["by_school"])
assert stats["top_foods"] # rice + egg present
def test_update_report_path(db_session, sample_result):
a = crud.create_analysis(db_session, result=sample_result)
db_session.commit()
assert crud.update_report_path(db_session, a.id, "/tmp/x.pdf") is True
db_session.commit()
assert crud.get_analysis(db_session, a.id).report_path == "/tmp/x.pdf"