Skip to content

Commit fae12ef

Browse files
author
Roman
committed
add unit tests
1 parent de95e32 commit fae12ef

File tree

2 files changed

+204
-90
lines changed

2 files changed

+204
-90
lines changed

tests/unit_tests/test_async_subtensor.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3955,3 +3955,108 @@ async def test_subnet(subtensor, mocker):
39553955
{"netuid": netuid, "price": Balance.from_tao(100.0)}
39563956
)
39573957
assert result == mocked_di_from_dict.return_value
3958+
3959+
3960+
@pytest.mark.asyncio
3961+
async def test_get_stake_operations_fee(subtensor, mocker):
3962+
"""Verify that `get_stake_operations_fee` calls proper methods and returns the correct value."""
3963+
# Preps
3964+
netuid = 1
3965+
amount = Balance.from_rao(100_000_000_000) # 100 Tao
3966+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
3967+
mocked_query_map = mocker.patch.object(
3968+
subtensor.substrate, "query", return_value=mocker.Mock(value=196)
3969+
)
3970+
3971+
# Call
3972+
result = await subtensor.get_stake_operations_fee(netuid=netuid, amount=amount)
3973+
3974+
# Assert
3975+
mocked_determine_block_hash.assert_awaited_once_with(
3976+
block=None, block_hash=None, reuse_block=False
3977+
)
3978+
mocked_query_map.assert_awaited_once_with(
3979+
module="Swap",
3980+
storage_function="FeeRate",
3981+
params=[netuid],
3982+
block_hash=mocked_determine_block_hash.return_value,
3983+
)
3984+
assert result == Balance.from_rao(299076829).set_unit(netuid)
3985+
3986+
3987+
@pytest.mark.asyncio
3988+
async def test_get_stake_add_fee(subtensor, mocker):
3989+
"""Verify that `get_stake_add_fee` calls proper methods and returns the correct value."""
3990+
# Preps
3991+
netuid = mocker.Mock()
3992+
amount = mocker.Mock()
3993+
mocked_get_stake_operations_fee = mocker.patch.object(
3994+
subtensor, "get_stake_operations_fee"
3995+
)
3996+
3997+
# Call
3998+
result = await subtensor.get_stake_add_fee(
3999+
amount=amount,
4000+
netuid=netuid,
4001+
coldkey_ss58=mocker.Mock(),
4002+
hotkey_ss58=mocker.Mock(),
4003+
)
4004+
4005+
# Asserts
4006+
mocked_get_stake_operations_fee.assert_awaited_once_with(
4007+
amount=amount, netuid=netuid, block=None
4008+
)
4009+
assert result == mocked_get_stake_operations_fee.return_value
4010+
4011+
4012+
@pytest.mark.asyncio
4013+
async def test_get_unstake_fee(subtensor, mocker):
4014+
"""Verify that `get_unstake_fee` calls proper methods and returns the correct value."""
4015+
# Preps
4016+
netuid = mocker.Mock()
4017+
amount = mocker.Mock()
4018+
mocked_get_stake_operations_fee = mocker.patch.object(
4019+
subtensor, "get_stake_operations_fee"
4020+
)
4021+
4022+
# Call
4023+
result = await subtensor.get_unstake_fee(
4024+
amount=amount,
4025+
netuid=netuid,
4026+
coldkey_ss58=mocker.Mock(),
4027+
hotkey_ss58=mocker.Mock(),
4028+
)
4029+
4030+
# Asserts
4031+
mocked_get_stake_operations_fee.assert_awaited_once_with(
4032+
amount=amount, netuid=netuid, block=None
4033+
)
4034+
assert result == mocked_get_stake_operations_fee.return_value
4035+
4036+
4037+
@pytest.mark.asyncio
4038+
async def test_get_stake_movement_fee(subtensor, mocker):
4039+
"""Verify that `get_stake_movement_fee` calls proper methods and returns the correct value."""
4040+
# Preps
4041+
netuid = mocker.Mock()
4042+
amount = mocker.Mock()
4043+
mocked_get_stake_operations_fee = mocker.patch.object(
4044+
subtensor, "get_stake_operations_fee"
4045+
)
4046+
4047+
# Call
4048+
result = await subtensor.get_stake_movement_fee(
4049+
amount=amount,
4050+
origin_netuid=netuid,
4051+
origin_hotkey_ss58=mocker.Mock(),
4052+
origin_coldkey_ss58=mocker.Mock(),
4053+
destination_netuid=mocker.Mock(),
4054+
destination_hotkey_ss58=mocker.Mock(),
4055+
destination_coldkey_ss58=mocker.Mock(),
4056+
)
4057+
4058+
# Asserts
4059+
mocked_get_stake_operations_fee.assert_awaited_once_with(
4060+
amount=amount, netuid=netuid, block=None
4061+
)
4062+
assert result == mocked_get_stake_operations_fee.return_value

