529ff967cc
CI / lint-test-build (push) Failing after 1m23s
- Implemented integration tests for the execution writer to ensure trade orders and PnL are persisted correctly. - Created integration tests for the metrics calculator to summarize execution data accurately. - Added integration tests for the opportunity writer to verify event persistence. - Established PostgreSQL schema validation tests to ensure all expected tables, columns, and constraints exist. - Removed outdated unit tests that relied on DuckDB and replaced them with tests using PgStore.
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
from datetime import UTC, datetime
|
|
|
|
import pytest
|
|
|
|
from arbitrade.config.settings import get_settings
|
|
from arbitrade.detection.engine import OpportunityEvent
|
|
from arbitrade.storage.pg_store import PgStore
|
|
from arbitrade.storage.opportunities import AsyncOpportunityWriter
|
|
from arbitrade.storage.repositories import OpportunityRepository
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@asynccontextmanager
|
|
async def _pg() -> AsyncIterator[PgStore]:
|
|
s = get_settings()
|
|
store = PgStore(s)
|
|
try:
|
|
await store.start()
|
|
await store.migrate()
|
|
yield store
|
|
finally:
|
|
await store.stop()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_opportunity_writer_persists_events() -> None:
|
|
async with _pg() as store:
|
|
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()
|
|
|
|
async with store.pool.acquire() as conn:
|
|
rows = await conn.fetch(
|
|
"SELECT cycle, gross_pct, net_pct, est_profit, executed FROM opportunities"
|
|
)
|
|
|
|
assert len(rows) == 1
|
|
assert rows[0]["cycle"] == "USD->BTC->ETH->USD"
|
|
assert rows[0]["gross_pct"] == 4.0
|
|
assert rows[0]["net_pct"] == 3.0
|
|
assert rows[0]["est_profit"] == 0.03
|
|
assert rows[0]["executed"] is False
|