updated tests
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
"""Root conftest for all tests."""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import pytest
|
||||
|
||||
|
||||
def pytest_configure(config: pytest.Config) -> None:
|
||||
"""Configure pytest environment before any imports happen."""
|
||||
# Set DATA_DIR before any modules are imported
|
||||
if "DATA_DIR" not in os.environ:
|
||||
tmpdir = tempfile.mkdtemp(prefix="baby_monitor_test_")
|
||||
os.environ["DATA_DIR"] = tmpdir
|
||||
os.environ["ENVIRONMENT"] = "development"
|
||||
@@ -1,28 +1,14 @@
|
||||
"""Integration tests configuration."""
|
||||
|
||||
import os
|
||||
from typing import Iterator
|
||||
from collections.abc import Iterator
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def set_test_environment() -> Iterator[None]:
|
||||
"""Set environment to development for all integration tests."""
|
||||
original_env = os.environ.get("ENVIRONMENT")
|
||||
os.environ["ENVIRONMENT"] = "development"
|
||||
yield
|
||||
# Cleanup: restore original environment
|
||||
if original_env is None:
|
||||
os.environ.pop("ENVIRONMENT", None)
|
||||
else:
|
||||
os.environ["ENVIRONMENT"] = original_env
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def app_test_client() -> Iterator[TestClient]:
|
||||
"""Provides a TestClient for the FastAPI app."""
|
||||
# Import here after environment is set
|
||||
# Import after environment is set by root conftest
|
||||
from baby_monitor.main import app
|
||||
|
||||
with TestClient(app) as client:
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
"""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)
|
||||
Reference in New Issue
Block a user