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

tests.test_ws_subscribe_candlesticks   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 9
1
import asyncio
2
from typing import List
3
4
from tests import APITestCase, MAINNET_WS_URI, WEBSOCKET_TIMEOUT_SUBSCRIPTION
5
from tradehub.websocket_client import DemexWebsocket
6
7
8
class TestWSSubscribeCandlesticks(APITestCase):
9
10
    def test_subscribe_candlesticks_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
        expect: dict = {
21
            'channel': str,
22
            'sequence_number': int,
23
            'result': {
24
                'id': int,
25
                'market': str,
26
                'time': str,
27
                'resolution': int,
28
                'open': str,
29
                'close': str,
30
                'high': str,
31
                'low': str,
32
                'volume': str,
33
                'quote_volume': str,
34
            }
35
        }
36
37
        # connect to websocket
38
        client = DemexWebsocket(uri=MAINNET_WS_URI)
39
        # little work around to save the response
40
        self.response: List[dict] = []
41
42
        async def on_connect():
43
            await client.subscribe_candlesticks('candlesticks', "swth_eth1", 1)
44
45
        async def on_message(message: dict):
46
            # save response into self
47
            print(message)
48
            self.response.append(message)
49
50
        try:
51
            loop = asyncio.get_event_loop()
52
            loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect,
53
                                                                    on_receive_message_callback=on_message),
54
                                                     2*WEBSOCKET_TIMEOUT_SUBSCRIPTION))
55
        except asyncio.TimeoutError:
56
            loop = asyncio.get_event_loop()
57
            loop.run_until_complete(client.disconnect())
58
59
        if not self.response:
60
            raise RuntimeError("Did not receive a response.")
61
62
        if len(self.response) < 2:
63
            self.skipTest(f"Did not receive candlesticks within time, test can not finish.")
64
65
        channel_subscription: dict = self.response[0]
66
        self.assertDictStructure(expect_subscription, channel_subscription)
67
68
        for message in self.response[1:]:
69
            if message["result"]:
70
                self.assertDictStructure(expect, message)
71
72
    def test_subscribe_candlesticks_wrong_granularity(self):
73
        """
74
        Check if the method catches wrong granularities.
75
        :return:
76
        """
77
78
        # connect to websocket
79
        client = DemexWebsocket("")
80
        for wrong_granularity in [0, 2, 4, 6, 100, 1500]:
81
            with self.assertRaises(ValueError):
82
                loop = asyncio.get_event_loop()
83
                loop.run_until_complete(client.subscribe_candlesticks("candle", "swth_eth1", wrong_granularity))
84