Files
calcount/backend/tests/test_nutrition.py
T
Craig e047d884b6 Initial project scaffold: FastAPI backend + Svelte 5 frontend
Backend (uv): FastAPI app with routers (foods, log, targets, OFF proxy),
SQLAlchemy models, Pydantic schemas, migration runner + 0001 initial schema,
example pytest suite (7 tests).
Frontend (npm): Vite 7 + Svelte 5 runes, lib/ (api, stores, scanner, format)
per spec §7, placeholder components, example vitest suite (4 tests).
SPEC.md §1: registered uvicorn and Vitest (rule §8.3.3).
2026-07-26 10:26:08 +01:00

47 lines
1.3 KiB
Python

"""Example unit tests for the nutrition service (spec §8.4: test the math)."""
from models import Food
from services import nutrition
def make_food(**overrides) -> Food:
"""In-memory Food (no DB needed for pure math tests)."""
defaults = dict(
id=1,
name="Test Food",
brand=None,
barcode=None,
source="manual",
is_meal=False,
unit_type="weight",
calories_per_unit=250.0,
protein_per_unit=None,
carbs_per_unit=None,
fat_per_unit=None,
fiber_per_unit=None,
saturated_fat_per_unit=None,
sugars_per_unit=None,
sodium_per_unit=None,
serving_size_g=None,
serving_name=None,
off_data=None,
deleted_at=None,
)
defaults.update(overrides)
return Food(**defaults)
def test_weight_type_scales_per_100g():
food = make_food(unit_type="weight", calories_per_unit=250.0)
assert nutrition.entry_calories(food, 200.0) == 500.0
def test_count_type_scales_per_item():
food = make_food(unit_type="count", calories_per_unit=80.0)
assert nutrition.entry_calories(food, 3.0) == 240.0
def test_missing_nutrition_counts_as_zero():
food = make_food(calories_per_unit=None)
assert nutrition.entry_calories(food, 100.0) == 0.0