import asyncio import logging import os import sys from pathlib import Path # Add project root to path so we can import modules sys.path.append(str(Path(__file__).resolve().parent.parent)) import config from database import init_db, AsyncSessionLocal, Alpha from core.wq_client import AsyncBrainClient from sqlalchemy.future import select logger = logging.getLogger("TestPhase1") async def test_database(): logger.info("--- Testing Database Setup ---") logger.info("Initializing database...") await init_db() logger.info("Creating a session and inserting a test alpha record...") async with AsyncSessionLocal() as session: # Check if record exists stmt = select(Alpha).where(Alpha.expression == "close/open") result = await session.execute(stmt) record = result.scalars().first() if not record: record = Alpha( expression="close/open", language="FASTEXPR", region="USA", universe="TOP3000", delay=1, decay=10, truncation=0.01, neutralization="SUBINDUSTRY", status="PENDING_GEN" ) session.add(record) await session.commit() logger.info("Inserted test alpha record successfully.") else: logger.info(f"Found existing alpha record: {record}") # Retrieve all records to verify queries stmt_all = select(Alpha) result_all = await session.execute(stmt_all) all_records = result_all.scalars().all() logger.info(f"Total alpha records in database: {len(all_records)}") for r in all_records: logger.info(f" ID: {r.id} | Expression: {r.expression} | Status: {r.status} | Created: {r.created_at}") async def test_config(): logger.info("--- Testing Configuration ---") logger.info(f"Project base directory: {config.BASE_DIR}") logger.info(f"Data directory: {config.DATA_DIR}") logger.info(f"Logs directory: {config.LOGS_DIR}") # Check regions in config logger.info(f"Supported regions: {list(config.VALID_CONFIGS.keys())}") for region, details in config.VALID_CONFIGS.items(): logger.info(f" {region}: delays={details['delays']}, universes={details['universes']}, neutralizations={details['neutralizations'][:2]}...") async def test_client(): logger.info("--- Testing WorldQuant Async Client ---") logger.info("Instantiating AsyncBrainClient...") # Set dummy credentials if environment is not set username = config.WQ_USERNAME or "dummy_user" password = config.WQ_PASSWORD or "dummy_pass" async with AsyncBrainClient(username=username, password=password) as client: logger.info("Client successfully initialized.") logger.info(f"Credentials loaded: username={client.username}, password={'***' if client.password else 'None'}") # Test authenticating with dummy or actual credentials if client.username != "dummy_user" and client.password != "dummy_pass": logger.info("Attempting authentication with loaded credentials...") auth_success = await client.authenticate() logger.info(f"Authentication result: {auth_success}") if auth_success: logger.info("Attempting to scrape first page of data fields for USA region...") # Fetch a single page to verify pagination and local caching works fields = await client.fetch_and_cache_data_fields( region="USA", delay=1, universe="TOP3000", limit=10, offset=0 ) logger.info(f"Scraped categories: {list(fields.keys())}") else: logger.warning("Using dummy credentials; skipping actual network authentication and scraping tests.") async def main(): logger.info("Starting Phase 1 Verification Tests") await test_config() await test_database() await test_client() logger.info("Verification Tests Complete.") if __name__ == "__main__": asyncio.run(main())