Skip to content

Commit

Permalink
Update documentation, cleanup, and more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Nov 13, 2019
1 parent c46f905 commit f55f563
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
1 change: 1 addition & 0 deletions newsfragments/1461.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pull formatting methods out of middleware
60 changes: 41 additions & 19 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 @@ -107,15 +107,16 @@ def test_default_input_munger_with_input_parameters_exception():


@pytest.mark.parametrize(
"method_config,args,kwargs,expected_result",
"method_config,args,kwargs,expected_request_result,expected_result_formatters_len",
(
(
{
'mungers': [],
},
[],
{},
ValueError
ValueError,
2
),
(
{
Expand All @@ -124,7 +125,8 @@ def test_default_input_munger_with_input_parameters_exception():
},
['unexpected_argument'],
{},
TypeError
TypeError,
2
),
(
{
Expand All @@ -133,7 +135,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 +145,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 +158,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 +172,7 @@ def test_default_input_munger_with_input_parameters_exception():
[1, 2, 3, 4],
{},
TypeError,
2,
),
(
{
Expand All @@ -175,7 +181,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 +194,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 +217,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_request_result,
expected_result_formatters_len):

if isclass(expected_result) and issubclass(expected_result, Exception):
with pytest.raises(expected_result):
if isclass(expected_request_result) and issubclass(expected_request_result, Exception):
with pytest.raises(expected_request_result):
method = Method(**method_config)
req_params, output_formatter = method.process_params(object(), *args, **kwargs)
request_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
request_params, output_formatter = method.process_params(object(), *args, **kwargs)
assert request_params == expected_request_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 All @@ -233,7 +256,7 @@ def formatter(params):

class FakeModule(ModuleV2):
method = Method(
'eth_getBalance',
'eth_method',
mungers=[keywords],
request_formatters=return_exception_raising_formatter)

Expand All @@ -252,8 +275,7 @@ def test_munger_class_method_access_raises_friendly_error():


def test_munger_arguments_by_keyword(dummy_w3):
addr = '0x' + '00' * 20
with pytest.raises(Success):
dummy_w3.fake.method(addr, keyword_two='latest')
dummy_w3.fake.method(keyword_one=1, keyword_two='latest')
with pytest.raises(Success):
dummy_w3.fake.method(addr, 'latest')
dummy_w3.fake.method(1, keyword_two=2)
5 changes: 4 additions & 1 deletion web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ 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)
Expand Down
4 changes: 2 additions & 2 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ def request_blocking(self, method, params, error_formatters=None):
Make a synchronous request using the provider
"""
response = self._make_request(method, params)
apply_error_formatters(error_formatters, response)

if "error" in response:
apply_error_formatters(error_formatters, response)
raise ValueError(response["error"])

return response['result']
Expand All @@ -119,9 +119,9 @@ async def coro_request(self, method, params, error_formatters=None):
Couroutine for making a request using the provider
"""
response = await self._coro_make_request(method, params)
apply_error_formatters(error_formatters, response)

if "error" in response:
apply_error_formatters(error_formatters, response)
raise ValueError(response["error"])

if response['result'] is None:
Expand Down
17 changes: 9 additions & 8 deletions web3/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ def getBalance_root_munger(module, account, block_identifier=None):
method inputs are passed to the method selection function, and the returned
method string is used.
3. request and response formatters are retrieved - formatters are retrieved
using the json rpc method string. The lookup function provided by the
formatter_lookup_fn configuration is passed the method string and is
expected to return a 2-tuple of lists containing the
request_formatters and response_formatters in that order.
e.g. ([*request_formatters], [*response_formatters]).
3. request and response formatters are set - formatters are retrieved
using the json rpc method string.
4. After the parameter processing from steps 1-3 the request is made using
the calling function returned by the module attribute ``retrieve_caller_fn``
Expand All @@ -95,8 +91,6 @@ def __init__(

self.json_rpc_method = json_rpc_method
self.mungers = mungers or [default_munger]
# TODO - decide if this request_formatters
# (and result_formatters) is worth keeping for testing
self.request_formatters = request_formatters or get_request_formatters
self.result_formatters = result_formatters or get_result_formatters
self.error_formatters = get_error_formatters
Expand All @@ -120,6 +114,13 @@ def method_selector_fn(self):
raise ValueError("``json_rpc_method`` config invalid. May be a string or function")

def input_munger(self, module, args, kwargs):
# This function takes the "root_munger" - the first munger in
# the list of mungers) and then pipes the return value of the
# previous munger as an argument to the next munger to return
# an array of arguments that have been formatted.
# See the test_process_params test
# in tests/core/method-class/test_method.py for an example
# with multiple mungers.
# TODO: Create friendly error output.
mungers_iter = iter(self.mungers)
root_munger = next(mungers_iter)
Expand Down

0 comments on commit f55f563

Please sign in to comment.