Skip to content

Commit b34d640

Browse files
Merge pull request #2724 from opentensor/tests/zyzniewski/refactor_duplicated_code
Refactor duplicated unittests code
2 parents 8922cb5 + 66be4e9 commit b34d640

16 files changed

+90
-161
lines changed

tests/helpers/helpers.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import time
44
from collections import deque
5-
from typing import Union
5+
from typing import Optional, Union
66

77
from bittensor_wallet.mock.wallet_mock import MockWallet as _MockWallet
88
from bittensor_wallet.mock.wallet_mock import get_mock_coldkey
@@ -44,6 +44,45 @@ def __eq__(self, __o: Union[float, int, Balance]) -> bool:
4444
) or ((__o - self.tolerance) <= self.value <= (__o + self.tolerance))
4545

4646

47+
def assert_submit_signed_extrinsic(
48+
substrate,
49+
keypair,
50+
call_module,
51+
call_function,
52+
call_params: Optional[dict] = None,
53+
era: Optional[dict] = None,
54+
nonce: Optional[int] = None,
55+
wait_for_inclusion: bool = False,
56+
wait_for_finalization: bool = True,
57+
):
58+
substrate.compose_call.assert_called_with(
59+
call_module,
60+
call_function,
61+
call_params,
62+
)
63+
64+
extrinsic = {
65+
"call": substrate.compose_call.return_value,
66+
"keypair": keypair,
67+
}
68+
69+
if era:
70+
extrinsic["era"] = era
71+
72+
if nonce:
73+
extrinsic["nonce"] = nonce
74+
75+
substrate.create_signed_extrinsic.assert_called_with(
76+
**extrinsic,
77+
)
78+
79+
substrate.submit_extrinsic.assert_called_with(
80+
substrate.create_signed_extrinsic.return_value,
81+
wait_for_inclusion=wait_for_inclusion,
82+
wait_for_finalization=wait_for_finalization,
83+
)
84+
85+
4786
def get_mock_neuron(**kwargs) -> NeuronInfo:
4887
"""
4988
Returns a mock neuron with the given kwargs overriding the default values.

tests/unit_tests/conftest.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22
from aioresponses import aioresponses
3-
from async_substrate_interface.sync_substrate import SubstrateInterface
43

54
import bittensor.core.subtensor
65

@@ -17,18 +16,17 @@ def mock_aio_response():
1716

1817

1918
@pytest.fixture
20-
def mock_substrate_interface(mocker):
21-
mocked = mocker.MagicMock(
22-
autospec=SubstrateInterface,
19+
def mock_substrate(mocker):
20+
mocked = mocker.patch(
21+
"bittensor.core.subtensor.SubstrateInterface",
22+
autospec=True,
2323
)
2424

25-
mocker.patch("bittensor.core.subtensor.SubstrateInterface", return_value=mocked)
26-
27-
return mocked
25+
return mocked.return_value
2826

2927

3028
@pytest.fixture
31-
def subtensor(mock_substrate_interface):
29+
def subtensor(mock_substrate):
3230
return bittensor.core.subtensor.Subtensor()
3331

3432

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
import bittensor.core.async_subtensor
4+
5+
6+
@pytest.fixture
7+
def mock_substrate(mocker):
8+
mocked = mocker.patch(
9+
"bittensor.core.async_subtensor.AsyncSubstrateInterface",
10+
autospec=True,
11+
)
12+
13+
return mocked.return_value
14+
15+
16+
@pytest.fixture
17+
def subtensor(mock_substrate):
18+
return bittensor.core.async_subtensor.AsyncSubtensor()

tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
from bittensor.core import async_subtensor as subtensor_module
22
from bittensor.core.chain_data import SubnetHyperparameters
3-
from bittensor.core.async_subtensor import AsyncSubtensor
43
from bittensor.core.extrinsics.asyncex import commit_reveal as async_commit_reveal
54
import pytest
65
import torch
76
import numpy as np
87
from bittensor_wallet import Wallet
98

109

11-
@pytest.fixture
12-
def subtensor(mocker):
13-
fake_substrate = mocker.AsyncMock()
14-
fake_substrate.websocket.socket.getsockopt.return_value = 0
15-
mocker.patch.object(
16-
subtensor_module, "AsyncSubstrateInterface", return_value=fake_substrate
17-
)
18-
yield AsyncSubtensor()
19-
20-
2110
@pytest.fixture
2211
def hyperparams():
2312
yield SubnetHyperparameters(

tests/unit_tests/extrinsics/asyncex/test_registration.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55
from bittensor.core.extrinsics.asyncex import registration as async_registration
66

77

8-
@pytest.fixture(autouse=True)
9-
def subtensor(mocker):
10-
fake_async_substrate = mocker.AsyncMock(
11-
autospec=async_subtensor.AsyncSubstrateInterface
12-
)
13-
mocker.patch.object(
14-
async_subtensor, "AsyncSubstrateInterface", return_value=fake_async_substrate
15-
)
16-
return async_subtensor.AsyncSubtensor()
17-
18-
198
@pytest.mark.asyncio
209
async def test_do_pow_register_success(subtensor, mocker):
2110
"""Tests successful PoW registration."""

tests/unit_tests/extrinsics/asyncex/test_root.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import pytest
22

3-
from bittensor.core import async_subtensor
43
from bittensor.core.errors import SubstrateRequestException
54
from bittensor.core.extrinsics.asyncex import root as async_root
65
from bittensor_wallet import Wallet
76

87

9-
@pytest.fixture(autouse=True)
10-
def subtensor(mocker):
11-
fake_async_substrate = mocker.AsyncMock(
12-
autospec=async_subtensor.AsyncSubstrateInterface
13-
)
14-
mocker.patch.object(
15-
async_subtensor, "AsyncSubstrateInterface", return_value=fake_async_substrate
16-
)
17-
return async_subtensor.AsyncSubtensor()
18-
19-
208
@pytest.mark.asyncio
219
async def test_get_limits_success(subtensor, mocker):
2210
"""Tests successful retrieval of weight limits."""

tests/unit_tests/extrinsics/asyncex/test_transfer.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import pytest
2-
from bittensor.core import async_subtensor
32
from bittensor_wallet import Wallet
43
from bittensor.core.extrinsics.asyncex import transfer as async_transfer
54
from bittensor.utils.balance import Balance
65

76

8-
@pytest.fixture(autouse=True)
9-
def subtensor(mocker):
10-
fake_async_substrate = mocker.AsyncMock(
11-
autospec=async_subtensor.AsyncSubstrateInterface
12-
)
13-
mocker.patch.object(
14-
async_subtensor, "AsyncSubstrateInterface", return_value=fake_async_substrate
15-
)
16-
return async_subtensor.AsyncSubtensor()
17-
18-
197
@pytest.mark.asyncio
208
async def test_do_transfer_success(subtensor, mocker):
219
"""Tests _do_transfer when the transfer is successful."""
@@ -460,7 +448,7 @@ async def test_transfer_extrinsic_keep_alive_false_and_transfer_all_true(
460448
mocked_get_chain_head = mocker.patch.object(
461449
subtensor.substrate, "get_chain_head", return_value="some_block_hash"
462450
)
463-
mocked_get_balance = mocker.patch.object(
451+
mocker.patch.object(
464452
subtensor,
465453
"get_balance",
466454
return_value=1,

tests/unit_tests/extrinsics/asyncex/test_weights.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44
from bittensor.core.extrinsics.asyncex import weights as async_weights
55

66

7-
@pytest.fixture(autouse=True)
8-
def subtensor(mocker):
9-
fake_async_substrate = mocker.AsyncMock(
10-
autospec=async_subtensor.AsyncSubstrateInterface
11-
)
12-
mocker.patch.object(
13-
async_subtensor, "AsyncSubstrateInterface", return_value=fake_async_substrate
14-
)
15-
return async_subtensor.AsyncSubtensor()
16-
17-
187
@pytest.mark.asyncio
198
async def test_do_set_weights_success(subtensor, mocker):
209
"""Tests _do_set_weights when weights are set successfully."""
@@ -56,7 +45,7 @@ async def fake_is_success():
5645

5746
# Asserts
5847
assert result is True
59-
assert message is ""
48+
assert message == ""
6049

6150

6251
@pytest.mark.asyncio
@@ -146,7 +135,7 @@ async def test_do_set_weights_no_waiting(subtensor, mocker):
146135

147136
# Asserts
148137
assert result is True
149-
assert message is ""
138+
assert message == ""
150139

151140

152141
@pytest.mark.asyncio
@@ -316,7 +305,7 @@ async def fake_is_success():
316305

317306
# Asserts
318307
assert result is True
319-
assert message is ""
308+
assert message == ""
320309

321310

322311
@pytest.mark.asyncio
@@ -399,7 +388,7 @@ async def test_do_commit_weights_no_waiting(subtensor, mocker):
399388

400389
# Asserts
401390
assert result is True
402-
assert message is ""
391+
assert message == ""
403392

404393

405394
@pytest.mark.asyncio

tests/unit_tests/extrinsics/test_commit_reveal.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperp
160160
"convert_weights_and_uids_for_emit",
161161
return_value=(mocked_uids, mocked_weights),
162162
)
163-
mocked_get_subnet_reveal_period_epochs = mocker.patch.object(
164-
subtensor, "get_subnet_reveal_period_epochs"
165-
)
163+
mocker.patch.object(subtensor, "get_subnet_reveal_period_epochs")
166164
mocked_get_encrypted_commit = mocker.patch.object(
167165
commit_reveal,
168166
"get_encrypted_commit",

tests/unit_tests/extrinsics/test_staking.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from bittensor.core.extrinsics import staking
2-
from bittensor.core.extrinsics import utils
32
from bittensor.utils.balance import Balance
43

54

0 commit comments

Comments
 (0)