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).
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
"""Test fixtures: fresh temp-file SQLite DB per test session (spec §8.4)."""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
|
||||
# Must be set before any app module is imported (engine binds at import time)
|
||||
_tmpdir = tempfile.mkdtemp(prefix="calcount-test-")
|
||||
os.environ["CALCOUNT_DATABASE_URL"] = f"sqlite:///{_tmpdir}/test.db"
|
||||
|
||||
from fastapi.testclient import TestClient # noqa: E402
|
||||
|
||||
from database import run_migrations # noqa: E402
|
||||
from main import app # noqa: E402
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client():
|
||||
run_migrations()
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Example API tests — scaffold for future suites (spec §8.4)."""
|
||||
|
||||
|
||||
def test_health(client):
|
||||
resp = client.get("/api/health")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_list_foods_empty(client):
|
||||
resp = client.get("/api/foods")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == []
|
||||
|
||||
|
||||
def test_get_log_empty(client):
|
||||
resp = client.get("/api/log", params={"date": "2026-07-25"})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == []
|
||||
|
||||
|
||||
def test_current_target_none(client):
|
||||
resp = client.get("/api/targets/current")
|
||||
assert resp.status_code == 404
|
||||
@@ -0,0 +1,46 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user