|
12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | 13 | # See the License for the specific language governing permissions and
|
14 | 14 | # limitations under the License.
|
| 15 | +import requests_mock |
| 16 | +import json |
15 | 17 |
|
16 |
| -from unittest import TestCase, main |
17 |
| - |
| 18 | +from unittest import main |
| 19 | +from unittest.mock import patch |
18 | 20 | 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 |
24 | 23 |
|
25 |
| -class TestCall(TestCase): |
26 | 24 |
|
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): |
33 | 27 |
|
34 |
| - def test_call(self): |
| 28 | + def test_call(self, _make_id): |
35 | 29 | # 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 | +} |
48 | 130 |
|
49 | 131 |
|
50 | 132 | if __name__ == "__main__":
|
|
0 commit comments