IDKHowToCodeFr/tinyml-backend
1
1import pytest2import pytest_asyncio3import os4import sys5import aiosqlite6from collections import namedtuple7 8sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../backend')))9import database10 11# Mock settings to avoid real huggingface calls12database.settings.hf_token = None13database.DB_PATH = "test_patient_history.db"14 15@pytest_asyncio.fixture(autouse=True)16async def setup_teardown():17 if os.path.exists(database.DB_PATH):18 os.remove(database.DB_PATH)19 await database.init_db()20 yield21 if os.path.exists(database.DB_PATH):22 try:23 os.remove(database.DB_PATH)24 except PermissionError:25 pass # Windows file lock fallback26 27@pytest.mark.asyncio28async def test_init_db():29 assert os.path.exists(database.DB_PATH)30 async with aiosqlite.connect(database.DB_PATH) as db:31 cursor = await db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='predictions'")32 table = await cursor.fetchone()33 assert table is not None34 35@pytest.mark.asyncio36async def test_log_and_get_history():37 Data = namedtuple('Data', ['Heart_Rate', 'SpO2_Level', 'Systolic_BP', 'Diastolic_BP', 'Body_Temp'])38 data = Data(80.0, 98.0, 120.0, 80.0, 37.0)39 40 await database.log_prediction(data, "Normal", 0.95)41 42 history = await database.get_history()43 assert len(history) == 144 assert history[0]['heart_rate'] == 80.045 assert history[0]['prediction_label'] == "Normal"46 assert history[0]['confidence'] == 0.9547 48@pytest.mark.asyncio49async def test_empty_history():50 if os.path.exists(database.DB_PATH):51 os.remove(database.DB_PATH)52 history = await database.get_history()53 assert history == []54 