Completed
Push — main ( 30c9e1...6da681 )
by Switcheolytics
42:03 queued 11s
created

tests.test_ws_subscribe_recent_trades   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 86.9 %

Importance

Changes 0
Metric Value
eloc 56
dl 73
loc 84
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B TestWSSubscribeRecentTrades.test_subscribe_recent_trades_structure() 73 73 5

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, List
4
5
from tests import APITestCase, DEVEL_AND_CO_SENTRY, WALLET_SWTH_ETH1_AMM, WEBSOCKET_TIMEOUT_SUBSCRIPTION
6
from tradehub.websocket_client import DemexWebsocket
7
8
9
class TestWSSubscribeRecentTrades(APITestCase):
10
11 View Code Duplication
    def test_subscribe_recent_trades_structure(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
        """
13
        Check if response match expected dict structure.
14
        :return:
15
        """
16
        expect_subscription: dict = {
17
            'id': str,
18
            'result': [str]
19
        }
20
21
        expect: dict = {
22
            'channel': str,
23
            'sequence_number': 812,
24
            'result': [
25
                {
26
                    'id': str,
27
                    'block_created_at': str,
28
                    'taker_id': str,
29
                    'taker_address': str,
30
                    'taker_fee_amount': str,
31
                    'taker_fee_denom': str,
32
                    'taker_side': str,
33
                    'maker_id': str,
34
                    'maker_address': str,
35
                    'maker_fee_amount': str,
36
                    'maker_fee_denom': str,
37
                    'maker_side': str,
38
                    'market': str,
39
                    'price': str,
40
                    'quantity': str,
41
                    'liquidation': str,
42
                    'taker_username': str,
43
                    'maker_username': str,
44
                    'block_height': str,
45
                }
46
            ]
47
        }
48
49
50
        # connect to websocket
51
        client = DemexWebsocket(f"ws://{DEVEL_AND_CO_SENTRY}:5000/ws")
52
        # little work around to save the response
53
        self.response: List[dict] = []
54
55
        async def on_connect():
56
            # use AMM to be sure deterministic of which tokens the wallet holds
57
            await client.subscribe_recent_trades('orders', "eth1_usdc1")
58
59
        async def on_message(message: dict):
60
            # save response into self
61
            self.response.append(message)
62
63
        try:
64
            loop = asyncio.get_event_loop()
65
            loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect,
66
                                                                    on_receive_message_callback=on_message),
67
                                                     WEBSOCKET_TIMEOUT_SUBSCRIPTION))
68
        except asyncio.TimeoutError:
69
            loop = asyncio.get_event_loop()
70
            loop.run_until_complete(client.disconnect())
71
72
        if not self.response:
73
            raise RuntimeError("Did not receive a response.")
74
75
        if len(self.response) < 2:
76
            self.skipTest(f"Did not receive orders within time, test can not finish.")
77
78
        channel_subscription: dict = self.response[0]
79
        self.assertDictStructure(expect_subscription, channel_subscription)
80
81
        for message in self.response[1:]:
82
            # if this fails, check if the AMM wallet own other tokens as expected
83
            self.assertDictStructure(expect, message)