|
2 | 2 | from datetime import datetime, timedelta, timezone |
3 | 3 | from unittest.mock import MagicMock, Mock |
4 | 4 |
|
| 5 | +import pytest |
| 6 | + |
5 | 7 | from pytrade.broker import Broker |
6 | 8 | from pytrade.instruments import MINUTES_MAP, Candlestick, FxInstrument, Granularity |
7 | 9 | from pytrade.models import Order |
@@ -117,3 +119,32 @@ def test_load_instrument_candles(): |
117 | 119 |
|
118 | 120 | broker.load_instrument_candles(instrument, granularity, 20) |
119 | 121 | assert len(instrument_data.df) == 20 |
| 122 | + |
| 123 | + |
| 124 | +def test_load_instrument_candles_after_subscribing(): |
| 125 | + client = MagicMock() |
| 126 | + client.get_candles.side_effect = _get_candles |
| 127 | + broker = Broker(client) |
| 128 | + |
| 129 | + instrument = FxInstrument.EURUSD |
| 130 | + granularity = Granularity.M1 |
| 131 | + |
| 132 | + assert len(broker._subscriptions) == 0 |
| 133 | + assert client.subscribe.call_count == 0 |
| 134 | + |
| 135 | + broker.subscribe(instrument, granularity) |
| 136 | + |
| 137 | + assert len(broker._subscriptions) == 1 |
| 138 | + assert client.subscribe.call_count == 1 |
| 139 | + assert len(broker._data_context._data) == 1 |
| 140 | + |
| 141 | + instrument_data = broker._data_context.get(instrument, granularity) |
| 142 | + assert len(instrument_data.df) == 0 |
| 143 | + |
| 144 | + with pytest.raises(RuntimeError) as e: |
| 145 | + broker.load_instrument_candles(instrument, granularity, 10) |
| 146 | + |
| 147 | + assert ( |
| 148 | + e.value.args[0] |
| 149 | + == "Consumers are already subscribed to FxInstrument.EURUSD[M1], can not populate historical data." |
| 150 | + ) |
0 commit comments