1
|
|
|
import asyncio |
2
|
|
|
import concurrent |
3
|
|
|
from typing import Optional |
4
|
|
|
|
5
|
|
|
from tests import APITestCase, DEVEL_AND_CO_SENTRY, WEBSOCKET_TIMEOUT_GET_REQUEST |
6
|
|
|
from tradehub.websocket_client import DemexWebsocket |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestWSGetRecentTrades(APITestCase): |
10
|
|
|
|
11
|
|
|
def test_get_recent_trades_structure(self): |
12
|
|
|
""" |
13
|
|
|
Check if response match expected dict structure. |
14
|
|
|
:return: |
15
|
|
|
""" |
16
|
|
|
expect: dict = { |
17
|
|
|
"id": str, |
18
|
|
|
"sequence_number": int, |
19
|
|
|
"result": [ |
20
|
|
|
{ |
21
|
|
|
"id": str, |
22
|
|
|
"block_created_at": str, |
23
|
|
|
"taker_id": str, |
24
|
|
|
"taker_address": str, |
25
|
|
|
"taker_fee_amount": str, |
26
|
|
|
"taker_fee_denom": str, |
27
|
|
|
"taker_side": str, |
28
|
|
|
"maker_id": str, |
29
|
|
|
"maker_address": str, |
30
|
|
|
"maker_fee_amount": str, |
31
|
|
|
"maker_fee_denom": str, |
32
|
|
|
"maker_side": str, |
33
|
|
|
"market": str, |
34
|
|
|
"price": str, |
35
|
|
|
"quantity": str, |
36
|
|
|
"liquidation": str, |
37
|
|
|
"taker_username": str, |
38
|
|
|
"maker_username": str, |
39
|
|
|
"block_height": str |
40
|
|
|
}, |
41
|
|
|
] |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
# connect to websocket |
45
|
|
|
client = DemexWebsocket(f"ws://{DEVEL_AND_CO_SENTRY}:5000/ws") |
46
|
|
|
# little work around to save the response |
47
|
|
|
self.response: Optional[dict] = None |
48
|
|
|
|
49
|
|
|
async def on_connect(): |
50
|
|
|
await client.get_recent_trades("recent_trades", "swth_eth1") |
51
|
|
|
|
52
|
|
|
async def on_message(message: dict): |
53
|
|
|
# save response into self |
54
|
|
|
self.response = message |
55
|
|
|
await client.disconnect() |
56
|
|
|
|
57
|
|
|
try: |
58
|
|
|
loop = asyncio.get_event_loop() |
59
|
|
|
loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect, |
60
|
|
|
on_receive_message_callback=on_message), |
61
|
|
|
WEBSOCKET_TIMEOUT_GET_REQUEST)) |
62
|
|
|
except asyncio.TimeoutError: |
63
|
|
|
raise TimeoutError("Test did not complete in time.") |
64
|
|
|
|
65
|
|
|
if not self.response: |
66
|
|
|
raise RuntimeError("Did not receive a response.") |
67
|
|
|
|
68
|
|
|
self.assertDictStructure(expect, self.response) |
69
|
|
|
|
70
|
|
|
# TODO remove or change if id in trade is no longer 'id' |
71
|
|
|
for trade in self.response["result"]: |
72
|
|
|
self.assertTrue(trade["id"] == "0", msg="Expected id to be '0'") |
73
|
|
|
|