Skip to content

Commit d6ae4fa

Browse files
Merge pull request #2730 from opentensor/tests/zyzniewski/fix_Wallet_spec
Tests: deduplicate fake_wallet and correctly create Mock
2 parents 37562ab + c4da857 commit d6ae4fa

16 files changed

+242
-327
lines changed

tests/unit_tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from aioresponses import aioresponses
3+
from bittensor_wallet import Wallet
34

45
import bittensor.core.subtensor
56

@@ -30,6 +31,11 @@ def subtensor(mock_substrate):
3031
return bittensor.core.subtensor.Subtensor()
3132

3233

34+
@pytest.fixture
35+
def fake_wallet(mocker):
36+
return mocker.Mock(spec_set=Wallet)
37+
38+
3339
@pytest.fixture
3440
def mock_get_external_ip(mocker):
3541
mocked = mocker.Mock(

tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import pytest
55
import torch
66
import numpy as np
7-
from bittensor_wallet import Wallet
87

98

109
@pytest.fixture
@@ -41,10 +40,9 @@ def hyperparams():
4140

4241

4342
@pytest.mark.asyncio
44-
async def test_do_commit_reveal_v3_success(mocker, subtensor):
43+
async def test_do_commit_reveal_v3_success(mocker, subtensor, fake_wallet):
4544
"""Test successful commit-reveal with wait for finalization."""
4645
# Preps
47-
fake_wallet = mocker.Mock(autospec=Wallet)
4846
fake_netuid = 1
4947
fake_commit = b"fake_commit"
5048
fake_reveal_round = 1
@@ -88,10 +86,9 @@ async def test_do_commit_reveal_v3_success(mocker, subtensor):
8886

8987

9088
@pytest.mark.asyncio
91-
async def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor):
89+
async def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor, fake_wallet):
9290
"""Test commit-reveal fails due to an error in submission."""
9391
# Preps
94-
fake_wallet = mocker.Mock(autospec=Wallet)
9592
fake_netuid = 1
9693
fake_commit = b"fake_commit"
9794
fake_reveal_round = 1
@@ -150,11 +147,10 @@ async def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor):
150147

151148
@pytest.mark.asyncio
152149
async def test_commit_reveal_v3_extrinsic_success_with_torch(
153-
mocker, subtensor, hyperparams
150+
mocker, subtensor, hyperparams, fake_wallet
154151
):
155152
"""Test successful commit-reveal with torch tensors."""
156153
# Preps
157-
fake_wallet = mocker.Mock(autospec=Wallet)
158154
fake_netuid = 1
159155
fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64)
160156
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(
231227

232228
@pytest.mark.asyncio
233229
async def test_commit_reveal_v3_extrinsic_success_with_numpy(
234-
mocker, subtensor, hyperparams
230+
mocker, subtensor, hyperparams, fake_wallet
235231
):
236232
"""Test successful commit-reveal with numpy arrays."""
237233
# Preps
238-
fake_wallet = mocker.Mock(autospec=Wallet)
239234
fake_netuid = 1
240235
fake_uids = np.array([1, 2, 3], dtype=np.int64)
241236
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(
279274

280275
@pytest.mark.asyncio
281276
async def test_commit_reveal_v3_extrinsic_response_false(
282-
mocker, subtensor, hyperparams
277+
mocker, subtensor, hyperparams, fake_wallet
283278
):
284279
"""Test unsuccessful commit-reveal with torch."""
285280
# Preps
286-
fake_wallet = mocker.Mock(autospec=Wallet)
287281
fake_netuid = 1
288282
fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64)
289283
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(
337331

338332

339333
@pytest.mark.asyncio
340-
async def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor):
334+
async def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor, fake_wallet):
341335
"""Test exception handling in commit-reveal."""
342336
# Preps
343-
fake_wallet = mocker.Mock(autospec=Wallet)
344337
fake_netuid = 1
345338
fake_uids = [1, 2, 3]
346339
fake_weights = [0.1, 0.2, 0.7]

tests/unit_tests/extrinsics/asyncex/test_registration.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import pytest
2-
from bittensor_wallet import Wallet
32

43
from bittensor.core import async_subtensor
54
from bittensor.core.extrinsics.asyncex import registration as async_registration
65

76

