Passed
Pull Request — main (#21)
by
unknown
02:37
created

tests.test_ws_get_account_trades   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TestWSGetAccountTrades.test_get_account_trades_pagination() 0 40 3
A TestWSGetAccountTrades.test_get_account_trades_structure() 0 55 3
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, WALLET_DEVEL
6
from tradehub.websocket_client import DemexWebsocket
7
8
9
class TestWSGetAccountTrades(APITestCase):
10
11
    def test_get_account_trades_structure(self):
12
        """
13
        Check if response match expected dict structure.
14
        :return:
15
        """
16
17
        expect: dict = {
18
            "id": str,
19
            "result": [
20
                {
21
                    "base_precision": int,
22
                    "quote_precision": int,
23
                    "fee_precision": int,
24
                    "order_id": str,
25
                    "market": str,
26
                    "side": str,
27
                    "quantity": str,
28
                    "price": str,
29
                    "fee_amount": str,
30
                    "fee_denom": str,
31
                    "address": str,
32
                    "block_height": str,
33
                    "block_created_at": str,
34
                    "id": int
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: Optional[dict] = None
43
44
        async def on_connect():
45
            await client.get_account_trades('account_trades', WALLET_DEVEL)
46
47
        async def on_message(message: dict):
48
            # save response into self
49
            self.response = message
50
            await client.disconnect()
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
                                                     WEBSOCKET_TIMEOUT_GET_REQUEST))
57
        except concurrent.futures._base.TimeoutError:
58
            raise TimeoutError("Test did not complete in time.")
59
60
        if not self.response:
61
            raise RuntimeError("Did not receive a response.")
62
63
        self.assertDictStructure(expect, self.response)
64
65
        self.assertTrue(len(self.response["result"]) == 100, msg=f"Expected 100 recent trades, got {len(self.response['result'])}")
66
67
    def test_get_account_trades_pagination(self):
68
        """
69
        Check if response match expected dict structure.
70
        :return:
71
        """
72
73
        # TODO This endpoint does not work currently
74
        expect: dict = {
75
            'id': str,
76
            'error': {
77
                'code': str,
78
                'message': str
79
            }
80
        }
81
82
        # connect to websocket
83
        client = DemexWebsocket(f"ws://{DEVEL_AND_CO_SENTRY}:5000/ws")
84
        # little work around to save the response
85
        self.response: Optional[dict] = None
86
87
        async def on_connect():
88
            await client.get_account_trades('account_trades', WALLET_DEVEL, page=1)
89
90
        async def on_message(message: dict):
91
            # save response into self
92
            self.response = message
93
            await client.disconnect()
94
95
        try:
96
            loop = asyncio.get_event_loop()
97
            loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect,
98
                                                                    on_receive_message_callback=on_message),
99
                                                     WEBSOCKET_TIMEOUT_GET_REQUEST))
100
        except concurrent.futures._base.TimeoutError:
101
            raise TimeoutError("Test did not complete in time.")
102
103
        if not self.response:
104
            raise RuntimeError("Did not receive a response.")
105
106
        self.assertDictStructure(expect, self.response)
107