Skip to content

Commit cb5c956

Browse files
committed
ChatGPT added unit tests
1 parent ea720ba commit cb5c956

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

tests/unit_tests/test_async_subtensor.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,105 @@ async def test_get_children_substrate_request_exception(subtensor, mocker):
19811981
assert result == (False, [], "Formatted error message")
19821982

19831983

1984+
@pytest.mark.asyncio
1985+
async def test_get_parents_success(subtensor, mocker):
1986+
"""Tests get_parents when parents are successfully retrieved and formatted."""
1987+
# Preps
1988+
fake_hotkey = "valid_hotkey"
1989+
fake_netuid = 1
1990+
fake_parents = mocker.Mock(
1991+
value=[
1992+
(1000, ["parent_key_1"]),
1993+
(2000, ["parent_key_2"]),
1994+
]
1995+
)
1996+
1997+
mocked_query = mocker.AsyncMock(return_value=fake_parents)
1998+
subtensor.substrate.query = mocked_query
1999+
2000+
mocked_decode_account_id = mocker.Mock(
2001+
side_effect=["decoded_parent_key_1", "decoded_parent_key_2"]
2002+
)
2003+
mocker.patch.object(async_subtensor, "decode_account_id", mocked_decode_account_id)
2004+
2005+
expected_formatted_parents = [
2006+
(u64_normalized_float(1000), "decoded_parent_key_1"),
2007+
(u64_normalized_float(2000), "decoded_parent_key_2"),
2008+
]
2009+
2010+
# Call
2011+
result = await subtensor.get_parents(hotkey=fake_hotkey, netuid=fake_netuid)
2012+
2013+
# Asserts
2014+
mocked_query.assert_called_once_with(
2015+
block_hash=None,
2016+
module="SubtensorModule",
2017+
storage_function="ParentKeys",
2018+
params=[fake_hotkey, fake_netuid],
2019+
reuse_block_hash=False,
2020+
)
2021+
mocked_decode_account_id.assert_has_calls(
2022+
[mocker.call("parent_key_1"), mocker.call("parent_key_2")]
2023+
)
2024+
assert result == (True, expected_formatted_parents, "")
2025+
2026+
2027+
@pytest.mark.asyncio
2028+
async def test_get_parents_no_parents(subtensor, mocker):
2029+
"""Tests get_parents when there are no parents to retrieve."""
2030+
# Preps
2031+
fake_hotkey = "valid_hotkey"
2032+
fake_netuid = 1
2033+
fake_parents = []
2034+
2035+
mocked_query = mocker.AsyncMock(return_value=fake_parents)
2036+
subtensor.substrate.query = mocked_query
2037+
2038+
# Call
2039+
result = await subtensor.get_parents(hotkey=fake_hotkey, netuid=fake_netuid)
2040+
2041+
# Asserts
2042+
mocked_query.assert_called_once_with(
2043+
block_hash=None,
2044+
module="SubtensorModule",
2045+
storage_function="ParentKeys",
2046+
params=[fake_hotkey, fake_netuid],
2047+
reuse_block_hash=False,
2048+
)
2049+
assert result == (True, [], "")
2050+
2051+
2052+
@pytest.mark.asyncio
2053+
async def test_get_parents_substrate_request_exception(subtensor, mocker):
2054+
"""Tests get_parents when SubstrateRequestException is raised."""
2055+
# Preps
2056+
fake_hotkey = "valid_hotkey"
2057+
fake_netuid = 1
2058+
fake_exception = async_subtensor.SubstrateRequestException("Test Exception")
2059+
2060+
mocked_query = mocker.AsyncMock(side_effect=fake_exception)
2061+
subtensor.substrate.query = mocked_query
2062+
2063+
mocked_format_error_message = mocker.Mock(return_value="Formatted error message")
2064+
mocker.patch.object(
2065+
async_subtensor, "format_error_message", mocked_format_error_message
2066+
)
2067+
2068+
# Call
2069+
result = await subtensor.get_parents(hotkey=fake_hotkey, netuid=fake_netuid)
2070+
2071+
# Asserts
2072+
mocked_query.assert_called_once_with(
2073+
block_hash=None,
2074+
module="SubtensorModule",
2075+
storage_function="ParentKeys",
2076+
params=[fake_hotkey, fake_netuid],
2077+
reuse_block_hash=False,
2078+
)
2079+
mocked_format_error_message.assert_called_once_with(fake_exception)
2080+
assert result == (False, [], "Formatted error message")
2081+
2082+
19842083
@pytest.mark.asyncio
19852084
async def test_get_children_pending(mock_substrate, subtensor):
19862085
mock_substrate.query.return_value.value = [

tests/unit_tests/test_subtensor.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,3 +3746,96 @@ def test_get_next_epoch_start_block(mocker, subtensor, call_return, expected):
37463746
)
37473747
subtensor.tempo.assert_called_once_with(netuid=netuid, block=block)
37483748
assert result == expected
3749+
3750+
3751+
def test_get_parents_success(subtensor, mocker):
3752+
"""Tests get_parents when parents are successfully retrieved and formatted."""
3753+
# Preps
3754+
fake_hotkey = "valid_hotkey"
3755+
fake_netuid = 1
3756+
fake_parents = mocker.Mock(
3757+
value=[
3758+
(1000, ["parent_key_1"]),
3759+
(2000, ["parent_key_2"]),
3760+
]
3761+
)
3762+
3763+
mocked_query = mocker.MagicMock(return_value=fake_parents)
3764+
subtensor.substrate.query = mocked_query
3765+
3766+
mocked_decode_account_id = mocker.Mock(
3767+
side_effect=["decoded_parent_key_1", "decoded_parent_key_2"]
3768+
)
3769+
mocker.patch.object(subtensor_module, "decode_account_id", mocked_decode_account_id)
3770+
3771+
expected_formatted_parents = [
3772+
(u64_normalized_float(1000), "decoded_parent_key_1"),
3773+
(u64_normalized_float(2000), "decoded_parent_key_2"),
3774+
]
3775+
3776+
# Call
3777+
result = subtensor.get_parents(hotkey=fake_hotkey, netuid=fake_netuid)
3778+
3779+
# Asserts
3780+
mocked_query.assert_called_once_with(
3781+
block_hash=None,
3782+
module="SubtensorModule",
3783+
storage_function="ParentKeys",
3784+
params=[fake_hotkey, fake_netuid],
3785+
)
3786+
mocked_decode_account_id.assert_has_calls(
3787+
[mocker.call("parent_key_1"), mocker.call("parent_key_2")]
3788+
)
3789+
assert result == (True, expected_formatted_parents, "")
3790+
3791+
3792+
def test_get_parents_no_parents(subtensor, mocker):
3793+
"""Tests get_parents when there are no parents to retrieve."""
3794+
# Preps
3795+
fake_hotkey = "valid_hotkey"
3796+
fake_netuid = 1
3797+
fake_parents = []
3798+
3799+
mocked_query = mocker.MagicMock(return_value=fake_parents)
3800+
subtensor.substrate.query = mocked_query
3801+
3802+
# Call
3803+
result = subtensor.get_parents(hotkey=fake_hotkey, netuid=fake_netuid)
3804+
3805+
# Asserts
3806+
mocked_query.assert_called_once_with(
3807+
block_hash=None,
3808+
module="SubtensorModule",
3809+
storage_function="ParentKeys",
3810+
params=[fake_hotkey, fake_netuid],
3811+
)
3812+
assert result == (True, [], "")
3813+
3814+
3815+
def test_get_parents_substrate_request_exception(subtensor, mocker):
3816+
"""Tests get_parents when SubstrateRequestException is raised."""
3817+
# Preps
3818+
fake_hotkey = "valid_hotkey"
3819+
fake_netuid = 1
3820+
fake_exception = subtensor_module.SubstrateRequestException("Test Exception")
3821+
3822+
mocked_query = mocker.MagicMock(side_effect=fake_exception)
3823+
subtensor.substrate.query = mocked_query
3824+
3825+
mocked_format_error_message = mocker.Mock(return_value="Formatted error message")
3826+
mocker.patch.object(
3827+
subtensor_module, "format_error_message", mocked_format_error_message
3828+
)
3829+
3830+
# Call
3831+
result = subtensor.get_parents(hotkey=fake_hotkey, netuid=fake_netuid)
3832+
3833+
# Asserts
3834+
mocked_query.assert_called_once_with(
3835+
block_hash=None,
3836+
module="SubtensorModule",
3837+
storage_function="ParentKeys",
3838+
params=[fake_hotkey, fake_netuid],
3839+
)
3840+
mocked_format_error_message.assert_called_once_with(fake_exception)
3841+
assert result == (False, [], "Formatted error message")

0 commit comments

Comments
 (0)