Spaces:
Paused

meloon / tests /conftest.py
isold's picture
Feature: Add new applications
4a91ea9
Raw History Blame Contribute Delete
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)
@pytest.fixture(scope="session")
def db_engine():
# Setup
Base.metadata.create_all(bind=engine)
yield engine
# Teardown
# Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def db(db_engine):
connection = db_engine.connect()
transaction = connection.begin()
session = TestingSessionLocal(bind=connection)
yield session
session.close()
transaction.rollback()
connection.close()
@pytest.fixture(scope="module")
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
@pytest.fixture(scope="module")
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}"}