1
|
|
|
import asyncio |
2
|
|
|
import concurrent |
3
|
|
|
from typing import Optional |
4
|
|
|
|
5
|
|
|
from tests import APITestCase, DEVEL_AND_CO_SENTRY, WALLET_SWTH_ETH1_AMM, WEBSOCKET_TIMEOUT_GET_REQUEST |
6
|
|
|
from tradehub.websocket_client import DemexWebsocket |
7
|
|
|
|
8
|
|
|
|
9
|
|
View Code Duplication |
class TestWSGetOpenOrders(APITestCase): |
|
|
|
|
10
|
|
|
|
11
|
|
|
def test_get_open_orders_structure(self): |
12
|
|
|
""" |
13
|
|
|
Check if response match expected dict structure. |
14
|
|
|
:return: |
15
|
|
|
""" |
16
|
|
|
expect: dict = { |
17
|
|
|
"id": str, |
18
|
|
|
"result": [ |
19
|
|
|
{ |
20
|
|
|
"order_id": str, |
21
|
|
|
"block_height": int, |
22
|
|
|
"triggered_block_height": int, |
23
|
|
|
"address": str, |
24
|
|
|
"market": str, |
25
|
|
|
"side": str, |
26
|
|
|
"price": str, |
27
|
|
|
"quantity": str, |
28
|
|
|
"available": str, |
29
|
|
|
"filled": str, |
30
|
|
|
"order_status": str, |
31
|
|
|
"order_type": str, |
32
|
|
|
"initiator": str, |
33
|
|
|
"time_in_force": str, |
34
|
|
|
"stop_price": str, |
35
|
|
|
"trigger_type": str, |
36
|
|
|
"allocated_margin_denom": str, |
37
|
|
|
"allocated_margin_amount": str, |
38
|
|
|
"is_liquidation": bool, |
39
|
|
|
"is_post_only": bool, |
40
|
|
|
"is_reduce_only": bool, |
41
|
|
|
"type": str, |
42
|
|
|
"block_created_at": str, |
43
|
|
|
"username": str, |
44
|
|
|
"id": str, |
45
|
|
|
} |
46
|
|
|
] |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
# connect to websocket |
50
|
|
|
client = DemexWebsocket(f"ws://{DEVEL_AND_CO_SENTRY}:5000/ws") |
51
|
|
|
# little work around to save the response |
52
|
|
|
self.response: Optional[dict] = None |
53
|
|
|
|
54
|
|
|
async def on_connect(): |
55
|
|
|
await client.get_open_orders('order_history', WALLET_SWTH_ETH1_AMM) |
56
|
|
|
|
57
|
|
|
async def on_message(message: dict): |
58
|
|
|
# save response into self |
59
|
|
|
self.response = message |
60
|
|
|
await client.disconnect() |
61
|
|
|
|
62
|
|
|
try: |
63
|
|
|
loop = asyncio.get_event_loop() |
64
|
|
|
loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect, |
65
|
|
|
on_receive_message_callback=on_message), |
66
|
|
|
WEBSOCKET_TIMEOUT_GET_REQUEST)) |
67
|
|
|
except concurrent.futures._base.TimeoutError: |
68
|
|
|
raise TimeoutError("Test did not complete in time.") |
69
|
|
|
|
70
|
|
|
if not self.response: |
71
|
|
|
raise RuntimeError("Did not receive a response.") |
72
|
|
|
|
73
|
|
|
self.assertDictStructure(expect, self.response) |
74
|
|
|
|
75
|
|
|
|