Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async formatters #1461

Merged
merged 7 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
180 changes: 90 additions & 90 deletions tests/core/method-class/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import pytest

from eth_utils.toolz import (
identity,
pipe,
compose,
)

from web3 import (
Expand All @@ -14,26 +13,27 @@
)
from web3.method import (
Method,
_apply_request_formatters,
default_root_munger,
)
from web3.module import (
ModuleV2,
apply_result_formatters,
)


def test_method_accepts_callable_for_selector():
method = Method(
mungers=[],
json_rpc_method=lambda *_: 'eth_method',
formatter_lookup_fn=''
)
assert method.method_selector_fn() == 'eth_method'


def test_method_selector_fn_accepts_str():
method = Method(
mungers=[],
mungers=None,
json_rpc_method='eth_method',
formatter_lookup_fn=''
)
assert method.method_selector_fn() == 'eth_method'

Expand All @@ -43,7 +43,6 @@ def test_method_selector_fn_invalid_arg():
method = Method(
mungers=[],
json_rpc_method=555555,
formatter_lookup_fn=''
)
method.method_selector_fn()

Expand All @@ -52,192 +51,193 @@ def test_get_formatters_default_formatter_for_falsy_config():
method = Method(
mungers=[],
json_rpc_method='eth_method',
formatter_lookup_fn=''
)

default_input_formatters, default_output_formatters = method.get_formatters('')

assert pipe(['a', 'b', 'c'], *default_input_formatters) == ['a', 'b', 'c']
assert pipe(['a', 'b', 'c'], *default_output_formatters) == ['a', 'b', 'c']
default_request_formatters = method.request_formatters(method.method_selector_fn())
default_result_formatters = method.result_formatters(method.method_selector_fn())
assert _apply_request_formatters(['a', 'b', 'c'], default_request_formatters) == ('a', 'b', 'c')
assert apply_result_formatters(
default_result_formatters, ['a', 'b', 'c']) == ['a', 'b', 'c']


def test_get_formatters_non_falsy_config_retrieval():
def formatter_lookup_fn(method):
if method == 'eth_method':
return 'match'
return 'nonmatch'
method = Method(
mungers=[],
json_rpc_method='eth_method',
formatter_lookup_fn=formatter_lookup_fn,
json_rpc_method='eth_getBalance',
)
assert method.get_formatters('eth_method') == 'match'
assert method.get_formatters('eth_nonmatching') == 'nonmatch'
method_name = method.method_selector_fn()
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():
method = Method(
mungers=[lambda m, z, y: ['success']],
json_rpc_method='eth_method',
formatter_lookup_fn=''
)
method.input_munger((object(), ['first', 'second'], {})) == 'success'
method.input_munger(object(), ['first', 'second'], {}) == 'success'


def test_input_munger_parameter_passthrough_mismatch_arity():
method = Method(
mungers=[lambda m, z, y: 'success'],
json_rpc_method='eth_method',
formatter_lookup_fn=''
)
with pytest.raises(TypeError):
method.input_munger((object(), ['first', 'second', 'third'], {}))
method.input_munger(object(), ['first', 'second', 'third'], {})


def test_input_munger_falsy_config_result_in_default_munger():
method = Method(
mungers=[],
json_rpc_method='eth_method',
formatter_lookup_fn=''
)
method.input_munger((object(), [], {})) == []
method.input_munger(object(), [], {}) == []


def test_default_input_munger_with_input_parameters_exception():
method = Method(
mungers=[],
json_rpc_method='eth_method',
formatter_lookup_fn=''
)
with pytest.raises(TypeError):
method.input_munger((object(), [1], {}))


def get_test_formatters(method):
def formatter(params):
return ['ok']

if method == 'eth_method':
return ([formatter], [identity])
method.input_munger(object(), [1], {})


