1
|
|
|
import asyncio |
2
|
|
|
import concurrent |
3
|
|
|
from typing import Optional, List |
4
|
|
|
|
5
|
|
|
from tests import APITestCase, DEVEL_AND_CO_SENTRY, WALLET_SWTH_ETH1_AMM, WEBSOCKET_TIMEOUT_SUBSCRIPTION |
6
|
|
|
from tradehub.websocket_client import DemexWebsocket |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestWSSubscribeCandlesticks(APITestCase): |
10
|
|
|
|
11
|
|
|
def test_subscribe_candlesticks_structure(self): |
12
|
|
|
""" |
13
|
|
|
Check if response match expected dict structure. |
14
|
|
|
:return: |
15
|
|
|
""" |
16
|
|
|
expect_subscription: dict = { |
17
|
|
|
'id': str, |
18
|
|
|
'result': [str] |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
expect: dict = { |
22
|
|
|
'channel': str, |
23
|
|
|
'sequence_number': int, |
24
|
|
|
'result': { |
25
|
|
|
'id': int, |
26
|
|
|
'market': str, |
27
|
|
|
'time': str, |
28
|
|
|
'resolution': int, |
29
|
|
|
'open': str, |
30
|
|
|
'close': str, |
31
|
|
|
'high': str, |
32
|
|
|
'low': str, |
33
|
|
|
'volume': str, |
34
|
|
|
'quote_volume': str, |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
# connect to websocket |
40
|
|
|
client = DemexWebsocket(f"ws://{DEVEL_AND_CO_SENTRY}:5000/ws") |
41
|
|
|
# little work around to save the response |
42
|
|
|
self.response: List[dict] = [] |
43
|
|
|
|
44
|
|
|
async def on_connect(): |
45
|
|
|
await client.subscribe_candlesticks('candlesticks', "swth_eth1", 1) |
46
|
|
|
|
47
|
|
|
async def on_message(message: dict): |
48
|
|
|
# save response into self |
49
|
|
|
print(message) |
50
|
|
|
self.response.append(message) |
51
|
|
|
|
52
|
|
|
try: |
53
|
|
|
loop = asyncio.get_event_loop() |
54
|
|
|
loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect, |
55
|
|
|
on_receive_message_callback=on_message), |
56
|
|
|
2*WEBSOCKET_TIMEOUT_SUBSCRIPTION)) |
57
|
|
|
except concurrent.futures._base.TimeoutError: |
58
|
|
|
loop = asyncio.get_event_loop() |
59
|
|
|
loop.run_until_complete(client.disconnect()) |
60
|
|
|
|
61
|
|
|
if not self.response: |
62
|
|
|
raise RuntimeError("Did not receive a response.") |
63
|
|
|
|
64
|
|
|
if len(self.response) < 2: |
65
|
|
|
self.skipTest(f"Did not receive candlesticks within time, test can not finish.") |
66
|
|
|
|
67
|
|
|
channel_subscription: dict = self.response[0] |
68
|
|
|
self.assertDictStructure(expect_subscription, channel_subscription) |
69
|
|
|
|
70
|
|
|
for message in self.response[1:]: |
71
|
|
|
self.assertDictStructure(expect, message) |
72
|
|
|
|
73
|
|
|
def test_subscribe_candlesticks_wrong_granularity(self): |
74
|
|
|
""" |
75
|
|
|
Check if the method catches wrong granularities. |
76
|
|
|
:return: |
77
|
|
|
""" |
78
|
|
|
|
79
|
|
|
# connect to websocket |
80
|
|
|
client = DemexWebsocket("") |
81
|
|
|
for wrong_granularity in [0, 2, 4, 6, 100, 1500]: |
82
|
|
|
with self.assertRaises(ValueError): |
83
|
|
|
loop = asyncio.get_event_loop() |
84
|
|
|
loop.run_until_complete(client.subscribe_candlesticks("candle", "swth_eth1", wrong_granularity)) |
85
|
|
|
|