Skip to content

Commit 10397ac

Browse files
committed
More tests for method class and cleanup from PR #2383 comments
1 parent e6f306c commit 10397ac

File tree

3 files changed

+79
-33
lines changed

3 files changed

+79
-33
lines changed

tests/core/method-class/test_method.py

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
)
44
import pytest
55

6+
from eth_utils import (
7+
ValidationError,
8+
)
69
from eth_utils.toolz import (
710
compose,
811
)
@@ -69,7 +72,7 @@ def test_get_formatters_non_falsy_config_retrieval():
6972
first_formatter = (method.request_formatters(method_name).first,)
7073
all_other_formatters = method.request_formatters(method_name).funcs
7174
assert len(first_formatter + all_other_formatters) == 2
72-
# assert method.request_formatters('eth_nonmatching') == 'nonmatch'
75+
assert (method.request_formatters('eth_getBalance').first,) == first_formatter
7376

7477

7578
def test_input_munger_parameter_passthrough_matching_arity():
@@ -89,24 +92,68 @@ def test_input_munger_parameter_passthrough_mismatch_arity():
8992
method.input_munger(object(), ['first', 'second', 'third'], {})
9093

9194

92-
def test_input_munger_falsy_config_result_in_default_munger():
95+
def test_default_input_munger_with_no_input_parameters():
9396
method = Method(
94-
mungers=[],
97+
json_rpc_method='eth_method',
98+
)
99+
assert method.input_munger(object(), [], {}) == []
100+
101+
102+
@pytest.mark.parametrize('empty', ([], (), None), ids=['empty-list', 'empty-tuple', 'None'])
103+
def test_empty_input_munger_with_no_input_parameters(empty):
104+
method = Method(
105+
mungers=empty,
95106
json_rpc_method='eth_method',
96107
)
97108
assert method.input_munger(object(), [], {}) == []
98109

99110

100111
def test_default_input_munger_with_input_parameters():
101112
method = Method(
102-
mungers=[],
103113
json_rpc_method='eth_method',
104114
)
105115
assert method.input_munger(object(), [1], {}) == [1]
106116

107117

