Completed
Push — main ( 6da681...4fccdc )
by Switcheolytics
12s queued 11s
created

TestNetworkCrawlerClient.test_parse_validator_status()   B

Complexity

Conditions 1

Size

Total Lines 64
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 53
nop 1
dl 0
loc 64
rs 8.5381
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
from tradehub.decentralized_client import NetworkCrawlerClient
2
from tests import APITestCase
3
4
5
class TestNetworkCrawlerClient(APITestCase):
6
7
    def setUp(self) -> None:
8
        self.tradehub_client = NetworkCrawlerClient(network='mainnet')
9
10
    def test_validator_crawler_mp(self):
11
        self.tradehub_client.validator_crawler_mp()
12
        self.assertIsNotNone(self.tradehub_client.active_sentry_api_list)
13
        self.assertIsNotNone(self.tradehub_client.active_validator_list)
14
15
    def test_validator_status_request(self):
16
        expect: dict = {
17
            'moniker': str,
18
            'id': str,
19
            'ip': str,
20
            'version': str,
21
            'network': str,
22
            'latest_block_hash': str,
23
            'latest_block_height': str,
24
            'latest_block_time': str,
25
            'earliest_block_height': str,
26
            'earliest_block_time': str,
27
            'catching_up': bool,
28
            'validator_address': str,
29
            'validator_pub_key_type': str,
30
            'validator_pub_key': str,
31
            'validator_voting_power': str,
32
            'validator_status': str,
33
            'connected_nodes': [{
34
                'node_id': str,
35
                'node_ip': str,
36
                'node_full': str,
37
            }]
38
        }
39
40
        result = self.tradehub_client.validator_status_request(validator_ip=self.tradehub_client.active_sentry_api_ip)
41
        self.assertDictStructure(expect, result)
42
43
    def test_parse_validator_status(self):
44
        expect: dict = {
45
            'moniker': str,
46
            'id': str,
47
            'ip': str,
48
            'version': str,
49
            'network': str,
50
            'latest_block_hash': str,
51
            'latest_block_height': str,
52
            'latest_block_time': str,
53
            'earliest_block_height': str,
54
            'earliest_block_time': str,
55
            'catching_up': bool,
56
            'validator_address': str,
57
            'validator_pub_key_type': str,
58
            'validator_pub_key': str,
59
            'validator_voting_power': str,
60
        }
61
62
        validator_status_json: dict = {
63
            'jsonrpc': '2.0',
64
            'id': -1,
65
            'result': {
66
                'node_info': {
67
                    'protocol_version': {
68
                        'p2p': '7',
69
                        'block': '10',
70
                        'app': '0'
71
                    },
72
                    'id': '756dece3e0a00705c61a7de701f78f51f5e9a91b',
73
                    'listen_addr': 'tcp://0.0.0.0:26656',
74
                    'network': 'switcheo-tradehub-1',
75
                    'version': '0.33.7',
76
                    'channels': '4020212223303800',
77
                    'moniker': 'pike',
78
                    'other': {
79
                        'tx_index': 'on',
80
                        'rpc_address': 'tcp://0.0.0.0:26659'
81
                    }
82
                },
83
                'sync_info': {
84
                    'latest_block_hash': '3E385BDDF88024B1767BC5E19D4C4F92122FDF16BF93702CCACBD5D1E2299252',
85
                    'latest_app_hash': '0470CC9EA15482ECB5D497A6D70EF9C1181B649244AB589D8858941FB65EC7FB',
86
                    'latest_block_height': '7559634',
87
                    'latest_block_time': '2021-02-16T07:25:24.602077456Z',
88
                    'earliest_block_hash': 'B4AF1F3D3D3FD5795BDDB7A6A2E6CA4E34D06338505D6EC46DD8F99E72ADCDAB',
89
                    'earliest_app_hash': '',
90
                    'earliest_block_height': '1',
91
                    'earliest_block_time': '2020-08-14T07:32:27.856700491Z',
92
                    'catching_up': False
93
                },
94
                'validator_info': {
95
                    'address': 'A84B0CB22673DCBCEC8C9EB0A2E83AAC8DE297A2',
96
                    'pub_key': {
97
                        'type': 'tendermint/PubKeyEd25519',
98
                        'value': 'qjqeC/G3OABg7RVbtGDOAS0nWAy3AheJaI7s7OPb5o0='
99
                    },
100
                    'voting_power': '0'
101
                }
102
            }
103
        }
104
105
        result = self.tradehub_client.parse_validator_status(request_json=validator_status_json, validator_ip=self.tradehub_client.active_sentry_api_ip)
106
        self.assertDictStructure(expect, result)
107
108
    def test_sentry_status_request(self):
109
        self.tradehub_client.sentry_status_request()
110
        self.assertIsNotNone(self.tradehub_client.active_sentry_api_list)
111
112
    def test_tradehub_get_request(self):
113
        expect: dict = {
114
            "jsonrpc": str,
115
            "id": int,
116
            "result": {
117
                "node_info": {
118
                    "protocol_version": {
119
                        "p2p": str,
120
                        "block":  str,
121
                        "app":  str,
122
                    },
123
                    "id": str,
124
                    "listen_addr": str,
125
                    "network": str,
126
                    "version": str,
127
                    "channels": str,
128
                    "moniker": str,
129
                    "other": {
130
                        "tx_index": str,
131
                        "rpc_address": str,
132
                    }
133
                },
134
                "sync_info": {
135
                    "latest_block_hash": str,
136
                    "latest_app_hash": str,
137
                    "latest_block_height": str,
138
                    "latest_block_time": str,
139
                    "earliest_block_hash": str,
140
                    "earliest_app_hash": str,
141
                    "earliest_block_height": str,
142
                    "earliest_block_time": str,
143
                    "catching_up": bool
144
                },
145
                "validator_info": {
146
                    "address": str,
147
                    "pub_key": {
148
                        "type": str,
149
                        "value": str,
150
                    },
151
                    "voting_power": str,
152
                }
153
            }
154
        }
155
156
        result = self.tradehub_client.tradehub_get_request(path='/get_status')
157
        self.assertDictStructure(expect, result)
158
159
        # This Sentry IP should fail and retry with an existing Sentry IP in the active list.
160
        self.tradehub_client.active_sentry_api_ip = "116.202.216.145"
161
        self.tradehub_client.active_sentry_api_list.append("116.202.216.145")
162
        result = self.tradehub_client.tradehub_get_request(path='/get_status')
163
        self.assertDictStructure(expect, result)
164