Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Refactor tests and change network in tests to localhost (#270)
Browse files Browse the repository at this point in the history
* change test network to localhost

* fix tests with mock account deploy

* mock gateways for test
  • Loading branch information
andrew-fleming authored Oct 31, 2022
1 parent 8fc0012 commit 7225ad7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 36 deletions.
44 changes: 30 additions & 14 deletions tests/commands/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from nile.core.account import Account

KEY = "TEST_KEY"
NETWORK = "goerli"
NETWORK = "localhost"
MOCK_ADDRESS = "0x123"
MOCK_TARGET_ADDRESS = 0x987
MOCK_INDEX = 0
MAX_FEE = 10

Expand All @@ -25,9 +26,8 @@ def tmp_working_dir(monkeypatch, tmp_path):
return tmp_path


@patch("nile.core.account.Account.deploy")
@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test_account_init(mock_deploy):
mock_deploy.return_value = MOCK_ADDRESS, MOCK_INDEX
account = Account(KEY, NETWORK)

assert account.address == MOCK_ADDRESS
Expand Down Expand Up @@ -72,10 +72,12 @@ def test_deploy_accounts_register(mock_register, mock_deploy):
)


@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch("nile.core.account.get_contract_class", return_value="ContractClass")
@patch("nile.core.account.declare")
def test_declare(mock_declare, mock_get_class):
def test_declare(mock_declare, mock_get_class, mock_deploy):
account = Account(KEY, NETWORK)

signature = [999, 888]
nonce = 4
max_fee = 1
Expand Down Expand Up @@ -117,14 +119,16 @@ def test_declare(mock_declare, mock_get_class):
)


@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch("nile.core.account.get_nonce", return_value=0)
@patch("nile.core.account.call_or_invoke")
def test_send_nonce_call(mock_call, mock_nonce):
@patch(
"nile.core.account.Account._get_target_address", return_value=MOCK_TARGET_ADDRESS
)
def test_send_nonce_call(mock_target_address, mock_call, mock_nonce, mock_deploy):
account = Account(KEY, NETWORK)

# Instead of creating and populating a tmp .txt file, this uses the
# deployed account address (contract_address) as the target
account.send(account.address, "method", [1, 2, 3], max_fee=1)
account.send(MOCK_TARGET_ADDRESS, "method", [1, 2, 3], max_fee=1)

# 'call_or_invoke' is called once for '__execute__'
assert mock_call.call_count == 1
Expand All @@ -133,7 +137,11 @@ def test_send_nonce_call(mock_call, mock_nonce):
mock_nonce.assert_called_once_with(account.address, NETWORK)


def test_send_sign_transaction_and_execute():
@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch(
"nile.core.account.Account._get_target_address", return_value=MOCK_TARGET_ADDRESS
)
def test_send_sign_transaction_and_execute(mock_target_address, mock_deploy):
account = Account(KEY, NETWORK)

calldata = ["111", "222", "333"]
Expand All @@ -143,7 +151,7 @@ def test_send_sign_transaction_and_execute():
account.signer.sign_transaction = MagicMock(return_value=return_signature)

with patch("nile.core.account.call_or_invoke") as mock_call:
send_args = [account.address, "method", [1, 2, 3]]
send_args = [MOCK_TARGET_ADDRESS, "method", [1, 2, 3]]
nonce = 4
max_fee = 1
account.send(*send_args, max_fee, nonce)
Expand All @@ -170,7 +178,8 @@ def test_send_sign_transaction_and_execute():
)


def test_estimate_fee():
@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test_estimate_fee(mock_deploy):
account = Account(KEY, NETWORK)
# Mock send
account.send = MagicMock()
Expand All @@ -182,7 +191,8 @@ def test_estimate_fee():
)


def test_simulate():
@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test_simulate(mock_deploy):
account = Account(KEY, NETWORK)
# Mock send
account.send = MagicMock()
Expand All @@ -195,12 +205,18 @@ def test_simulate():


@pytest.mark.parametrize("query_type", ["estimate_fee", "simulate"])
@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch(
"nile.core.account.Account._get_target_address", return_value=MOCK_TARGET_ADDRESS
)
@patch("nile.core.account.get_nonce", return_value=0)
@patch("nile.core.account.call_or_invoke")
def test_execute_query(mock_call, mock_nonce, query_type):
def test_execute_query(
mock_call, mock_nonce, mock_target_address, mock_deploy, query_type
):
account = Account(KEY, NETWORK)

send_args = [account.address, "method", [1, 2, 3]]
send_args = [MOCK_TARGET_ADDRESS, "method", [1, 2, 3]]
calldata = ["111", "222", "333"]
sig_r, sig_s = [999, 888]
return_signature = [calldata, sig_r, sig_s]
Expand Down
25 changes: 12 additions & 13 deletions tests/commands/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
from nile.utils.debug import _abi_to_build_path, _locate_error_lines_with_abis, debug

MOCK_HASH = 1234
NETWORK = "goerli"
ERROR_MESSAGE = "Error at pc=0:1:\nAn ASSERT_EQ instruction failed: 3 != 0."
NETWORK = "localhost"
DEBUG_ADDRESS = "0x07826b88e404632d9835ab1ec2076c6cf1910e6ecb2ed270647fc211ff55e76f"
ABI_PATH = "path/to/abis/test_contract.json"
ALIAS = "contract_alias"
MOCK_FILE = 123


def mocked_json_message(arg):
return {"tx_status": arg, "tx_failure_reason": {"error_message": ERROR_MESSAGE}}
ACCEPTED_OUT = b'{"tx_status": "ACCEPTED_ON_L2"}'
REJECTED_OUT = b'{"tx_failure_reason": {"error_message": "E"}, "tx_status": "REJECTED"}'


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -65,22 +62,24 @@ def test__locate_error_lines_with_abis_misformatted_line(mock_path, caplog):


