Skip to content

Commit bf70ddd

Browse files
author
Roman
committed
add unit tests
1 parent 6b6fbd9 commit bf70ddd

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

tests/unit_tests/test_async_subtensor.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,3 +3875,85 @@ async def fake_current_sqrt_prices():
38753875
page_size=129, # total number of subnets
38763876
)
38773877
assert result == expected_prices
3878+
3879+
3880+
@pytest.mark.asyncio
3881+
async def test_all_subnets(subtensor, mocker):
3882+
"""Verify that `all_subnets` calls proper methods and returns the correct value."""
3883+
# Preps
3884+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
3885+
mocked_di_list_from_dicts = mocker.patch.object(
3886+
async_subtensor.DynamicInfo, "list_from_dicts"
3887+
)
3888+
mocked_get_subnet_prices = mocker.patch.object(
3889+
subtensor,
3890+
"get_subnet_prices",
3891+
return_value={0: Balance.from_tao(1), 1: Balance.from_tao(0.029258617)},
3892+
)
3893+
mocked_decode = mocker.Mock(return_value=[{"netuid": 0}, {"netuid": 1}])
3894+
mocked_runtime_call = mocker.Mock(decode=mocked_decode)
3895+
mocker.patch.object(
3896+
subtensor.substrate, "runtime_call", return_value=mocked_runtime_call
3897+
)
3898+
3899+
# Call
3900+
result = await subtensor.all_subnets()
3901+
3902+
# Asserts
3903+
mocked_determine_block_hash.assert_awaited_once_with(
3904+
block=None, block_hash=None, reuse_block=False
3905+
)
3906+
subtensor.substrate.runtime_call.assert_called_once_with(
3907+
api="SubnetInfoRuntimeApi",
3908+
method="get_all_dynamic_info",
3909+
block_hash=mocked_determine_block_hash.return_value,
3910+
)
3911+
mocked_get_subnet_prices.assert_called_once()
3912+
mocked_di_list_from_dicts.assert_called_once_with(
3913+
[
3914+
{"netuid": 0, "price": Balance.from_tao(1)},
3915+
{"netuid": 1, "price": Balance.from_tao(0.029258617)},
3916+
]
3917+
)
3918+
assert result == mocked_di_list_from_dicts.return_value
3919+
3920+
3921+
@pytest.mark.asyncio
3922+
async def test_subnet(subtensor, mocker):
3923+
"""Verify that `subnet` calls proper methods and returns the correct value."""
3924+
# Preps
3925+
netuid = 14
3926+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
3927+
mocked_di_from_dict = mocker.patch.object(async_subtensor.DynamicInfo, "from_dict")
3928+
mocked_get_subnet_price = mocker.patch.object(
3929+
subtensor, "get_subnet_price", return_value=Balance.from_tao(100.0)
3930+
)
3931+
mocked_decode = mocker.Mock(return_value={"netuid": netuid})
3932+
mocked_runtime_call = mocker.Mock(decode=mocked_decode)
3933+
mocker.patch.object(
3934+
subtensor.substrate, "runtime_call", return_value=mocked_runtime_call
3935+
)
3936+
3937+
# Call
3938+
result = await subtensor.subnet(netuid=netuid)
3939+
3940+
# Asserts
3941+
mocked_determine_block_hash.assert_awaited_once_with(
3942+
block=None, block_hash=None, reuse_block=False
3943+
)
3944+
subtensor.substrate.runtime_call.assert_awaited_once_with(
3945+
"SubnetInfoRuntimeApi",
3946+
"get_dynamic_info",
3947+
params=[netuid],
3948+
block_hash=mocked_determine_block_hash.return_value,
3949+
)
3950+
mocked_get_subnet_price.assert_awaited_once_with(
3951+
netuid=netuid,
3952+
block=None,
3953+
block_hash=mocked_determine_block_hash.return_value,
3954+
reuse_block=False,
3955+
)
3956+
mocked_di_from_dict.assert_called_once_with(
3957+
{"netuid": netuid, "price": Balance.from_tao(100.0)}
3958+
)
3959+
assert result == mocked_di_from_dict.return_value

