diff --git a/tests/unit_tests/extrinsics/test_network.py b/tests/unit_tests/extrinsics/test_network.py index 160348c3e6..a11f53111f 100644 --- a/tests/unit_tests/extrinsics/test_network.py +++ b/tests/unit_tests/extrinsics/test_network.py @@ -2,6 +2,10 @@ from unittest.mock import MagicMock, patch from bittensor.subtensor import subtensor as Subtensor from bittensor.wallet import wallet as Wallet +from bittensor.extrinsics.network import ( + set_hyperparameter_extrinsic, + register_subnetwork_extrinsic, +) # Mock the bittensor and related modules to avoid real network calls and wallet operations @@ -10,6 +14,8 @@ def mock_subtensor(): subtensor = MagicMock(spec=Subtensor) subtensor.get_balance.return_value = 100 subtensor.get_subnet_burn_cost.return_value = 10 + subtensor.substrate = MagicMock() + subtensor.substrate.get_block_hash = MagicMock(return_value="0x" + "0" * 64) return subtensor @@ -21,6 +27,13 @@ def mock_wallet(): return wallet +@pytest.fixture +def mock_other_owner_wallet(): + wallet = MagicMock(spec=Wallet) + wallet.coldkeypub.ss58_address = "fake_other_owner" + return wallet + + @pytest.mark.parametrize( "test_id, wait_for_inclusion, wait_for_finalization, prompt, expected", [ @@ -40,15 +53,9 @@ def test_register_subnetwork_extrinsic_happy_path( expected, ): # Arrange - mock_subtensor.substrate = MagicMock( - get_block_hash=MagicMock(return_value="0x" + "0" * 64), - submit_extrinsic=MagicMock(), - ) mock_subtensor.substrate.submit_extrinsic.return_value.is_success = True # Act - from bittensor.extrinsics.network import register_subnetwork_extrinsic - result = register_subnetwork_extrinsic( mock_subtensor, mock_wallet, wait_for_inclusion, wait_for_finalization, prompt ) @@ -81,12 +88,70 @@ def test_register_subnetwork_extrinsic_edge_cases( mock_subtensor.get_balance.return_value = balance mock_subtensor.get_subnet_burn_cost.return_value = burn_cost monkeypatch.setattr("rich.prompt.Confirm.ask", lambda x: prompt_input) - mock_subtensor.substrate = MagicMock() # Act - from bittensor.extrinsics.network import register_subnetwork_extrinsic - result = register_subnetwork_extrinsic(mock_subtensor, mock_wallet, prompt=True) # Assert assert result == expected + + +@pytest.mark.parametrize( + "netuid, parameter, value, is_owner, wait_for_inclusion, wait_for_finalization, prompt, extrinsic_success, expected_result", + [ + # Success - no wait + (1, "serving_rate_limit", 49, True, False, False, False, True, True), + # Success - with wait + (1, "serving_rate_limit", 50, True, True, True, False, True, True), + # Failure - wallet doesn't own subnet + (1, "serving_rate_limit", 50, False, True, True, False, True, False), + # Failure - invalid hyperparameter + (1, None, 50, True, True, False, False, False, False), + ], + ids=[ + "success-no-wait", + "success-with-wait", + "failure-not-owner", + "failure-invalid-hyperparameter", + ], +) +def test_set_hyperparameter_extrinsic( + mock_subtensor, + mock_wallet, + mock_other_owner_wallet, + netuid, + parameter, + value, + is_owner, + wait_for_inclusion, + wait_for_finalization, + prompt, + extrinsic_success, + expected_result, +): + # Arrange + with patch.object( + mock_subtensor, + "get_subnet_owner", + return_value=mock_wallet.coldkeypub.ss58_address + if is_owner + else mock_other_owner_wallet.coldkeypub.ss58_address, + ), patch.object( + mock_subtensor.substrate, + "submit_extrinsic", + return_value=MagicMock(is_success=extrinsic_success), + ): + # Act + result = set_hyperparameter_extrinsic( + subtensor=mock_subtensor, + wallet=mock_wallet, + netuid=netuid, + parameter=parameter, + value=value, + wait_for_inclusion=wait_for_inclusion, + wait_for_finalization=wait_for_finalization, + prompt=prompt, + ) + + # Assert + assert result == expected_result