import httpx import pytest from cryptography.fernet import Fernet from app.config import Settings from app.main import create_app from app.models import Base from app.worldquant import WqClient ADMIN_PASSWORD = "test-admin-password-only" @pytest.fixture def settings(tmp_path): return Settings( _env_file=None, database_url=f"sqlite+aiosqlite:///{tmp_path}/test.db", admin_password=ADMIN_PASSWORD, encryption_key=Fernet.generate_key().decode(), enable_runner=False, public_origin="http://testserver", ) @pytest.fixture async def app(settings): def unexpected_request(request): raise AssertionError("Unit tests must never access the real platform") application = create_app(settings, WqClient(settings, transport=httpx.MockTransport(unexpected_request))) async with application.state.engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) async with application.router.lifespan_context(application): yield application @pytest.fixture async def client(app): async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://testserver", headers={"X-WQ-Request": "1"} ) as client: yield client @pytest.fixture async def logged_in(client): response = await client.post("/api/v1/auth/login", json={"username": "admin", "password": ADMIN_PASSWORD}) assert response.status_code == 200 return client def alpha(alpha_id="alpha1", **changes): raw = { "id": alpha_id, "name": "Reversal", "type": "REGULAR", "stage": "IS", "status": "UNSUBMITTED", "hidden": False, "regular": {"code": "-rank(ts_delta(close, 5))"}, "settings": {"region": "USA", "universe": "TOP3000", "language": "FASTEXPR", "delay": 1}, "is": { "sharpe": 1.5, "fitness": 0.8, "turnover": 0.2, "returns": 0.04, "checks": [{"name": "LOW_SHARPE", "result": "FAIL", "value": 1.5, "limit": 1.58}], }, "dateCreated": "2025-01-01T00:00:00Z", } return {**raw, **changes}