Passed
Pull Request — main (#42)
by Switcheolytics
55s
created

tests.test_ws_get_account_trades   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 90 %

Importance

Changes 0
Metric Value
eloc 78
dl 108
loc 120
rs 10
c 0
b 0
f 0
wmc 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import asyncio
2
import concurrent
3
from typing import Optional
4
5
from tests import APITestCase, MAINNET_WS_URI, 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(uri=MAINNET_WS_URI)
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
        expect: dict = {
74
            "id": str,
75
            "result": [
76
                {
77
                    "base_precision": int,
78
                    "quote_precision": int,
79
                    "fee_precision": int,
80
                    "order_id": str,
81
                    "market": str,
82
                    "side": str,
83
                    "quantity": str,
84
                    "price": str,
85
                    "fee_amount": str,
86
                    "fee_denom": str,
87
                    "address": str,
88
                    "block_height": str,
89
                    "block_created_at": str,
90
                    "id": int
91
                }
92
            ]
93
        }
94
95
        # connect to websocket
96
        client = DemexWebsocket(uri=MAINNET_WS_URI)
97
        # little work around to save the response
98
        self.response: Optional[dict] = None
99
100
        async def on_connect():
101
            await client.get_account_trades('account_trades', WALLET_DEVEL, page=1)
102
103
        async def on_message(message: dict):
104
            # save response into self
105
            self.response = message
106
            await client.disconnect()
107
108
        try:
109
            loop = asyncio.get_event_loop()
110
            loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect,
111
                                                                    on_receive_message_callback=on_message),
112
                                                     WEBSOCKET_TIMEOUT_GET_REQUEST))
113
        except asyncio.TimeoutError:
114
            raise TimeoutError("Test did not complete in time.")
115
116
        if not self.response:
117
            raise RuntimeError("Did not receive a response.")
118
119
        self.assertDictStructure(expect, self.response)
120