Download tests/conftest.py from isold/meloon: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/isold/meloon/resolve/main/tests/conftest.py
- Command line
-
hf download hf://spaces/isold/meloon/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/isold/meloon/resolve/main/tests/conftest.py
1.96 kB
| import pytest | |
| import warnings | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from fastapi.testclient import TestClient | |
| from app.main import app | |
| from app.core.database import Base, get_db | |
| from app.core.config import settings | |
| # Supress Pydantic user warnings | |
| warnings.filterwarnings("ignore", category=UserWarning, module="pydantic") | |
| # Use a separate test database or the same one for simplicity ensuring cleanup | |
| # ideally you'd use a test DB. For now, we will assume the environment provided is dev/test. | |
| # WARNING: This truncates tables! | |
| SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL | |
| engine = create_engine(SQLALCHEMY_DATABASE_URL) | |
| TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| def db_engine(): | |
| # Setup | |
| Base.metadata.create_all(bind=engine) | |
| yield engine | |
| # Teardown | |
| # Base.metadata.drop_all(bind=engine) | |
| def db(db_engine): | |
| connection = db_engine.connect() | |
| transaction = connection.begin() | |
| session = TestingSessionLocal(bind=connection) | |
| yield session | |
| session.close() | |
| transaction.rollback() | |
| connection.close() | |
| def client(): | |
| # Override dependency | |
| def override_get_db(): | |
| try: | |
| db = TestingSessionLocal() | |
| yield db | |
| finally: | |
| db.close() | |
| app.dependency_overrides[get_db] = override_get_db | |
| with TestClient(app) as c: | |
| yield c | |
| def normal_user_token_headers(client): | |
| import time | |
| email = f"test{int(time.time())}@example.com" | |
| password = "password" | |
| client.post("/api/v1/auth/register", json={"email": email, "password": password}) | |
| response = client.post("/api/v1/auth/login", json={"email": email, "password": password}) | |
| token = response.json()["token"] | |
| return {"Authorization": f"Bearer {token}"} | |