87
@pytest.mark.asyncio
9-
async def test_do_pow_register_success(subtensor, mocker):
8+
async def test_do_pow_register_success(subtensor, fake_wallet, mocker):
109
"""Tests successful PoW registration."""
1110
# Preps
12-
fake_wallet = mocker.Mock(autospec=Wallet)
1311
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
1412
fake_wallet.coldkeypub.ss58_address = "coldkey_ss58"
1513
fake_pow_result = mocker.Mock(
@@ -66,10 +64,9 @@ async def test_do_pow_register_success(subtensor, mocker):
6664

6765

6866
@pytest.mark.asyncio
69-
async def test_do_pow_register_failure(subtensor, mocker):
67+
async def test_do_pow_register_failure(subtensor, fake_wallet, mocker):
7068
"""Tests failed PoW registration."""
7169
# Preps
72-
fake_wallet = mocker.Mock(autospec=Wallet)
7370
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
7471
fake_wallet.coldkeypub.ss58_address = "coldkey_ss58"
7572
fake_pow_result = mocker.Mock(
@@ -121,10 +118,9 @@ async def test_do_pow_register_failure(subtensor, mocker):
121118

122119

123120
@pytest.mark.asyncio
124-
async def test_do_pow_register_no_waiting(subtensor, mocker):
121+
async def test_do_pow_register_no_waiting(subtensor, fake_wallet, mocker):
125122
"""Tests PoW registration without waiting for inclusion or finalization."""
126123
# Preps
127-
fake_wallet = mocker.Mock(autospec=Wallet)
128124
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
129125
fake_wallet.coldkeypub.ss58_address = "coldkey_ss58"
130126
fake_pow_result = mocker.Mock(
@@ -166,10 +162,9 @@ async def test_do_pow_register_no_waiting(subtensor, mocker):
166162

167163

168164
@pytest.mark.asyncio
169-
async def test_register_extrinsic_success(subtensor, mocker):
165+
async def test_register_extrinsic_success(subtensor, fake_wallet, mocker):
170166
"""Tests successful registration."""
171167
# Preps
172-
fake_wallet = mocker.Mock(autospec=Wallet)
173168
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
174169
fake_wallet.coldkey.ss58_address = "coldkey_ss58"
175170

@@ -221,10 +216,9 @@ async def test_register_extrinsic_success(subtensor, mocker):
221216

222217

223218
@pytest.mark.asyncio
224-
async def test_register_extrinsic_success_with_cuda(subtensor, mocker):
219+
async def test_register_extrinsic_success_with_cuda(subtensor, fake_wallet, mocker):
225220
"""Tests successful registration with CUDA enabled."""
226221
# Preps
227-
fake_wallet = mocker.Mock(autospec=Wallet)
228222
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
229223
fake_wallet.coldkey.ss58_address = "coldkey_ss58"
230224

@@ -278,10 +272,9 @@ async def test_register_extrinsic_success_with_cuda(subtensor, mocker):
278272

279273

280274
@pytest.mark.asyncio
281-
async def test_register_extrinsic_failed_with_cuda(subtensor, mocker):
275+
async def test_register_extrinsic_failed_with_cuda(subtensor, fake_wallet, mocker):
282276
"""Tests failed registration with CUDA enabled."""
283277
# Preps
284-
fake_wallet = mocker.Mock(autospec=Wallet)
285278
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
286279
fake_wallet.coldkey.ss58_address = "coldkey_ss58"
287280

@@ -319,11 +312,9 @@ async def test_register_extrinsic_failed_with_cuda(subtensor, mocker):
319312

320313

321314
@pytest.mark.asyncio
322-
async def test_register_extrinsic_subnet_not_exists(subtensor, mocker):
315+
async def test_register_extrinsic_subnet_not_exists(subtensor, fake_wallet, mocker):
323316
"""Tests registration when subnet does not exist."""
324317
# Preps
325-
fake_wallet = mocker.Mock(autospec=Wallet)
326-
327318
mocked_subnet_exists = mocker.patch.object(
328319
subtensor, "subnet_exists", return_value=False
329320
)
@@ -344,10 +335,9 @@ async def test_register_extrinsic_subnet_not_exists(subtensor, mocker):
344335

345336

346337
@pytest.mark.asyncio
347-
async def test_register_extrinsic_already_registered(subtensor, mocker):
338+
async def test_register_extrinsic_already_registered(subtensor, fake_wallet, mocker):
348339
"""Tests registration when the key is already registered."""
349340
# Preps
350-
fake_wallet = mocker.Mock(autospec=Wallet)
351341
mocked_get_neuron = mocker.patch.object(
352342
subtensor,
353343
"get_neuron_for_pubkey_and_subnet",
@@ -371,9 +361,8 @@ async def test_register_extrinsic_already_registered(subtensor, mocker):
371361

372362

373363
@pytest.mark.asyncio
374-
async def test_register_extrinsic_max_attempts_reached(subtensor, mocker):
364+
async def test_register_extrinsic_max_attempts_reached(subtensor, fake_wallet, mocker):
375365
# Preps
376-
fake_wallet = mocker.Mock(autospec=Wallet)
377366
fake_wallet.hotkey.ss58_address = "hotkey_ss58"
378367
fake_wallet.coldkey.ss58_address = "coldkey_ss58"
379368

@@ -439,10 +428,9 @@ async def is_stale_side_effect(*_, **__):
439428

440429

441430
@pytest.mark.asyncio
442-
async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker):
431+
async def test_set_subnet_identity_extrinsic_is_success(subtensor, fake_wallet, mocker):
443432
"""Verify that set_subnet_identity_extrinsic calls the correct functions and returns the correct result."""
444433
# Preps
445-
wallet = mocker.MagicMock(autospec=Wallet)
446434
netuid = 123
447435
subnet_name = "mock_subnet_name"
448436
github_repo = "mock_github_repo"
@@ -463,7 +451,7 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker):
463451
# Call
464452
result = await async_registration.set_subnet_identity_extrinsic(
465453
subtensor=subtensor,
466-
wallet=wallet,
454+
wallet=fake_wallet,
467455
netuid=netuid,
468456
subnet_name=subnet_name,
469457
github_repo=github_repo,
@@ -479,7 +467,7 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker):
479467
call_module="SubtensorModule",
480468
call_function="set_subnet_identity",
481469
call_params={
482-
"hotkey": wallet.hotkey.ss58_address,
470+
"hotkey": fake_wallet.hotkey.ss58_address,
483471
"netuid": netuid,
484472
"subnet_name": subnet_name,
485473
"github_repo": github_repo,
@@ -492,7 +480,7 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker):
492480
)
493481
mocked_submit_extrinsic.assert_awaited_once_with(
494482
call=mocked_compose_call.return_value,
495-
wallet=wallet,
483+
wallet=fake_wallet,
496484
wait_for_inclusion=False,
497485
wait_for_finalization=True,
498486
)
@@ -501,10 +489,9 @@ async def test_set_subnet_identity_extrinsic_is_success(subtensor, mocker):
501489

502490

503491
@pytest.mark.asyncio
504-
async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker):
492+
async def test_set_subnet_identity_extrinsic_is_failed(subtensor, fake_wallet, mocker):
505493
"""Verify that set_subnet_identity_extrinsic calls the correct functions and returns False with bad result."""
506494
# Preps
507-
wallet = mocker.MagicMock(autospec=Wallet)
508495
netuid = 123
509496
subnet_name = "mock_subnet_name"
510497
github_repo = "mock_github_repo"
@@ -527,7 +514,7 @@ async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker):
527514
# Call
528515
result = await async_registration.set_subnet_identity_extrinsic(
529516
subtensor=subtensor,
530-
wallet=wallet,
517+
wallet=fake_wallet,
531518
netuid=netuid,
532519
subnet_name=subnet_name,
533520
github_repo=github_repo,
@@ -545,7 +532,7 @@ async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker):
545532
call_module="SubtensorModule",
546533
call_function="set_subnet_identity",
547534
call_params={
548-
"hotkey": wallet.hotkey.ss58_address,
535+
"hotkey": fake_wallet.hotkey.ss58_address,
549536
"netuid": netuid,
550537
"subnet_name": subnet_name,
551538
"github_repo": github_repo,
@@ -558,7 +545,7 @@ async def test_set_subnet_identity_extrinsic_is_failed(subtensor, mocker):
558545
)
559546
mocked_submit_extrinsic.assert_awaited_once_with(
560547
call=mocked_compose_call.return_value,
561-
wallet=wallet,
548+
wallet=fake_wallet,
562549
wait_for_inclusion=True,
563550
wait_for_finalization=True,
564551
)

0 commit comments

Comments
 (0)