Skip to content

Commit 3fb4e66

Browse files
endlessgatesink772
andauthored
Refactor unit tests to use the requests-mock (#61)
* Remove dependency of t-bears docker container Co-authored-by: Jaechang Namgoong <jnamgoong@iconloop.com>
1 parent cb29368 commit 3fb4e66

36 files changed

+3147
-1065
lines changed

.travis.yml

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ dist: bionic
33
python:
44
- "3.7"
55

6-
services:
7-
- docker
8-
9-
install:
10-
- docker run -d -it --name tbears-container -p 9000:9000 iconloop/tbears:mainnet
11-
126
script:
137
- python setup.py test
148

iconsdk/providers/http_provider.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ def _make_post_request(request_url: str, data: dict, **kwargs) -> requests.Respo
9999
response = session.post(url=request_url, data=json.dumps(data), **kwargs)
100100
return response
101101

102+
def _make_id(self) -> int:
103+
return int(time())
104+
102105
def make_request(self, method: str, params=None, full_response: bool = False) -> Union[str, list, dict]:
103106
rpc_dict = {
104107
'jsonrpc': '2.0',
105108
'method': method,
106-
'id': int(time())
109+
'id': self._make_id()
107110
}
108111
if params:
109112
rpc_dict['params'] = params

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ eth-keyfile~=0.5.1
22
coincurve~=13.0.0
33
multipledispatch~=0.6.0
44
requests>=2.20.0
5+
requests-mock~=1.8.0

tests/api_call/test_call.py

+109-27
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,121 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import requests_mock
16+
import json
1517

16-
from unittest import TestCase, main
17-
18+
from unittest import main
19+
from unittest.mock import patch
1820
from iconsdk.builder.call_builder import CallBuilder
19-
from iconsdk.icon_service import IconService
20-
from iconsdk.providers.http_provider import HTTPProvider
21-
from iconsdk.wallet.wallet import KeyWallet
22-
from tests.example_config import BASE_DOMAIN_URL_V3_FOR_TEST, VERSION_FOR_TEST
23-
21+
from tests.api_send.test_send_super import TestSendSuper
22+
from tests.example_config import BASE_DOMAIN_URL_V3_FOR_TEST
2423

25-
class TestCall(TestCase):
2624

27-
@classmethod
28-
def setUpClass(cls):
29-
cls.wallet = KeyWallet.create()
30-
cls.address = cls.wallet.get_address()
31-
cls.to = "cx0000000000000000000000000000000000000001"
32-
cls.icon_service = IconService(HTTPProvider(BASE_DOMAIN_URL_V3_FOR_TEST, VERSION_FOR_TEST))
25+
@patch('iconsdk.providers.http_provider.HTTPProvider._make_id', return_value=1234)
26+
class TestCall(TestSendSuper):
3327

34-
def test_call(self):
28+
def test_call(self, _make_id):
3529
# with from
36-
test_call = CallBuilder().from_(self.address).to(self.to).method("getStepCosts").params("").build()
37-
result = self.icon_service.call(test_call)
38-
self.assertEqual(type(result), dict)
39-
40-
test_call = CallBuilder().from_(self.address).to(self.to).method("getStepCosts").build()
41-
result = self.icon_service.call(test_call)
42-
self.assertTrue(type(result), dict)
43-
44-
# without from
45-
test_call = CallBuilder().to(self.to).method("getStepCosts").params("").build()
46-
result = self.icon_service.call(test_call)
47-
self.assertEqual(type(result), dict)
30+
test_call = CallBuilder() \
31+
.from_(self.setting["from"]) \
32+
.to(self.setting["to"]) \
33+
.method("getStepCosts") \
34+
.params({}) \
35+
.build()
36+
37+
with requests_mock.Mocker() as m:
38+
expected_request = {
39+
"jsonrpc": "2.0",
40+
"method": "icx_call",
41+
"id": 1234,
42+
"params": {
43+
"to": self.setting["to"],
44+
"dataType": "call",
45+
"data": {
46+
"method": "getStepCosts"
47+
},
48+
"from": self.setting["from"]
49+
}
50+
}
51+
52+
m.post(f"{BASE_DOMAIN_URL_V3_FOR_TEST}/api/v3/", json=response_json)
53+
result = self.icon_service.call(test_call)
54+
actual_request = json.loads(m._adapter.last_request.text)
55+
self.assertEqual(expected_request, actual_request)
56+
self.assertTrue(result)
57+
58+
def test_call_without_params(self, _make_id):
59+
test_call = CallBuilder() \
60+
.from_(self.setting["from"]) \
61+
.to(self.setting["to"]) \
62+
.method("getStepCosts") \
63+
.build()
64+
65+
with requests_mock.Mocker() as m:
66+
expected_request = {
67+
"jsonrpc": "2.0",
68+
"method": "icx_call",
69+
"id": 1234,
70+
"params": {
71+
"to": self.setting["to"],
72+
"dataType": "call",
73+
"data": {
74+
"method": "getStepCosts"
75+
},
76+
"from": self.setting["from"]
77+
}
78+
}
79+
m.post(f"{BASE_DOMAIN_URL_V3_FOR_TEST}/api/v3/", json=response_json)
80+
result = self.icon_service.call(test_call)
81+
actual_request = json.loads(m._adapter.last_request.text)
82+
self.assertEqual(expected_request, actual_request)
83+
self.assertTrue(result)
84+
85+
def test_call_without_from(self, _make_id):
86+
test_call = CallBuilder() \
87+
.to(self.setting["to"]) \
88+
.method("getStepCosts") \
89+
.build()
90+
91+
with requests_mock.Mocker() as m:
92+
expected_request = {
93+
"jsonrpc": "2.0",
94+
"method": "icx_call",
95+
"id": 1234,
96+
"params": {
97+
"to": self.setting["to"],
98+
"dataType": "call",
99+
"data": {
100+
"method": "getStepCosts"
101+
},
102+
}
103+
}
104+
m.post(f"{BASE_DOMAIN_URL_V3_FOR_TEST}/api/v3/", json=response_json)
105+
result = self.icon_service.call(test_call)
106+
actual_request = json.loads(m._adapter.last_request.text)
107+
self.assertEqual(expected_request, actual_request)
108+
self.assertTrue(result)
109+
110+
111+
response_json = {
112+
"jsonrpc": "2.0",
113+
"result": {
114+
"default": "0x186a0",
115+
"contractCall": "0x61a8",
116+
"contractCreate": "0x3b9aca00",
117+
"contractUpdate": "0x5f5e1000",
118+
"contractDestruct": "-0x11170",
119+
"contractSet": "0x7530",
120+
"get": "0x0",
121+
"set": "0x140",
122+
"replace": "0x50",
123+
"delete": "-0xf0",
124+
"input": "0xc8",
125+
"eventLog": "0x64",
126+
"apiCall": "0x2710"
127+
},
128+
"id": 1234
129+
}
48130

49131

50132
if __name__ == "__main__":

0 commit comments

Comments
 (0)