tests/unit_tests/test_subtensor.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,3 +4195,74 @@ def test_get_subnet_prices(subtensor, mocker):
41954195
page_size=129, # total number of subnets
41964196
)
41974197
assert result == expected_prices
4198+
4199+
4200+
def test_all_subnets(subtensor, mocker):
4201+
"""Verify that `all_subnets` calls proper methods and returns the correct value."""
4202+
# Preps
4203+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
4204+
mocked_di_list_from_dicts = mocker.patch.object(
4205+
subtensor_module.DynamicInfo, "list_from_dicts"
4206+
)
4207+
mocked_get_subnet_prices = mocker.patch.object(
4208+
subtensor,
4209+
"get_subnet_prices",
4210+
return_value={0: Balance.from_tao(1), 1: Balance.from_tao(0.029258617)},
4211+
)
4212+
mocked_decode = mocker.Mock(return_value=[{"netuid": 0}, {"netuid": 1}])
4213+
mocked_runtime_call = mocker.Mock(decode=mocked_decode)
4214+
mocker.patch.object(
4215+
subtensor.substrate, "runtime_call", return_value=mocked_runtime_call
4216+
)
4217+
4218+
# Call
4219+
result = subtensor.all_subnets()
4220+
4221+
# Asserts
4222+
mocked_determine_block_hash.assert_called_once_with(block=None)
4223+
subtensor.substrate.runtime_call.assert_called_once_with(
4224+
api="SubnetInfoRuntimeApi",
4225+
method="get_all_dynamic_info",
4226+
block_hash=mocked_determine_block_hash.return_value,
4227+
)
4228+
mocked_get_subnet_prices.assert_called_once()
4229+
mocked_di_list_from_dicts.assert_called_once_with(
4230+
[
4231+
{"netuid": 0, "price": Balance.from_tao(1)},
4232+
{"netuid": 1, "price": Balance.from_tao(0.029258617)},
4233+
]
4234+
)
4235+
assert result == mocked_di_list_from_dicts.return_value
4236+
4237+
4238+
def test_subnet(subtensor, mocker):
4239+
"""Verify that `subnet` calls proper methods and returns the correct value."""
4240+
# Preps
4241+
netuid = 14
4242+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
4243+
mocked_di_from_dict = mocker.patch.object(subtensor_module.DynamicInfo, "from_dict")
4244+
mocked_get_subnet_price = mocker.patch.object(
4245+
subtensor, "get_subnet_price", return_value=Balance.from_tao(100.0)
4246+
)
4247+
mocked_decode = mocker.Mock(return_value={"netuid": netuid})
4248+
mocked_runtime_call = mocker.Mock(decode=mocked_decode)
4249+
mocker.patch.object(
4250+
subtensor.substrate, "runtime_call", return_value=mocked_runtime_call
4251+
)
4252+
4253+
# Call
4254+
result = subtensor.subnet(netuid=netuid)
4255+
4256+
# Asserts
4257+
subtensor.substrate.runtime_call.assert_called_once_with(
4258+
api="SubnetInfoRuntimeApi",
4259+
method="get_dynamic_info",
4260+
params=[netuid],
4261+
block_hash=mocked_determine_block_hash.return_value,
4262+
)
4263+
mocked_determine_block_hash.assert_called_once_with(block=None)
4264+
mocked_get_subnet_price.assert_called_once_with(netuid=netuid, block=None)
4265+
mocked_di_from_dict.assert_called_once_with(
4266+
{"netuid": netuid, "price": Balance.from_tao(100.0)}
4267+
)
4268+
assert result == mocked_di_from_dict.return_value

0 commit comments

Comments
 (0)