Skip to content

Commit c521167

Browse files
committed
Make name-to-address middleware tests more robust
- Add tests for sequence arg types for methods as well as dict arg type
1 parent ce34259 commit c521167

File tree

1 file changed

+91
-7
lines changed

1 file changed

+91
-7
lines changed

tests/core/middleware/test_name_to_address_middleware.py

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
EthereumTesterProvider,
2121
)
2222

23-
2423
NAME = "tester.eth"
2524

2625

@@ -42,16 +41,58 @@ def ens_mapped_address(_w3_setup):
4241
return _w3_setup.eth.accounts[0]
4342

4443

44+
@pytest.fixture
45+
def ens_addr_account_balance(ens_mapped_address, _w3_setup):
46+
return _w3_setup.eth.get_balance(ens_mapped_address)
47+
48+
4549
@pytest.fixture
4650
def w3(_w3_setup, ens_mapped_address):
4751
_w3_setup.ens = TempENS({NAME: ens_mapped_address})
4852
_w3_setup.middleware_onion.add(name_to_address_middleware(_w3_setup))
4953
return _w3_setup
5054

5155

52-
def test_pass_name_resolver(w3, ens_mapped_address):
53-
known_account_balance = w3.eth.get_balance(ens_mapped_address)
54-
assert w3.eth.get_balance(NAME) == known_account_balance
56+
@pytest.mark.parametrize(
57+
"params",
58+
(
59+
[NAME, "latest"],
60+
[NAME, 0],
61+
[NAME],
62+
),
63+
)
64+
def test_pass_name_resolver_get_balance_list_args(
65+
w3,
66+
ens_addr_account_balance,
67+
params,
68+
):
69+
assert w3.eth.get_balance(*params) == ens_addr_account_balance
70+
71+
72+
@pytest.mark.parametrize(
73+
"params",
74+
(
75+
{"value": 1, "from": NAME, "to": NAME, "gas": 21000},
76+
{
77+
"value": 1,
78+
"maxPriorityFeePerGas": 10**9,
79+
"from": NAME,
80+
"to": NAME,
81+
"gas": 21000,
82+
},
83+
{"value": 1, "to": NAME, "gas": 21000, "from": NAME},
84+
),
85+
)
86+
def test_pass_name_resolver_send_transaction_dict_args(
87+
w3,
88+
params,
89+
ens_mapped_address,
90+
):
91+
tx_hash = w3.eth.send_transaction(params)
92+
93+
tx = w3.eth.get_transaction(tx_hash)
94+
assert tx["from"] == ens_mapped_address
95+
assert tx["to"] == ens_mapped_address
5596

5697

5798
def test_fail_name_resolver(w3):
@@ -78,6 +119,11 @@ async def async_ens_mapped_address(_async_w3_setup):
78119
return accts[0]
79120

80121

122+
@pytest_asyncio.fixture
123+
async def async_ens_addr_account_balance(async_ens_mapped_address, _async_w3_setup):
124+
return await _async_w3_setup.eth.get_balance(async_ens_mapped_address)
125+
126+
81127
@pytest_asyncio.fixture
82128
async def async_w3(_async_w3_setup, async_ens_mapped_address):
83129
_async_w3_setup.ens = AsyncTempENS({NAME: async_ens_mapped_address})
@@ -86,9 +132,47 @@ async def async_w3(_async_w3_setup, async_ens_mapped_address):
86132

87133

88134
@pytest.mark.asyncio
89-
async def test_async_pass_name_resolver(async_w3, async_ens_mapped_address):
90-
known_account_balance = await async_w3.eth.get_balance(async_ens_mapped_address)
91-
assert await async_w3.eth.get_balance(NAME) == known_account_balance
135+
@pytest.mark.parametrize(
136+
"params",
137+
(
138+
[NAME, "latest"],
139+
[NAME, 0],
140+
[NAME],
141+
),
142+
)
143+
async def test_async_pass_name_resolver_get_balance_list_args(
144+
async_w3,
145+
async_ens_addr_account_balance,
146+
params,
147+
):
148+
assert await async_w3.eth.get_balance(*params) == async_ens_addr_account_balance
149+
150+
151+
@pytest.mark.asyncio
152+
@pytest.mark.parametrize(
153+
"params",
154+
(
155+
{"value": 1, "from": NAME, "to": NAME, "gas": 21000},
156+
{
157+
"value": 1,
158+
"maxPriorityFeePerGas": 10**9,
159+
"from": NAME,
160+
"to": NAME,
161+
"gas": 21000,
162+
},
163+
{"value": 1, "to": NAME, "gas": 21000, "from": NAME},
164+
),
165+
)
166+
async def test_async_pass_name_resolver_send_transaction_dict_args(
167+
async_w3,
168+
params,
169+
async_ens_mapped_address,
170+
):
171+
tx_hash = await async_w3.eth.send_transaction(params)
172+
173+
tx = await async_w3.eth.get_transaction(tx_hash)
174+
assert tx["from"] == async_ens_mapped_address
175+
assert tx["to"] == async_ens_mapped_address
92176

93177

94178
@pytest.mark.asyncio

0 commit comments

Comments
 (0)