Skip to content

Commit

Permalink
Feature/254o (neonevm#30)
Browse files Browse the repository at this point in the history
* Update test_single_client_activity.py

* code review fixes

* code review fixes

* refactoring and clean-up

* Update test_rpc_calls.py

* Update test_rpc_calls.py

* Update test_rpc_calls.py

* refactoring

* Create input_data.py

* refactoring

* refactoring

* code clean-up

* the remainder
  • Loading branch information
apetrovskiy authored Mar 11, 2022
1 parent 75ed3d5 commit 68d50b8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 52 deletions.
20 changes: 16 additions & 4 deletions integration/tests/basic/helpers/json_rpc_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import dataclasses
import requests
from requests.models import Response
from typing import Union
from typing import Type, Union

from integration.tests.basic.model.model import JsonRpcErrorResponse, JsonRpcRequest, JsonRpcResponse

Expand All @@ -20,17 +20,29 @@ def request_json_rpc(self, data: JsonRpcRequest) -> Response:
# TODO: deserialize subobject
@allure.step("deserializing response from JSON")
def deserialize_response(
self, response: Response
) -> Union[JsonRpcResponse, JsonRpcErrorResponse]:
self,
response: Response,
type: Type = None) -> Union[JsonRpcResponse, JsonRpcErrorResponse]:
str_data = self.stringify(response.json())
with allure.step("deserialized"):
if 'result' in str_data:
return JsonRpcResponse(**response.json())
# return JsonRpcResponse(**response.json())
return self.deserialize_successful_response(response=response,
type=type)
elif 'error' in str_data:
return JsonRpcErrorResponse(**response.json())
else:
return JsonRpcErrorResponse(**response.json())

def deserialize_successful_response(self, response: Response,
type: Type) -> JsonRpcResponse:
json_rpc_response = JsonRpcResponse(**response.json())
if type == None:
return json_rpc_response
subobject = type(**json_rpc_response.result)
json_rpc_response.result = subobject
return json_rpc_response

@allure.step("showing as JSON")
def stringify(self, data) -> str:
return str(data)
65 changes: 39 additions & 26 deletions integration/tests/basic/helpers/rpc_request_factory.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import allure
import random
from typing import List, Union
from integration.tests.basic.model.json_rpc_methods import JsonRpcMethods
from integration.tests.basic.model.model import JsonRpcRequest, JsonRpcRequestParams

ALLURE_RETURN_VALUE_DESCRIPTION = "the model built"
ALLURE_PARAMS_BUILT = "parameters built"


class RpcRequestFactory:
GET_BLOCK_BY_HASH = "eth_getBlockByHash"
GET_BLOCK_BY_NUMBER = "eth_getBlockByNumber"
BLOCK_NUMBER = "eth_blockNumber"
CALL = "eth_call"
ESTIMATE_GAS = "eth_estimateGas"
GAS_PRICE = "eth_gasPrice"
GET_LOGS = "eth_getLogs"
GET_BALANCE = "eth_getBalance"
GET_TRX_COUNT = "eth_getTransactionCount"
GET_CODE = "eth_getCode"
SEND_RAW_TRX = "eth_sendRawTransaction"
GET_TRX_BY_HASH = "eth_getTransactionByHash"
GET_TRX_RECEIPT = "eth_getTransactionReceipt"
GET_STORAGE_AT = "eth_getStorageAt"
WEB3_CLIENT_VERSION = "web3_clientVersion"
NET_VERSION = "net_version"

