|
| 1 | +"""Simplified tests for OrderBook public API only.""" |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from unittest.mock import AsyncMock, MagicMock |
| 5 | + |
| 6 | +import polars as pl |
| 7 | +import pytest |
| 8 | +import pytz |
| 9 | + |
| 10 | +from project_x_py.orderbook import OrderBook |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +async def orderbook(): |
| 15 | + """Create an OrderBook instance for testing.""" |
| 16 | + mock_client = MagicMock() |
| 17 | + mock_event_bus = MagicMock() |
| 18 | + mock_event_bus.emit = AsyncMock() |
| 19 | + mock_event_bus.subscribe = AsyncMock() |
| 20 | + |
| 21 | + ob = OrderBook( |
| 22 | + instrument="MNQ", |
| 23 | + event_bus=mock_event_bus, |
| 24 | + project_x=mock_client, |
| 25 | + ) |
| 26 | + |
| 27 | + # Mock realtime-related attributes for testing |
| 28 | + ob.realtime_client = MagicMock() |
| 29 | + ob.realtime_client.is_connected = MagicMock(return_value=True) |
| 30 | + ob.realtime_client.add_callback = AsyncMock() |
| 31 | + ob.realtime_client.subscribe_market_data = AsyncMock() |
| 32 | + ob.is_streaming = False |
| 33 | + |
| 34 | + return ob |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +class TestOrderBookPublicAPI: |
| 39 | + """Test OrderBook public API methods.""" |
| 40 | + |
| 41 | + async def test_initialize_realtime(self, orderbook): |
| 42 | + """Test initializing real-time orderbook feed.""" |
| 43 | + ob = orderbook |
| 44 | + |
| 45 | + # Mock realtime client |
| 46 | + mock_realtime = MagicMock() |
| 47 | + mock_realtime.is_connected = MagicMock(return_value=True) |
| 48 | + mock_realtime.add_callback = AsyncMock() |
| 49 | + mock_realtime.subscribe_market_data = AsyncMock() |
| 50 | + |
| 51 | + result = await ob.initialize( |
| 52 | + realtime_client=mock_realtime, |
| 53 | + subscribe_to_depth=True, |
| 54 | + subscribe_to_quotes=True, |
| 55 | + ) |
| 56 | + |
| 57 | + assert result is True |
| 58 | + |
| 59 | + # Should register callbacks |
| 60 | + mock_realtime.add_callback.assert_called() |
| 61 | + |
| 62 | + async def test_cleanup(self, orderbook): |
| 63 | + """Test cleaning up orderbook resources.""" |
| 64 | + ob = orderbook |
| 65 | + |
| 66 | + # Call cleanup |
| 67 | + await ob.cleanup() |
| 68 | + |
| 69 | + # Memory manager should have cleaned up |
| 70 | + assert ob.memory_manager is not None |
| 71 | + |
| 72 | + async def test_get_market_imbalance(self, orderbook): |
| 73 | + """Test getting market imbalance.""" |
| 74 | + ob = orderbook |
| 75 | + |
| 76 | + # Get imbalance with empty orderbook |
| 77 | + result = await ob.get_market_imbalance(levels=3) |
| 78 | + |
| 79 | + # Should return imbalance metrics |
| 80 | + assert result is not None |
| 81 | + assert "depth_imbalance" in result |
| 82 | + assert "bid_liquidity" in result |
| 83 | + assert "ask_liquidity" in result |
| 84 | + |
| 85 | + async def test_get_statistics(self, orderbook): |
| 86 | + """Test getting orderbook statistics.""" |
| 87 | + ob = orderbook |
| 88 | + |
| 89 | + # Get statistics |
| 90 | + stats = await ob.get_statistics() |
| 91 | + |
| 92 | + # Should return stats dict |
| 93 | + assert stats is not None |
| 94 | + assert isinstance(stats, dict) |
| 95 | + |
| 96 | + async def test_get_spread_analysis(self, orderbook): |
| 97 | + """Test spread analysis.""" |
| 98 | + ob = orderbook |
| 99 | + |
| 100 | + # Get spread analysis |
| 101 | + analysis = await ob.get_spread_analysis() |
| 102 | + |
| 103 | + # Should return spread metrics |
| 104 | + assert analysis is not None |
| 105 | + assert "avg_spread" in analysis |
| 106 | + assert "spread_volatility" in analysis |
| 107 | + |
| 108 | + async def test_get_orderbook_depth(self, orderbook): |
| 109 | + """Test getting orderbook depth.""" |
| 110 | + ob = orderbook |
| 111 | + |
| 112 | + # Get depth |
| 113 | + depth = await ob.get_orderbook_depth(price_range=1.0) |
| 114 | + |
| 115 | + assert depth is not None |
| 116 | + assert "estimated_fill_price" in depth |
| 117 | + |
| 118 | + async def test_get_cumulative_delta(self, orderbook): |
| 119 | + """Test getting cumulative delta.""" |
| 120 | + ob = orderbook |
| 121 | + |
| 122 | + # Get cumulative delta |
| 123 | + delta = await ob.get_cumulative_delta() |
| 124 | + |
| 125 | + assert delta is not None |
| 126 | + assert "cumulative_delta" in delta |
| 127 | + assert "buy_volume" in delta |
| 128 | + assert "sell_volume" in delta |
| 129 | + |
| 130 | + async def test_get_volume_profile(self, orderbook): |
| 131 | + """Test getting volume profile.""" |
| 132 | + ob = orderbook |
| 133 | + |
| 134 | + # Get volume profile |
| 135 | + profile = await ob.get_volume_profile() |
| 136 | + |
| 137 | + assert profile is not None |
| 138 | + assert "poc" in profile |
| 139 | + assert "value_area_high" in profile |
| 140 | + assert "value_area_low" in profile |
| 141 | + |
| 142 | + async def test_get_trade_flow_summary(self, orderbook): |
| 143 | + """Test getting trade flow summary.""" |
| 144 | + ob = orderbook |
| 145 | + |
| 146 | + # Get trade flow summary |
| 147 | + summary = await ob.get_trade_flow_summary() |
| 148 | + |
| 149 | + assert summary is not None |
| 150 | + assert "aggressive_buy_volume" in summary |
| 151 | + assert "aggressive_sell_volume" in summary |
| 152 | + |
| 153 | + async def test_detect_iceberg_orders(self, orderbook): |
| 154 | + """Test detection of iceberg orders.""" |
| 155 | + ob = orderbook |
| 156 | + |
| 157 | + # Detect icebergs |
| 158 | + result = await ob.detect_iceberg_orders() |
| 159 | + |
| 160 | + # Should return detection result |
| 161 | + assert result is not None |
| 162 | + assert isinstance(result, dict) |
| 163 | + |
| 164 | + async def test_detect_order_clusters(self, orderbook): |
| 165 | + """Test detecting order clusters.""" |
| 166 | + ob = orderbook |
| 167 | + |
| 168 | + # Detect clusters |
| 169 | + clusters = await ob.detect_order_clusters() |
| 170 | + |
| 171 | + assert clusters is not None |
| 172 | + assert isinstance(clusters, (dict, list)) |
| 173 | + |
| 174 | + async def test_get_advanced_market_metrics(self, orderbook): |
| 175 | + """Test getting advanced market metrics.""" |
| 176 | + ob = orderbook |
| 177 | + |
| 178 | + # Get metrics |
| 179 | + metrics = await ob.get_advanced_market_metrics() |
| 180 | + |
| 181 | + assert metrics is not None |
| 182 | + assert isinstance(metrics, dict) |
| 183 | + |
| 184 | + async def test_get_liquidity_levels(self, orderbook): |
| 185 | + """Test getting liquidity levels.""" |
| 186 | + ob = orderbook |
| 187 | + |
| 188 | + # Get liquidity levels |
| 189 | + levels = await ob.get_liquidity_levels() |
| 190 | + |
| 191 | + assert levels is not None |
| 192 | + assert isinstance(levels, dict) |
| 193 | + |
| 194 | + async def test_get_support_resistance_levels(self, orderbook): |
| 195 | + """Test getting support and resistance levels.""" |
| 196 | + ob = orderbook |
| 197 | + |
| 198 | + # Get support/resistance levels |
| 199 | + levels = await ob.get_support_resistance_levels() |
| 200 | + |
| 201 | + assert levels is not None |
| 202 | + assert isinstance(levels, dict) |
| 203 | + |
| 204 | + async def test_get_memory_stats(self, orderbook): |
| 205 | + """Test getting memory statistics.""" |
| 206 | + ob = orderbook |
| 207 | + |
| 208 | + # Get memory stats |
| 209 | + stats = await ob.get_memory_stats() |
| 210 | + |
| 211 | + # Should return memory stats |
| 212 | + assert stats is not None |
| 213 | + assert isinstance(stats, dict) |
0 commit comments