Skip to content

Commit

Permalink
test: super fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 10, 2022
1 parent 1e71da1 commit e84a19f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 113 deletions.
12 changes: 11 additions & 1 deletion src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,17 @@ def send_transaction(self, txn: TransactionAPI) -> ReceiptAPI:
try:
txn_hash = self.web3.eth.send_raw_transaction(txn.serialize_transaction())
except ValueError as err:
raise self.get_virtual_machine_error(err) from err
vm_err = self.get_virtual_machine_error(err)

if "nonce too low" in str(vm_err):
# Add additional nonce information
new_err_msg = f"Nonce '{txn.nonce}' is too low"
raise VirtualMachineError(
base_err=vm_err.base_err, message=new_err_msg, code=vm_err.code
) from err

else:
raise vm_err from err

required_confirmations = (
txn.required_confirmations
Expand Down
80 changes: 46 additions & 34 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,43 +130,57 @@ def runner():
yield CliRunner()


@pytest.fixture(scope="session")
def networks_connected_to_tester():
with ape.networks.parse_network_choice("::test"):
@pytest.fixture
def networks_disconnected():
provider = ape.networks.active_provider
ape.networks.active_provider = None

try:
yield ape.networks
finally:
ape.networks.active_provider = provider


@pytest.fixture
def networks_disconnected(networks):
provider = networks.active_provider
networks.active_provider = None
yield networks
networks.active_provider = provider
def ethereum(networks):
return networks.ethereum


@pytest.fixture(autouse=True)
def eth_tester_provider():
if not ape.networks.active_provider or ape.networks.provider.name != "test":
with ape.networks.ethereum.local.use_provider("test") as provider:
yield provider
else:
yield ape.networks.provider


@pytest.fixture
def ethereum(networks):
return networks.ethereum
def networks_connected_to_tester(eth_tester_provider):
return eth_tester_provider.network_manager


@pytest.fixture(scope="session")
def eth_tester_provider(networks_connected_to_tester):
yield networks_connected_to_tester.provider
@pytest.fixture
def geth_provider(networks):
if not networks.active_provider or networks.provider.name != "geth":
with networks.ethereum.local.use_provider(
"geth", provider_settings={"uri": GETH_URI}
) as provider:
yield provider
else:
yield networks.provider


@pytest.fixture(autouse=True)
def isolation(chain):
if ape.networks.active_provider is None or ape.networks.provider.name == "geth":
# Isolation requires provider connected at start of test
# yield still required so that fixture is always an iterator.
yield
return
@contextmanager
def _isolation():
if ape.networks.active_provider is None:
raise AssertionError("Isolation should only be used with a connected provider.")

init_network_name = chain.provider.network.name
init_provider_name = chain.provider.name
init_network_name = ape.chain.provider.network.name
init_provider_name = ape.chain.provider.name

try:
snapshot = chain.snapshot()
snapshot = ape.chain.snapshot()
except APINotImplementedError:
# Provider not used or connected in test.
snapshot = None
Expand All @@ -176,19 +190,25 @@ def isolation(chain):
if (
snapshot is None
or ape.networks.active_provider is None
or chain.provider.network.name != init_network_name
or chain.provider.name != init_provider_name
or ape.chain.provider.network.name != init_network_name
or ape.chain.provider.name != init_provider_name
):
return

try:
chain.restore(snapshot)
ape.chain.restore(snapshot)
except UnknownSnapshotError:
# Assume snapshot removed for testing reasons
# or the provider was not needed to be connected for the test.
pass


@pytest.fixture(autouse=True)
def eth_tester_isolation(eth_tester_provider):
with _isolation():
yield


@pytest.fixture(scope="session")
def temp_config(config):
@contextmanager
Expand Down Expand Up @@ -216,11 +236,3 @@ def empty_data_folder():
ape.config.DATA_FOLDER = Path(mkdtemp()).resolve()
yield
ape.config.DATA_FOLDER = current_data_folder


@pytest.fixture(scope="session")
def geth(networks):
with networks.ethereum.local.use_provider(
"geth", provider_settings={"uri": GETH_URI}
) as provider:
yield provider
41 changes: 27 additions & 14 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import threading
import time
from contextlib import contextmanager
from distutils.dir_util import copy_tree
from pathlib import Path
from typing import Dict, Optional
Expand Down Expand Up @@ -87,22 +88,32 @@ def owner(test_accounts):
return test_accounts[2]


@pytest.fixture(scope="session")
def keyfile_account(sender, keyparams, temp_accounts_path, eth_tester_provider):
test_keyfile_path = temp_accounts_path / f"{ALIAS}.json"
yield _make_keyfile_account(temp_accounts_path, ALIAS, keyparams, sender)
@contextmanager
def _temp_keyfile_account(base_path: Path, alias: str, keyparams, sender):
test_keyfile_path = base_path / f"{alias}.json"

if test_keyfile_path.is_file():
test_keyfile_path.unlink()
if not test_keyfile_path.is_file():
account = _make_keyfile_account(base_path, alias, keyparams, sender)
else:
account = ape.accounts.load(ALIAS)

try:
yield account
finally:
if test_keyfile_path.is_file():
test_keyfile_path.unlink()

@pytest.fixture(scope="session")
def second_keyfile_account(sender, keyparams, temp_accounts_path, eth_tester_provider):
test_keyfile_path = temp_accounts_path / f"{ALIAS_2}.json"
yield _make_keyfile_account(temp_accounts_path, ALIAS_2, keyparams, sender)

if test_keyfile_path.is_file():
test_keyfile_path.unlink()
@pytest.fixture
def keyfile_account(sender, keyparams, temp_accounts_path):
with _temp_keyfile_account(temp_accounts_path, ALIAS, keyparams, sender) as account:
yield account


@pytest.fixture
def second_keyfile_account(sender, keyparams, temp_accounts_path):
with _temp_keyfile_account(temp_accounts_path, ALIAS_2, keyparams, sender) as account:
yield account


def _make_keyfile_account(base_path: Path, alias: str, params: Dict, funder):
Expand Down Expand Up @@ -161,12 +172,14 @@ def contract_container(


@pytest.fixture(params=("solidity", "vyper"))
def contract_instance(request, solidity_contract_instance, vyper_contract_instance):
def contract_instance(
eth_tester_provider, request, solidity_contract_instance, vyper_contract_instance
):
return solidity_contract_instance if request.param == "solidity" else vyper_contract_instance


@pytest.fixture
def ds_note_test_contract(vyper_contract_type, owner, eth_tester_provider):
def ds_note_test_contract(eth_tester_provider, vyper_contract_type, owner):
contract_type = ContractType.parse_raw(DS_NOTE_TEST_CONTRACT_TYPE)
contract_container = ContractContainer(contract_type=contract_type)
return contract_container.deploy(sender=owner)
Expand Down
18 changes: 8 additions & 10 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
APE_ACCOUNTS_PATH = "ape_accounts.accounts.KeyfileAccount"


@pytest.fixture(autouse=True, scope="module")
def connected(eth_tester_provider):
yield


@pytest.fixture
def signer(test_accounts):
return test_accounts[2]
Expand Down Expand Up @@ -59,6 +54,7 @@ def test_sign_eip712_message(signer):

def test_sign_message_with_prompts(runner, keyfile_account):
# "y\na\ny": yes sign, password, yes keep unlocked
start_nonce = keyfile_account.nonce
with runner.isolation(input="y\na\ny"):
message = encode_defunct(text="Hello Apes!")
signature = keyfile_account.sign_message(message)
Expand All @@ -69,11 +65,15 @@ def test_sign_message_with_prompts(runner, keyfile_account):
signature = keyfile_account.sign_message(message)
assert signature is None

# Nonce should not change from signing messages.
end_nonce = keyfile_account.nonce
assert start_nonce == end_nonce


def test_transfer(sender, receiver, eth_tester_provider):
initial_receiver_balance = receiver.balance
initial_sender_balance = sender.balance
value_str = "2 gwei"
value_str = "24 gwei"
value_int = convert(value_str, int)

receipt = sender.transfer(receiver, value_str)
Expand Down Expand Up @@ -117,9 +117,7 @@ def test_transfer_without_value_send_everything_true_with_low_gas(sender, receiv
sender.transfer(receiver, send_everything=True)


def test_transfer_without_value_send_everything_true_with_high_gas(
sender, receiver, eth_tester_provider
):
def test_transfer_without_value_send_everything_true_with_high_gas(sender, receiver):
initial_receiver_balance = receiver.balance
initial_sender_balance = sender.balance

Expand All @@ -140,7 +138,7 @@ def test_transfer_without_value_send_everything_true_with_high_gas(
sender.transfer(receiver, send_everything=True, gas=21000)


def test_transfer_with_value_send_everything_true(sender, receiver, isolation):
def test_transfer_with_value_send_everything_true(sender, receiver):
with pytest.raises(AccountsError, match="Cannot use 'send_everything=True' with 'VALUE'."):
sender.transfer(receiver, 1, send_everything=True)

Expand Down
6 changes: 0 additions & 6 deletions tests/functional/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
from ape_ethereum.transactions import Receipt, TransactionStatusEnum


@pytest.fixture(scope="module", autouse=True)
def connection(networks):
with networks.ethereum.local.use_provider("test"):
yield


@pytest.fixture
def contract_0(project_with_contract):
return project_with_contract.ApeContract0
Expand Down
Loading

0 comments on commit e84a19f

Please sign in to comment.