Files
worldquant-alpha-system/backend/tests/test_correlation_recheck.py
T
yuxuanhui 2080f812b4
Deploy production / deploy (push) Successful in 54s
feat: add local correlation filters and full recheck action
2026-09-13 00:03:17 +08:00

51 lines
2.6 KiB
Python

"""Full rechecks freeze all passing targets and overwrite existing correlation results."""
import pytest
from app.alphas import upsert_alpha
from app.models import Alpha, SelfCorrelation
from tests.conftest import alpha
from tests.test_jobs import result
async def test_recheck_all_passed_targets_deduplicates_and_repeats(app, logged_in):
runner = app.state.runner
async with runner.sessions.begin() as db:
for i in range(105):
row = await upsert_alpha(db, alpha(f"pass{i:03}"))
row.check_type = "PRE_CHECK" if i % 2 else "PASS"
for name, status in [("failed", "FAIL_1"), ("failed2", "FAIL_2"), ("pending", "PENDING")]:
row = await upsert_alpha(db, alpha(name))
row.check_type = status
db.add(SelfCorrelation(alpha_id="pass000", region="USA", stale=False,
result={"status": "high", "max_correlation": 0.9}))
response = await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})
assert response.status_code == 202
job = response.json()
assert job["total"] == 105
assert job["payload"]["alpha_ids"] == [f"pass{i:03}" for i in range(105)]
duplicate = (await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})).json()
assert duplicate["id"] == job["id"]
await runner.execute(job["id"])
completed = await result(runner, job["id"])
assert completed.status == "completed" and completed.processed == 105
async with runner.sessions() as db:
for i in range(105):
cached = await db.get(SelfCorrelation, f"pass{i:03}")
assert cached.stale is False and cached.result["max_correlation"] == 0
assert await db.get(SelfCorrelation, "failed") is None
assert (await db.get(Alpha, "pass000")).check_type == "PASS"
again = (await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})).json()
assert again["id"] != job["id"] and again["total"] == 105
async def test_empty_recheck_completes_without_platform_connection(logged_in):
response = await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})
assert response.status_code == 202
assert response.json()["total"] == 0 and response.json()["status"] == "completed"
@pytest.mark.parametrize("extra", [{"alpha_ids": ["target"]}, {"submission": "UNSUBMITTED"}, {"date_from": "2026-01-01"}])
async def test_recheck_rejects_partial_scope(logged_in, extra):
assert (await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck", **extra})).status_code == 422