Total Complexity | 3 |
Total Lines | 68 |
Duplicated Lines | 85.29 % |
Changes | 0 |
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 Optional |
||
3 | |||
4 | from tests import APITestCase, MAINNET_WS_URI, WEBSOCKET_TIMEOUT_GET_REQUEST |
||
5 | from tradehub.websocket_client import DemexWebsocket |
||
6 | |||
7 | |||
8 | class TestWSGetRecentTrades(APITestCase): |
||
9 | |||
10 | def test_get_recent_trades_structure(self): |
||
11 | """ |
||
12 | Check if response match expected dict structure. |
||
13 | :return: |
||
14 | """ |
||
15 | expect: dict = { |
||
16 | "id": str, |
||
17 | "sequence_number": int, |
||
18 | "result": [ |
||
19 | { |
||
20 | "id": str, |
||
21 | "block_created_at": str, |
||
22 | "taker_id": str, |
||
23 | "taker_address": str, |
||
24 | "taker_fee_amount": str, |
||
25 | "taker_fee_denom": str, |
||
26 | "taker_side": str, |
||
27 | "maker_id": str, |
||
28 | "maker_address": str, |
||
29 | "maker_fee_amount": str, |
||
30 | "maker_fee_denom": str, |
||
31 | "maker_side": str, |
||
32 | "market": str, |
||
33 | "price": str, |
||
34 | "quantity": str, |
||
35 | "liquidation": str, |
||
36 | "taker_username": str, |
||
37 | "maker_username": str, |
||
38 | "block_height": str |
||
39 | }, |
||
40 | ] |
||
41 | } |
||
42 | |||
43 | # connect to websocket |
||
44 | client = DemexWebsocket(uri=MAINNET_WS_URI) |
||
45 | # little work around to save the response |
||
46 | self.response: Optional[dict] = None |
||
47 | |||
48 | async def on_connect(): |
||
49 | await client.get_recent_trades("recent_trades", "swth_eth1") |
||
50 | |||
51 | async def on_message(message: dict): |
||
52 | # save response into self |
||
53 | self.response = message |
||
54 | await client.disconnect() |
||
55 | |||
56 | try: |
||
57 | loop = asyncio.get_event_loop() |
||
58 | loop.run_until_complete(asyncio.wait_for(client.connect(on_connect_callback=on_connect, |
||
59 | on_receive_message_callback=on_message), |
||
60 | WEBSOCKET_TIMEOUT_GET_REQUEST)) |
||
61 | except asyncio.TimeoutError: |
||
62 | raise TimeoutError("Test did not complete in time.") |
||
63 | |||
64 | if not self.response: |
||
65 | raise RuntimeError("Did not receive a response.") |
||
66 | |||
67 | self.assertDictStructure(expect, self.response) |
||
68 |