Spaces:
Configuration error
Configuration error
Download platform/api/tests/conftest.py from Sakshamyadav15/SupportRAG: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/spaces/Sakshamyadav15/SupportRAG/resolve/main/platform/api/tests/conftest.py
- Command line
-
hf download hf://spaces/Sakshamyadav15/SupportRAG/platform/api/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/Sakshamyadav15/SupportRAG/resolve/main/platform/api/tests/conftest.py
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() | |
| 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) | |
| 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() | |
| def test_user_data(): | |
| """Sample user data for testing.""" | |
| return { | |
| "email": "test@example.com", | |
| "password": "testpassword123" | |
| } | |
| 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() | |
| 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"] | |
| def auth_headers(auth_token): | |
| """Create authorization headers.""" | |
| return {"Authorization": f"Bearer {auth_token}"} | |