Skip to content

Commit

Permalink
test result formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Nov 11, 2019
1 parent 130ef90 commit 81a2bac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
50 changes: 36 additions & 14 deletions tests/core/method-class/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_method_accepts_callable_for_selector():

def test_method_selector_fn_accepts_str():
method = Method(
mungers=[],
mungers=None,
json_rpc_method='eth_method',
)
assert method.method_selector_fn() == 'eth_method'
Expand Down Expand Up @@ -69,7 +69,6 @@ def test_get_formatters_non_falsy_config_retrieval():
first_formatter = (method.request_formatters(method_name).first,)
all_other_formatters = method.request_formatters(method_name).funcs
assert len(first_formatter + all_other_formatters) == 2
# assert method.request_formatters('eth_nonmatching') == 'nonmatch'


def test_input_munger_parameter_passthrough_matching_arity():
Expand Down Expand Up @@ -107,15 +106,16 @@ def test_default_input_munger_with_input_parameters_exception():


@pytest.mark.parametrize(
"method_config,args,kwargs,expected_result",
"method_config,args,kwargs,expected_req_result,expected_result_formatters_len",
(
(
{
'mungers': [],
},
[],
{},
ValueError
ValueError,
2
),
(
{
Expand All @@ -124,7 +124,8 @@ def test_default_input_munger_with_input_parameters_exception():
},
['unexpected_argument'],
{},
TypeError
TypeError,
2
),
(
{
Expand All @@ -133,7 +134,8 @@ def test_default_input_munger_with_input_parameters_exception():
},
['0x0000000000000000000000000000000000000000', 3],
{},
('eth_getBalance', (('0x' + '00' * 20), "0x3"))
('eth_getBalance', (('0x' + '00' * 20), "0x3")),
2
),
(
{
Expand All @@ -142,7 +144,8 @@ def test_default_input_munger_with_input_parameters_exception():
},
['0x0000000000000000000000000000000000000000', 3],
{},
('eth_getBalance', (('0x' + '00' * 20), "0x3"))
('eth_getBalance', (('0x' + '00' * 20), "0x3")),
2
),
(
{
Expand All @@ -154,7 +157,8 @@ def test_default_input_munger_with_input_parameters_exception():
},
[1, 2, 3, ('0x' + '00' * 20)],
{},
('eth_getBalance', (('0x' + '00' * 20), "1"))
('eth_getBalance', (('0x' + '00' * 20), "1")),
2,
),
(
{
Expand All @@ -167,6 +171,7 @@ def test_default_input_munger_with_input_parameters_exception():
[1, 2, 3, 4],
{},
TypeError,
2,
),
(
{
Expand All @@ -175,7 +180,8 @@ def test_default_input_munger_with_input_parameters_exception():
},
('0x0000000000000000000000000000000000000000', 3),
{},
('eth_getBalance', ('0x0000000000000000000000000000000000000000', '0x3'))
('eth_getBalance', ('0x0000000000000000000000000000000000000000', '0x3')),
2,
),
(
{
Expand All @@ -187,7 +193,18 @@ def test_default_input_munger_with_input_parameters_exception():
},
[('0x' + '00' * 20), 1, 2, 3],
{},
('eth_getBalance', (('0x' + '00' * 20), '1'))
('eth_getBalance', (('0x' + '00' * 20), '1')),
2,
),
(
{
'mungers': None,
'json_rpc_method': 'eth_chainId',
},
[],
{},
('eth_chainId', ()),
2,
)
),
ids=[
Expand All @@ -199,22 +216,27 @@ def test_default_input_munger_with_input_parameters_exception():
'test-munger-wrong-length-arg',
'test-request-formatters',
'test-mungers-and-request-formatters',
'test-response-formatters',
]
)
def test_process_params(
method_config,
args,
kwargs,
expected_result,):
expected_req_result,
expected_result_formatters_len):

if isclass(expected_result) and issubclass(expected_result, Exception):
with pytest.raises(expected_result):
if isclass(expected_req_result) and issubclass(expected_req_result, Exception):
with pytest.raises(expected_req_result):
method = Method(**method_config)
req_params, output_formatter = method.process_params(object(), *args, **kwargs)
else:
method = Method(**method_config)
req_params, output_formatter = method.process_params(object(), *args, **kwargs)
assert req_params == expected_result
assert req_params == expected_req_result
first_formatter = (output_formatter[0].first,)
all_other_formatters = output_formatter[0].funcs
assert len(first_formatter + all_other_formatters) == expected_result_formatters_len


def keywords(module, keyword_one, keyword_two):
Expand Down
8 changes: 4 additions & 4 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,16 @@ def get_request_formatters(method_name):


def get_result_formatters(method_name):
formatters = combine_formatters((PYTHONIC_RESULT_FORMATTERS,), method_name)
formatters = combine_formatters(
(PYTHONIC_RESULT_FORMATTERS,),
method_name
)
attrdict_formatter = apply_formatter_if(is_dict and not_attrdict, AttributeDict.recursive)

return compose(*formatters, attrdict_formatter)


def get_error_formatters(method_name):
# Note error formatters work on the full response dict
# TODO - test this function
error_formatter_maps = ()
formatters = combine_formatters(error_formatter_maps, method_name)

return compose(*formatters)

0 comments on commit 81a2bac

Please sign in to comment.