Skip to content

Commit 831c1c0

Browse files
committed
Fix unit tests + ruff
1 parent a52f356 commit 831c1c0

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed
Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest.mock
1+
from unittest.mock import AsyncMock, MagicMock
22

33
import pytest
44
from websockets.exceptions import InvalidURI
@@ -24,42 +24,70 @@ async def test_invalid_url_raises_exception():
2424
@pytest.mark.asyncio
2525
async def test_runtime_call(monkeypatch):
2626
substrate = AsyncSubstrateInterface("ws://localhost", _mock=True)
27-
substrate._metadata = unittest.mock.Mock()
28-
substrate.metadata_v15 = unittest.mock.Mock(
29-
**{
30-
"value.return_value": {
31-
"apis": [
27+
28+
fake_runtime = MagicMock()
29+
fake_metadata_v15 = MagicMock()
30+
fake_metadata_v15.value.return_value = {
31+
"apis": [
32+
{
33+
"name": "SubstrateApi",
34+
"methods": [
3235
{
33-
"name": "SubstrateApi",
34-
"methods": [
35-
{
36-
"name": "SubstrateMethod",
37-
"inputs": [],
38-
"output": "1",
39-
},
40-
],
36+
"name": "SubstrateMethod",
37+
"inputs": [],
38+
"output": "1",
4139
},
4240
],
4341
},
44-
}
45-
)
46-
substrate.rpc_request = unittest.mock.AsyncMock(
47-
return_value={
48-
"result": "0x00",
42+
],
43+
"types": {
44+
"types": [
45+
{
46+
"id": "1",
47+
"type": {
48+
"path": ["Vec"],
49+
"def": {"sequence": {"type": "4"}},
50+
},
51+
},
52+
]
4953
},
54+
}
55+
fake_runtime.metadata_v15 = fake_metadata_v15
56+
substrate.init_runtime = AsyncMock(return_value=fake_runtime)
57+
58+
# Patch encode_scale (should not be called in this test since no inputs)
59+
substrate.encode_scale = AsyncMock()
60+
61+
# Patch decode_scale to produce a dummy value
62+
substrate.decode_scale = AsyncMock(return_value="decoded_result")
63+
64+
# Patch RPC request with correct behavior
65+
substrate.rpc_request = AsyncMock(
66+
side_effect=lambda method, params: {
67+
"result": "0x00" if method == "state_call" else {"parentHash": "0xDEADBEEF"}
68+
}
5069
)
51-
substrate.decode_scale = unittest.mock.AsyncMock()
5270

71+
# Patch get_block_runtime_info
72+
substrate.get_block_runtime_info = AsyncMock(return_value={"specVersion": "1"})
73+
74+
# Run the call
5375
result = await substrate.runtime_call(
5476
"SubstrateApi",
5577
"SubstrateMethod",
5678
)
5779

80+
# Validate the result is wrapped in ScaleObj
5881
assert isinstance(result, ScaleObj)
59-
assert result.value is substrate.decode_scale.return_value
82+
assert result.value == "decoded_result"
6083

61-
substrate.rpc_request.assert_called_once_with(
62-
"state_call",
63-
["SubstrateApi_SubstrateMethod", "", None],
64-
)
84+
# Check decode_scale called correctly
6585
substrate.decode_scale.assert_called_once_with("scale_info::1", b"\x00")
86+
87+
# encode_scale should not be called since no inputs
88+
substrate.encode_scale.assert_not_called()
89+
90+
# Check RPC request called for the state_call
91+
substrate.rpc_request.assert_any_call(
92+
"state_call", ["SubstrateApi_SubstrateMethod", "", None]
93+
)

tests/unit_tests/test_cache.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async def test_cached_fetcher_fetches_and_caches():
2828
assert result3 == "result_key2"
2929
assert mock_method.await_count == 2
3030

31+
3132
@pytest.mark.asyncio
3233
async def test_cached_fetcher_handles_inflight_requests():
3334
"""Tests that CachedFetcher waits for in-flight results instead of re-fetching."""
@@ -53,9 +54,11 @@ async def slow_method(x):
5354
result1, result2 = await asyncio.gather(task1, task2)
5455
assert result1 == result2 == "slow_result_key1"
5556

57+
5658
@pytest.mark.asyncio
5759
async def test_cached_fetcher_propagates_errors():
5860
"""Tests that CachedFetcher correctly propagates errors."""
61+
5962
async def error_method(x):
6063
raise ValueError("Boom!")
6164

@@ -64,6 +67,7 @@ async def error_method(x):
6467
with pytest.raises(ValueError, match="Boom!"):
6568
await fetcher.execute("key1")
6669

70+
6771
@pytest.mark.asyncio
6872
async def test_cached_fetcher_eviction():
6973
"""Tests that LRU eviction works in CachedFetcher."""
@@ -80,4 +84,4 @@ async def test_cached_fetcher_eviction():
8084
# key1 should be evicted
8185
assert "key1" not in fetcher._cache.cache
8286
assert "key2" in fetcher._cache.cache
83-
assert "key3" in fetcher._cache.cache
87+
assert "key3" in fetcher._cache.cache

0 commit comments

Comments
 (0)