phase 3.2 & 4.1
This commit is contained in:
107
backend/tests/conftest.py
Normal file
107
backend/tests/conftest.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""Pytest configuration and fixtures for all tests."""
|
||||
|
||||
import os
|
||||
from typing 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"}
|
||||
|
||||
Reference in New Issue
Block a user