118+
@pytest.mark.parametrize('empty', ([], (), None), ids=['empty-list', 'empty-tuple', 'None'])
119+
def test_empty_input_mungers_with_input_parameters(empty):
120+
method = Method(
121+
mungers=empty,
122+
json_rpc_method='eth_method',
123+
)
124+
assert method.input_munger(object(), [1], {}) == [1]
125+
126+
127+
@pytest.mark.parametrize('empty', ([], (), None), ids=['empty-list', 'empty-tuple', 'None'])
128+
def test_empty_mungers_for_property_with_no_input_parameters(empty):
129+
method = Method(
130+
is_property=True,
131+
mungers=empty,
132+
json_rpc_method='eth_method',
133+
)
134+
assert method.input_munger(object(), [], {}) == ()
135+
136+
137+
def test_property_with_input_parameters_raises_ValidationError():
138+
method = Method(
139+
is_property=True,
140+
json_rpc_method='eth_method',
141+
)
142+
with pytest.raises(ValidationError, match='Parameters cannot be passed to a property'):
143+
method.input_munger(object(), [1], {})
144+
145+
146+
def test_property_with_mungers_raises_ValidationError():
147+
with pytest.raises(ValidationError, match='Mungers cannot be used with a property'):
148+
Method(
149+
is_property=True,
150+
mungers=[lambda m, z, y: 'success'],
151+
json_rpc_method='eth_method',
152+
)
153+
154+
108155
@pytest.mark.parametrize(
109-
"method_config,args,kwargs,expected_request_result,expected_result_formatters_len",
156+
"method_config,args,kwargs,expected_request_result",
110157
(
111158
(
112159
{
@@ -115,17 +162,15 @@ def test_default_input_munger_with_input_parameters():
115162
[],
116163
{},
117164
ValueError,
118-
2
119165
),
120166
(
121167
{
122168
'mungers': [],
123169
'json_rpc_method': 'eth_getBalance',
124170
},
125-
['unexpected_argument'],
171+
['only_the_first_argument_but_expects_two'],
126172
{},
127173
IndexError,
128-
2
129174
),
130175
(
131176
{
@@ -135,7 +180,6 @@ def test_default_input_munger_with_input_parameters():
135180
['0x0000000000000000000000000000000000000000', 3],
136181
{},
137182
('eth_getBalance', (('0x' + '00' * 20), "0x3")),
138-
2
139183
),
140184
(
141185
{
@@ -145,7 +189,6 @@ def test_default_input_munger_with_input_parameters():
145189
['0x0000000000000000000000000000000000000000', 3],
146190
{},
147191
('eth_getBalance', (('0x' + '00' * 20), "0x3")),
148-
2
149192
),
150193
(
151194
{
@@ -158,7 +201,6 @@ def test_default_input_munger_with_input_parameters():
158201
[1, 2, 3, ('0x' + '00' * 20)],
159202
{},
160203
('eth_getBalance', (('0x' + '00' * 20), "1")),
161-
2,
162204
),
163205
(
164206
{
@@ -171,7 +213,6 @@ def test_default_input_munger_with_input_parameters():
171213
[1, 2, 3, 4],
172214
{},
173215
TypeError,
174-
2,
175216
),
176217
(
177218
{
@@ -181,7 +222,6 @@ def test_default_input_munger_with_input_parameters():
181222
('0x0000000000000000000000000000000000000000', 3),
182223
{},
183224
('eth_getBalance', ('0x0000000000000000000000000000000000000000', '0x3')),
184-
2,
185225
),
186226
(
187227
{
@@ -190,7 +230,6 @@ def test_default_input_munger_with_input_parameters():
190230
('0x0000000000000000000000000000000000000000', 3),
191231
{},
192232
('eth_getBalance', ('0x0000000000000000000000000000000000000000', '0x3')),
193-
2,
194233
),
195234
(
196235
{
@@ -203,7 +242,6 @@ def test_default_input_munger_with_input_parameters():
203242
[('0x' + '00' * 20), 1, 2, 3],
204243
{},
205244
('eth_getBalance', (('0x' + '00' * 20), '1')),
206-
2,
207245
),
208246
(
209247
{
@@ -213,7 +251,6 @@ def test_default_input_munger_with_input_parameters():
213251
[],
214252
{},
215253
('eth_chainId', ()),
216-
2,
217254
),
218255
(
219256
{
@@ -223,16 +260,15 @@ def test_default_input_munger_with_input_parameters():
223260
[],
224261
{},
225262
('eth_chainId', ()),
226-
2,
227263
),
228264
),
229265
ids=[
230266
'raises-error-no-rpc-method',
231-
'test-unexpected-arg',
267+
'test-missing-argument',
232268
'test-rpc-method-as-string',
233269
'test-rpc-method-as-callable',
234270
'test-arg-munger',
235-
'test-munger-wrong-length-arg',
271+
'test-munger-too-many-args',
236272
'test-request-formatters-default-root-munger-explicit',
237273
'test-request-formatters-default-root-munger-implicit',
238274
'test-mungers-and-request-formatters',
@@ -245,7 +281,7 @@ def test_process_params(
245281
args,
246282
kwargs,
247283
expected_request_result,
248-
expected_result_formatters_len):
284+
):
249285

250286
if isclass(expected_request_result) and issubclass(expected_request_result, Exception):
251287
with pytest.raises(expected_request_result):
@@ -257,7 +293,9 @@ def test_process_params(
257293
assert request_params == expected_request_result
258294
first_formatter = (output_formatter[0].first,)
259295
all_other_formatters = output_formatter[0].funcs
260-
assert len(first_formatter + all_other_formatters) == expected_result_formatters_len
296+
297+
# the expected result formatters length is 2
298+
assert len(first_formatter + all_other_formatters) == 2
261299

262300

263301
def keywords(module, keyword_one, keyword_two):

tests/core/module-class/test_module.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,20 @@ def test_attach_methods_to_module(web3_with_external_modules):
5959
def test_attach_methods_with_mungers(web3_with_external_modules):
6060
w3 = web3_with_external_modules
6161

62+
# `method1` uses `eth_getBlockByNumber` but makes use of unique mungers
6263
w3.module1.attach_methods({
6364
'method1': Method('eth_getBlockByNumber', mungers=[
64-
lambda _method, block_id, f, _z: (block_id, f),
65-
lambda _m, block_id, _f: (block_id - 1,),
65+
lambda _method, block_id, full_transactions: (block_id, full_transactions),
66+
# take the user-provided `block_id` and subtract 1
67+
lambda _method, block_id, full_transactions: (block_id - 1, full_transactions),
6668
]),
6769
})
6870

69-
assert w3.eth.get_block(0)['baseFeePerGas'] == 1000000000
70-
assert w3.eth.get_block(1)['baseFeePerGas'] == 875000000
71+
assert w3.eth.get_block(0, False)['baseFeePerGas'] == 1000000000
72+
assert w3.eth.get_block(1, False)['baseFeePerGas'] == 875000000
7173

72-
# `method1` should take a higher block number than `eth_getBlockByNumber` due to mungers and no
73-
# other params should matter
74-
assert w3.module1.method1(1, False, '_is_never_used_')['baseFeePerGas'] == 1000000000
75-
assert w3.module1.method1(2, '_will_be_overridden_', None)['baseFeePerGas'] == 875000000
74+
# Testing the mungers work:
75+
# `method1` also calls 'eth_getBlockByNumber' but subtracts 1 from the user-provided `block_id`
76+
# due to the second munger. So, `0` from above is a `1` here and `1` is `2`.
77+
assert w3.module1.method1(1, False)['baseFeePerGas'] == 1000000000
78+
assert w3.module1.method1(2, False)['baseFeePerGas'] == 875000000

web3/method.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
)
1616
import warnings
1717

18+
from eth_utils import (
19+
ValidationError,
20+
)
1821
from eth_utils.curried import (
1922
to_tuple,
2023
)
@@ -62,6 +65,9 @@ def inner(args: Any) -> TReturn:
6265

6366

6467
def _set_mungers(mungers: Optional[Sequence[Munger]], is_property: bool) -> Sequence[Any]:
68+
if is_property and mungers:
69+
raise ValidationError("Mungers cannot be used with a property.")
70+
6571
return (
6672
mungers if mungers
6773
else [default_munger] if is_property
@@ -70,10 +76,9 @@ def _set_mungers(mungers: Optional[Sequence[Munger]], is_property: bool) -> Sequ
7076

7177

7278
def default_munger(_module: "Module", *args: Any, **kwargs: Any) -> Tuple[()]:
73-
if not args and not kwargs:
74-
return ()
75-
else:
76-
raise TypeError("Parameters passed to method without parameter mungers defined.")
79+
if args or kwargs:
80+
raise ValidationError("Parameters cannot be passed to a property.")
81+
return ()
7782

7883

7984
def default_root_munger(_module: "Module", *args: Any) -> List[Any]:

0 commit comments

Comments
 (0)