diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-01 15:56:35 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-01 15:56:35 +0900 |
| commit | 33b14aaa2344b0fd95d1629627c3d135b24ae102 (patch) | |
| tree | 90b214758bc3b076baa7711226a1a1be6268e72e /services/data-collector/tests/test_storage.py | |
| parent | 9360f1a800aa29b40399a2f3bfbfcf215a04e279 (diff) | |
feat: initial trading platform implementation
Binance spot crypto trading platform with microservices architecture:
- shared: Pydantic models, Redis Streams broker, asyncpg DB layer
- data-collector: Binance WebSocket/REST market data collection
- strategy-engine: Plugin-based strategy execution (RSI, Grid)
- order-executor: Order execution with risk management
- portfolio-manager: Position tracking and PnL calculation
- backtester: Historical strategy testing with simulator
- cli: Click-based CLI for all operations
- Docker Compose orchestration with Redis and PostgreSQL
- 24 test files covering all modules
Diffstat (limited to 'services/data-collector/tests/test_storage.py')
| -rw-r--r-- | services/data-collector/tests/test_storage.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/services/data-collector/tests/test_storage.py b/services/data-collector/tests/test_storage.py new file mode 100644 index 0000000..6b27414 --- /dev/null +++ b/services/data-collector/tests/test_storage.py @@ -0,0 +1,62 @@ +"""Tests for storage module.""" +import pytest +from decimal import Decimal +from datetime import datetime, timezone +from unittest.mock import AsyncMock, MagicMock + +from shared.models import Candle +from data_collector.storage import CandleStorage + + +def _make_candle(symbol: str = "BTCUSDT") -> Candle: + return Candle( + symbol=symbol, + timeframe="1m", + open_time=datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc), + open=Decimal("30000"), + high=Decimal("30100"), + low=Decimal("29900"), + close=Decimal("30050"), + volume=Decimal("1.5"), + ) + + +@pytest.mark.asyncio +async def test_storage_saves_to_db_and_publishes(): + """Verify that store() calls insert_candle on db and publish on broker.""" + mock_db = MagicMock() + mock_db.insert_candle = AsyncMock() + mock_broker = MagicMock() + mock_broker.publish = AsyncMock() + + storage = CandleStorage(db=mock_db, broker=mock_broker) + candle = _make_candle() + + await storage.store(candle) + + mock_db.insert_candle.assert_called_once_with(candle) + mock_broker.publish.assert_called_once() + + stream_arg = mock_broker.publish.call_args[0][0] + assert stream_arg == "candles.BTCUSDT" + + data_arg = mock_broker.publish.call_args[0][1] + assert data_arg["type"] == "CANDLE" + assert data_arg["data"]["symbol"] == "BTCUSDT" + + +@pytest.mark.asyncio +async def test_storage_batch_store(): + """Verify that store_batch() calls store for each candle.""" + mock_db = MagicMock() + mock_db.insert_candle = AsyncMock() + mock_broker = MagicMock() + mock_broker.publish = AsyncMock() + + storage = CandleStorage(db=mock_db, broker=mock_broker) + candles = [_make_candle() for _ in range(3)] + + await storage.store_batch(candles) + + assert mock_db.insert_candle.call_count == 3 + assert mock_broker.publish.call_count == 3 |
