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

tests.test_ws_get_candlesticks   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TestWSGetCandlesticks.test_get_candlesticks_wrong_granularity() 0 12 3
B TestWSGetCandlesticks.test_get_candlesticks_structure() 0 42 5
1
import asyncio
2
import concurrent
3
from typing import Optional, List
4
5
from tests import APITestCase, DEVEL_AND_CO_SENTRY, WEBSOCKET_TIMEOUT_GET_REQUEST
6
from tradehub.websocket_client import DemexWebsocket
7
8
9
class TestWSGetCandlesticks(APITestCase):
10
11
    def test_get_candlesticks_structure(self):
12
        """
13
        Check if response match expected dict structure.
14
        :return:
15
        """
16
17
        # TODO This endpoint does not work currently
18
        expect: dict = {
19
            'id': str,
20
            'error': {
21
                'code': str,
22
                'message': str
23
            }
24
        }
25
26
        # connect to websocket
27
        client = DemexWebsocket(f"ws://{DEVEL_AND_CO_SENTRY}:5000/ws")
28
        # little work around to save the response
29
        self.response: List[Optional[dict]] = []
30
31
        async def on_connect():
32
            for granularity in [1, 5, 15, 30, 60, 360, 1440]:
33
                await client.get_candlesticks('candlesticks', "swth_eth1", granularity)
34
35
        async def on_message(message: dict):
36
            # save response into self
37
            self.response.append(message)
38
39
        try:
40
            loop = asyncio.get_event_loop()
41
            loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect,
42
                                                                    on_receive_message_callback=on_message),
43
                                                     WEBSOCKET_TIMEOUT_GET_REQUEST))
44
        except asyncio.TimeoutError:
45
            loop = asyncio.get_event_loop()
46
            loop.run_until_complete(client.disconnect())
47
48
        if not self.response:
49
            raise RuntimeError("Did not receive a response.")
50
51
        for response in self.response:
52
            self.assertDictStructure(expect, response)
53
54
    def test_get_candlesticks_wrong_granularity(self):
55
        """
56
        Check if the method catches wrong granularities.
57
        :return:
58
        """
59
60
        # connect to websocket
61
        client = DemexWebsocket("")
62
        for wrong_granularity in [0, 2, 4, 6, 100, 1500]:
63
            with self.assertRaises(ValueError):
64
                loop = asyncio.get_event_loop()
65
                loop.run_until_complete(client.get_candlesticks("candle", "swth_eth1", wrong_granularity))
66