Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Added coverage for set_weights #1825

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions tests/unit_tests/extrinsics/test_set_weights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import torch
import pytest
from unittest.mock import MagicMock, patch
from bittensor import subtensor, wallet
from bittensor.extrinsics.set_weights import set_weights_extrinsic


@pytest.fixture
def mock_subtensor():
mock = MagicMock(spec=subtensor)
mock.network = "mock_network"
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, prompt, user_accepts, expected_success, expected_message",
[
(
[1, 2],
[0.5, 0.5],
0,
True,
False,
True,
True,
True,
"Successfully set weights and Finalized.",
),
(
[1, 2],
[0.5, 0.4],
0,
False,
False,
False,
True,
True,
"Not waiting for finalization or inclusion.",
),
([1, 2], [0.5, 0.5], 0, True, False, True, True, False, "Mock error message"),
([1, 2], [0.5, 0.5], 0, True, True, True, False, False, "Prompt refused."),
],
ids=[
"happy-flow",
"not-waiting-finalization-inclusion",
"error-flow",
"prompt-refused",
],
)
def test_set_weights_extrinsic(
mock_subtensor,
mock_wallet,
uids,
weights,
version_key,
wait_for_inclusion,
wait_for_finalization,
prompt,
user_accepts,
expected_success,
expected_message,
):
uids_tensor = torch.tensor(uids, dtype=torch.int64)
weights_tensor = torch.tensor(weights, dtype=torch.float32)
with patch(
"bittensor.utils.weight_utils.convert_weights_and_uids_for_emit",
return_value=(uids_tensor, weights_tensor),
), patch("rich.prompt.Confirm.ask", return_value=user_accepts), patch.object(
mock_subtensor,
"_do_set_weights",
return_value=(expected_success, "Mock error message"),
) as mock_do_set_weights:
result, message = set_weights_extrinsic(
subtensor=mock_subtensor,
wallet=mock_wallet,
netuid=123,
uids=uids,
weights=weights,
version_key=version_key,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
prompt=prompt,
)

assert result == expected_success, f"Test {expected_message} failed."
assert message == expected_message, f"Test {expected_message} failed."
if user_accepts is not False:
mock_do_set_weights.assert_called_once_with(
wallet=mock_wallet,
netuid=123,
uids=uids_tensor,
vals=weights_tensor,
version_key=version_key,
wait_for_finalization=wait_for_finalization,
wait_for_inclusion=wait_for_inclusion,
)