73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
"""Pytest configuration for integration tests."""
|
|
|
|
import pytest
|
|
import tempfile
|
|
import os
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from fastapi.testclient import TestClient
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add parent directories to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
|
|
from backend.core.database import get_db
|
|
from backend.core.database_registry import registry
|
|
from backend.main import app
|
|
|
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
|
def test_db():
|
|
"""Create a fresh in-memory test database for each test."""
|
|
# Use in-memory SQLite database (no persistence, no file conflicts)
|
|
db_url = "sqlite:///:memory:"
|
|
|
|
# Create engine and session
|
|
engine = create_engine(
|
|
db_url,
|
|
connect_args={
|
|
"check_same_thread": False,
|
|
}
|
|
)
|
|
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
# Import models to ensure they're registered
|
|
from backend.models import User, RefreshToken, APIKey, EmailVerificationToken, PasswordResetToken, Summary, ExportHistory
|
|
|
|
# Create tables using the registry's create method
|
|
registry.create_all_tables(engine)
|
|
|
|
# Override the get_db dependency BEFORE any tests run
|
|
def override_get_db():
|
|
try:
|
|
db = TestingSessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
# Clear any existing overrides first
|
|
app.dependency_overrides.clear()
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
yield TestingSessionLocal
|
|
|
|
# Cleanup
|
|
app.dependency_overrides.clear()
|
|
engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
"""Create a test client with the test database."""
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture
|
|
def test_user_data():
|
|
"""Standard test user data."""
|
|
return {
|
|
"email": "test@example.com",
|
|
"password": "SecurePassword123!",
|
|
"confirm_password": "SecurePassword123!"
|
|
} |