tests/unit_tests/test_subtensor.py

Lines changed: 99 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,96 +3271,6 @@ def test_get_timestamp(mocker, subtensor):
32713271
assert expected_result == actual_result
32723272

32733273

3274-
def test_stake_fee_methods(mocker, subtensor):
3275-
"""Test the three stake fee calculation methods."""
3276-
# Mock data
3277-
fake_hotkey = "hk1"
3278-
fake_coldkey = "ck1"
3279-
fake_amount = Balance.from_tao(100)
3280-
netuid = 1
3281-
fake_fee = 1_000_000
3282-
3283-
# Mock return fee
3284-
mock_query = mocker.patch.object(
3285-
subtensor,
3286-
"query_runtime_api",
3287-
side_effect=lambda runtime_api, method, params, block: (
3288-
fake_fee
3289-
if runtime_api == "StakeInfoRuntimeApi" and method == "get_stake_fee"
3290-
else None
3291-
),
3292-
)
3293-
3294-
# get_stake_add_fee
3295-
result = subtensor.get_stake_add_fee(
3296-
amount=fake_amount,
3297-
netuid=netuid,
3298-
coldkey_ss58=fake_coldkey,
3299-
hotkey_ss58=fake_hotkey,
3300-
)
3301-
assert isinstance(result, Balance)
3302-
assert result == Balance.from_rao(fake_fee)
3303-
mock_query.assert_called_with(
3304-
runtime_api="StakeInfoRuntimeApi",
3305-
method="get_stake_fee",
3306-
params=[
3307-
None,
3308-
fake_coldkey,
3309-
(fake_hotkey, netuid),
3310-
fake_coldkey,
3311-
fake_amount.rao,
3312-
],
3313-
block=None,
3314-
)
3315-
3316-
# get_unstake_fee
3317-
result = subtensor.get_unstake_fee(
3318-
amount=fake_amount,
3319-
netuid=netuid,
3320-
coldkey_ss58=fake_coldkey,
3321-
hotkey_ss58=fake_hotkey,
3322-
)
3323-
assert isinstance(result, Balance)
3324-
assert result == Balance.from_rao(fake_fee)
3325-
mock_query.assert_called_with(
3326-
runtime_api="StakeInfoRuntimeApi",
3327-
method="get_stake_fee",
3328-
params=[
3329-
(fake_hotkey, netuid),
3330-
fake_coldkey,
3331-
None,
3332-
fake_coldkey,
3333-
fake_amount.rao,
3334-
],
3335-
block=None,
3336-
)
3337-
3338-
# get_stake_movement_fee
3339-
result = subtensor.get_stake_movement_fee(
3340-
amount=fake_amount,
3341-
origin_netuid=2,
3342-
origin_hotkey_ss58=fake_hotkey,
3343-
origin_coldkey_ss58=fake_coldkey,
3344-
destination_netuid=3,
3345-
destination_hotkey_ss58="hk2",
3346-
destination_coldkey_ss58=fake_coldkey,
3347-
)
3348-
assert isinstance(result, Balance)
3349-
assert result == Balance.from_rao(fake_fee)
3350-
mock_query.assert_called_with(
3351-
runtime_api="StakeInfoRuntimeApi",
3352-
method="get_stake_fee",
3353-
params=[
3354-
(fake_hotkey, 2),
3355-
fake_coldkey,
3356-
("hk2", 3),
3357-
fake_coldkey,
3358-
fake_amount.rao,
3359-
],
3360-
block=None,
3361-
)
3362-
3363-
33643274
def test_get_owned_hotkeys_happy_path(subtensor, mocker):
33653275
"""Tests that the output of get_owned_hotkeys."""
33663276
# Prep
@@ -4266,3 +4176,102 @@ def test_subnet(subtensor, mocker):
42664176
{"netuid": netuid, "price": Balance.from_tao(100.0)}
42674177
)
42684178
assert result == mocked_di_from_dict.return_value
4179+
4180+
4181+
def test_get_stake_operations_fee(subtensor, mocker):
4182+
"""Verify that `get_stake_operations_fee` calls proper methods and returns the correct value."""
4183+
# Preps
4184+
netuid = 1
4185+
amount = Balance.from_rao(100_000_000_000) # 100 Tao
4186+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
4187+
mocked_query_map = mocker.patch.object(
4188+
subtensor.substrate, "query", return_value=mocker.Mock(value=196)
4189+
)
4190+
4191+
# Call
4192+
result = subtensor.get_stake_operations_fee(amount=amount, netuid=netuid)
4193+
4194+
# Assert
4195+
mocked_determine_block_hash.assert_called_once_with(block=None)
4196+
mocked_query_map.assert_called_once_with(
4197+
module="Swap",
4198+
storage_function="FeeRate",
4199+
params=[netuid],
4200+
block_hash=mocked_determine_block_hash.return_value,
4201+
)
4202+
assert result == Balance.from_rao(299076829).set_unit(netuid)
4203+
4204+
4205+
def test_get_stake_add_fee(subtensor, mocker):
4206+
"""Verify that `get_stake_add_fee` calls proper methods and returns the correct value."""
4207+
# Preps
4208+
netuid = mocker.Mock()
4209+
amount = mocker.Mock()
4210+
mocked_get_stake_operations_fee = mocker.patch.object(
4211+
subtensor, "get_stake_operations_fee"
4212+
)
4213+
4214+
# Call
4215+
result = subtensor.get_stake_add_fee(
4216+
amount=amount,
4217+
netuid=netuid,
4218+
coldkey_ss58=mocker.Mock(),
4219+
hotkey_ss58=mocker.Mock(),
4220+
)
4221+
4222+
# Asserts
4223+
mocked_get_stake_operations_fee.assert_called_once_with(
4224+
amount=amount, netuid=netuid, block=None
4225+
)
4226+
assert result == mocked_get_stake_operations_fee.return_value
4227+
4228+
4229+
def test_get_unstake_fee(subtensor, mocker):
4230+
"""Verify that `get_unstake_fee` calls proper methods and returns the correct value."""
4231+
# Preps
4232+
netuid = mocker.Mock()
4233+
amount = mocker.Mock()
4234+
mocked_get_stake_operations_fee = mocker.patch.object(
4235+
subtensor, "get_stake_operations_fee"
4236+
)
4237+
4238+
# Call
4239+
result = subtensor.get_unstake_fee(
4240+
amount=amount,
4241+
netuid=netuid,
4242+
coldkey_ss58=mocker.Mock(),
4243+
hotkey_ss58=mocker.Mock(),
4244+
)
4245+
4246+
# Asserts
4247+
mocked_get_stake_operations_fee.assert_called_once_with(
4248+
amount=amount, netuid=netuid, block=None
4249+
)
4250+
assert result == mocked_get_stake_operations_fee.return_value
4251+
4252+
4253+
def test_get_stake_movement_fee(subtensor, mocker):
4254+
"""Verify that `get_stake_movement_fee` calls proper methods and returns the correct value."""
4255+
# Preps
4256+
netuid = mocker.Mock()
4257+
amount = mocker.Mock()
4258+
mocked_get_stake_operations_fee = mocker.patch.object(
4259+
subtensor, "get_stake_operations_fee"
4260+
)
4261+
4262+
# Call
4263+
result = subtensor.get_stake_movement_fee(
4264+
amount=amount,
4265+
origin_netuid=netuid,
4266+
origin_hotkey_ss58=mocker.Mock(),
4267+
origin_coldkey_ss58=mocker.Mock(),
4268+
destination_netuid=mocker.Mock(),
4269+
destination_hotkey_ss58=mocker.Mock(),
4270+
destination_coldkey_ss58=mocker.Mock(),
4271+
)
4272+
4273+
# Asserts
4274+
mocked_get_stake_operations_fee.assert_called_once_with(
4275+
amount=amount, netuid=netuid, block=None
4276+
)
4277+
assert result == mocked_get_stake_operations_fee.return_value

0 commit comments

Comments
 (0)