SupportRAG / platform /api /tests /conftest.py
Sakshamyadav15's picture
feat: integrate System Platform as platform/ — async job worker for RAG
5292340
Raw History Blame Contribute Delete
2.22 kB
"""
Test Configuration and Fixtures
Provides pytest fixtures for testing the application.
"""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from app.main import app
from app.models.base import Base
from app.core.database import get_db
# Use in-memory SQLite for testing
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)
def override_get_db():
"""Override database dependency for testing."""
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
@pytest.fixture(scope="function")
def db_session():
"""Create a fresh database for each test."""
Base.metadata.create_all(bind=engine)
db = TestingSessionLocal()
try:
yield db
finally:
db.close()
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def client(db_session):
"""Create a test client with database override."""
app.dependency_overrides[get_db] = override_get_db
Base.metadata.create_all(bind=engine)
with TestClient(app) as test_client:
yield test_client
Base.metadata.drop_all(bind=engine)
app.dependency_overrides.clear()
@pytest.fixture
def test_user_data():
"""Sample user data for testing."""
return {
"email": "test@example.com",
"password": "testpassword123"
}
@pytest.fixture
def registered_user(client, test_user_data):
"""Create and return a registered user."""
response = client.post("/auth/register", json=test_user_data)
return response.json()
@pytest.fixture
def auth_token(client, test_user_data, registered_user):
"""Get auth token for a registered user."""
response = client.post("/auth/login", json=test_user_data)
return response.json()["access_token"]
@pytest.fixture
def auth_headers(auth_token):
"""Create authorization headers."""
return {"Authorization": f"Bearer {auth_token}"}