from __future__ import annotations from datetime import UTC, datetime import pytest from arbitrade.config.settings import Settings from arbitrade.detection.engine import OpportunityEvent from arbitrade.storage.db import DuckDBStore from arbitrade.storage.opportunities import AsyncOpportunityWriter from arbitrade.storage.repositories import OpportunityRepository @pytest.mark.asyncio async def test_async_opportunity_writer_persists_events(tmp_path) -> None: settings = Settings(_env_file=None, DUCKDB_PATH=tmp_path / "test.duckdb") store = DuckDBStore(settings) store.migrate() repository = OpportunityRepository(store) writer = AsyncOpportunityWriter(repository, max_queue_size=10) await writer.start() event = OpportunityEvent( detected_at=datetime.now(UTC), cycle="USD->BTC->ETH->USD", updated_pair="BTC/USD", gross_rate=1.04, net_rate=1.03, gross_pct=4.0, net_pct=3.0, est_profit=0.03, ) await writer.enqueue(event) await writer.stop() with store.connect() as conn: rows = conn.execute( "SELECT cycle, gross_pct, net_pct, est_profit, executed FROM opportunities" ).fetchall() assert len(rows) == 1 assert rows[0][0] == "USD->BTC->ETH->USD" assert rows[0][1] == 4.0 assert rows[0][2] == 3.0 assert rows[0][3] == 0.03 assert rows[0][4] is False