Skip to content

Commit 1743313

Browse files
committed
Adding test for load historic candles
1 parent e6b890d commit 1743313

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pytrade/broker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def load_instrument_candles(
5252
if key in self._subscriptions:
5353
raise RuntimeError(
5454
f"Consumers are already subscribed to {instrument}[{granularity.value}], \
55-
can not populate historical data."
55+
can not populate historical data."
5656
)
5757

5858
instrument_data = self._data_context.get(instrument, granularity)

tests/unit/test_broker.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from datetime import datetime, timedelta, timezone
33
from unittest.mock import MagicMock, Mock
44

5+
import pytest
6+
57
from pytrade.broker import Broker
68
from pytrade.instruments import MINUTES_MAP, Candlestick, FxInstrument, Granularity
79
from pytrade.models import Order
@@ -117,3 +119,32 @@ def test_load_instrument_candles():
117119

118120
broker.load_instrument_candles(instrument, granularity, 20)
119121
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

Comments
 (0)