From 7225ad78407fdc0350ec96cc7b6a8c531dae77e5 Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Mon, 31 Oct 2022 15:28:27 -0400 Subject: [PATCH] Refactor tests and change network in tests to `localhost` (#270) * change test network to localhost * fix tests with mock account deploy * mock gateways for test --- tests/commands/test_account.py | 44 ++++++++++++++++++++--------- tests/commands/test_debug.py | 25 ++++++++-------- tests/commands/test_declare.py | 2 +- tests/commands/test_deploy.py | 2 +- tests/commands/test_get_accounts.py | 28 +++++++++++++----- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/tests/commands/test_account.py b/tests/commands/test_account.py index cce9cce0..c29bd63b 100644 --- a/tests/commands/test_account.py +++ b/tests/commands/test_account.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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"] @@ -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) @@ -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() @@ -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() @@ -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] diff --git a/tests/commands/test_debug.py b/tests/commands/test_debug.py index 2977971a..629f8f40 100644 --- a/tests/commands/test_debug.py +++ b/tests/commands/test_debug.py @@ -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) @@ -65,11 +62,10 @@ 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( @@ -77,10 +73,13 @@ def test__locate_error_lines_with_abis_misformatted_line(mock_path, caplog): 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) diff --git a/tests/commands/test_declare.py b/tests/commands/test_declare.py index e1861b66..07a77b64 100644 --- a/tests/commands/test_declare.py +++ b/tests/commands/test_declare.py @@ -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 diff --git a/tests/commands/test_deploy.py b/tests/commands/test_deploy.py index 39fbfa70..56bde6fb 100644 --- a/tests/commands/test_deploy.py +++ b/tests/commands/test_deploy.py @@ -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" diff --git a/tests/commands/test_get_accounts.py b/tests/commands/test_get_accounts.py index 63dec290..aa9eee65 100644 --- a/tests/commands/test_get_accounts.py +++ b/tests/commands/test_get_accounts.py @@ -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, @@ -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], @@ -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) @@ -84,7 +91,10 @@ 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) @@ -92,7 +102,8 @@ def test__check_and_return_account_with_mismatching_keys(private_keys, public_ke 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() @@ -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 @@ -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" @@ -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()