Skip to content

Commit 24a5ec5

Browse files
committed
Updates to test suite after running for EELS backend
1 parent f368a19 commit 24a5ec5

33 files changed

+264
-115
lines changed

conftest.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import warnings
44

55
from eth_tester import (
6-
EELSBackend,
76
EthereumTester,
87
)
98
import pytest_asyncio
@@ -75,17 +74,15 @@ def _wait_for_transaction(w3, txn_hash, timeout=120):
7574

7675

7776
@pytest.fixture
78-
def w3():
79-
t = EthereumTester(backend=EELSBackend("cancun"))
80-
w3 = Web3(EthereumTesterProvider(t))
77+
def w3(backend_class):
78+
w3 = Web3(EthereumTesterProvider(EthereumTester(backend=backend_class())))
8179
w3.eth.default_account = w3.eth.accounts[0]
8280
return w3
8381

8482

8583
@pytest.fixture(scope="module")
86-
def w3_non_strict_abi():
87-
t = EthereumTester(backend=EELSBackend("cancun"))
88-
w3 = Web3(EthereumTesterProvider(t))
84+
def w3_non_strict_abi(backend_class):
85+
w3 = Web3(EthereumTesterProvider(EthereumTester(backend=backend_class())))
8986
w3.eth.default_account = w3.eth.accounts[0]
9087
w3.strict_bytes_type_checking = False
9188
return w3

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
setup,
55
)
66

7+
CUSTOM_ETH_TESTER_BRANCH = " @ git+https://github.com/fselmo/eth-tester@eels-backend"
8+
79
extras_require = {
810
"tester": [
911
# Note: ethereum-maintained libraries in this list should be added to the
1012
# `install_pre_releases.py` script.
11-
"eth-tester[py-evm]>=0.13.0b1,<0.14.0b1",
13+
f"eth-tester[py-evm]{CUSTOM_ETH_TESTER_BRANCH}",
14+
# if python version >= 3.10, install the eels backend:
15+
f"eth-tester[eels]{CUSTOM_ETH_TESTER_BRANCH} ; python_version >= '3.10'",
1216
"py-geth>=5.1.0",
1317
],
1418
"dev": [

tests/conftest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Type,
44
)
55

6+
from eth_tester import (
7+
PyEVMBackend,
8+
)
69
from eth_utils import (
710
event_signature_to_log_topic,
811
to_bytes,
@@ -19,6 +22,8 @@
1922
RequestMocker,
2023
)
2124

25+
SUPPORTED_ETH_TESTER_BACKENDS = {"pyevm", "eels"}
26+
2227

2328
@pytest.fixture(scope="module", params=[lambda x: to_bytes(hexstr=x), identity])
2429
def address_conversion_func(request):
@@ -28,6 +33,47 @@ def address_conversion_func(request):
2833
# --- session-scoped constants --- #
2934

3035

36+
def pytest_addoption(parser):
37+
parser.addoption(
38+
"--backend",
39+
action="store",
40+
default=None,
41+
help="Specify the backend for `EthereumTester` to use.",
42+
)
43+
44+
45+
def pytest_collection_modifyitems(config, items):
46+
backend_required_for_tests = any(
47+
"backend_class" in item.fixturenames for item in items
48+
)
49+
if backend_required_for_tests:
50+
backend = config.getoption("--backend")
51+
if not backend:
52+
raise pytest.UsageError(
53+
"This test run requires a specified a backend via the `--backend` "
54+
"command line option. Supported backends are: "
55+
f"{SUPPORTED_ETH_TESTER_BACKENDS}"
56+
)
57+
elif backend not in SUPPORTED_ETH_TESTER_BACKENDS:
58+
raise pytest.UsageError(f"Unsupported backend: `{backend}`.")
59+
60+
61+
@pytest.fixture(scope="session")
62+
def backend_class(request):
63+
backend = request.config.getoption("--backend")
64+
if backend == "pyevm":
65+
return PyEVMBackend
66+
elif backend == "eels":
67+
# conditionally import since eels is only supported on python >= 3.10
68+
from eth_tester.backends.eels import (
69+
EELSBackend,
70+
)
71+
72+
return EELSBackend
73+
else:
74+
raise ValueError("Invariant: Unreachable code path.")
75+
76+
3177
@pytest.fixture(scope="session")
3278
def emitter_contract_data():
3379
return EMITTER_CONTRACT_DATA

tests/core/conftest.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22

33
from eth_tester import (
4-
EELSBackend,
54
EthereumTester,
65
)
76
import pytest_asyncio
@@ -121,19 +120,16 @@ def __init__(self, a, b):
121120

122121

123122
@pytest_asyncio.fixture
124-
async def async_w3():
125-
t = EthereumTester(backend=EELSBackend("cancun"))
126-
provider = AsyncEthereumTesterProvider()
127-
provider.ethereum_tester = t
128-
w3 = AsyncWeb3(provider)
123+
async def async_w3(backend_class):
124+
w3 = AsyncWeb3(AsyncEthereumTesterProvider(EthereumTester(backend=backend_class())))
129125
accounts = await w3.eth.accounts
130126
w3.eth.default_account = accounts[0]
131127
return w3
132128

133129

134130
@pytest_asyncio.fixture
135-
async def async_w3_non_strict_abi():
136-
w3 = AsyncWeb3(AsyncEthereumTesterProvider())
131+
async def async_w3_non_strict_abi(backend_class):
132+
w3 = AsyncWeb3(AsyncEthereumTesterProvider(EthereumTester(backend=backend_class())))
137133
w3.strict_bytes_type_checking = False
138134
accounts = await w3.eth.accounts
139135
w3.eth.default_account = accounts[0]

tests/core/contracts/test_contract_attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)
77

