Spaces:
Running
Running
File size: 1,961 Bytes
1e68437 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import asyncio
import sys
from pathlib import Path
from datetime import datetime, timezone
# Add project root to path
sys.path.append(str(Path(__file__).resolve().parent.parent))
async def test_db_operations():
from database.db import init_db, AsyncSessionLocal, engine
from database.models import Alpha
from sqlalchemy import select
# Initialize tables
print("Initializing database...")
await init_db()
# Open session
print("Opening async session...")
async with AsyncSessionLocal() as session:
# Create a new Alpha record
new_alpha = Alpha(
expression="rank(close - open)",
language="FASTEXPR",
region="USA",
universe="TOP3000",
delay=1,
decay=4,
truncation=0.08,
neutralization="SUBINDUSTRY",
pasteurization="On",
status="PENDING_GEN",
retry_count=0
)
session.add(new_alpha)
await session.commit()
print(f"Committed new Alpha with expression: {new_alpha.expression}")
# Query the Alpha
result = await session.execute(select(Alpha).where(Alpha.expression == "rank(close - open)"))
alpha_record = result.scalars().first()
assert alpha_record is not None
print(f"Queried Alpha back: id={alpha_record.id}, region={alpha_record.region}, status={alpha_record.status}")
# Update Alpha status
alpha_record.status = "SIMULATING"
await session.commit()
print(f"Updated Alpha status to SIMULATING. New status: {alpha_record.status}")
# Clean up by deleting the alpha
await session.delete(alpha_record)
await session.commit()
print("Cleaned up and deleted test Alpha record.")
await engine.dispose()
print("Database connection pool disposed.")
if __name__ == "__main__":
asyncio.run(test_db_operations())
|