@classmethod
def get_random_value(cls) -> int:
return random.randint(0, 100)
Expand All @@ -17,129 +33,126 @@ def get_random_value(cls) -> int:
def get_block_by_hash(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(
id=cls.get_random_value(),
method=JsonRpcMethods.GET_BLOCK_BY_HASH.value,
params=params)
return JsonRpcRequest(id=cls.get_random_value(),
method=cls.GET_BLOCK_BY_HASH,
params=params)

@classmethod
def get_block_by_number(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(
id=cls.get_random_value(),
method=JsonRpcMethods.GET_BLOCK_BY_NUMBER.value,
params=params)
return JsonRpcRequest(id=cls.get_random_value(),
method=cls.GET_BLOCK_BY_NUMBER,
params=params)

@classmethod
def get_block_number(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.BLOCK_NUMBER.value,
method=cls.BLOCK_NUMBER,
params=params)

@classmethod
def get_call(cls, params: Union[List,
JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.CALL.value,
method=cls.CALL,
params=params)

@classmethod
def get_estimate_gas(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.ESTIMATE_GAS.value,
method=cls.ESTIMATE_GAS,
params=params)

@classmethod
def get_gas_price(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GAS_PRICE.value,
method=cls.GAS_PRICE,
params=params)

@classmethod
def get_logs(cls, params: Union[List,
JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_LOGS.value,
method=cls.GET_LOGS,
params=params)

@classmethod
def get_balance(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_BALANCE.value,
method=cls.GET_BALANCE,
params=params)

@classmethod
def get_trx_count(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_TRX_COUNT.value,
method=cls.GET_TRX_COUNT,
params=params)

@classmethod
def get_code(cls, params: Union[List,
JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_CODE.value,
method=cls.GET_CODE,
params=params)

@classmethod
def get_send_raw_trx(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.SEND_RAW_TRX.value,
method=cls.SEND_RAW_TRX,
params=params)

@classmethod
def get_trx_by_hash(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_TRX_BY_HASH.value,
method=cls.GET_TRX_BY_HASH,
params=params)

@classmethod
def get_trx_receipt(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_TRX_RECEIPT.value,
method=cls.GET_TRX_RECEIPT,
params=params)

@classmethod
def get_storage_at(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.GET_STORAGE_AT.value,
method=cls.GET_STORAGE_AT,
params=params)

@classmethod
def get_web3_client_version(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(
id=cls.get_random_value(),
method=JsonRpcMethods.WEB3_CLIENT_VERSION.value,
params=params)
return JsonRpcRequest(id=cls.get_random_value(),
method=cls.WEB3_CLIENT_VERSION,
params=params)

@classmethod
def get_net_version(
cls, params: Union[List, JsonRpcRequestParams]) -> JsonRpcRequest:
with allure.step(ALLURE_RETURN_VALUE_DESCRIPTION):
return JsonRpcRequest(id=cls.get_random_value(),
method=JsonRpcMethods.NET_VERSION.value,
method=cls.NET_VERSION,
params=params)
20 changes: 0 additions & 20 deletions integration/tests/basic/model/json_rpc_methods.py

This file was deleted.

2 changes: 1 addition & 1 deletion integration/tests/basic/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class JsonRpcErrorResponse:

# TODO: used only once
@dataclass
class BlockByHashResponse:
class BlockByResponse:
number: Union[int, None]
hash: Union[str, None]
parenthash: str
Expand Down
5 changes: 4 additions & 1 deletion integration/tests/basic/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from integration.tests.basic.helpers.rpc_request_params_factory import RpcRequestParamsFactory
from integration.tests.basic.helpers.basic import BasicTests
from integration.tests.basic.helpers.rpc_request_factory import RpcRequestFactory
from integration.tests.basic.model.model import JsonRpcResponse
from integration.tests.basic.model.model import BlockByResponse, JsonRpcResponse
from integration.tests.basic.model.tags import Tag
from integration.tests.basic.test_data.input_data import InputData
'''
Expand Down Expand Up @@ -51,6 +51,7 @@ def test_rpc_call_eth_getBlockByHash(self, prepare_accounts):

response = self.jsonrpc_requester.request_json_rpc(model)
actual_result = self.jsonrpc_requester.deserialize_response(response)
# , BlockByResponse)

assert actual_result.id == model.id, AssertMessage.WRONG_ID.value
assert self.assert_no_error_object(
Expand All @@ -73,6 +74,7 @@ def test_rpc_call_eth_getBlockByNumber_via_tags(self,

response = self.jsonrpc_requester.request_json_rpc(model)
actual_result = self.jsonrpc_requester.deserialize_response(response)
# , BlockByResponse)

assert actual_result.id == model.id, AssertMessage.WRONG_ID.value
# assert self.assert_no_error_object(
Expand All @@ -96,6 +98,7 @@ def test_rpc_call_eth_getBlockByNumber_via_numbers(self, prepare_accounts):

response = self.jsonrpc_requester.request_json_rpc(model)
actual_result = self.jsonrpc_requester.deserialize_response(response)
# , BlockByResponse)

assert actual_result.id == model.id, AssertMessage.WRONG_ID.value
assert self.assert_no_error_object(
Expand Down
2 changes: 2 additions & 0 deletions integration/tests/basic/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@ def test_rpc_call_eth_getTransactionReceipt(self, prepare_accounts):
actual_result), AssertMessage.CONTAINS_ERROR
assert self.assert_result_object(
actual_result), AssertMessage.DOES_NOT_CONTAIN_RESULT

result_object = self.jsonrpc_requester.deserialize_response

0 comments on commit 68d50b8

Please sign in to comment.