Skip to content

Commit

Permalink
test: use ape test to test ape
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Oct 17, 2024
1 parent 3f2f301 commit 6f61772
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 100 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ jobs:
pip install .[test]
- name: Run Functional Tests
run: pytest tests/functional -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup
run: ape test tests/functional -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup

- name: Run Integration Tests
run: pytest tests/integration -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup
run: ape test tests/integration -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup

- name: Run Performance Tests
run: pytest tests/performance -s
run: ape test tests/performance -s

fuzzing:
runs-on: ubuntu-latest
Expand All @@ -123,4 +123,4 @@ jobs:
pip install .[test]
- name: Run Tests
run: pytest -m "fuzzing" --no-cov -s
run: ape test -m "fuzzing" --no-cov -s
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ norecursedirs = "projects"
# NOTE: 'no:ape_test' Prevents the ape plugin from activating on our tests
# And 'pytest_ethereum' is not used and causes issues in some environments.
addopts = """
-p no:ape_test
-p no:pytest_ethereum
"""

Expand Down
52 changes: 19 additions & 33 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def validate_cwd(start_dir):


@pytest.fixture
def project():
def example_project():
path = "tests/functional/data/contracts/ethereum/local"
with ape.project.temp_config(contracts_folder=path):
ape.project.clean()
Expand Down Expand Up @@ -135,7 +135,8 @@ def plugin_manager():


@pytest.fixture(scope="session")
def accounts():
def account_manager():
# NOTE: `accounts` fixture comes with ape_test as the test-accounts.
return ape.accounts


Expand All @@ -145,43 +146,28 @@ def compilers():


@pytest.fixture(scope="session")
def networks():
return ape.networks
def owner(accounts):
return accounts[0]


@pytest.fixture(scope="session")
def chain():
return ape.chain
def sender(accounts):
return accounts[1]


@pytest.fixture(scope="session")
def test_accounts(accounts):
return accounts.test_accounts
def receiver(accounts):
return accounts[2]


@pytest.fixture(scope="session")
def owner(test_accounts):
return test_accounts[0]
def not_owner(accounts):
return accounts[3]


@pytest.fixture(scope="session")
def sender(test_accounts):
return test_accounts[1]


@pytest.fixture(scope="session")
def receiver(test_accounts):
return test_accounts[2]


@pytest.fixture(scope="session")
def not_owner(test_accounts):
return test_accounts[3]


@pytest.fixture(scope="session")
def helper(test_accounts):
return test_accounts[4]
def helper(accounts):
return accounts[4]


@pytest.fixture(scope="session")
Expand All @@ -190,18 +176,18 @@ def mystruct_c():


@pytest.fixture
def signer(test_accounts):
return test_accounts[5]
def signer(accounts):
return accounts[5]


@pytest.fixture
def geth_account(test_accounts):
return test_accounts[6]
def geth_account(accounts):
return accounts[6]


@pytest.fixture
def geth_second_account(test_accounts):
return test_accounts[7]
def geth_second_account(accounts):
return accounts[7]


