From 36d1e3c26eb6db931b2aa65cae936d850cd467ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBy=C5=BAniewski?= Date: Fri, 7 Mar 2025 12:16:34 +0100 Subject: [PATCH] refactor: deduplicate fake_wallet and correctly create Mock --- tests/unit_tests/conftest.py | 6 + .../extrinsics/asyncex/test_commit_reveal.py | 19 +- .../extrinsics/asyncex/test_registration.py | 47 ++--- .../extrinsics/asyncex/test_root.py | 59 +++--- .../extrinsics/asyncex/test_transfer.py | 28 +-- .../extrinsics/asyncex/test_weights.py | 42 ++--- .../extrinsics/test_commit_reveal.py | 25 ++- .../extrinsics/test_commit_weights.py | 8 +- .../unit_tests/extrinsics/test_set_weights.py | 20 +- tests/unit_tests/extrinsics/test_transfer.py | 9 +- tests/unit_tests/extrinsics/test_unstaking.py | 6 +- tests/unit_tests/test_async_subtensor.py | 50 +++-- tests/unit_tests/test_subnets.py | 9 +- tests/unit_tests/test_subtensor.py | 48 ++--- tests/unit_tests/test_subtensor_extended.py | 173 +++++++++--------- tests/unit_tests/utils/test_utils.py | 20 +- 16 files changed, 242 insertions(+), 327 deletions(-) diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index b1293c4e2c..9fb5d19e41 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -1,5 +1,6 @@ import pytest from aioresponses import aioresponses +from bittensor_wallet import Wallet import bittensor.core.subtensor @@ -30,6 +31,11 @@ def subtensor(mock_substrate): return bittensor.core.subtensor.Subtensor() +@pytest.fixture +def fake_wallet(mocker): + return mocker.Mock(spec_set=Wallet) + + @pytest.fixture def mock_get_external_ip(mocker): mocked = mocker.Mock( diff --git a/tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py b/tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py index 5dc076f1c4..4dec244651 100644 --- a/tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py +++ b/tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py @@ -4,7 +4,6 @@ import pytest import torch import numpy as np -from bittensor_wallet import Wallet @pytest.fixture @@ -41,10 +40,9 @@ def hyperparams(): @pytest.mark.asyncio -async def test_do_commit_reveal_v3_success(mocker, subtensor): +async def test_do_commit_reveal_v3_success(mocker, subtensor, fake_wallet): """Test successful commit-reveal with wait for finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit = b"fake_commit" fake_reveal_round = 1 @@ -88,10 +86,9 @@ async def test_do_commit_reveal_v3_success(mocker, subtensor): @pytest.mark.asyncio -async def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): +async def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor, fake_wallet): """Test commit-reveal fails due to an error in submission.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit = b"fake_commit" fake_reveal_round = 1 @@ -150,11 +147,10 @@ async def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): @pytest.mark.asyncio async def test_commit_reveal_v3_extrinsic_success_with_torch( - mocker, subtensor, hyperparams + mocker, subtensor, hyperparams, fake_wallet ): """Test successful commit-reveal with torch tensors.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) @@ -231,11 +227,10 @@ async def test_commit_reveal_v3_extrinsic_success_with_torch( @pytest.mark.asyncio async def test_commit_reveal_v3_extrinsic_success_with_numpy( - mocker, subtensor, hyperparams + mocker, subtensor, hyperparams, fake_wallet ): """Test successful commit-reveal with numpy arrays.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = np.array([1, 2, 3], dtype=np.int64) fake_weights = np.array([0.1, 0.2, 0.7], dtype=np.float32) @@ -279,11 +274,10 @@ async def test_commit_reveal_v3_extrinsic_success_with_numpy( @pytest.mark.asyncio async def test_commit_reveal_v3_extrinsic_response_false( - mocker, subtensor, hyperparams + mocker, subtensor, hyperparams, fake_wallet ): """Test unsuccessful commit-reveal with torch.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) @@ -337,10 +331,9 @@ async def test_commit_reveal_v3_extrinsic_response_false( @pytest.mark.asyncio -async def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor): +async def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor, fake_wallet): """Test exception handling in commit-reveal.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] diff --git a/tests/unit_tests/extrinsics/asyncex/test_registration.py b/tests/unit_tests/extrinsics/asyncex/test_registration.py index b102602a68..703e1df4cb 100644 --- a/tests/unit_tests/extrinsics/asyncex/test_registration.py +++ b/tests/unit_tests/extrinsics/asyncex/test_registration.py @@ -1,15 +1,13 @@ import pytest -from bittensor_wallet import Wallet from bittensor.core import async_subtensor from bittensor.core.extrinsics.asyncex import registration as async_registration @pytest.mark.asyncio -async def test_do_pow_register_success(subtensor, mocker): +async def test_do_pow_register_success(subtensor, fake_wallet, mocker): """Tests successful PoW registration.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkeypub.ss58_address = "coldkey_ss58" fake_pow_result = mocker.Mock( @@ -66,10 +64,9 @@ async def test_do_pow_register_success(subtensor, mocker): @pytest.mark.asyncio -async def test_do_pow_register_failure(subtensor, mocker): +async def test_do_pow_register_failure(subtensor, fake_wallet, mocker): """Tests failed PoW registration.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkeypub.ss58_address = "coldkey_ss58" fake_pow_result = mocker.Mock( @@ -121,10 +118,9 @@ async def test_do_pow_register_failure(subtensor, mocker): @pytest.mark.asyncio -async def test_do_pow_register_no_waiting(subtensor, mocker): +async def test_do_pow_register_no_waiting(subtensor, fake_wallet, mocker): """Tests PoW registration without waiting for inclusion or finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkeypub.ss58_address = "coldkey_ss58" fake_pow_result = mocker.Mock( @@ -166,10 +162,9 @@ async def test_do_pow_register_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_register_extrinsic_success(subtensor, mocker): +async def test_register_extrinsic_success(subtensor, fake_wallet, mocker): """Tests successful registration.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkey.ss58_address = "coldkey_ss58" @@ -221,10 +216,9 @@ async def test_register_extrinsic_success(subtensor, mocker): @pytest.mark.asyncio -async def test_register_extrinsic_success_with_cuda(subtensor, mocker): +async def test_register_extrinsic_success_with_cuda(subtensor, fake_wallet, mocker): """Tests successful registration with CUDA enabled.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkey.ss58_address = "coldkey_ss58" @@ -278,10 +272,9 @@ async def test_register_extrinsic_success_with_cuda(subtensor, mocker): @pytest.mark.asyncio -async def test_register_extrinsic_failed_with_cuda(subtensor, mocker): +async def test_register_extrinsic_failed_with_cuda(subtensor, fake_wallet, mocker): """Tests failed registration with CUDA enabled.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkey.ss58_address = "coldkey_ss58" @@ -319,11 +312,9 @@ async def test_register_extrinsic_failed_with_cuda(subtensor, mocker): @pytest.mark.asyncio -async def test_register_extrinsic_subnet_not_exists(subtensor, mocker): +async def test_register_extrinsic_subnet_not_exists(subtensor, fake_wallet, mocker): """Tests registration when subnet does not exist.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) - mocked_subnet_exists = mocker.patch.object( subtensor, "subnet_exists", return_value=False ) @@ -344,10 +335,9 @@ async def test_register_extrinsic_subnet_not_exists(subtensor, mocker): @pytest.mark.asyncio -async def test_register_extrinsic_already_registered(subtensor, mocker): +async def test_register_extrinsic_already_registered(subtensor, fake_wallet, mocker): """Tests registration when the key is already registered.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) mocked_get_neuron = mocker.patch.object( subtensor, "get_neuron_for_pubkey_and_subnet", @@ -371,9 +361,8 @@ async def test_register_extrinsic_already_registered(subtensor, mocker): @pytest.mark.asyncio -async def test_register_extrinsic_max_attempts_reached(subtensor, mocker): +async def test_register_extrinsic_max_attempts_reached(subtensor, fake_wallet, mocker): # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey_ss58" fake_wallet.coldkey.ss58_address = "coldkey_ss58" @@ -439,10 +428,9 @@ async def is_stale_side_effect(*_, **__): @pytest.mark.asyncio -async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker): +async def test_set_subnet_identity_extrinsic_is_success(subtensor, fake_wallet, mocker): """Verify that set_subnet_identity_extrinsic calls the correct functions and returns the correct result.""" # Preps - wallet = mocker.MagicMock(autospec=Wallet) netuid = 123 subnet_name = "mock_subnet_name" github_repo = "mock_github_repo" @@ -463,7 +451,7 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker): # Call result = await async_registration.set_subnet_identity_extrinsic( subtensor=subtensor, - wallet=wallet, + wallet=fake_wallet, netuid=netuid, subnet_name=subnet_name, github_repo=github_repo, @@ -479,7 +467,7 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker): call_module="SubtensorModule", call_function="set_subnet_identity", call_params={ - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, "netuid": netuid, "subnet_name": subnet_name, "github_repo": github_repo, @@ -492,7 +480,7 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker): ) mocked_submit_extrinsic.assert_awaited_once_with( call=mocked_compose_call.return_value, - wallet=wallet, + wallet=fake_wallet, wait_for_inclusion=False, wait_for_finalization=True, ) @@ -501,10 +489,9 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker): @pytest.mark.asyncio -async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker): +async def test_set_subnet_identity_extrinsic_is_failed(subtensor, fake_wallet, mocker): """Verify that set_subnet_identity_extrinsic calls the correct functions and returns False with bad result.""" # Preps - wallet = mocker.MagicMock(autospec=Wallet) netuid = 123 subnet_name = "mock_subnet_name" github_repo = "mock_github_repo" @@ -527,7 +514,7 @@ async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker): # Call result = await async_registration.set_subnet_identity_extrinsic( subtensor=subtensor, - wallet=wallet, + wallet=fake_wallet, netuid=netuid, subnet_name=subnet_name, github_repo=github_repo, @@ -545,7 +532,7 @@ async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker): call_module="SubtensorModule", call_function="set_subnet_identity", call_params={ - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, "netuid": netuid, "subnet_name": subnet_name, "github_repo": github_repo, @@ -558,7 +545,7 @@ async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker): ) mocked_submit_extrinsic.assert_awaited_once_with( call=mocked_compose_call.return_value, - wallet=wallet, + wallet=fake_wallet, wait_for_inclusion=True, wait_for_finalization=True, ) diff --git a/tests/unit_tests/extrinsics/asyncex/test_root.py b/tests/unit_tests/extrinsics/asyncex/test_root.py index 3e5cd84906..10a6363c37 100644 --- a/tests/unit_tests/extrinsics/asyncex/test_root.py +++ b/tests/unit_tests/extrinsics/asyncex/test_root.py @@ -2,7 +2,6 @@ from bittensor.core.errors import SubstrateRequestException from bittensor.core.extrinsics.asyncex import root as async_root -from bittensor_wallet import Wallet @pytest.mark.asyncio @@ -35,10 +34,9 @@ async def test_get_limits_success(subtensor, mocker): @pytest.mark.asyncio -async def test_root_register_extrinsic_success(subtensor, mocker): +async def test_root_register_extrinsic_success(subtensor, fake_wallet, mocker): """Tests successful registration to root network.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" fake_wallet.hotkey_str = "fake_hotkey" fake_uid = 123 @@ -89,11 +87,9 @@ async def test_root_register_extrinsic_success(subtensor, mocker): @pytest.mark.asyncio -async def test_root_register_extrinsic_unlock_failed(subtensor, mocker): +async def test_root_register_extrinsic_unlock_failed(subtensor, fake_wallet, mocker): """Tests registration fails due to unlock failure.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) - mocked_unlock_key = mocker.patch.object( async_root, "unlock_key", @@ -114,10 +110,11 @@ async def test_root_register_extrinsic_unlock_failed(subtensor, mocker): @pytest.mark.asyncio -async def test_root_register_extrinsic_already_registered(subtensor, mocker): +async def test_root_register_extrinsic_already_registered( + subtensor, fake_wallet, mocker +): """Tests registration when hotkey is already registered.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" mocked_unlock_key = mocker.patch.object( @@ -148,10 +145,11 @@ async def test_root_register_extrinsic_already_registered(subtensor, mocker): @pytest.mark.asyncio -async def test_root_register_extrinsic_transaction_failed(subtensor, mocker): +async def test_root_register_extrinsic_transaction_failed( + subtensor, fake_wallet, mocker +): """Tests registration fails due to transaction failure.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" mocked_unlock_key = mocker.patch.object( @@ -190,10 +188,9 @@ async def test_root_register_extrinsic_transaction_failed(subtensor, mocker): @pytest.mark.asyncio -async def test_root_register_extrinsic_uid_not_found(subtensor, mocker): +async def test_root_register_extrinsic_uid_not_found(subtensor, fake_wallet, mocker): """Tests registration fails because UID is not found after successful transaction.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" mocked_unlock_key = mocker.patch.object( @@ -242,10 +239,9 @@ async def test_root_register_extrinsic_uid_not_found(subtensor, mocker): @pytest.mark.asyncio -async def test_do_set_root_weights_success(subtensor, mocker): +async def test_do_set_root_weights_success(subtensor, fake_wallet, mocker): """Tests _do_set_root_weights when weights are set successfully.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -302,10 +298,9 @@ async def test_do_set_root_weights_success(subtensor, mocker): @pytest.mark.asyncio -async def test_do_set_root_weights_failure(subtensor, mocker): +async def test_do_set_root_weights_failure(subtensor, fake_wallet, mocker): """Tests _do_set_root_weights when setting weights fails.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -352,10 +347,9 @@ async def fake_is_success(): @pytest.mark.asyncio -async def test_do_set_root_weights_no_waiting(subtensor, mocker): +async def test_do_set_root_weights_no_waiting(subtensor, fake_wallet, mocker): """Tests _do_set_root_weights when not waiting for inclusion or finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey_address" fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -394,9 +388,8 @@ async def test_do_set_root_weights_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_success(subtensor, mocker): +async def test_set_root_weights_extrinsic_success(subtensor, fake_wallet, mocker): """Tests successful setting of root weights.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" netuids = [1, 2, 3] weights = [0.1, 0.2, 0.7] @@ -427,9 +420,8 @@ async def test_set_root_weights_extrinsic_success(subtensor, mocker): @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_no_waiting(subtensor, mocker): +async def test_set_root_weights_extrinsic_no_waiting(subtensor, fake_wallet, mocker): """Tests setting root weights without waiting for inclusion or finalization.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" netuids = [1, 2, 3] weights = [0.1, 0.2, 0.7] @@ -460,9 +452,10 @@ async def test_set_root_weights_extrinsic_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_not_registered(subtensor, mocker): +async def test_set_root_weights_extrinsic_not_registered( + subtensor, fake_wallet, mocker +): """Tests failure when hotkey is not registered.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" mocker.patch.object(subtensor.substrate, "query", return_value=None) @@ -478,9 +471,10 @@ async def test_set_root_weights_extrinsic_not_registered(subtensor, mocker): @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_insufficient_weights(subtensor, mocker): +async def test_set_root_weights_extrinsic_insufficient_weights( + subtensor, fake_wallet, mocker +): """Tests failure when number of weights is less than the minimum allowed.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" netuids = [1, 2] weights = [0.5, 0.5] @@ -501,9 +495,8 @@ async def test_set_root_weights_extrinsic_insufficient_weights(subtensor, mocker @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_unlock_failed(subtensor, mocker): +async def test_set_root_weights_extrinsic_unlock_failed(subtensor, fake_wallet, mocker): """Tests failure due to unlock key error.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" mocker.patch.object(subtensor.substrate, "query", return_value=123) @@ -524,9 +517,10 @@ async def test_set_root_weights_extrinsic_unlock_failed(subtensor, mocker): @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_transaction_failed(subtensor, mocker): +async def test_set_root_weights_extrinsic_transaction_failed( + subtensor, fake_wallet, mocker +): """Tests failure when transaction is not successful.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" mocker.patch.object(subtensor.substrate, "query", return_value=123) @@ -557,9 +551,10 @@ async def test_set_root_weights_extrinsic_transaction_failed(subtensor, mocker): @pytest.mark.asyncio -async def test_set_root_weights_extrinsic_request_exception(subtensor, mocker): +async def test_set_root_weights_extrinsic_request_exception( + subtensor, fake_wallet, mocker +): """Tests failure due to SubstrateRequestException.""" - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "fake_hotkey" mocker.patch.object(subtensor.substrate, "query", return_value=123) diff --git a/tests/unit_tests/extrinsics/asyncex/test_transfer.py b/tests/unit_tests/extrinsics/asyncex/test_transfer.py index 74d866bf14..0fa70a8e75 100644 --- a/tests/unit_tests/extrinsics/asyncex/test_transfer.py +++ b/tests/unit_tests/extrinsics/asyncex/test_transfer.py @@ -1,14 +1,12 @@ import pytest -from bittensor_wallet import Wallet from bittensor.core.extrinsics.asyncex import transfer as async_transfer from bittensor.utils.balance import Balance @pytest.mark.asyncio -async def test_do_transfer_success(subtensor, mocker): +async def test_do_transfer_success(subtensor, fake_wallet, mocker): """Tests _do_transfer when the transfer is successful.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_destination = "destination_address" fake_amount = mocker.Mock(autospec=Balance, rao=1000) @@ -58,10 +56,9 @@ async def test_do_transfer_success(subtensor, mocker): @pytest.mark.asyncio -async def test_do_transfer_failure(subtensor, mocker): +async def test_do_transfer_failure(subtensor, fake_wallet, mocker): """Tests _do_transfer when the transfer fails.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_destination = "destination_address" fake_amount = mocker.Mock(autospec=Balance, rao=1000) @@ -118,10 +115,9 @@ async def test_do_transfer_failure(subtensor, mocker): @pytest.mark.asyncio -async def test_do_transfer_no_waiting(subtensor, mocker): +async def test_do_transfer_no_waiting(subtensor, fake_wallet, mocker): """Tests _do_transfer when no waiting for inclusion or finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_destination = "destination_address" fake_amount = mocker.Mock(autospec=Balance, rao=1000) @@ -168,10 +164,9 @@ async def test_do_transfer_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_transfer_extrinsic_success(subtensor, mocker): +async def test_transfer_extrinsic_success(subtensor, fake_wallet, mocker): """Tests successful transfer.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.coldkeypub.ss58_address = "fake_ss58_address" fake_destination = "valid_ss58_address" fake_amount = Balance(15) @@ -232,11 +227,10 @@ async def test_transfer_extrinsic_success(subtensor, mocker): @pytest.mark.asyncio async def test_transfer_extrinsic_call_successful_with_failed_response( - subtensor, mocker + subtensor, fake_wallet, mocker ): """Tests successful transfer call is successful with failed response.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.coldkeypub.ss58_address = "fake_ss58_address" fake_destination = "valid_ss58_address" fake_amount = Balance(15) @@ -297,10 +291,9 @@ async def test_transfer_extrinsic_call_successful_with_failed_response( @pytest.mark.asyncio -async def test_transfer_extrinsic_insufficient_balance(subtensor, mocker): +async def test_transfer_extrinsic_insufficient_balance(subtensor, fake_wallet, mocker): """Tests transfer when balance is insufficient.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.coldkeypub.ss58_address = "fake_ss58_address" fake_destination = "valid_ss58_address" fake_amount = Balance(5000) @@ -354,10 +347,9 @@ async def test_transfer_extrinsic_insufficient_balance(subtensor, mocker): @pytest.mark.asyncio -async def test_transfer_extrinsic_invalid_destination(subtensor, mocker): +async def test_transfer_extrinsic_invalid_destination(subtensor, fake_wallet, mocker): """Tests transfer with invalid destination address.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.coldkeypub.ss58_address = "fake_ss58_address" fake_destination = "invalid_address" fake_amount = Balance(15) @@ -386,10 +378,9 @@ async def test_transfer_extrinsic_invalid_destination(subtensor, mocker): @pytest.mark.asyncio -async def test_transfer_extrinsic_unlock_key_false(subtensor, mocker): +async def test_transfer_extrinsic_unlock_key_false(subtensor, fake_wallet, mocker): """Tests transfer failed unlock_key.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.coldkeypub.ss58_address = "fake_ss58_address" fake_destination = "invalid_address" fake_amount = Balance(15) @@ -426,11 +417,10 @@ async def test_transfer_extrinsic_unlock_key_false(subtensor, mocker): @pytest.mark.asyncio async def test_transfer_extrinsic_keep_alive_false_and_transfer_all_true( - subtensor, mocker + subtensor, fake_wallet, mocker ): """Tests transfer with keep_alive flag set to False and transfer_all flag set to True.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_wallet.coldkeypub.ss58_address = "fake_ss58_address" fake_destination = "valid_ss58_address" fake_amount = Balance(15) diff --git a/tests/unit_tests/extrinsics/asyncex/test_weights.py b/tests/unit_tests/extrinsics/asyncex/test_weights.py index 3ec310af89..8a297602f1 100644 --- a/tests/unit_tests/extrinsics/asyncex/test_weights.py +++ b/tests/unit_tests/extrinsics/asyncex/test_weights.py @@ -1,14 +1,12 @@ import pytest from bittensor.core import async_subtensor -from bittensor_wallet import Wallet from bittensor.core.extrinsics.asyncex import weights as async_weights @pytest.mark.asyncio -async def test_do_set_weights_success(subtensor, mocker): +async def test_do_set_weights_success(subtensor, fake_wallet, mocker): """Tests _do_set_weights when weights are set successfully.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_uids = [1, 2, 3] fake_vals = [100, 200, 300] fake_netuid = 0 @@ -49,10 +47,9 @@ async def fake_is_success(): @pytest.mark.asyncio -async def test_do_set_weights_failure(subtensor, mocker): +async def test_do_set_weights_failure(subtensor, fake_wallet, mocker): """Tests _do_set_weights when setting weights fails.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_uids = [1, 2, 3] fake_vals = [100, 200, 300] fake_netuid = 0 @@ -102,10 +99,9 @@ async def fake_is_success(): @pytest.mark.asyncio -async def test_do_set_weights_no_waiting(subtensor, mocker): +async def test_do_set_weights_no_waiting(subtensor, fake_wallet, mocker): """Tests _do_set_weights when not waiting for inclusion or finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_uids = [1, 2, 3] fake_vals = [100, 200, 300] fake_netuid = 0 @@ -139,10 +135,11 @@ async def test_do_set_weights_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_set_weights_extrinsic_success_with_finalization(subtensor, mocker): +async def test_set_weights_extrinsic_success_with_finalization( + subtensor, fake_wallet, mocker +): """Tests set_weights_extrinsic when weights are successfully set with finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -178,10 +175,9 @@ async def test_set_weights_extrinsic_success_with_finalization(subtensor, mocker @pytest.mark.asyncio -async def test_set_weights_extrinsic_no_waiting(subtensor, mocker): +async def test_set_weights_extrinsic_no_waiting(subtensor, fake_wallet, mocker): """Tests set_weights_extrinsic when no waiting for inclusion or finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -208,10 +204,9 @@ async def test_set_weights_extrinsic_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_set_weights_extrinsic_failure(subtensor, mocker): +async def test_set_weights_extrinsic_failure(subtensor, fake_wallet, mocker): """Tests set_weights_extrinsic when setting weights fails.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -238,10 +233,9 @@ async def test_set_weights_extrinsic_failure(subtensor, mocker): @pytest.mark.asyncio -async def test_set_weights_extrinsic_exception(subtensor, mocker): +async def test_set_weights_extrinsic_exception(subtensor, fake_wallet, mocker): """Tests set_weights_extrinsic when an exception is raised.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] @@ -268,10 +262,9 @@ async def test_set_weights_extrinsic_exception(subtensor, mocker): @pytest.mark.asyncio -async def test_do_commit_weights_success(subtensor, mocker): +async def test_do_commit_weights_success(subtensor, fake_wallet, mocker): """Tests _do_commit_weights when the commit is successful.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit_hash = "test_hash" @@ -309,10 +302,9 @@ async def fake_is_success(): @pytest.mark.asyncio -async def test_do_commit_weights_failure(subtensor, mocker): +async def test_do_commit_weights_failure(subtensor, fake_wallet, mocker): """Tests _do_commit_weights when the commit fails.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit_hash = "test_hash" @@ -357,10 +349,9 @@ async def fake_is_success(): @pytest.mark.asyncio -async def test_do_commit_weights_no_waiting(subtensor, mocker): +async def test_do_commit_weights_no_waiting(subtensor, fake_wallet, mocker): """Tests _do_commit_weights when not waiting for inclusion or finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit_hash = "test_hash" @@ -392,10 +383,9 @@ async def test_do_commit_weights_no_waiting(subtensor, mocker): @pytest.mark.asyncio -async def test_do_commit_weights_exception(subtensor, mocker): +async def test_do_commit_weights_exception(subtensor, fake_wallet, mocker): """Tests _do_commit_weights when an exception is raised.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit_hash = "test_hash" @@ -418,10 +408,9 @@ async def test_do_commit_weights_exception(subtensor, mocker): @pytest.mark.asyncio -async def test_commit_weights_extrinsic_success(subtensor, mocker): +async def test_commit_weights_extrinsic_success(subtensor, fake_wallet, mocker): """Tests commit_weights_extrinsic when the commit is successful.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit_hash = "test_hash" @@ -453,10 +442,9 @@ async def test_commit_weights_extrinsic_success(subtensor, mocker): @pytest.mark.asyncio -async def test_commit_weights_extrinsic_failure(subtensor, mocker): +async def test_commit_weights_extrinsic_failure(subtensor, fake_wallet, mocker): """Tests commit_weights_extrinsic when the commit fails.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit_hash = "test_hash" diff --git a/tests/unit_tests/extrinsics/test_commit_reveal.py b/tests/unit_tests/extrinsics/test_commit_reveal.py index e2d428ec29..406bd0a824 100644 --- a/tests/unit_tests/extrinsics/test_commit_reveal.py +++ b/tests/unit_tests/extrinsics/test_commit_reveal.py @@ -1,7 +1,6 @@ import numpy as np import pytest import torch -from bittensor_wallet import Wallet from bittensor.core import subtensor as subtensor_module from bittensor.core.chain_data import SubnetHyperparameters @@ -41,10 +40,9 @@ def hyperparams(): ) -def test_do_commit_reveal_v3_success(mocker, subtensor): +def test_do_commit_reveal_v3_success(mocker, subtensor, fake_wallet): """Test successful commit-reveal with wait for finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit = b"fake_commit" fake_reveal_round = 1 @@ -87,10 +85,9 @@ def test_do_commit_reveal_v3_success(mocker, subtensor): assert result == (True, "") -def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): +def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor, fake_wallet): """Test commit-reveal fails due to an error in submission.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit = b"fake_commit" fake_reveal_round = 1 @@ -141,10 +138,11 @@ def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): assert result == (False, "Formatted error") -def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperparams): +def test_commit_reveal_v3_extrinsic_success_with_torch( + mocker, subtensor, hyperparams, fake_wallet +): """Test successful commit-reveal with torch tensors.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) @@ -213,10 +211,11 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperp ) -def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor, hyperparams): +def test_commit_reveal_v3_extrinsic_success_with_numpy( + mocker, subtensor, hyperparams, fake_wallet +): """Test successful commit-reveal with numpy arrays.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = np.array([1, 2, 3], dtype=np.int64) fake_weights = np.array([0.1, 0.2, 0.7], dtype=np.float32) @@ -258,10 +257,11 @@ def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor, hyperp mock_do_commit.assert_called_once() -def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparams): +def test_commit_reveal_v3_extrinsic_response_false( + mocker, subtensor, hyperparams, fake_wallet +): """Test unsuccessful commit-reveal with torch.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) @@ -314,10 +314,9 @@ def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparam ) -def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor): +def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor, fake_wallet): """Test exception handling in commit-reveal.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] diff --git a/tests/unit_tests/extrinsics/test_commit_weights.py b/tests/unit_tests/extrinsics/test_commit_weights.py index 14993562a2..71771d2247 100644 --- a/tests/unit_tests/extrinsics/test_commit_weights.py +++ b/tests/unit_tests/extrinsics/test_commit_weights.py @@ -1,5 +1,3 @@ -from bittensor_wallet import Wallet - from bittensor.core.extrinsics.commit_weights import ( _do_commit_weights, _do_reveal_weights, @@ -7,10 +5,9 @@ from bittensor.core.settings import version_as_int -def test_do_commit_weights(subtensor, mocker): +def test_do_commit_weights(subtensor, fake_wallet, mocker): """Successful _do_commit_weights call.""" # Preps - fake_wallet = mocker.MagicMock() netuid = 1 commit_hash = "fake_commit_hash" wait_for_inclusion = True @@ -65,10 +62,9 @@ def test_do_commit_weights(subtensor, mocker): ) -def test_do_reveal_weights(subtensor, mocker): +def test_do_reveal_weights(subtensor, fake_wallet, mocker): """Verifies that the `_do_reveal_weights` method interacts with the right substrate methods.""" # Preps - fake_wallet = mocker.MagicMock(autospec=Wallet) fake_wallet.hotkey.ss58_address = "hotkey" netuid = 1 diff --git a/tests/unit_tests/extrinsics/test_set_weights.py b/tests/unit_tests/extrinsics/test_set_weights.py index a2aaa4aaab..fdded8d442 100644 --- a/tests/unit_tests/extrinsics/test_set_weights.py +++ b/tests/unit_tests/extrinsics/test_set_weights.py @@ -2,7 +2,6 @@ import pytest import torch -from bittensor_wallet import Wallet from bittensor.core import subtensor as subtensor_module from bittensor.core.extrinsics.set_weights import ( @@ -21,12 +20,6 @@ def mock_subtensor(): return mock -@pytest.fixture -def mock_wallet(): - mock = MagicMock(spec=Wallet) - return mock - - @pytest.mark.parametrize( "uids, weights, version_key, wait_for_inclusion, wait_for_finalization, expected_success, expected_message", [ @@ -66,7 +59,7 @@ def mock_wallet(): ) def test_set_weights_extrinsic( mock_subtensor, - mock_wallet, + fake_wallet, uids, weights, version_key, @@ -86,7 +79,7 @@ def test_set_weights_extrinsic( ): result, message = set_weights_extrinsic( subtensor=mock_subtensor, - wallet=mock_wallet, + wallet=fake_wallet, netuid=123, uids=uids, weights=weights, @@ -99,10 +92,9 @@ def test_set_weights_extrinsic( assert message == expected_message, f"Test {expected_message} failed." -def test_do_set_weights_is_success(mock_subtensor, mocker): +def test_do_set_weights_is_success(mock_subtensor, fake_wallet, mocker): """Successful _do_set_weights call.""" # Prep - fake_wallet = mocker.MagicMock() fake_uids = [1, 2, 3] fake_vals = [4, 5, 6] fake_netuid = 1 @@ -144,10 +136,9 @@ def test_do_set_weights_is_success(mock_subtensor, mocker): assert result == (True, "Successfully set weights.") -def test_do_set_weights_is_not_success(mock_subtensor, mocker): +def test_do_set_weights_is_not_success(mock_subtensor, fake_wallet, mocker): """Unsuccessful _do_set_weights call.""" # Prep - fake_wallet = mocker.MagicMock() fake_uids = [1, 2, 3] fake_vals = [4, 5, 6] fake_netuid = 1 @@ -200,10 +191,9 @@ def test_do_set_weights_is_not_success(mock_subtensor, mocker): ) -def test_do_set_weights_no_waits(mock_subtensor, mocker): +def test_do_set_weights_no_waits(mock_subtensor, fake_wallet, mocker): """Successful _do_set_weights call without wait flags for fake_wait_for_inclusion and fake_wait_for_finalization.""" # Prep - fake_wallet = mocker.MagicMock() fake_uids = [1, 2, 3] fake_vals = [4, 5, 6] fake_netuid = 1 diff --git a/tests/unit_tests/extrinsics/test_transfer.py b/tests/unit_tests/extrinsics/test_transfer.py index 95f1cf8de5..9424352a55 100644 --- a/tests/unit_tests/extrinsics/test_transfer.py +++ b/tests/unit_tests/extrinsics/test_transfer.py @@ -2,10 +2,9 @@ from bittensor.utils.balance import Balance -def test_do_transfer_is_success_true(subtensor, mocker): +def test_do_transfer_is_success_true(subtensor, fake_wallet, mocker): """Successful do_transfer call.""" # Prep - fake_wallet = mocker.MagicMock() fake_dest = "SS58PUBLICKEY" fake_transfer_balance = Balance(1) fake_wait_for_inclusion = True @@ -45,10 +44,9 @@ def test_do_transfer_is_success_true(subtensor, mocker): ) -def test_do_transfer_is_success_false(subtensor, mocker): +def test_do_transfer_is_success_false(subtensor, fake_wallet, mocker): """Successful do_transfer call.""" # Prep - fake_wallet = mocker.MagicMock() fake_dest = "SS58PUBLICKEY" fake_transfer_balance = Balance(1) fake_wait_for_inclusion = True @@ -97,10 +95,9 @@ def test_do_transfer_is_success_false(subtensor, mocker): ) -def test_do_transfer_no_waits(subtensor, mocker): +def test_do_transfer_no_waits(subtensor, fake_wallet, mocker): """Successful do_transfer call.""" # Prep - fake_wallet = mocker.MagicMock() fake_dest = "SS58PUBLICKEY" fake_transfer_balance = Balance(1) fake_wait_for_inclusion = False diff --git a/tests/unit_tests/extrinsics/test_unstaking.py b/tests/unit_tests/extrinsics/test_unstaking.py index b93f335162..224fe4b640 100644 --- a/tests/unit_tests/extrinsics/test_unstaking.py +++ b/tests/unit_tests/extrinsics/test_unstaking.py @@ -2,7 +2,7 @@ from bittensor.utils.balance import Balance -def test_unstake_extrinsic(mocker): +def test_unstake_extrinsic(fake_wallet, mocker): # Preps fake_subtensor = mocker.Mock( **{ @@ -12,7 +12,6 @@ def test_unstake_extrinsic(mocker): "get_stake.return_value": Balance(10.0), } ) - fake_wallet = mocker.Mock() fake_wallet.coldkeypub.ss58_address = "hotkey_owner" hotkey_ss58 = "hotkey" fake_netuid = 1 @@ -54,7 +53,7 @@ def test_unstake_extrinsic(mocker): ) -def test_unstake_multiple_extrinsic(mocker): +def test_unstake_multiple_extrinsic(fake_wallet, mocker): """Verify that sync `unstake_multiple_extrinsic` method calls proper async method.""" # Preps fake_subtensor = mocker.Mock( @@ -68,7 +67,6 @@ def test_unstake_multiple_extrinsic(mocker): mocker.patch.object( unstaking, "get_old_stakes", return_value=[Balance(1.1), Balance(0.3)] ) - fake_wallet = mocker.Mock() fake_wallet.coldkeypub.ss58_address = "hotkey_owner" hotkey_ss58s = ["hotkey1", "hotkey2"] fake_netuids = [1, 2] diff --git a/tests/unit_tests/test_async_subtensor.py b/tests/unit_tests/test_async_subtensor.py index 355f6cd27f..d66346a2fa 100644 --- a/tests/unit_tests/test_async_subtensor.py +++ b/tests/unit_tests/test_async_subtensor.py @@ -596,10 +596,10 @@ async def test_get_balance(subtensor, mocker): @pytest.mark.parametrize("balance", [100, 100.1]) @pytest.mark.asyncio -async def test_get_transfer_fee(subtensor, mocker, balance): +async def test_get_transfer_fee(subtensor, fake_wallet, mocker, balance): """Tests get_transfer_fee method.""" # Preps - fake_wallet = mocker.Mock(coldkeypub="coldkeypub", autospec=Wallet) + fake_wallet.coldkeypub = "coldkeypub" fake_dest = "fake_dest" fake_value = Balance(balance) @@ -827,9 +827,9 @@ async def test_filter_netuids_by_registered_hotkeys( ): """Tests filter_netuids_by_registered_hotkeys method.""" # Preps - fake_wallet_1 = mocker.Mock(autospec=Wallet) + fake_wallet_1 = mocker.Mock(spec_set=Wallet) fake_wallet_1.hotkey.ss58_address = "ss58_address_1" - fake_wallet_2 = mocker.Mock(autospec=Wallet) + fake_wallet_2 = mocker.Mock(spec_set=Wallet) fake_wallet_2.hotkey.ss58_address = "ss58_address_2" fake_all_netuids = all_netuids @@ -1605,11 +1605,12 @@ async def test_get_hotkey_owner_non_existent_hotkey(subtensor, mocker): @pytest.mark.asyncio -async def test_sign_and_send_extrinsic_success_finalization(subtensor, mocker): +async def test_sign_and_send_extrinsic_success_finalization( + subtensor, fake_wallet, mocker +): """Tests sign_and_send_extrinsic when the extrinsic is successfully finalized.""" # Preps fake_call = mocker.Mock() - fake_wallet = mocker.Mock() fake_extrinsic = mocker.Mock() fake_response = mocker.Mock() @@ -1647,11 +1648,12 @@ async def fake_is_success(): @pytest.mark.asyncio -async def test_sign_and_send_extrinsic_error_finalization(subtensor, mocker): +async def test_sign_and_send_extrinsic_error_finalization( + subtensor, fake_wallet, mocker +): """Tests sign_and_send_extrinsic when the extrinsic is error finalized.""" # Preps fake_call = mocker.Mock() - fake_wallet = mocker.Mock() fake_extrinsic = mocker.Mock() fake_response = mocker.Mock() @@ -1698,12 +1700,11 @@ async def fake_error_message(): @pytest.mark.asyncio async def test_sign_and_send_extrinsic_success_without_inclusion_finalization( - subtensor, mocker + subtensor, fake_wallet, mocker ): """Tests sign_and_send_extrinsic when extrinsic is submitted without waiting for inclusion or finalization.""" # Preps fake_call = mocker.Mock() - fake_wallet = mocker.Mock() fake_extrinsic = mocker.Mock() mocked_create_signed_extrinsic = mocker.AsyncMock(return_value=fake_extrinsic) @@ -1735,11 +1736,12 @@ async def test_sign_and_send_extrinsic_success_without_inclusion_finalization( @pytest.mark.asyncio -async def test_sign_and_send_extrinsic_substrate_request_exception(subtensor, mocker): +async def test_sign_and_send_extrinsic_substrate_request_exception( + subtensor, fake_wallet, mocker +): """Tests sign_and_send_extrinsic when SubstrateRequestException is raised.""" # Preps fake_call = mocker.Mock() - fake_wallet = mocker.Mock() fake_extrinsic = mocker.Mock() fake_exception = async_subtensor.SubstrateRequestException("Test Exception") @@ -2337,10 +2339,9 @@ async def test_get_subnet_reveal_period_epochs(subtensor, mocker): @pytest.mark.asyncio -async def test_transfer_success(subtensor, mocker): +async def test_transfer_success(subtensor, fake_wallet, mocker): """Tests transfer when the transfer is successful.""" # Preps - fake_wallet = mocker.Mock() fake_destination = "destination_address" fake_amount = Balance.from_tao(100.0) fake_transfer_all = False @@ -2373,10 +2374,9 @@ async def test_transfer_success(subtensor, mocker): @pytest.mark.asyncio -async def test_register_success(subtensor, mocker): +async def test_register_success(subtensor, fake_wallet, mocker): """Tests register when there is enough balance and registration succeeds.""" # Preps - fake_wallet = mocker.Mock() fake_netuid = 1 mocked_register_extrinsic = mocker.AsyncMock() @@ -2407,10 +2407,9 @@ async def test_register_success(subtensor, mocker): @pytest.mark.asyncio -async def test_set_weights_success(subtensor, mocker): +async def test_set_weights_success(subtensor, fake_wallet, mocker): """Tests set_weights with successful weight setting on the first try.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.3, 0.5, 0.2] @@ -2465,10 +2464,9 @@ async def test_set_weights_success(subtensor, mocker): @pytest.mark.asyncio -async def test_set_weights_with_exception(subtensor, mocker): +async def test_set_weights_with_exception(subtensor, fake_wallet, mocker): """Tests set_weights when set_weights_extrinsic raises an exception.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.3, 0.5, 0.2] @@ -2510,10 +2508,9 @@ async def test_set_weights_with_exception(subtensor, mocker): @pytest.mark.asyncio -async def test_root_set_weights_success(subtensor, mocker): +async def test_root_set_weights_success(subtensor, fake_wallet, mocker): """Tests root_set_weights when the setting of weights is successful.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuids = [1, 2, 3] fake_weights = [0.3, 0.5, 0.2] @@ -2552,10 +2549,9 @@ async def test_root_set_weights_success(subtensor, mocker): @pytest.mark.asyncio -async def test_commit_weights_success(subtensor, mocker): +async def test_commit_weights_success(subtensor, fake_wallet, mocker): """Tests commit_weights when the weights are committed successfully.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_salt = [12345, 67890] fake_uids = [1, 2, 3] @@ -2604,10 +2600,9 @@ async def test_commit_weights_success(subtensor, mocker): @pytest.mark.asyncio -async def test_commit_weights_with_exception(subtensor, mocker): +async def test_commit_weights_with_exception(subtensor, fake_wallet, mocker): """Tests commit_weights when an exception is raised during weight commitment.""" # Preps - fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_salt = [12345, 67890] fake_uids = [1, 2, 3] @@ -2672,10 +2667,9 @@ async def test_get_all_subnets_info_success(mocker, subtensor): @pytest.mark.asyncio -async def test_set_subnet_identity(mocker, subtensor): +async def test_set_subnet_identity(mocker, subtensor, fake_wallet): """Verify that subtensor method `set_subnet_identity` calls proper function with proper arguments.""" # Preps - fake_wallet = mocker.Mock() fake_netuid = 123 fake_subnet_identity = mocker.MagicMock() diff --git a/tests/unit_tests/test_subnets.py b/tests/unit_tests/test_subnets.py index 4db91ae01a..00a5276264 100644 --- a/tests/unit_tests/test_subnets.py +++ b/tests/unit_tests/test_subnets.py @@ -14,11 +14,10 @@ def process_responses(self, responses): return responses -def test_instance_creation(mocker): +def test_instance_creation(fake_wallet, mocker): """Test the creation of a MySubnetsAPI instance.""" # Prep mocked_dendrite = mocker.patch.object(subnets, "Dendrite") - fake_wallet = mocker.MagicMock() # Call instance = MySubnetsAPI(fake_wallet) @@ -31,7 +30,7 @@ def test_instance_creation(mocker): @pytest.mark.asyncio -async def test_query_api(mocker): +async def test_query_api(fake_wallet, mocker): """Test querying the MySubnetsAPI instance asynchronously.""" # Prep mocked_async_dendrite = mocker.AsyncMock() @@ -39,7 +38,6 @@ async def test_query_api(mocker): subnets, "Dendrite", return_value=mocked_async_dendrite ) - fake_wallet = mocker.MagicMock() fake_axon = mocker.MagicMock() mocked_synapse = mocker.MagicMock() @@ -59,7 +57,7 @@ async def test_query_api(mocker): @pytest.mark.asyncio -async def test_test_instance_call(mocker): +async def test_test_instance_call(fake_wallet, mocker): """Test the MySubnetsAPI instance call with asynchronous handling.""" # Prep mocked_async_dendrite = mocker.AsyncMock() @@ -69,7 +67,6 @@ async def test_test_instance_call(mocker): mocked_query_api = mocker.patch.object( MySubnetsAPI, "query_api", new=mocker.AsyncMock() ) - fake_wallet = mocker.MagicMock() fake_axon = mocker.MagicMock() # Call diff --git a/tests/unit_tests/test_subtensor.py b/tests/unit_tests/test_subtensor.py index 9d79dd937d..cc47344eeb 100644 --- a/tests/unit_tests/test_subtensor.py +++ b/tests/unit_tests/test_subtensor.py @@ -1166,10 +1166,9 @@ def test_is_hotkey_registered_with_netuid(subtensor, mocker): assert result == mocked_is_hotkey_registered_on_subnet.return_value -def test_set_weights(subtensor, mocker): +def test_set_weights(subtensor, mocker, fake_wallet): """Successful set_weights call.""" # Preps - fake_wallet = mocker.MagicMock() fake_netuid = 1 fake_uids = [2, 4] fake_weights = [0.4, 0.6] @@ -1268,10 +1267,9 @@ def test_get_block_hash(subtensor, mocker): assert result == subtensor.substrate.get_block_hash.return_value -def test_commit(subtensor, mocker): +def test_commit(subtensor, fake_wallet, mocker): """Test successful commit call.""" # Preps - fake_wallet = mocker.MagicMock() fake_netuid = 1 fake_data = "some data to network" mocked_publish_metadata = mocker.patch.object(subtensor_module, "publish_metadata") @@ -1313,10 +1311,9 @@ def test_subnetwork_n(subtensor, mocker): assert result == mocked_get_hyperparameter.return_value -def test_transfer(subtensor, mocker): +def test_transfer(subtensor, fake_wallet, mocker): """Tests successful transfer call.""" # Prep - fake_wallet = mocker.MagicMock() fake_dest = "SS58PUBLICKEY" fake_amount = 1.1 fake_wait_for_inclusion = True @@ -1478,11 +1475,10 @@ def test_neuron_for_uid_success(subtensor, mocker): ], ) def test_do_serve_axon_is_success( - subtensor, mocker, fake_call_params, expected_call_function + subtensor, fake_wallet, mocker, fake_call_params, expected_call_function ): """Successful do_serve_axon call.""" # Prep - fake_wallet = mocker.MagicMock() fake_wait_for_inclusion = True fake_wait_for_finalization = True @@ -1520,10 +1516,9 @@ def test_do_serve_axon_is_success( assert result[1] is None -def test_do_serve_axon_is_not_success(subtensor, mocker, fake_call_params): +def test_do_serve_axon_is_not_success(subtensor, fake_wallet, mocker, fake_call_params): """Unsuccessful do_serve_axon call.""" # Prep - fake_wallet = mocker.MagicMock() fake_wait_for_inclusion = True fake_wait_for_finalization = True @@ -1562,10 +1557,9 @@ def test_do_serve_axon_is_not_success(subtensor, mocker, fake_call_params): ) -def test_do_serve_axon_no_waits(subtensor, mocker, fake_call_params): +def test_do_serve_axon_no_waits(subtensor, fake_wallet, mocker, fake_call_params): """Unsuccessful do_serve_axon call.""" # Prep - fake_wallet = mocker.MagicMock() fake_wait_for_inclusion = False fake_wait_for_finalization = False @@ -1884,10 +1878,9 @@ def test_max_weight_limit(subtensor, mocker): assert result == mocked_u16_normalized_float.return_value -def test_get_transfer_fee(subtensor, mocker): +def test_get_transfer_fee(subtensor, fake_wallet, mocker): """Successful get_transfer_fee call.""" # Preps - fake_wallet = mocker.MagicMock() fake_dest = "SS58ADDRESS" value = Balance(1) @@ -1937,10 +1930,9 @@ def test_get_existential_deposit(subtensor, mocker): assert result == Balance.from_rao(value) -def test_commit_weights(subtensor, mocker): +def test_commit_weights(subtensor, fake_wallet, mocker): """Successful commit_weights call.""" # Preps - fake_wallet = mocker.MagicMock() netuid = 1 salt = [1, 3] uids = [2, 4] @@ -1991,10 +1983,9 @@ def test_commit_weights(subtensor, mocker): assert result == expected_result -def test_reveal_weights(subtensor, mocker): +def test_reveal_weights(subtensor, fake_wallet, mocker): """Successful test_reveal_weights call.""" # Preps - fake_wallet = mocker.MagicMock() netuid = 1 uids = [1, 2, 3, 4] weights = [0.1, 0.2, 0.3, 0.4] @@ -2030,10 +2021,9 @@ def test_reveal_weights(subtensor, mocker): ) -def test_reveal_weights_false(subtensor, mocker): +def test_reveal_weights_false(subtensor, fake_wallet, mocker): """Failed test_reveal_weights call.""" # Preps - fake_wallet = mocker.MagicMock() netuid = 1 uids = [1, 2, 3, 4] weights = [0.1, 0.2, 0.3, 0.4] @@ -2847,10 +2837,9 @@ def test_is_hotkey_delegate_empty_list(mocker, subtensor): assert result is False -def test_add_stake_success(mocker, subtensor): +def test_add_stake_success(mocker, fake_wallet, subtensor): """Test add_stake returns True on successful staking.""" # Prep - fake_wallet = mocker.Mock() fake_hotkey_ss58 = "fake_hotkey" fake_amount = 10.0 @@ -2880,10 +2869,9 @@ def test_add_stake_success(mocker, subtensor): assert result == mock_add_stake_extrinsic.return_value -def test_add_stake_multiple_success(mocker, subtensor): +def test_add_stake_multiple_success(mocker, fake_wallet, subtensor): """Test add_stake_multiple successfully stakes for all hotkeys.""" # Prep - fake_wallet = mocker.Mock() fake_hotkey_ss58 = ["fake_hotkey"] fake_amount = [10.0] @@ -2914,10 +2902,9 @@ def test_add_stake_multiple_success(mocker, subtensor): assert result == mock_add_stake_multiple_extrinsic.return_value -def test_unstake_success(mocker, subtensor): +def test_unstake_success(mocker, subtensor, fake_wallet): """Test unstake operation is successful.""" # Preps - fake_wallet = mocker.Mock() fake_hotkey_ss58 = "hotkey_1" fake_amount = 10.0 @@ -2945,10 +2932,9 @@ def test_unstake_success(mocker, subtensor): assert result == mock_unstake_extrinsic.return_value -def test_unstake_multiple_success(mocker, subtensor): +def test_unstake_multiple_success(mocker, subtensor, fake_wallet): """Test unstake_multiple succeeds for all hotkeys.""" # Preps - fake_wallet = mocker.Mock() fake_hotkeys = ["hotkey_1", "hotkey_2"] fake_amounts = [10.0, 20.0] @@ -2979,10 +2965,9 @@ def test_unstake_multiple_success(mocker, subtensor): assert result == mock_unstake_multiple_extrinsic.return_value -def test_set_weights_with_commit_reveal_enabled(subtensor, mocker): +def test_set_weights_with_commit_reveal_enabled(subtensor, fake_wallet, mocker): """Test set_weights with commit_reveal_enabled is True.""" # Preps - fake_wallet = mocker.Mock() fake_netuid = 1 fake_uids = [1, 5] fake_weights = [0.1, 0.9] @@ -3051,10 +3036,9 @@ def test_connection_limit(mocker): Subtensor("test") -def test_set_subnet_identity(mocker, subtensor): +def test_set_subnet_identity(mocker, subtensor, fake_wallet): """Verify that subtensor method `set_subnet_identity` calls proper function with proper arguments.""" # Preps - fake_wallet = mocker.Mock() fake_netuid = 123 fake_subnet_identity = mocker.MagicMock() diff --git a/tests/unit_tests/test_subtensor_extended.py b/tests/unit_tests/test_subtensor_extended.py index 4c951f6704..be9cb99d0f 100644 --- a/tests/unit_tests/test_subtensor_extended.py +++ b/tests/unit_tests/test_subtensor_extended.py @@ -14,11 +14,6 @@ from tests.helpers.helpers import assert_submit_signed_extrinsic -@pytest.fixture -def wallet(): - return unittest.mock.Mock() - - @pytest.fixture def mock_delegate_info(): return { @@ -169,7 +164,7 @@ def test_bonds(mock_substrate, subtensor, mocker): ) -def test_burned_register(mock_substrate, subtensor, wallet, mocker): +def test_burned_register(mock_substrate, subtensor, fake_wallet, mocker): mocker.patch.object( subtensor, "get_neuron_for_pubkey_and_subnet", @@ -178,26 +173,26 @@ def test_burned_register(mock_substrate, subtensor, wallet, mocker): mocker.patch.object(subtensor, "get_balance") success = subtensor.burned_register( - wallet, + fake_wallet, netuid=1, ) assert success is True subtensor.get_neuron_for_pubkey_and_subnet.assert_called_once_with( - wallet.hotkey.ss58_address, + fake_wallet.hotkey.ss58_address, netuid=1, block=mock_substrate.get_block_number.return_value, ) assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="burned_register", call_params={ "netuid": 1, - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, }, wait_for_finalization=True, wait_for_inclusion=False, @@ -327,7 +322,7 @@ def test_get_block_hash_none(mock_substrate, subtensor): mock_substrate.get_chain_head.assert_called_once() -def test_get_children(mock_substrate, subtensor, wallet): +def test_get_children(mock_substrate, subtensor, fake_wallet): mock_substrate.query.return_value.value = [ ( 2**64 - 1, @@ -357,7 +352,7 @@ def test_get_children(mock_substrate, subtensor, wallet): ) -def test_get_current_weight_commit_info(mock_substrate, subtensor, wallet, mocker): +def test_get_current_weight_commit_info(mock_substrate, subtensor, fake_wallet, mocker): mock_substrate.query_map.return_value.records = [ ( mocker.ANY, @@ -560,7 +555,7 @@ def test_get_stake_for_coldkey(mock_substrate, subtensor, mocker): def test_filter_netuids_by_registered_hotkeys( - mock_substrate, subtensor, wallet, mocker + mock_substrate, subtensor, fake_wallet, mocker ): mock_substrate.query_map.return_value = mocker.MagicMock( **{ @@ -586,7 +581,7 @@ def test_filter_netuids_by_registered_hotkeys( result = subtensor.filter_netuids_by_registered_hotkeys( all_netuids=[0, 1, 2], filter_for_netuids=[2], - all_hotkeys=[wallet], + all_hotkeys=[fake_wallet], block=10, ) @@ -596,7 +591,7 @@ def test_filter_netuids_by_registered_hotkeys( mock_substrate.query_map.assert_called_once_with( module="SubtensorModule", storage_function="IsNetworkMember", - params=[wallet.hotkey.ss58_address], + params=[fake_wallet.hotkey.ss58_address], block_hash=mock_substrate.get_block_hash.return_value, ) @@ -621,9 +616,9 @@ def test_last_drand_round(mock_substrate, subtensor): False, ), ) -def test_move_stake(mock_substrate, subtensor, wallet, wait): +def test_move_stake(mock_substrate, subtensor, fake_wallet, wait): success = subtensor.move_stake( - wallet, + fake_wallet, origin_hotkey="origin_hotkey", origin_netuid=1, destination_hotkey="destination_hotkey", @@ -637,7 +632,7 @@ def test_move_stake(mock_substrate, subtensor, wallet, wait): assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="move_stake", call_params={ @@ -652,11 +647,11 @@ def test_move_stake(mock_substrate, subtensor, wallet, wait): ) -def test_move_stake_insufficient_stake(mock_substrate, subtensor, wallet, mocker): +def test_move_stake_insufficient_stake(mock_substrate, subtensor, fake_wallet, mocker): mocker.patch.object(subtensor, "get_stake", return_value=Balance(0)) success = subtensor.move_stake( - wallet, + fake_wallet, origin_hotkey="origin_hotkey", origin_netuid=1, destination_hotkey="destination_hotkey", @@ -669,14 +664,14 @@ def test_move_stake_insufficient_stake(mock_substrate, subtensor, wallet, mocker mock_substrate.submit_extrinsic.assert_not_called() -def test_move_stake_error(mock_substrate, subtensor, wallet, mocker): +def test_move_stake_error(mock_substrate, subtensor, fake_wallet, mocker): mock_substrate.submit_extrinsic.return_value = mocker.Mock( error_message="ERROR", is_success=False, ) success = subtensor.move_stake( - wallet, + fake_wallet, origin_hotkey="origin_hotkey", origin_netuid=1, destination_hotkey="destination_hotkey", @@ -688,7 +683,7 @@ def test_move_stake_error(mock_substrate, subtensor, wallet, mocker): assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="move_stake", call_params={ @@ -703,11 +698,11 @@ def test_move_stake_error(mock_substrate, subtensor, wallet, mocker): ) -def test_move_stake_exception(mock_substrate, subtensor, wallet): +def test_move_stake_exception(mock_substrate, subtensor, fake_wallet): mock_substrate.submit_extrinsic.side_effect = RuntimeError success = subtensor.move_stake( - wallet, + fake_wallet, origin_hotkey="origin_hotkey", origin_netuid=1, destination_hotkey="destination_hotkey", @@ -719,7 +714,7 @@ def test_move_stake_exception(mock_substrate, subtensor, wallet): assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="move_stake", call_params={ @@ -891,18 +886,18 @@ def test_subtensor_contextmanager(mock_substrate, subtensor): mock_substrate.close.assert_called_once() -def test_swap_stake(mock_substrate, subtensor, wallet, mocker): +def test_swap_stake(mock_substrate, subtensor, fake_wallet, mocker): mocker.patch.object(subtensor, "get_stake", return_value=Balance(1000)) mocker.patch.object( subtensor, "get_hotkey_owner", autospec=True, - return_value=wallet.coldkeypub.ss58_address, + return_value=fake_wallet.coldkeypub.ss58_address, ) result = subtensor.swap_stake( - wallet, - wallet.hotkey.ss58_address, + fake_wallet, + fake_wallet.hotkey.ss58_address, origin_netuid=1, destination_netuid=2, amount=Balance(999), @@ -912,11 +907,11 @@ def test_swap_stake(mock_substrate, subtensor, wallet, mocker): assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="swap_stake", call_params={ - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, "origin_netuid": 1, "destination_netuid": 2, "alpha_amount": 999, @@ -972,7 +967,7 @@ def test_query_identity(mock_substrate, subtensor, query, result): ) -def test_register(mock_substrate, subtensor, wallet, mocker): +def test_register(mock_substrate, subtensor, fake_wallet, mocker): create_pow = mocker.patch( "bittensor.core.extrinsics.registration.create_pow", return_value=mocker.Mock( @@ -989,20 +984,20 @@ def test_register(mock_substrate, subtensor, wallet, mocker): ) result = subtensor.register( - wallet, + fake_wallet, netuid=1, ) assert result is True subtensor.get_neuron_for_pubkey_and_subnet.assert_called_once_with( - hotkey_ss58=wallet.hotkey.ss58_address, + hotkey_ss58=fake_wallet.hotkey.ss58_address, netuid=1, block=mock_substrate.get_block_number.return_value, ) create_pow.assert_called_once_with( subtensor=subtensor, - wallet=wallet, + wallet=fake_wallet, netuid=1, output_in_place=True, cuda=False, @@ -1013,13 +1008,13 @@ def test_register(mock_substrate, subtensor, wallet, mocker): assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="register", call_params={ "block_number": create_pow.return_value.block_number, - "coldkey": wallet.coldkeypub.ss58_address, - "hotkey": wallet.hotkey.ss58_address, + "coldkey": fake_wallet.coldkeypub.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, "netuid": 1, "nonce": create_pow.return_value.nonce, "work": [1, 2, 3], @@ -1034,7 +1029,7 @@ def test_register(mock_substrate, subtensor, wallet, mocker): False, ], ) -def test_register_subnet(mock_substrate, subtensor, wallet, mocker, success): +def test_register_subnet(mock_substrate, subtensor, fake_wallet, mocker, success): mocker.patch.object(subtensor, "get_balance", return_value=Balance(100)) mocker.patch.object(subtensor, "get_subnet_burn_cost", return_value=Balance(10)) @@ -1043,29 +1038,31 @@ def test_register_subnet(mock_substrate, subtensor, wallet, mocker, success): ) result = subtensor.register_subnet( - wallet, + fake_wallet, ) assert result is success assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="register_network", call_params={ - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, "mechid": 1, }, ) -def test_register_subnet_insufficient_funds(mock_substrate, subtensor, wallet, mocker): +def test_register_subnet_insufficient_funds( + mock_substrate, subtensor, fake_wallet, mocker +): mocker.patch.object(subtensor, "get_balance", return_value=Balance(0)) mocker.patch.object(subtensor, "get_subnet_burn_cost", return_value=Balance(10)) success = subtensor.register_subnet( - wallet, + fake_wallet, ) assert success is False @@ -1073,7 +1070,7 @@ def test_register_subnet_insufficient_funds(mock_substrate, subtensor, wallet, m mock_substrate.submit_extrinsic.assert_not_called() -def test_root_register(mock_substrate, subtensor, wallet, mocker): +def test_root_register(mock_substrate, subtensor, fake_wallet, mocker): mocker.patch.object( subtensor, "get_balance", autospec=True, return_value=Balance(100) ) @@ -1082,33 +1079,35 @@ def test_root_register(mock_substrate, subtensor, wallet, mocker): subtensor, "is_hotkey_registered_on_subnet", autospec=True, return_value=False ) - success = subtensor.root_register(wallet) + success = subtensor.root_register(fake_wallet) assert success is True subtensor.get_balance.assert_called_once_with( - wallet.coldkeypub.ss58_address, + fake_wallet.coldkeypub.ss58_address, block=mock_substrate.get_block_number.return_value, ) subtensor.get_hyperparameter.assert_called_once() subtensor.is_hotkey_registered_on_subnet.assert_called_once_with( - wallet.hotkey.ss58_address, + fake_wallet.hotkey.ss58_address, 0, None, ) assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="root_register", call_params={ - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, }, ) -def test_root_register_is_already_registered(mock_substrate, subtensor, wallet, mocker): +def test_root_register_is_already_registered( + mock_substrate, subtensor, fake_wallet, mocker +): mocker.patch.object( subtensor, "get_balance", autospec=True, return_value=Balance(100) ) @@ -1117,36 +1116,38 @@ def test_root_register_is_already_registered(mock_substrate, subtensor, wallet, subtensor, "is_hotkey_registered_on_subnet", autospec=True, return_value=True ) - success = subtensor.root_register(wallet) + success = subtensor.root_register(fake_wallet) assert success is True subtensor.is_hotkey_registered_on_subnet.assert_called_once_with( - wallet.hotkey.ss58_address, + fake_wallet.hotkey.ss58_address, 0, None, ) mock_substrate.submit_extrinsic.assert_not_called() -def test_root_register_insufficient_balance(mock_substrate, subtensor, wallet, mocker): +def test_root_register_insufficient_balance( + mock_substrate, subtensor, fake_wallet, mocker +): mocker.patch.object( subtensor, "get_balance", autospec=True, return_value=Balance(1) ) mocker.patch.object(subtensor, "get_hyperparameter", autospec=True, return_value=10) - success = subtensor.root_register(wallet) + success = subtensor.root_register(fake_wallet) assert success is False subtensor.get_balance.assert_called_once_with( - wallet.coldkeypub.ss58_address, + fake_wallet.coldkeypub.ss58_address, block=mock_substrate.get_block_number.return_value, ) mock_substrate.submit_extrinsic.assert_not_called() -def test_root_set_weights(mock_substrate, subtensor, wallet, mocker): +def test_root_set_weights(mock_substrate, subtensor, fake_wallet, mocker): MIN_ALLOWED_WEIGHTS = 0 MAX_WEIGHTS_LIMIT = 1 @@ -1162,7 +1163,7 @@ def test_root_set_weights(mock_substrate, subtensor, wallet, mocker): ) subtensor.root_set_weights( - wallet, + fake_wallet, netuids=[1, 2], weights=[0.5, 0.5], ) @@ -1176,12 +1177,12 @@ def test_root_set_weights(mock_substrate, subtensor, wallet, mocker): mock_substrate.query.assert_called_once_with( "SubtensorModule", "Uids", - [0, wallet.hotkey.ss58_address], + [0, fake_wallet.hotkey.ss58_address], ) assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="set_root_weights", call_params={ @@ -1189,7 +1190,7 @@ def test_root_set_weights(mock_substrate, subtensor, wallet, mocker): "weights": [65535, 65535], "netuid": 0, "version_key": 0, - "hotkey": wallet.hotkey.ss58_address, + "hotkey": fake_wallet.hotkey.ss58_address, }, era={ "period": 5, @@ -1199,11 +1200,11 @@ def test_root_set_weights(mock_substrate, subtensor, wallet, mocker): ) -def test_root_set_weights_no_uid(mock_substrate, subtensor, wallet, mocker): +def test_root_set_weights_no_uid(mock_substrate, subtensor, fake_wallet, mocker): mock_substrate.query.return_value = None success = subtensor.root_set_weights( - wallet, + fake_wallet, netuids=[1, 2], weights=[0.5, 0.5], ) @@ -1213,13 +1214,13 @@ def test_root_set_weights_no_uid(mock_substrate, subtensor, wallet, mocker): mock_substrate.query.assert_called_once_with( "SubtensorModule", "Uids", - [0, wallet.hotkey.ss58_address], + [0, fake_wallet.hotkey.ss58_address], ) mock_substrate.submit_extrinsic.assert_not_called() def test_root_set_weights_min_allowed_weights( - mock_substrate, subtensor, wallet, mocker + mock_substrate, subtensor, fake_wallet, mocker ): mocker.patch.object( subtensor, @@ -1234,7 +1235,7 @@ def test_root_set_weights_min_allowed_weights( match="The minimum number of weights required to set weights is 5, got 2", ): subtensor.root_set_weights( - wallet, + fake_wallet, netuids=[1, 2], weights=[0.5, 0.5], ) @@ -1243,25 +1244,25 @@ def test_root_set_weights_min_allowed_weights( mock_substrate.submit_extrinsic.assert_not_called() -def test_sign_and_send_extrinsic(mock_substrate, subtensor, wallet, mocker): +def test_sign_and_send_extrinsic(mock_substrate, subtensor, fake_wallet, mocker): call = mocker.Mock() subtensor.sign_and_send_extrinsic( call, - wallet, + fake_wallet, use_nonce=True, period=10, ) mock_substrate.get_account_next_index.assert_called_once_with( - wallet.hotkey.ss58_address, + fake_wallet.hotkey.ss58_address, ) mock_substrate.create_signed_extrinsic.assert_called_once_with( call=call, era={ "period": 10, }, - keypair=wallet.coldkey, + keypair=fake_wallet.coldkey, nonce=mock_substrate.get_account_next_index.return_value, ) mock_substrate.submit_extrinsic.assert_called_once_with( @@ -1278,16 +1279,16 @@ def test_sign_and_send_extrinsic(mock_substrate, subtensor, wallet, mocker): False, ), ) -def test_transfer_stake(mock_substrate, subtensor, wallet, mocker, wait): +def test_transfer_stake(mock_substrate, subtensor, fake_wallet, mocker, wait): mocker.patch.object( subtensor, "get_hotkey_owner", autospec=True, - return_value=wallet.coldkeypub.ss58_address, + return_value=fake_wallet.coldkeypub.ss58_address, ) success = subtensor.transfer_stake( - wallet, + fake_wallet, "dest", "hotkey_ss58", origin_netuid=1, @@ -1301,7 +1302,7 @@ def test_transfer_stake(mock_substrate, subtensor, wallet, mocker, wait): assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="transfer_stake", call_params={ @@ -1328,17 +1329,19 @@ def test_transfer_stake(mock_substrate, subtensor, wallet, mocker, wait): RuntimeError, ), ) -def test_transfer_stake_error(mock_substrate, subtensor, wallet, mocker, side_effect): +def test_transfer_stake_error( + mock_substrate, subtensor, fake_wallet, mocker, side_effect +): mocker.patch.object( subtensor, "get_hotkey_owner", autospec=True, - return_value=wallet.coldkeypub.ss58_address, + return_value=fake_wallet.coldkeypub.ss58_address, ) mock_substrate.submit_extrinsic.return_value = side_effect success = subtensor.transfer_stake( - wallet, + fake_wallet, "dest", "hotkey_ss58", origin_netuid=1, @@ -1350,7 +1353,7 @@ def test_transfer_stake_error(mock_substrate, subtensor, wallet, mocker, side_ef assert_submit_signed_extrinsic( mock_substrate, - wallet.coldkey, + fake_wallet.coldkey, call_module="SubtensorModule", call_function="transfer_stake", call_params={ @@ -1365,7 +1368,7 @@ def test_transfer_stake_error(mock_substrate, subtensor, wallet, mocker, side_ef ) -def test_transfer_stake_non_owner(mock_substrate, subtensor, wallet, mocker): +def test_transfer_stake_non_owner(mock_substrate, subtensor, fake_wallet, mocker): mocker.patch.object( subtensor, "get_hotkey_owner", @@ -1374,7 +1377,7 @@ def test_transfer_stake_non_owner(mock_substrate, subtensor, wallet, mocker): ) success = subtensor.transfer_stake( - wallet, + fake_wallet, "dest", "hotkey_ss58", origin_netuid=1, @@ -1390,12 +1393,14 @@ def test_transfer_stake_non_owner(mock_substrate, subtensor, wallet, mocker): mock_substrate.submit_extrinsic.assert_not_called() -def test_transfer_stake_insufficient_stake(mock_substrate, subtensor, wallet, mocker): +def test_transfer_stake_insufficient_stake( + mock_substrate, subtensor, fake_wallet, mocker +): mocker.patch.object( subtensor, "get_hotkey_owner", autospec=True, - return_value=wallet.coldkeypub.ss58_address, + return_value=fake_wallet.coldkeypub.ss58_address, ) with unittest.mock.patch.object( @@ -1404,7 +1409,7 @@ def test_transfer_stake_insufficient_stake(mock_substrate, subtensor, wallet, mo return_value=Balance(0), ): success = subtensor.transfer_stake( - wallet, + fake_wallet, "dest", "hotkey_ss58", origin_netuid=1, diff --git a/tests/unit_tests/utils/test_utils.py b/tests/unit_tests/utils/test_utils.py index 4c2c6b8b08..9a6527d643 100644 --- a/tests/unit_tests/utils/test_utils.py +++ b/tests/unit_tests/utils/test_utils.py @@ -1,5 +1,4 @@ import pytest -from bittensor_wallet import Wallet from bittensor import warnings, __getattr__, version_split, logging, trace, debug, utils from bittensor.core.settings import SS58_FORMAT @@ -185,23 +184,21 @@ def test_is_valid_bittensor_address_or_public_key(mocker, test_input, expected_r ("hotkey", "unlock_hotkey"), ], ) -def test_unlock_key(mocker, unlock_type, wallet_method): +def test_unlock_key(fake_wallet, unlock_type, wallet_method): """Test the unlock key function.""" - # Preps - mock_wallet = mocker.Mock(autospec=Wallet) # Call - result = utils.unlock_key(mock_wallet, unlock_type=unlock_type) + result = utils.unlock_key(fake_wallet, unlock_type=unlock_type) # Asserts - getattr(mock_wallet, wallet_method).assert_called_once() + getattr(fake_wallet, wallet_method).assert_called_once() assert result == utils.UnlockStatus(True, "") -def test_unlock_key_raise_value_error(mocker): +def test_unlock_key_raise_value_error(fake_wallet): """Test the unlock key function raises ValueError.""" with pytest.raises(ValueError): - utils.unlock_key(wallet=mocker.Mock(autospec=Wallet), unlock_type="coldkeypub") + utils.unlock_key(wallet=fake_wallet, unlock_type="coldkeypub") @pytest.mark.parametrize( @@ -223,11 +220,10 @@ def test_unlock_key_raise_value_error(mocker): ], ids=["PasswordError", "KeyFileError"], ) -def test_unlock_key_errors(mocker, side_effect, response): +def test_unlock_key_errors(fake_wallet, side_effect, response): """Test the unlock key function handles the errors.""" - mock_wallet = mocker.Mock(autospec=Wallet) - mock_wallet.unlock_coldkey.side_effect = side_effect - result = utils.unlock_key(wallet=mock_wallet) + fake_wallet.unlock_coldkey.side_effect = side_effect + result = utils.unlock_key(wallet=fake_wallet) assert result == response