Skip to content

Commit 93c2d6a

Browse files
committed
tests: Add test for get_ohlcv to ensure behavior is and stays correct
1 parent 649739b commit 93c2d6a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/exchange/test_exchange_ws.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,73 @@ async def sleeper(*args, **kwargs):
113113
finally:
114114
# Cleanup
115115
exchange_ws.cleanup()
116+
117+
118+
async def test_exchangews_get_ohlcv(mocker, caplog):
119+
config = MagicMock()
120+
ccxt_object = MagicMock()
121+
ccxt_object.ohlcvs = {
122+
"ETH/USDT": {
123+
"1m": [
124+
[1635840000000, 100, 200, 300, 400, 500],
125+
[1635840060000, 101, 201, 301, 401, 501],
126+
[1635840120000, 102, 202, 302, 402, 502],
127+
],
128+
"5m": [
129+
[1635840000000, 100, 200, 300, 400, 500],
130+
[1635840300000, 105, 201, 301, 401, 501],
131+
[1635840600000, 102, 202, 302, 402, 502],
132+
],
133+
}
134+
}
135+
mocker.patch("freqtrade.exchange.exchange_ws.ExchangeWS._start_forever", MagicMock())
136+
137+
exchange_ws = ExchangeWS(config, ccxt_object)
138+
exchange_ws.klines_last_refresh = {
139+
("ETH/USDT", "1m", CandleType.SPOT): 1635840120000,
140+
("ETH/USDT", "5m", CandleType.SPOT): 1635840600000,
141+
}
142+
143+
# Matching last candle time - drop hint is true
144+
resp = await exchange_ws.get_ohlcv("ETH/USDT", "1m", CandleType.SPOT, 1635840120000)
145+
assert resp[0] == "ETH/USDT"
146+
assert resp[1] == "1m"
147+
assert resp[3] == [
148+
[1635840000000, 100, 200, 300, 400, 500],
149+
[1635840060000, 101, 201, 301, 401, 501],
150+
[1635840120000, 102, 202, 302, 402, 502],
151+
]
152+
assert resp[4] is True
153+
154+
# expected time > last candle time - drop hint is false
155+
resp = await exchange_ws.get_ohlcv("ETH/USDT", "1m", CandleType.SPOT, 1635840180000)
156+
assert resp[0] == "ETH/USDT"
157+
assert resp[1] == "1m"
158+
assert resp[3] == [
159+
[1635840000000, 100, 200, 300, 400, 500],
160+
[1635840060000, 101, 201, 301, 401, 501],
161+
[1635840120000, 102, 202, 302, 402, 502],
162+
]
163+
assert resp[4] is False
164+
165+
# Change "received" times to be before the candle starts.
166+
# This should trigger the "time sync" warning.
167+
exchange_ws.klines_last_refresh = {
168+
("ETH/USDT", "1m", CandleType.SPOT): 1635840110000,
169+
("ETH/USDT", "5m", CandleType.SPOT): 1635840600000,
170+
}
171+
msg = r".*Candle date > last refresh.*"
172+
assert not log_has_re(msg, caplog)
173+
resp = await exchange_ws.get_ohlcv("ETH/USDT", "1m", CandleType.SPOT, 1635840120000)
174+
assert resp[0] == "ETH/USDT"
175+
assert resp[1] == "1m"
176+
assert resp[3] == [
177+
[1635840000000, 100, 200, 300, 400, 500],
178+
[1635840060000, 101, 201, 301, 401, 501],
179+
[1635840120000, 102, 202, 302, 402, 502],
180+
]
181+
assert resp[4] is True
182+
183+
assert log_has_re(msg, caplog)
184+
185+
exchange_ws.cleanup()

0 commit comments

Comments
 (0)