Files
webref/backend/tests/conftest.py
Danilo Reyes 948fe591dc
All checks were successful
CI/CD Pipeline / VM Test - security (push) Successful in 7s
CI/CD Pipeline / Backend Linting (push) Successful in 4s
CI/CD Pipeline / VM Test - backend-integration (push) Successful in 11s
CI/CD Pipeline / VM Test - full-stack (push) Successful in 8s
CI/CD Pipeline / VM Test - performance (push) Successful in 8s
CI/CD Pipeline / Nix Flake Check (push) Successful in 38s
CI/CD Pipeline / CI Summary (push) Successful in 0s
CI/CD Pipeline / Frontend Linting (push) Successful in 17s
phase 13
2025-11-02 14:48:03 -06:00

107 lines
2.3 KiB
Python

"""Pytest configuration and fixtures for all tests."""
from collections.abc import Generator
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.pool import StaticPool
from app.core.deps import get_db
from app.database.base import Base
from app.main import app
# Use in-memory SQLite for tests
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@pytest.fixture(scope="function")
def db() -> Generator[Session, None, None]:
"""
Create a fresh database for each test.
Yields:
Database session
"""
# Create all tables
Base.metadata.create_all(bind=engine)
# Create session
session = TestingSessionLocal()
try:
yield session
finally:
session.close()
# Drop all tables after test
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def client(db: Session) -> Generator[TestClient, None, None]:
"""
Create a test client with database override.
Args:
db: Test database session
Yields:
FastAPI test client
"""
def override_get_db():
try:
yield db
finally:
pass
app.dependency_overrides[get_db] = override_get_db
with TestClient(app) as test_client:
yield test_client
app.dependency_overrides.clear()
@pytest.fixture
def test_user_data() -> dict:
"""
Standard test user data.
Returns:
Dictionary with test user credentials
"""
return {"email": "test@example.com", "password": "TestPassword123"}
@pytest.fixture
def test_user_data_weak_password() -> dict:
"""
Test user data with weak password.
Returns:
Dictionary with weak password
"""
return {"email": "test@example.com", "password": "weak"}
@pytest.fixture
def test_user_data_no_uppercase() -> dict:
"""
Test user data with no uppercase letter.
Returns:
Dictionary with invalid password
"""
return {"email": "test@example.com", "password": "testpassword123"}