Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from aioresponses import aioresponses
from bittensor_wallet import Wallet

import bittensor.core.subtensor

Expand Down Expand Up @@ -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(
Expand Down
19 changes: 6 additions & 13 deletions tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest
import torch
import numpy as np
from bittensor_wallet import Wallet


@pytest.fixture
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
47 changes: 17 additions & 30 deletions tests/unit_tests/extrinsics/asyncex/test_registration.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
)
Expand All @@ -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",
Expand All @@ -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"

Expand Down Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
)
Expand All @@ -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"
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
)
Expand Down
Loading
Loading