Skip to content

Commit 5f86e9b

Browse files
author
Roman
committed
add unit tests
1 parent e7c3e4c commit 5f86e9b

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

tests/unit_tests/test_async_subtensor.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,3 +3813,65 @@ async def test_toggle_user_liquidity(subtensor, fake_wallet, mocker):
38133813
period=None,
38143814
)
38153815
assert result == mocked_extrinsic.return_value
3816+
3817+
3818+
@pytest.mark.asyncio
3819+
async def test_get_subnet_price(subtensor, mocker):
3820+
"""Test get_subnet_price returns the correct value."""
3821+
# preps
3822+
netuid = 123
3823+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
3824+
fake_price = {"bits": 3155343338053956962}
3825+
expected_price = Balance.from_tao(0.029258617)
3826+
mocked_query = mocker.patch.object(
3827+
subtensor.substrate, "query", return_value=fake_price
3828+
)
3829+
3830+
# Call
3831+
result = await subtensor.get_subnet_price(
3832+
netuid=netuid,
3833+
)
3834+
3835+
# Asserts
3836+
mocked_determine_block_hash.assert_awaited_once_with(
3837+
block=None, block_hash=None, reuse_block=False
3838+
)
3839+
mocked_query.assert_awaited_once_with(
3840+
module="Swap",
3841+
storage_function="AlphaSqrtPrice",
3842+
params=[netuid],
3843+
block_hash=mocked_determine_block_hash.return_value,
3844+
)
3845+
3846+
assert result == expected_price
3847+
3848+
3849+
@pytest.mark.asyncio
3850+
async def test_get_subnet_prices(subtensor, mocker):
3851+
"""Test get_subnet_prices returns the correct value."""
3852+
# preps
3853+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
3854+
3855+
async def fake_current_sqrt_prices():
3856+
yield [0, {"bits": 0}]
3857+
yield [1, {"bits": 3155343338053956962}]
3858+
3859+
expected_prices = {0: Balance.from_tao(1), 1: Balance.from_tao(0.029258617)}
3860+
mocked_query_map = mocker.patch.object(
3861+
subtensor.substrate, "query_map", return_value=fake_current_sqrt_prices()
3862+
)
3863+
3864+
# Call
3865+
result = await subtensor.get_subnet_prices()
3866+
3867+
# Asserts
3868+
mocked_determine_block_hash.assert_awaited_once_with(
3869+
block=None, block_hash=None, reuse_block=False
3870+
)
3871+
mocked_query_map.assert_awaited_once_with(
3872+
module="Swap",
3873+
storage_function="AlphaSqrtPrice",
3874+
block_hash=mocked_determine_block_hash.return_value,
3875+
page_size=129, # total number of subnets
3876+
)
3877+
assert result == expected_prices

tests/unit_tests/test_subtensor.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,3 +4140,58 @@ def test_toggle_user_liquidity(subtensor, fake_wallet, mocker):
41404140
period=None,
41414141
)
41424142
assert result == mocked_extrinsic.return_value
4143+
4144+
4145+
def test_get_subnet_price(subtensor, mocker):
4146+
"""Test get_subnet_price returns the correct value."""
4147+
# preps
4148+
netuid = 123
4149+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
4150+
fake_price = {"bits": 3155343338053956962}
4151+
expected_price = Balance.from_tao(0.029258617)
4152+
mocked_query = mocker.patch.object(
4153+
subtensor.substrate, "query", return_value=fake_price
4154+
)
4155+
4156+
# Call
4157+
result = subtensor.get_subnet_price(
4158+
netuid=netuid,
4159+
)
4160+
4161+
# Asserts
4162+
mocked_determine_block_hash.assert_called_once_with(block=None)
4163+
mocked_query.assert_called_once_with(
4164+
module="Swap",
4165+
storage_function="AlphaSqrtPrice",
4166+
params=[netuid],
4167+
block_hash=mocked_determine_block_hash.return_value,
4168+
)
4169+
4170+
assert result == expected_price
4171+
4172+
4173+
def test_get_subnet_prices(subtensor, mocker):
4174+
"""Test get_subnet_prices returns the correct value."""
4175+
# preps
4176+
mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash")
4177+
fake_prices = [
4178+
[0, {"bits": 0}],
4179+
[1, {"bits": 3155343338053956962}],
4180+
]
4181+
expected_prices = {0: Balance.from_tao(1), 1: Balance.from_tao(0.029258617)}
4182+
mocked_query_map = mocker.patch.object(
4183+
subtensor.substrate, "query_map", return_value=fake_prices
4184+
)
4185+
4186+
# Call
4187+
result = subtensor.get_subnet_prices()
4188+
4189+
# Asserts
4190+
mocked_determine_block_hash.assert_called_once_with(block=None)
4191+
mocked_query_map.assert_called_once_with(
4192+
module="Swap",
4193+
storage_function="AlphaSqrtPrice",
4194+
block_hash=mocked_determine_block_hash.return_value,
4195+
page_size=129, # total number of subnets
4196+
)
4197+
assert result == expected_prices

0 commit comments

Comments
 (0)