Skip to content

Commit fe74e25

Browse files
committed
Add tests
1 parent d205015 commit fe74e25

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

tests/helpers/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
)
3535

3636
ARCHIVE_ENTRYPOINT = "wss://archive.chain.opentensor.ai:443"
37+
38+
LATENT_LITE_ENTRYPOINT = "wss://lite.sub.latent.to:443"

tests/unit_tests/asyncio_/test_substrate_interface.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from unittest.mock import AsyncMock, MagicMock, ANY
33

44
import pytest
5+
from scalecodec import ss58_encode
56
from websockets.exceptions import InvalidURI
67

78
from async_substrate_interface.async_substrate import AsyncSubstrateInterface
89
from async_substrate_interface.types import ScaleObj
9-
from tests.helpers.settings import ARCHIVE_ENTRYPOINT
10+
from tests.helpers.settings import ARCHIVE_ENTRYPOINT, LATENT_LITE_ENTRYPOINT
1011

1112

1213
@pytest.mark.asyncio
@@ -100,7 +101,7 @@ async def test_runtime_call(monkeypatch):
100101
@pytest.mark.asyncio
101102
async def test_websocket_shutdown_timer():
102103
# using default ws shutdown timer of 5.0 seconds
103-
async with AsyncSubstrateInterface("wss://lite.sub.latent.to:443") as substrate:
104+
async with AsyncSubstrateInterface(LATENT_LITE_ENTRYPOINT) as substrate:
104105
await substrate.get_chain_head()
105106
await asyncio.sleep(6)
106107
assert (
@@ -141,3 +142,43 @@ async def test_legacy_decoding():
141142
block_hash=block_hash,
142143
)
143144
assert timestamp.value == 1716358476004
145+
146+
147+
@pytest.mark.asyncio
148+
async def test_ss58_conversion():
149+
async with AsyncSubstrateInterface(
150+
LATENT_LITE_ENTRYPOINT, ss58_format=42, decode_ss58=False
151+
) as substrate:
152+
block_hash = await substrate.get_chain_finalised_head()
153+
qm = await substrate.query_map(
154+
"SubtensorModule",
155+
"OwnedHotkeys",
156+
block_hash=block_hash,
157+
)
158+
# only do the first page, bc otherwise this will be massive
159+
for key, value in qm.records:
160+
assert isinstance(key, tuple)
161+
assert isinstance(value, ScaleObj)
162+
assert isinstance(value.value, list)
163+
assert len(key) == 1
164+
for key_tuple in value.value:
165+
assert len(key_tuple[0]) == 32
166+
random_key = key_tuple[0]
167+
168+
ss58_of_key = ss58_encode(bytes(random_key), substrate.ss58_format)
169+
assert isinstance(ss58_of_key, str)
170+
171+
substrate.decode_ss58 = True # change to decoding True
172+
173+
qm = await substrate.query_map(
174+
"SubtensorModule",
175+
"OwnedHotkeys",
176+
block_hash=block_hash,
177+
)
178+
for key, value in qm.records:
179+
assert isinstance(key, str)
180+
assert isinstance(value, ScaleObj)
181+
assert isinstance(value.value, list)
182+
if len(value.value) > 0:
183+
for decoded_key in value.value:
184+
assert isinstance(decoded_key, str)

tests/unit_tests/sync/test_substrate_interface.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from unittest.mock import MagicMock
22

3+
from scalecodec import ss58_encode
4+
35
from async_substrate_interface.sync_substrate import SubstrateInterface
46
from async_substrate_interface.types import ScaleObj
57

6-
from tests.helpers.settings import ARCHIVE_ENTRYPOINT
8+
from tests.helpers.settings import ARCHIVE_ENTRYPOINT, LATENT_LITE_ENTRYPOINT
79

810

911
def test_runtime_call(monkeypatch):
@@ -101,3 +103,42 @@ def test_legacy_decoding():
101103
block_hash=block_hash,
102104
)
103105
assert timestamp.value == 1716358476004
106+
107+
108+
def test_ss58_conversion():
109+
with SubstrateInterface(
110+
LATENT_LITE_ENTRYPOINT, ss58_format=42, decode_ss58=False
111+
) as substrate:
112+
block_hash = substrate.get_chain_finalised_head()
113+
qm = substrate.query_map(
114+
"SubtensorModule",
115+
"OwnedHotkeys",
116+
block_hash=block_hash,
117+
)
118+
# only do the first page, bc otherwise this will be massive
119+
for key, value in qm.records:
120+
assert isinstance(key, tuple)
121+
assert isinstance(value, ScaleObj)
122+
assert isinstance(value.value, list)
123+
assert len(key) == 1
124+
for key_tuple in value.value:
125+
assert len(key_tuple[0]) == 32
126+
random_key = key_tuple[0]
127+
128+
ss58_of_key = ss58_encode(bytes(random_key), substrate.ss58_format)
129+
assert isinstance(ss58_of_key, str)
130+
131+
substrate.decode_ss58 = True # change to decoding True
132+
133+
qm = substrate.query_map(
134+
"SubtensorModule",
135+
"OwnedHotkeys",
136+
block_hash=block_hash,
137+
)
138+
for key, value in qm.records:
139+
assert isinstance(key, str)
140+
assert isinstance(value, ScaleObj)
141+
assert isinstance(value.value, list)
142+
if len(value.value) > 0:
143+
for decoded_key in value.value:
144+
assert isinstance(decoded_key, str)

0 commit comments

Comments
 (0)