Skip to content

Add proper mock support #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import ssl
import time
from unittest.mock import AsyncMock
from hashlib import blake2b
from typing import (
Optional,
Expand Down Expand Up @@ -697,13 +698,16 @@ def __init__(
self.chain_endpoint = url
self.url = url
self._chain = chain_name
self.ws = Websocket(
url,
options={
"max_size": self.ws_max_size,
"write_limit": 2**16,
},
)
if not _mock:
self.ws = Websocket(
url,
options={
"max_size": self.ws_max_size,
"write_limit": 2**16,
},
)
else:
self.ws = AsyncMock(spec=Websocket)
self._lock = asyncio.Lock()
self.config = {
"use_remote_preset": use_remote_preset,
Expand All @@ -726,9 +730,11 @@ def __init__(
self._initializing = False
self.registry_type_map = {}
self.type_id_to_name = {}
self._mock = _mock

async def __aenter__(self):
await self.initialize()
if not self._mock:
await self.initialize()
return self

async def initialize(self):
Expand Down
11 changes: 8 additions & 3 deletions async_substrate_interface/sync_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import socket
from hashlib import blake2b
from typing import Optional, Union, Callable, Any
from unittest.mock import MagicMock

from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
from scalecodec import (
Expand All @@ -13,7 +14,7 @@
MultiAccountId,
)
from scalecodec.base import RuntimeConfigurationObject, ScaleBytes, ScaleType
from websockets.sync.client import connect
from websockets.sync.client import connect, ClientConnection
from websockets.exceptions import ConnectionClosed

from async_substrate_interface.const import SS58_FORMAT
Expand Down Expand Up @@ -522,14 +523,18 @@ def __init__(
)
self.metadata_version_hex = "0x0f000000" # v15
self.reload_type_registry()
self.ws = self.connect(init=True)
self.registry_type_map = {}
self.type_id_to_name = {}
self._mock = _mock
if not _mock:
self.ws = self.connect(init=True)
self.initialize()
else:
self.ws = MagicMock(spec=ClientConnection)

def __enter__(self):
self.initialize()
if not self._mock:
self.initialize()
return self

def __del__(self):
Expand Down
29 changes: 29 additions & 0 deletions tests/unit_tests/test_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from websockets.exceptions import InvalidURI
import pytest

from async_substrate_interface import AsyncSubstrateInterface, SubstrateInterface


@pytest.mark.asyncio
async def test_async_mock():
ssi = AsyncSubstrateInterface("notreal")
assert isinstance(ssi, AsyncSubstrateInterface)
with pytest.raises(InvalidURI):
await ssi.initialize()
async with AsyncSubstrateInterface("notreal", _mock=True) as ssi:
assert isinstance(ssi, AsyncSubstrateInterface)
ssi = AsyncSubstrateInterface("notreal", _mock=True)
async with ssi:
pass


def test_sync_mock():
with pytest.raises(InvalidURI):
SubstrateInterface("notreal")
ssi = SubstrateInterface("notreal", _mock=True)
assert isinstance(ssi, SubstrateInterface)
with pytest.raises(InvalidURI):
with SubstrateInterface("notreal") as ssi:
pass
with SubstrateInterface("notreal", _mock=True) as ssi:
assert isinstance(ssi, SubstrateInterface)
Loading