@pytest.mark.parametrize(
"method_config,args,kwargs,expected_result",
"method_config,args,kwargs,expected_request_result,expected_result_formatters_len",
(
(
{
'mungers': [],
'formatter_lookup_fn': ''
},
[],
{},
ValueError
ValueError,
2
),
(
{
'mungers': [],
'json_rpc_method': 'eth_method',
'formatter_lookup_fn': ''
'json_rpc_method': 'eth_getBalance',
},
['unexpected_argument'],
{},
TypeError
TypeError,
2
),
(
{
'mungers': [],
'json_rpc_method': 'eth_method',
'formatter_lookup_fn': ''
'mungers': [default_root_munger],
'json_rpc_method': 'eth_getBalance',
},
[],
['0x0000000000000000000000000000000000000000', 3],
{},
('eth_method', ())
('eth_getBalance', (('0x' + '00' * 20), "0x3")),
2
),
(
{
'mungers': [],
'json_rpc_method': lambda *_: 'eth_method',
'formatter_lookup_fn': ''
'mungers': [default_root_munger],
'json_rpc_method': lambda *_: 'eth_getBalance',
},
[],
['0x0000000000000000000000000000000000000000', 3],
{},
('eth_method', ())
('eth_getBalance', (('0x' + '00' * 20), "0x3")),
2
),
(
{
'mungers': [
lambda m, x, y, z: [x, y],
lambda m, x, y: [x],
lambda m, x: [str(x)]],
'json_rpc_method': 'eth_method',
'formatter_lookup_fn': ''
lambda m, x, y, z, addr: [x, y, addr],
lambda m, x, y, addr: [x, addr],
lambda m, x, addr: [addr, str(x)]],
'json_rpc_method': 'eth_getBalance',
},
[1, 2, 3],
[1, 2, 3, ('0x' + '00' * 20)],
{},
('eth_method', ["1"])
('eth_getBalance', (('0x' + '00' * 20), "1")),
2,
),
(
{
'mungers': [
lambda m, x, y, z: [x, y],
lambda m, x, y: [x],
lambda m, x: [str(x)]],
'json_rpc_method': 'eth_method',
'formatter_lookup_fn': ''
'json_rpc_method': 'eth_getBalance',
},
[1, 2, 3, 4],
{},
TypeError,
2,
),
(
{
'mungers': [],
'json_rpc_method': 'eth_method',
'formatter_lookup_fn': get_test_formatters
'mungers': [default_root_munger],
'json_rpc_method': 'eth_getBalance',
},
[],
('0x0000000000000000000000000000000000000000', 3),
{},
('eth_method', ['ok'])
('eth_getBalance', ('0x0000000000000000000000000000000000000000', '0x3')),
2,
),
(
{
'mungers': [],
'json_rpc_method': 'eth_mismatch',
'formatter_lookup_fn': get_test_formatters
'mungers': [
lambda m, addr, x, y, z: [addr, x, y],
lambda m, addr, x, y: [addr, x],
lambda m, addr, x: [addr, str(x)]],
'json_rpc_method': 'eth_getBalance',
},
[],
[('0x' + '00' * 20), 1, 2, 3],
{},
('eth_mismatch', ())
('eth_getBalance', (('0x' + '00' * 20), '1')),
2,
),
(
{
'mungers': [
lambda m, x, y, z: [x, y],
lambda m, x, y: [x],
lambda m, x: [str(x)]],
'json_rpc_method': 'eth_method',
'formatter_lookup_fn': get_test_formatters
'mungers': None,
'json_rpc_method': 'eth_chainId',
},
[1, 2, 3],
[],
{},
('eth_method', ['ok'])
),
)
('eth_chainId', ()),
2,
)
),
ids=[
'raises-error-no-rpc-method',
'test-unexpected-arg',
'test-rpc-method-as-string',
'test-rpc-method-as-callable',
'test-arg-munger',
'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 @@ -251,14 +251,14 @@ class Success(Exception):
def return_exception_raising_formatter(method):
def formatter(params):
raise Success()
return ([formatter], [])
return compose(formatter)


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


@pytest.fixture
Expand All @@ -270,12 +270,12 @@ def dummy_w3():


def test_munger_class_method_access_raises_friendly_error():
with pytest.raises(TypeError):
with pytest.raises(TypeError, match='Direct calls to methods are not supported'):
FakeModule.method(1, 2)


def test_munger_arguments_by_keyword(dummy_w3):
with pytest.raises(Success):
dummy_w3.fake.method(keyword_one=1, keyword_two=2)
dummy_w3.fake.method(keyword_one=1, keyword_two='latest')
with pytest.raises(Success):
dummy_w3.fake.method(1, keyword_two=2)
Loading