Passed
Pull Request — main (#43)
by Switcheolytics
01:26
created

tests.test_ws_subscribe_balance   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 88.41 %

Importance

Changes 0
Metric Value
eloc 42
dl 61
loc 69
rs 10
c 0
b 0
f 0
wmc 4

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
from typing import List
3
4
from tests import APITestCase, MAINNET_WS_URI, WALLET_SWTH_ETH1_AMM, WEBSOCKET_TIMEOUT_SUBSCRIPTION
5
from tradehub.websocket_client import DemexWebsocket
6
7
8
class TestWSSubscribeBalance(APITestCase):
9
10
    def test_subscribe_balance_structure(self):
11
        """
12
        Check if response match expected dict structure.
13
        :return:
14
        """
15
        expect_subscription: dict = {
16
            'id': str,
17
            'result': [str]
18
        }
19
20
        balance: dict = {
21
            'available': str,
22
            'order': str,
23
            'position': str,
24
            'denom': str
25
        }
26
27
        expect: dict = {
28
            'channel': str,
29
            'result': {
30
                'eth1': balance,
31
                'eth1-80-swth-20-lp1': balance,
32
                'swth': balance
33
            }
34
        }
35
36
        # connect to websocket
37
        client = DemexWebsocket(uri=MAINNET_WS_URI)
38
        # little work around to save the response
39
        self.response: List[dict] = []
40
41
        async def on_connect():
42
            # use AMM to be sure deterministic of which tokens the wallet holds
43
            await client.subscribe_balances('balance', WALLET_SWTH_ETH1_AMM)
44
45
        async def on_message(message: dict):
46
            # save response into self
47
            self.response.append(message)
48
49
        try:
50
            loop = asyncio.get_event_loop()
51
            loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect,
52
                                                                    on_receive_message_callback=on_message),
53
                                                     WEBSOCKET_TIMEOUT_SUBSCRIPTION))
54
        except asyncio.TimeoutError:
55
            loop = asyncio.get_event_loop()
56
            loop.run_until_complete(client.disconnect())
57
58
        if not self.response:
59
            raise RuntimeError("Did not receive a response.")
60
61
        self.assertTrue(len(self.response) >= 2, msg=f"Expected at least 2 messages: channel subscription and and first update message")
62
63
        channel_subscription: dict = self.response[0]
64
        self.assertDictStructure(expect_subscription, channel_subscription)
65
66
        for message in self.response[1:]:
67
            # if this fails, check if the AMM wallet own other tokens as expected
68
            self.assertDictStructure(expect, message)
69