nutrition-mcp/tests/common/mod.rs
Anthony Merlo ebb44fd712 test: add 25-test suite + fix sum_nutriments display keys
- 25 integration tests: DB init, default goals, FTS5 cache, upsert,
  portion scaling (100g/50g/200g/empty/non-numeric), sum_nutriments
  (single/multiple/empty/partial/no-data), meal detection, date format,
  log entry + summary round trip, custom food + sum, delete entry,
  goals get/update, weight log + history + upsert
- Fix: sum_nutriments now uses TRACKED_NUTRIENTS display names (first
  tuple element) instead of deriving from OFF field names — fixes
  proteins→protein, energy-kcal→energy_kcal mismatches
- Fix: weight_log.date is now UNIQUE for proper upsert behavior
- Fix: build_daily_summary remaining keys use display names
- Added lib.rs for integration test access
- Added regex dev-dependency

Closes nutrition-mcp-45t
2026-08-20 17:25:41 +01:00

37 lines
No EOL
997 B
Rust

// Common test utilities.
use std::path::PathBuf;
use nutrition_mcp::db::{self, DbPool};
/// A temporary SQLite database for testing.
/// Creates a unique DB file in /tmp that is cleaned up on drop.
pub struct TestDb {
pub pool: DbPool,
_path: PathBuf,
}
impl TestDb {
pub async fn new() -> Self {
let path = std::env::temp_dir().join(format!(
"nutrition-test-{}-{}.db",
std::process::id(),
uuid::Uuid::new_v4()
));
let pool = db::init_database(&path).await.expect("failed to init test DB");
Self {
pool,
_path: path,
}
}
}
impl Drop for TestDb {
fn drop(&mut self) {
// Best-effort cleanup — ignore errors
let _ = std::fs::remove_file(&self._path);
let wal = self._path.with_extension("db-wal");
let shm = self._path.with_extension("db-shm");
let _ = std::fs::remove_file(&wal);
let _ = std::fs::remove_file(&shm);
}
}