88

9-
@pytest.fixture()
9+
@pytest.fixture
1010
def abi():
1111
return """[{"anonymous":false,"inputs":[{"indexed":false,"name":"value","type":"uint256"}],"name":"Increased","type":"function"}, {"anonymous":false,"inputs":[{"indexed":false,"name":"value","type":"uint256"}],"name":"Increased","type":"event"}]""" # noqa: E501
1212

tests/core/contracts/test_contract_caller_interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
)
99

1010

11-
@pytest.fixture()
11+
@pytest.fixture
1212
def address(w3):
1313
return w3.eth.accounts[1]
1414

1515

16-
@pytest.fixture()
16+
@pytest.fixture
1717
def transaction_dict(w3, address):
1818
return {
1919
"from": address,

tests/core/contracts/test_contract_example.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# of how to write unit tests with web3.py
33
import pytest
44

5+
from eth_tester import (
6+
EthereumTester,
7+
)
58
import pytest_asyncio
69

710
from web3 import (
@@ -15,8 +18,8 @@
1518

1619

1720
@pytest.fixture
18-
def tester_provider():
19-
return EthereumTesterProvider()
21+
def tester_provider(backend_class):
22+
return EthereumTesterProvider(EthereumTester(backend=backend_class()))
2023

2124

2225
@pytest.fixture
@@ -118,8 +121,10 @@ def async_eth_tester():
118121

119122

120123
@pytest_asyncio.fixture()
121-
async def async_w3():
122-
async_w3 = AsyncWeb3(AsyncEthereumTesterProvider())
124+
async def async_w3(backend_class):
125+
async_w3 = AsyncWeb3(
126+
AsyncEthereumTesterProvider(EthereumTester(backend=backend_class()))
127+
)
123128
accounts = await async_w3.eth.accounts
124129
async_w3.eth.default_account = accounts[0]
125130
return async_w3

tests/core/contracts/test_contract_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313

14-
@pytest.fixture()
14+
@pytest.fixture
1515
def math_addr(math_contract_factory, address_conversion_func):
1616
w3 = math_contract_factory.w3
1717
deploy_txn = math_contract_factory.constructor().transact(

tests/core/contracts/test_extracting_event_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929

3030

31-
@pytest.fixture()
31+
@pytest.fixture
3232
def dup_txn_receipt(w3, indexed_event_contract, wait_for_transaction, event_contract):
3333
emitter_fn = indexed_event_contract.functions.logTwoEvents
3434

tests/core/contracts/test_extracting_event_data_old.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
)
1010

1111

12+
@pytest.fixture
13+
def emitter(
14+
w3,
15+
emitter_contract_data,
16+
wait_for_transaction,
17+
wait_for_block,
18+
address_conversion_func,
19+
):
20+
emitter_contract_factory = w3.eth.contract(**emitter_contract_data)
21+
22+
wait_for_block(w3)
23+
deploy_txn_hash = emitter_contract_factory.constructor().transact({"gas": 10000000})
24+
deploy_receipt = wait_for_transaction(w3, deploy_txn_hash)
25+
contract_address = address_conversion_func(deploy_receipt["contractAddress"])
26+
27+
bytecode = w3.eth.get_code(contract_address)
28+
assert bytecode == emitter_contract_factory.bytecode_runtime
29+
_emitter = emitter_contract_factory(address=contract_address)
30+
assert _emitter.address == contract_address
31+
return _emitter
32+
33+
1234
@pytest.mark.parametrize(
1335
"contract_fn,event_name,call_args,expected_args",
1436
(

0 commit comments

Comments
 (0)