|
| 1 | +"""Tests for supplement and medication SQLite logging. |
| 2 | +
|
| 3 | +Covers: SQLite table creation, dual-write from MCP tools, query via SQL. |
| 4 | +""" |
| 5 | + |
| 6 | +import uuid |
| 7 | +from datetime import datetime, timezone |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from engine.gateway.db import get_db, init_db |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def db(tmp_path, monkeypatch): |
| 16 | + """Fresh SQLite database with schema applied.""" |
| 17 | + monkeypatch.setattr("mcp_server.tools.PROJECT_ROOT", tmp_path) |
| 18 | + (tmp_path / "data").mkdir(exist_ok=True) |
| 19 | + (tmp_path / "data" / "users" / "andrew").mkdir(parents=True, exist_ok=True) |
| 20 | + db_path = tmp_path / "data" / "kasane.db" |
| 21 | + init_db(str(db_path)) |
| 22 | + conn = get_db(str(db_path)) |
| 23 | + |
| 24 | + # Insert a person for testing |
| 25 | + now = datetime.now(timezone.utc).isoformat() |
| 26 | + conn.execute( |
| 27 | + "INSERT INTO person (id, name, health_engine_user_id, created_at, updated_at) " |
| 28 | + "VALUES (?, ?, ?, ?, ?)", |
| 29 | + ("p1", "Andrew", "andrew", now, now), |
| 30 | + ) |
| 31 | + conn.commit() |
| 32 | + return conn |
| 33 | + |
| 34 | + |
| 35 | +class TestSupplementTable: |
| 36 | + def test_supplement_log_table_exists(self, db): |
| 37 | + """Schema should create supplement_log table.""" |
| 38 | + row = db.execute( |
| 39 | + "SELECT name FROM sqlite_master WHERE type='table' AND name='supplement_log'" |
| 40 | + ).fetchone() |
| 41 | + assert row is not None, "supplement_log table missing from schema" |
| 42 | + |
| 43 | + def test_insert_supplement(self, db): |
| 44 | + """Can insert a supplement log entry.""" |
| 45 | + now = datetime.now(timezone.utc).isoformat() |
| 46 | + rid = str(uuid.uuid4()) |
| 47 | + db.execute( |
| 48 | + "INSERT INTO supplement_log (id, person_id, date, name, dose, stack, source, created_at, updated_at) " |
| 49 | + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 50 | + (rid, "p1", "2026-04-04", "vitamin_d", "5000 IU", "morning", "mcp", now, now), |
| 51 | + ) |
| 52 | + db.commit() |
| 53 | + |
| 54 | + row = db.execute("SELECT * FROM supplement_log WHERE id = ?", (rid,)).fetchone() |
| 55 | + assert row is not None |
| 56 | + assert row["name"] == "vitamin_d" |
| 57 | + assert row["dose"] == "5000 IU" |
| 58 | + assert row["stack"] == "morning" |
| 59 | + |
| 60 | + def test_query_supplements_by_date(self, db): |
| 61 | + """Can query supplements for a specific date.""" |
| 62 | + now = datetime.now(timezone.utc).isoformat() |
| 63 | + for name, dose in [("vitamin_d", "5000 IU"), ("fish_oil", "2 capsules")]: |
| 64 | + db.execute( |
| 65 | + "INSERT INTO supplement_log (id, person_id, date, name, dose, stack, source, created_at, updated_at) " |
| 66 | + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 67 | + (str(uuid.uuid4()), "p1", "2026-04-04", name, dose, "morning", "mcp", now, now), |
| 68 | + ) |
| 69 | + db.commit() |
| 70 | + |
| 71 | + rows = db.execute( |
| 72 | + "SELECT name, dose FROM supplement_log WHERE person_id = ? AND date = ?", |
| 73 | + ("p1", "2026-04-04"), |
| 74 | + ).fetchall() |
| 75 | + assert len(rows) == 2 |
| 76 | + names = {r["name"] for r in rows} |
| 77 | + assert names == {"vitamin_d", "fish_oil"} |
| 78 | + |
| 79 | + |
| 80 | +class TestMedicationTable: |
| 81 | + def test_medication_log_table_exists(self, db): |
| 82 | + """Schema should create medication_log table.""" |
| 83 | + row = db.execute( |
| 84 | + "SELECT name FROM sqlite_master WHERE type='table' AND name='medication_log'" |
| 85 | + ).fetchone() |
| 86 | + assert row is not None, "medication_log table missing from schema" |
| 87 | + |
| 88 | + def test_insert_medication(self, db): |
| 89 | + """Can insert a medication log entry.""" |
| 90 | + now = datetime.now(timezone.utc).isoformat() |
| 91 | + rid = str(uuid.uuid4()) |
| 92 | + db.execute( |
| 93 | + "INSERT INTO medication_log (id, person_id, date, name, dose, route, notes, source, created_at, updated_at) " |
| 94 | + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 95 | + (rid, "p1", "2026-04-04", "tirzepatide", "2.5mg", "subcutaneous", "injection site: abdomen", "mcp", now, now), |
| 96 | + ) |
| 97 | + db.commit() |
| 98 | + |
| 99 | + row = db.execute("SELECT * FROM medication_log WHERE id = ?", (rid,)).fetchone() |
| 100 | + assert row is not None |
| 101 | + assert row["name"] == "tirzepatide" |
| 102 | + assert row["dose"] == "2.5mg" |
| 103 | + assert row["route"] == "subcutaneous" |
| 104 | + |
| 105 | + def test_query_medications_by_person(self, db): |
| 106 | + """Can query medication history for a person.""" |
| 107 | + now = datetime.now(timezone.utc).isoformat() |
| 108 | + for date in ["2026-04-01", "2026-04-02", "2026-04-03"]: |
| 109 | + db.execute( |
| 110 | + "INSERT INTO medication_log (id, person_id, date, name, dose, route, notes, source, created_at, updated_at) " |
| 111 | + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 112 | + (str(uuid.uuid4()), "p1", date, "tirzepatide", "2.5mg", "subcutaneous", "", "mcp", now, now), |
| 113 | + ) |
| 114 | + db.commit() |
| 115 | + |
| 116 | + rows = db.execute( |
| 117 | + "SELECT date FROM medication_log WHERE person_id = ? ORDER BY date", |
| 118 | + ("p1",), |
| 119 | + ).fetchall() |
| 120 | + assert len(rows) == 3 |
| 121 | + |
| 122 | + |
| 123 | +class TestToolDualWrite: |
| 124 | + """Verify the MCP tools write to both CSV and SQLite.""" |
| 125 | + |
| 126 | + def test_log_supplements_writes_sqlite(self, db, tmp_path, monkeypatch): |
| 127 | + """log_supplements should write to SQLite when person exists.""" |
| 128 | + monkeypatch.setattr("mcp_server.tools.PROJECT_ROOT", tmp_path) |
| 129 | + |
| 130 | + from mcp_server.tools import _log_supplements |
| 131 | + result = _log_supplements(stack="morning", user_id="andrew") |
| 132 | + assert result["logged"] is True |
| 133 | + |
| 134 | + rows = db.execute( |
| 135 | + "SELECT name, dose, stack FROM supplement_log WHERE person_id = 'p1'" |
| 136 | + ).fetchall() |
| 137 | + assert len(rows) > 0 |
| 138 | + names = {r["name"] for r in rows} |
| 139 | + assert "vitamin_d" in names |
| 140 | + |
| 141 | + def test_log_medication_writes_sqlite(self, db, tmp_path, monkeypatch): |
| 142 | + """log_medication should write to SQLite when person exists.""" |
| 143 | + monkeypatch.setattr("mcp_server.tools.PROJECT_ROOT", tmp_path) |
| 144 | + |
| 145 | + from mcp_server.tools import _log_medication |
| 146 | + result = _log_medication(name="tirzepatide", dose="2.5mg", route="subcutaneous", user_id="andrew") |
| 147 | + assert result["logged"] is True |
| 148 | + |
| 149 | + row = db.execute( |
| 150 | + "SELECT name, dose, route FROM medication_log WHERE person_id = 'p1'" |
| 151 | + ).fetchone() |
| 152 | + assert row is not None |
| 153 | + assert row["name"] == "tirzepatide" |
| 154 | + assert row["dose"] == "2.5mg" |
| 155 | + assert row["route"] == "subcutaneous" |
| 156 | + |
| 157 | + def test_log_supplements_no_person_still_works(self, db, tmp_path, monkeypatch): |
| 158 | + """log_supplements should still work (CSV only) when no person record exists.""" |
| 159 | + monkeypatch.setattr("mcp_server.tools.PROJECT_ROOT", tmp_path) |
| 160 | + (tmp_path / "data" / "users" / "unknown").mkdir(parents=True, exist_ok=True) |
| 161 | + |
| 162 | + from mcp_server.tools import _log_supplements |
| 163 | + result = _log_supplements(stack="morning", user_id="unknown") |
| 164 | + assert result["logged"] is True |
| 165 | + |
| 166 | + # No SQLite row since person doesn't exist |
| 167 | + rows = db.execute("SELECT * FROM supplement_log").fetchall() |
| 168 | + assert len(rows) == 0 |
0 commit comments