@pytest.fixture(scope="session")
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/geth/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_revert_out_of_gas_error_allow(geth_account, geth_second_account, geth_p


@geth_process_test
def test_revert_allow(test_accounts, geth_contract):
not_owner = test_accounts[0]
def test_revert_allow(accounts, geth_contract):
not_owner = accounts[0]

# 'sender' is not the owner so it will revert (with a message)
receipt = geth_contract.setNumber(100199, sender=not_owner, raise_on_revert=False)
Expand Down
90 changes: 42 additions & 48 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
)
from ape.types import AutoGasLimit
from ape.types.signatures import recover_signer
from ape.utils.testing import DEFAULT_NUMBER_OF_TEST_ACCOUNTS
from ape_accounts import (
KeyfileAccount,
generate_account,
Expand Down Expand Up @@ -362,7 +361,7 @@ def test_send_transaction_without_enough_funds_impersonated_account(
):
address = "0x4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97" # Not a test account!
impersonated_account = ImpersonatedAccount(raw_address=address)
accounts.test_accounts._impersonated_accounts[address] = impersonated_account
accounts._impersonated_accounts[address] = impersonated_account

# Basically, it failed anywhere else besides the AccountsError you get from not
# enough balance.
Expand All @@ -376,31 +375,29 @@ def test_send_transaction_sets_defaults(sender, receiver):
assert receipt.required_confirmations == 0


def test_accounts_splice_access(test_accounts):
a, b = test_accounts[:2]
assert a == test_accounts[0]
assert b == test_accounts[1]
c = test_accounts[-1]
assert c == test_accounts[len(test_accounts) - 1]
expected = (
(len(test_accounts) // 2) if len(test_accounts) % 2 == 0 else (len(test_accounts) // 2 + 1)
)
assert len(test_accounts[::2]) == expected
def test_accounts_splice_access(accounts):
a, b = accounts[:2]
assert a == accounts[0]
assert b == accounts[1]
c = accounts[-1]
assert c == accounts[len(accounts) - 1]
expected = (len(accounts) // 2) if len(accounts) % 2 == 0 else (len(accounts) // 2 + 1)
assert len(accounts[::2]) == expected


def test_accounts_address_access(owner, accounts):
assert accounts[owner.address] == owner


def test_accounts_address_access_conversion_fail(accounts):
def test_accounts_address_access_conversion_fail(account_manager):
with pytest.raises(
KeyError,
match=(
r"No account with ID 'FAILS'\. "
r"Do you have the necessary conversion plugins installed?"
),
):
_ = accounts["FAILS"]
_ = account_manager["FAILS"]


def test_accounts_address_access_not_found(accounts):
Expand All @@ -409,15 +406,15 @@ def test_accounts_address_access_not_found(accounts):
_ = accounts[address]


def test_test_accounts_address_access_conversion_fail(test_accounts):
def test_test_accounts_address_access_conversion_fail(accounts):
with pytest.raises(KeyError, match=r"No account with ID 'FAILS'"):
_ = test_accounts["FAILS"]
_ = accounts["FAILS"]


def test_test_accounts_address_access_not_found(test_accounts):
def test_test_accounts_address_access_not_found(accounts):
address = "0x1222262222222922222222222222222222222222"
with pytest.raises(KeyError, match=rf"No account with address '{address}'\."):
_ = test_accounts[address]
_ = accounts[address]


def test_accounts_contains(accounts, owner):
Expand Down Expand Up @@ -459,9 +456,9 @@ def test_impersonated_account_ignores_signature_check_on_txn(accounts, address):
account = ImpersonatedAccount(raw_address=address)

# Impersonate hack, since no providers in core actually support it.
accounts.test_accounts._impersonated_accounts[address] = account
other_0 = accounts.test_accounts[8]
other_1 = accounts.test_accounts[9]
accounts._impersonated_accounts[address] = account
other_0 = accounts[8]
other_1 = accounts[9]
txn = other_0.transfer(other_1, "1 gwei").transaction

# Hack in fake sender.
Expand Down Expand Up @@ -553,52 +550,49 @@ def test_unlock_with_wrong_passphrase_from_env(keyfile_account):
assert keyfile_account.locked


def test_unlock_and_reload(runner, accounts, keyfile_account, message):
def test_unlock_and_reload(runner, account_manager, keyfile_account, message):
"""
Tests against a condition where reloading after unlocking
would not honor unlocked state.
"""
keyfile_account.unlock(passphrase=PASSPHRASE)
reloaded_account = accounts.load(keyfile_account.alias)
reloaded_account = account_manager.load(keyfile_account.alias)

# y: yes, sign (note: unlocking makes the key available but is not the same as autosign).
with runner.isolation(input="y\n"):
signature = reloaded_account.sign_message(message)
assert keyfile_account.check_signature(message, signature)


def test_custom_num_of_test_accounts_config(test_accounts, project):
custom_number_of_test_accounts = 20
def test_custom_num_of_test_accounts_config(accounts, project):
custom_number_of_test_accounts = 25
test_config = {
"test": {
"number_of_accounts": custom_number_of_test_accounts,
}
}

assert len(test_accounts) == DEFAULT_NUMBER_OF_TEST_ACCOUNTS

with project.temp_config(**test_config):
assert len(test_accounts) == custom_number_of_test_accounts
assert len(accounts) == custom_number_of_test_accounts


def test_test_accounts_repr(test_accounts):
actual = repr(test_accounts)
assert all(a.address in actual for a in test_accounts)
def test_test_accounts_repr(accounts):
actual = repr(accounts)
assert all(a.address in actual for a in accounts)


def test_account_comparison_to_non_account(core_account):
# Before, would get a ConversionError.
assert core_account != "foo"


def test_create_account(test_accounts):
length_at_start = len(test_accounts)
created_account = test_accounts.generate_test_account()
def test_create_account(accounts):
length_at_start = len(accounts)
created_account = accounts.generate_test_account()

assert isinstance(created_account, TestAccount)
assert created_account.index == length_at_start

second_created_account = test_accounts.generate_test_account()
second_created_account = accounts.generate_test_account()

assert created_account.address != second_created_account.address
assert second_created_account.index == created_account.index + 1
Expand Down Expand Up @@ -627,42 +621,42 @@ def test_is_not_contract(owner, keyfile_account):
assert not keyfile_account.is_contract


def test_using_different_hd_path(test_accounts, project, eth_tester_provider):
def test_using_different_hd_path(accounts, project, eth_tester_provider):
test_config = {
"test": {
"hd_path": "m/44'/60'/0/0",
}
}

old_address = test_accounts[0].address
old_address = accounts[0].address
original_settings = eth_tester_provider.settings.model_dump(by_alias=True)
with project.temp_config(**test_config):
eth_tester_provider.update_settings(test_config["test"])
new_address = test_accounts[0].address
new_address = accounts[0].address

eth_tester_provider.update_settings(original_settings)
assert old_address != new_address


def test_using_random_mnemonic(test_accounts, project, eth_tester_provider):
def test_using_random_mnemonic(accounts, project, eth_tester_provider):
mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"
test_config = {"test": {"mnemonic": mnemonic}}

old_address = test_accounts[0].address
old_address = accounts[0].address
original_settings = eth_tester_provider.settings.model_dump(by_alias=True)
with project.temp_config(**test_config):
eth_tester_provider.update_settings(test_config["test"])
new_address = test_accounts[0].address
new_address = accounts[0].address

eth_tester_provider.update_settings(original_settings)
assert old_address != new_address


def test_iter_test_accounts(test_accounts):
test_accounts.reset()
accounts = list(iter(test_accounts))
def test_iter_test_accounts(accounts):
accounts.reset()
accounts = list(iter(accounts))
actual = len(accounts)
expected = len(test_accounts)
expected = len(accounts)
assert actual == expected


Expand Down Expand Up @@ -908,6 +902,6 @@ def test_import_account_from_private_key_insecure_passphrase(delete_account_afte
import_account_from_private_key(simple_alias, "simple", PRIVATE_KEY)


def test_load(accounts, keyfile_account):
account = accounts.load(keyfile_account.alias)
def test_load(account_manager, keyfile_account):
account = account_manager.load(keyfile_account.alias)
assert account == keyfile_account
18 changes: 9 additions & 9 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,21 @@ def _teardown_numb_acct_change(accounts):


@pytest.fixture
def no_accounts(accounts, empty_data_folder, project):
data = _setup_temp_acct_number_change(accounts, 0)
def no_accounts(account_manager, empty_data_folder, project):
data = _setup_temp_acct_number_change(account_manager, 0)
with project.temp_config(**data):
yield

_teardown_numb_acct_change(accounts)
_teardown_numb_acct_change(account_manager)


@pytest.fixture
def one_account(accounts, empty_data_folder, project, test_accounts):
data = _setup_temp_acct_number_change(accounts, 1)
def one_account(account_manager, empty_data_folder, project):
data = _setup_temp_acct_number_change(account_manager, 1)
with project.temp_config(**data):
yield test_accounts[0]
yield account_manager.test_accounts[0]

_teardown_numb_acct_change(accounts)
_teardown_numb_acct_change(account_manager)


def get_expected_account_str(acct):
Expand Down Expand Up @@ -371,9 +371,9 @@ def cmd(account):


@pytest.mark.parametrize("test_key", ("test", "TEST"))
def test_account_option_can_use_test_account(runner, test_accounts, test_key):
def test_account_option_can_use_test_account(runner, accounts, test_key):
index = 7
test_account = test_accounts[index]
test_account = accounts[index]

@click.command()
@account_option()
Expand Down
Loading

0 comments on commit 6f61772

Please sign in to comment.