-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into release/6.7.2
- Loading branch information
Showing
7 changed files
with
718 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
from bittensor.subtensor import subtensor as Subtensor | ||
from bittensor.wallet import wallet as Wallet | ||
|
||
|
||
# Mock the bittensor and related modules to avoid real network calls and wallet operations | ||
@pytest.fixture | ||
def mock_subtensor(): | ||
subtensor = MagicMock(spec=Subtensor) | ||
subtensor.get_balance.return_value = 100 | ||
subtensor.get_subnet_burn_cost.return_value = 10 | ||
return subtensor | ||
|
||
|
||
@pytest.fixture | ||
def mock_wallet(): | ||
wallet = MagicMock(spec=Wallet) | ||
wallet.coldkeypub.ss58_address = "fake_address" | ||
wallet.coldkey = MagicMock() | ||
return wallet | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_id, wait_for_inclusion, wait_for_finalization, prompt, expected", | ||
[ | ||
("happy-path-01", False, True, False, True), | ||
("happy-path-02", True, False, False, True), | ||
("happy-path-03", False, False, False, True), | ||
("happy-path-04", True, True, False, True), | ||
], | ||
) | ||
def test_register_subnetwork_extrinsic_happy_path( | ||
mock_subtensor, | ||
mock_wallet, | ||
test_id, | ||
wait_for_inclusion, | ||
wait_for_finalization, | ||
prompt, | ||
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 | ||
) | ||
|
||
# Assert | ||
assert result == expected | ||
|
||
|
||
# Edge cases | ||
@pytest.mark.parametrize( | ||
"test_id, balance, burn_cost, prompt_input, expected", | ||
[ | ||
("edge-case-01", 0, 10, False, False), # Balance is zero | ||
("edge-case-02", 10, 10, False, False), # Balance equals burn cost | ||
("edge-case-03", 9, 10, False, False), # Balance less than burn cost | ||
("edge-case-04", 100, 10, True, True), # User declines prompt | ||
], | ||
) | ||
def test_register_subnetwork_extrinsic_edge_cases( | ||
mock_subtensor, | ||
mock_wallet, | ||
test_id, | ||
balance, | ||
burn_cost, | ||
prompt_input, | ||
expected, | ||
monkeypatch, | ||
): | ||
# Arrange | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
import bittensor | ||
from bittensor.subtensor import subtensor as Subtensor | ||
from bittensor.wallet import wallet as Wallet | ||
from bittensor.extrinsics.prometheus import prometheus_extrinsic | ||
|
||
|
||
# Mocking the bittensor and networking modules | ||
@pytest.fixture | ||
def mock_bittensor(): | ||
with patch("bittensor.subtensor") as mock: | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_wallet(): | ||
with patch("bittensor.wallet") as mock: | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_net(): | ||
with patch("bittensor.utils.networking") as mock: | ||
yield mock | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"ip, port, netuid, wait_for_inclusion, wait_for_finalization, expected_result, test_id", | ||
[ | ||
(None, 9221, 0, False, True, True, "happy-path-default-ip"), | ||
("192.168.0.1", 9221, 0, False, True, True, "happy-path-custom-ip"), | ||
(None, 9221, 0, True, False, True, "happy-path-wait-for-inclusion"), | ||
(None, 9221, 0, False, False, True, "happy-path-no-waiting"), | ||
], | ||
) | ||
def test_prometheus_extrinsic_happy_path( | ||
mock_bittensor, | ||
mock_wallet, | ||
mock_net, | ||
ip, | ||
port, | ||
netuid, | ||
wait_for_inclusion, | ||
wait_for_finalization, | ||
expected_result, | ||
test_id, | ||
): | ||
# Arrange | ||
subtensor = MagicMock(spec=Subtensor) | ||
subtensor.network = "test_network" | ||
wallet = MagicMock(spec=Wallet) | ||
mock_net.get_external_ip.return_value = "192.168.0.1" | ||
mock_net.ip_to_int.return_value = 3232235521 # IP in integer form | ||
mock_net.ip_version.return_value = 4 | ||
neuron = MagicMock() | ||
neuron.is_null = False | ||
neuron.prometheus_info.version = bittensor.__version_as_int__ | ||
neuron.prometheus_info.ip = 3232235521 | ||
neuron.prometheus_info.port = port | ||
neuron.prometheus_info.ip_type = 4 | ||
subtensor.get_neuron_for_pubkey_and_subnet.return_value = neuron | ||
subtensor._do_serve_prometheus.return_value = (True, None) | ||
|
||
# Act | ||
result = prometheus_extrinsic( | ||
subtensor=subtensor, | ||
wallet=wallet, | ||
ip=ip, | ||
port=port, | ||
netuid=netuid, | ||
wait_for_inclusion=wait_for_inclusion, | ||
wait_for_finalization=wait_for_finalization, | ||
) | ||
|
||
# Assert | ||
assert result == expected_result, f"Test ID: {test_id}" | ||
|
||
|
||
# Edge cases | ||
@pytest.mark.parametrize( | ||
"ip, port, netuid, test_id", | ||
[ | ||
("0.0.0.0", 0, 0, "edge-case-min-values"), | ||
("255.255.255.255", 65535, 2147483647, "edge-case-max-values"), | ||
], | ||
) | ||
def test_prometheus_extrinsic_edge_cases( | ||
mock_bittensor, mock_wallet, mock_net, ip, port, netuid, test_id | ||
): | ||
# Arrange | ||
subtensor = MagicMock(spec=Subtensor) | ||
subtensor.network = "test_network" | ||
wallet = MagicMock(spec=Wallet) | ||
mock_net.get_external_ip.return_value = ip | ||
mock_net.ip_to_int.return_value = 3232235521 # IP in integer form | ||
mock_net.ip_version.return_value = 4 | ||
neuron = MagicMock() | ||
neuron.is_null = True | ||
subtensor.get_neuron_for_pubkey_and_subnet.return_value = neuron | ||
subtensor._do_serve_prometheus.return_value = (True, None) | ||
|
||
# Act | ||
result = prometheus_extrinsic( | ||
subtensor=subtensor, | ||
wallet=wallet, | ||
ip=ip, | ||
port=port, | ||
netuid=netuid, | ||
wait_for_inclusion=False, | ||
wait_for_finalization=True, | ||
) | ||
|
||
# Assert | ||
assert result == True, f"Test ID: {test_id}" | ||
|
||
|
||
# Error cases | ||
@pytest.mark.parametrize( | ||
"ip, port, netuid, exception, test_id", | ||
[ | ||
( | ||
None, | ||
9221, | ||
0, | ||
RuntimeError("Unable to attain your external ip."), | ||
"error-case-no-external-ip", | ||
), | ||
], | ||
) | ||
def test_prometheus_extrinsic_error_cases( | ||
mock_bittensor, mock_wallet, mock_net, ip, port, netuid, exception, test_id | ||
): | ||
# Arrange | ||
subtensor = MagicMock(spec=Subtensor) | ||
subtensor.network = "test_network" | ||
wallet = MagicMock(spec=Wallet) | ||
mock_net.get_external_ip.side_effect = exception | ||
neuron = MagicMock() | ||
neuron.is_null = True | ||
subtensor.get_neuron_for_pubkey_and_subnet.return_value = neuron | ||
subtensor._do_serve_prometheus.return_value = (True,) | ||
|
||
# Act & Assert | ||
with pytest.raises(ValueError): | ||
prometheus_extrinsic( | ||
subtensor=subtensor, | ||
wallet=wallet, | ||
ip=ip, | ||
port=port, | ||
netuid=netuid, | ||
wait_for_inclusion=False, | ||
wait_for_finalization=True, | ||
) |
Oops, something went wrong.