-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_chainbase_api.py
74 lines (50 loc) · 2.33 KB
/
test_chainbase_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import unittest
import requests
import requests_mock
from chainbase_sdk.chainbase_api import DEFAULT_ERROR, ChainbaseAPI
@requests_mock.mock()
class TestChainbaseAPI(unittest.TestCase):
def setUp(self):
self.url = "http://api.example.com"
self.body = {"query": "test"}
self.api = ChainbaseAPI(self.url, "api-key")
def test_post_success(self, requests_mock_obj):
response_body = {"code": 0, "data": {"meta": {}}}
requests_mock_obj.post(self.url, json=response_body)
response = self.api.post(self.body)
self.assertEqual(response, {"code": 0, "data": {"meta": {}}})
def test_post_error_code(self, requests_mock_obj):
response_body = {"code": 1, "error": "error"}
requests_mock_obj.post(self.url, json=response_body)
with self.assertRaises(Exception) as err:
self.api.post(self.body)
assert "error" in str(err.exception)
response_body = {"code": 1, "error": None}
requests_mock_obj.post(self.url, json=response_body)
with self.assertRaises(Exception) as err:
self.api.post(self.body)
assert DEFAULT_ERROR in str(err.exception)
response_body = {"code": 1}
requests_mock_obj.post(self.url, json=response_body)
with self.assertRaises(Exception) as err:
self.api.post(self.body)
assert DEFAULT_ERROR in str(err.exception)
def test_post_no_data(self, requests_mock_obj):
response_body = {"code": 0, "data": None}
requests_mock_obj.post(self.url, json=response_body)
with self.assertRaises(Exception) as err:
self.api.post(self.body)
assert DEFAULT_ERROR in str(err.exception)
def test_post_err_msg(self, requests_mock_obj):
response_body = {"code": 0, "data": {"err_msg": "error"}}
requests_mock_obj.post(self.url, json=response_body)
with self.assertRaises(Exception) as err:
self.api.post(self.body)
assert "error" in str(err.exception)
def test_post_http_error(self, requests_mock_obj):
requests_mock_obj.post(self.url, status_code=404)
with self.assertRaises(requests.exceptions.HTTPError) as err:
self.api.post(self.body)
assert "404 Client Error: None for url: http://api.example.com/" in str(
err.exception
)