Files
Brian Bjarke Jensen d953564986
Build and Push Docker Image / build-and-push (push) Failing after 15s
Python Code Quality / python-code-quality (push) Successful in 9s
Python Test / python-test (push) Successful in 14s
formatting fixes
2025-11-06 22:08:07 +01:00

140 lines
3.8 KiB
Python

"""Unit tests for database module."""
from pathlib import Path
from sqlalchemy.orm import Session
from baby_monitor.repositories.dependencies.get_database import get_database, init_db
def test_get_db_yields_session() -> None:
"""Test that get_db yields a valid database session."""
init_db()
db_generator = get_database()
db = next(db_generator)
assert isinstance(db, Session)
assert db.is_active
# Close the session
try:
next(db_generator)
except StopIteration:
pass
def test_get_db_closes_session() -> None:
"""Test that get_db properly closes the session."""
from unittest.mock import patch
init_db()
db_generator = get_database()
db = next(db_generator)
assert db.is_active
# Patch the close method to verify it gets called
with patch.object(db, "close", wraps=db.close) as mock_close:
# Complete the generator to trigger finally block
try:
next(db_generator)
except StopIteration:
pass
# Verify that close() was called
mock_close.assert_called_once()
def test_get_db_closes_on_exception() -> None:
"""Test that get_db closes session when used in context manager."""
from unittest.mock import patch
init_db()
# Simulate how FastAPI actually uses the dependency
db = None
db_generator = get_database()
try:
db = next(db_generator)
assert db.is_active
# Patch close to verify it gets called even with an exception
with patch.object(db, "close", wraps=db.close) as mock_close:
try:
# Simulate an exception occurring during request processing
raise ValueError("Simulated error during request")
except ValueError:
pass
finally:
# FastAPI ensures the generator completes
try:
next(db_generator)
except StopIteration:
pass
# Verify close() was called despite the exception
mock_close.assert_called_once()
except Exception:
# Ensure we have a reference to db for the final assertion
pass
# Verify db was created
assert db is not None
# def test_init_db_creates_directory(tmp_path: Path) -> None:
# """Test that init_db creates the data directory."""
# import os
# # Use a fresh temp directory for this test
# test_dir = tmp_path / "test_data"
# # Reinitialize database module with new path
# from baby_monitor.repositories.dependencies.get_database import (
# DATA_DIR,
# DATABASE_URL,
# create_engine,
# sessionmaker,
# )
# DATABASE_URL = f"sqlite:///{test_dir}/baby_monitor.db"
# engine = create_engine(
# DATABASE_URL,
# connect_args={"check_same_thread": False},
# echo=False,
# )
# SessionLocal = sessionmaker(
# autocommit=False,
# autoflush=False,
# bind=engine
# )
# init_db()
# assert isinstance(SessionLocal, sessionmaker)
# assert test_dir.exists()
# assert test_dir.is_dir()
def test_init_db_creates_tables(tmp_path: Path) -> None:
"""Test that init_db creates database tables."""
init_db()
# Check that database file exists
import os
data_dir = Path(os.environ["DATA_DIR"])
db_path = data_dir / "baby_monitor.db"
assert db_path.exists()
# Verify tables were created by checking metadata
from baby_monitor.repositories.dependencies.get_database import engine
from sqlalchemy import inspect
inspector = inspect(engine)
tables = inspector.get_table_names()
# Should have at least some tables (depends on models)
# For now, just verify the function runs without error
assert isinstance(tables, list)