1
|
|
|
import tradehub.utils as utils |
2
|
|
|
from tests import APITestCase, NEO_ADDRESS, NEO_CONTRACT, ETH_ADDRESS, ETH_CONTRACT, WEB3_API_URL |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class TestTradeHubUtils(APITestCase): |
6
|
|
|
|
7
|
|
|
def setUp(self) -> None: |
8
|
|
|
self.r = utils.Request(api_url='https://jsonplaceholder.typicode.com/') |
9
|
|
|
self.s = utils.Request() |
10
|
|
|
|
11
|
|
|
def test_request_get(self): |
12
|
|
|
json_msg = { |
13
|
|
|
"userId": 1, |
14
|
|
|
"id": 1, |
15
|
|
|
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", |
16
|
|
|
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"} |
17
|
|
|
self.assertDictEqual(self.r.get(path='/posts/1'), json_msg) |
18
|
|
|
|
19
|
|
|
def test_request_post(self): |
20
|
|
|
json_dict = { |
21
|
|
|
'title': 'foo', |
22
|
|
|
'body': 'bar', |
23
|
|
|
'userId': 1} |
24
|
|
|
json_msg = { |
25
|
|
|
'id': 101, |
26
|
|
|
'title': 'foo', |
27
|
|
|
'body': 'bar', |
28
|
|
|
'userId': 1} |
29
|
|
|
self.assertDictEqual(self.r.post(path='/posts', json_data=json_dict), json_msg) |
30
|
|
|
|
31
|
|
|
def test_request_status(self): |
32
|
|
|
expect: dict = { |
33
|
|
|
"jsonrpc": str, |
34
|
|
|
"id": int, |
35
|
|
|
"result": { |
36
|
|
|
"node_info": { |
37
|
|
|
"protocol_version": { |
38
|
|
|
"p2p": str, |
39
|
|
|
"block": str, |
40
|
|
|
"app": str, |
41
|
|
|
}, |
42
|
|
|
"id": str, |
43
|
|
|
"listen_addr": str, |
44
|
|
|
"network": str, |
45
|
|
|
"version": str, |
46
|
|
|
"channels": str, |
47
|
|
|
"moniker": str, |
48
|
|
|
"other": { |
49
|
|
|
"tx_index": str, |
50
|
|
|
"rpc_address": str, |
51
|
|
|
} |
52
|
|
|
}, |
53
|
|
|
"sync_info": { |
54
|
|
|
"latest_block_hash": str, |
55
|
|
|
"latest_app_hash": str, |
56
|
|
|
"latest_block_height": str, |
57
|
|
|
"latest_block_time": str, |
58
|
|
|
"earliest_block_hash": str, |
59
|
|
|
"earliest_app_hash": str, |
60
|
|
|
"earliest_block_height": str, |
61
|
|
|
"earliest_block_time": str, |
62
|
|
|
"catching_up": bool |
63
|
|
|
}, |
64
|
|
|
"validator_info": { |
65
|
|
|
"address": str, |
66
|
|
|
"pub_key": { |
67
|
|
|
"type": str, |
68
|
|
|
"value": str, |
69
|
|
|
}, |
70
|
|
|
"voting_power": str, |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
self.assertDictStructure(expect, self.s.status()) |
76
|
|
|
|
77
|
|
|
def test_sort_and_stringify_json(self): |
78
|
|
|
json_msg = {"name": "John Smith", "age": 27, "siblings": ["Jane", "Joe"]} |
79
|
|
|
stringify_msg = '{"age":27,"name":"John Smith","siblings":["Jane","Joe"]}' |
80
|
|
|
self.assertEqual(utils.sort_and_stringify_json(json_msg), stringify_msg) |
81
|
|
|
|
82
|
|
|
def test_to_tradehub_asset_amount(self): |
83
|
|
|
self.assertEqual(utils.to_tradehub_asset_amount(amount=float('123.4567')), '12345670000') |
84
|
|
|
self.assertEqual(utils.to_tradehub_asset_amount(amount=float('123.4567'), decimals=16), '1234567000000000000') |
85
|
|
|
|
86
|
|
|
def test_reverse_hex(self): |
87
|
|
|
self.assertEqual(utils.reverse_hex('ABCD'), 'CDAB') |
88
|
|
|
self.assertEqual(utils.reverse_hex('0000000005f5e100'), '00e1f50500000000') |
89
|
|
|
|
90
|
|
|
def test_is_valid_neo_public_address(self): |
91
|
|
|
self.assertTrue(utils.is_valid_neo_public_address(address=NEO_ADDRESS)) |
92
|
|
|
self.assertFalse(utils.is_valid_neo_public_address(address=NEO_CONTRACT)) |
93
|
|
|
|
94
|
|
|
def test_neo_get_scripthash_from_address(self): |
95
|
|
|
self.assertEqual(utils.neo_get_scripthash_from_address(address=NEO_ADDRESS), 'fea2b883725ef2d194c9060f606cd0a0468a2c59') |
96
|
|
|
|
97
|
|
|
def test_is_eth_contract(self): |
98
|
|
|
self.assertTrue(utils.is_eth_contract(address=ETH_CONTRACT, |
99
|
|
|
web3_uri=WEB3_API_URL)) |
100
|
|
|
self.assertFalse(utils.is_eth_contract(address=ETH_ADDRESS, |
101
|
|
|
web3_uri=WEB3_API_URL)) |
102
|
|
|
|
103
|
|
|
def test_format_withdraw_address(self): |
104
|
|
|
valid_neo_address = utils.format_withdraw_address(address=NEO_ADDRESS, blockchain='neo') |
105
|
|
|
self.assertEqual(valid_neo_address, '592c8a46a0d06c600f06c994d1f25e7283b8a2fe') |
106
|
|
|
with self.assertRaises(ValueError): |
107
|
|
|
utils.format_withdraw_address(address=NEO_CONTRACT, blockchain='neo') |
108
|
|
|
valid_eth_address = utils.format_withdraw_address(address=ETH_ADDRESS, blockchain='eth', web3_uri=WEB3_API_URL) |
109
|
|
|
self.assertEqual(valid_eth_address, '32c46323b51c977814E05EF5E258Ee4da0E4c3c3') |
110
|
|
|
with self.assertRaises(ValueError): |
111
|
|
|
utils.format_withdraw_address(address=ETH_CONTRACT, blockchain='eth', web3_uri=WEB3_API_URL) |
112
|
|
|
|