Skip to content

Commit a88a787

Browse files
authored
Merge pull request #123 from opentensor/fix/thewhaleking/fix-mocking-support
Add proper mock support
2 parents 83acb8c + f5f38ff commit a88a787

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import ssl
1111
import time
12+
from unittest.mock import AsyncMock
1213
from hashlib import blake2b
1314
from typing import (
1415
Optional,
@@ -697,13 +698,16 @@ def __init__(
697698
self.chain_endpoint = url
698699
self.url = url
699700
self._chain = chain_name
700-
self.ws = Websocket(
701-
url,
702-
options={
703-
"max_size": self.ws_max_size,
704-
"write_limit": 2**16,
705-
},
706-
)
701+
if not _mock:
702+
self.ws = Websocket(
703+
url,
704+
options={
705+
"max_size": self.ws_max_size,
706+
"write_limit": 2**16,
707+
},
708+
)
709+
else:
710+
self.ws = AsyncMock(spec=Websocket)
707711
self._lock = asyncio.Lock()
708712
self.config = {
709713
"use_remote_preset": use_remote_preset,
@@ -726,9 +730,11 @@ def __init__(
726730
self._initializing = False
727731
self.registry_type_map = {}
728732
self.type_id_to_name = {}
733+
self._mock = _mock
729734

730735
async def __aenter__(self):
731-
await self.initialize()
736+
if not self._mock:
737+
await self.initialize()
732738
return self
733739

734740
async def initialize(self):

async_substrate_interface/sync_substrate.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import socket
44
from hashlib import blake2b
55
from typing import Optional, Union, Callable, Any
6+
from unittest.mock import MagicMock
67

78
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
89
from scalecodec import (
@@ -13,7 +14,7 @@
1314
MultiAccountId,
1415
)
1516
from scalecodec.base import RuntimeConfigurationObject, ScaleBytes, ScaleType
16-
from websockets.sync.client import connect
17+
from websockets.sync.client import connect, ClientConnection
1718
from websockets.exceptions import ConnectionClosed
1819

1920
from async_substrate_interface.const import SS58_FORMAT
@@ -522,14 +523,18 @@ def __init__(
522523
)
523524
self.metadata_version_hex = "0x0f000000" # v15
524525
self.reload_type_registry()
525-
self.ws = self.connect(init=True)
526526
self.registry_type_map = {}
527527
self.type_id_to_name = {}
528+
self._mock = _mock
528529
if not _mock:
530+
self.ws = self.connect(init=True)
529531
self.initialize()
532+
else:
533+
self.ws = MagicMock(spec=ClientConnection)
530534

531535
def __enter__(self):
532-
self.initialize()
536+
if not self._mock:
537+
self.initialize()
533538
return self
534539

535540
def __del__(self):

tests/unit_tests/test_mock.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from websockets.exceptions import InvalidURI
2+
import pytest
3+
4+
from async_substrate_interface import AsyncSubstrateInterface, SubstrateInterface
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_async_mock():
9+
ssi = AsyncSubstrateInterface("notreal")
10+
assert isinstance(ssi, AsyncSubstrateInterface)
11+
with pytest.raises(InvalidURI):
12+
await ssi.initialize()
13+
async with AsyncSubstrateInterface("notreal", _mock=True) as ssi:
14+
assert isinstance(ssi, AsyncSubstrateInterface)
15+
ssi = AsyncSubstrateInterface("notreal", _mock=True)
16+
async with ssi:
17+
pass
18+
19+
20+
def test_sync_mock():
21+
with pytest.raises(InvalidURI):
22+
SubstrateInterface("notreal")
23+
ssi = SubstrateInterface("notreal", _mock=True)
24+
assert isinstance(ssi, SubstrateInterface)
25+
with pytest.raises(InvalidURI):
26+
with SubstrateInterface("notreal") as ssi:
27+
pass
28+
with SubstrateInterface("notreal", _mock=True) as ssi:
29+
assert isinstance(ssi, SubstrateInterface)

0 commit comments

Comments
 (0)