test: upgrade backend test suite, implement unit tests, and modernize CRUD patterns
Build Backend / build (push) Successful in 6m39s

This commit is contained in:
2026-06-08 16:17:11 +02:00
parent 4d53887fcc
commit a9e1e96622
13 changed files with 759 additions and 25 deletions
+29
View File
@@ -0,0 +1,29 @@
import pytest
from unittest.mock import patch
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from sqlmodel import SQLModel
from app.database import init_db
@pytest.mark.asyncio
async def test_init_db_creates_tables():
"""Verifies that init_db() successfully creates all necessary tables.
This test uses a patched in-memory SQLite engine to ensure isolation
and verify the database initialization logic.
"""
# Use an in-memory SQLite engine for testing to avoid any impact on real data
test_engine = create_async_engine("sqlite+aiosqlite:///:memory:")
# Patch the engine in app.database so init_db uses our test engine
with patch("app.database.engine", test_engine):
await init_db()
# Check if the expected table "sleep_session" exists in the schema
async with test_engine.connect() as conn:
result = await conn.execute(text("SELECT name FROM sqlite_master WHERE type='table' AND name='sleep_session';"))
row = result.fetchone()
assert row is not None, "Table 'sleep_session' was not created by init_db()"
assert row[0] == "sleep_session"
await test_engine.dispose()