@pytest.mark.parametrize(
"args, expected",
"output, expected",
[
("ACCEPTED", "No error in transaction"),
("REJECTED", "The transaction was rejected"),
("REJECTED", ERROR_MESSAGE),
(ACCEPTED_OUT, "No error in transaction"),
(REJECTED_OUT, "The transaction was rejected"),
],
)
@pytest.mark.xfail(
sys.version_info >= (3, 10),
reason="Issue in cairo-lang. "
"See https://github.com/starkware-libs/cairo-lang/issues/27",
)
@patch("nile.utils.debug.json.loads")
def test_debug_feedback_with_message(mock_json, caplog, args, expected):
@patch("nile.utils.debug.subprocess.check_output")
@patch("nile.utils.debug.GATEWAYS", return_value="123")
def test_debug_feedback_with_message(
mock_gateway, mock_output, output, expected, caplog
):
logging.getLogger().setLevel(logging.INFO)
mock_json.return_value = mocked_json_message(args)
mock_output.return_value = output

debug(MOCK_HASH, NETWORK)

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def tmp_working_dir(monkeypatch, tmp_path):
SENDER = "0x1234"
CONTRACT = "contract"
SIGNATURE = [123, 321]
NETWORK = "goerli"
NETWORK = "localhost"
ALIAS = "alias"
PATH = "path"
MAX_FEE = 432
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def tmp_working_dir(monkeypatch, tmp_path):


CONTRACT = "contract"
NETWORK = "goerli"
NETWORK = "localhost"
ALIAS = "alias"
ABI = f"{ABIS_DIRECTORY}/{CONTRACT}.json"
ABI_OVERRIDE = f"{ABIS_DIRECTORY}/override.json"
Expand Down
28 changes: 21 additions & 7 deletions tests/commands/test_get_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from tests.mocks.mock_response import MockResponse

NETWORK = "goerli"
NETWORK = "localhost"
GATEWAYS = {"localhost": "http://127.0.0.1:5050/"}
PUBKEYS = [
883045738439352841478194533192765345509759306772397516907181243450667673002,
Expand All @@ -25,6 +25,10 @@
INDEXES = [0, 1]
ALIASES = ["TEST_KEY", "TEST_KEY_2"]


MOCK_ADDRESS = "0x123"
MOCK_INDEX = 0

MOCK_ACCOUNTS = {
PUBKEYS[0]: {
"address": ADDRESSES[0],
Expand Down Expand Up @@ -70,7 +74,10 @@ def mock_subprocess():
([ALIASES[1], PUBKEYS[1]]),
],
)
def test__check_and_return_account_with_matching_keys(private_keys, public_keys):
@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test__check_and_return_account_with_matching_keys(
mock_deploy, private_keys, public_keys
):
# Check matching public/private keys
account = _check_and_return_account(private_keys, public_keys, NETWORK)

Expand All @@ -84,15 +91,19 @@ def test__check_and_return_account_with_matching_keys(private_keys, public_keys)
([ALIASES[1], PUBKEYS[0]]),
],
)
def test__check_and_return_account_with_mismatching_keys(private_keys, public_keys):
@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test__check_and_return_account_with_mismatching_keys(
mock_deploy, private_keys, public_keys
):
# Check mismatched public/private keys
with pytest.raises(AssertionError) as err:
_check_and_return_account(private_keys, public_keys, NETWORK)

assert "Signer pubkey does not match deployed pubkey" in str(err.value)


def test_get_accounts_no_activated_accounts_feedback(capsys):
@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test_get_accounts_no_activated_accounts_feedback(mock_deploy, capsys):
get_accounts(NETWORK)
# This test uses capsys in order to test the print statements (instead of logging)
captured = capsys.readouterr()
Expand All @@ -106,10 +117,11 @@ def test_get_accounts_no_activated_accounts_feedback(capsys):
)


@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch("nile.utils.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS)))
@patch("nile.utils.get_accounts.open", MagicMock())
@patch("nile.utils.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS))
def test_get_accounts_activated_accounts_feedback(caplog):
def test_get_accounts_activated_accounts_feedback(mock_deploy, caplog):
logging.getLogger().setLevel(logging.INFO)

# Default argument
Expand All @@ -126,10 +138,11 @@ def test_get_accounts_activated_accounts_feedback(caplog):
assert "\n🚀 Successfully retrieved deployed accounts" in caplog.text


@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch("nile.utils.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS)))
@patch("nile.utils.get_accounts.open", MagicMock())
@patch("nile.utils.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS))
def test_get_accounts_with_keys():
def test_get_accounts_with_keys(mock_deploy):

with patch(
"nile.utils.get_accounts._check_and_return_account"
Expand Down Expand Up @@ -179,11 +192,12 @@ def test_get_predeployed_accounts(mock_response, mock_return_account, mock_gatew
assert len(result) == len(JSON_DATA)


@patch("nile.core.account.Account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch("nile.common.get_gateway", return_value=GATEWAYS)
@patch("nile.utils.get_accounts._check_and_return_account")
@patch("requests.get", return_value=MockResponse(JSON_DATA, 200))
def test_get_predeployed_accounts_logging(
mock_response, mock_return_account, mock_gateways, caplog
mock_response, mock_return_account, mock_gateways, mock_deploy, caplog
):
# make logs visible to test
logger = logging.getLogger()
Expand Down

0 comments on commit 7225ad7

Please sign in to comment.