File tree Expand file tree Collapse file tree 3 files changed +51
-11
lines changed
async_substrate_interface Expand file tree Collapse file tree 3 files changed +51
-11
lines changed Original file line number Diff line number Diff line change 10
10
import ssl
11
11
import time
12
12
import warnings
13
+ from unittest .mock import AsyncMock
13
14
from hashlib import blake2b
14
15
from typing import (
15
16
Optional ,
@@ -714,13 +715,16 @@ def __init__(
714
715
self .chain_endpoint = url
715
716
self .url = url
716
717
self ._chain = chain_name
717
- self .ws = Websocket (
718
- url ,
719
- options = {
720
- "max_size" : self .ws_max_size ,
721
- "write_limit" : 2 ** 16 ,
722
- },
723
- )
718
+ if not _mock :
719
+ self .ws = Websocket (
720
+ url ,
721
+ options = {
722
+ "max_size" : self .ws_max_size ,
723
+ "write_limit" : 2 ** 16 ,
724
+ },
725
+ )
726
+ else :
727
+ self .ws = AsyncMock (spec = Websocket )
724
728
self ._lock = asyncio .Lock ()
725
729
self .config = {
726
730
"use_remote_preset" : use_remote_preset ,
@@ -743,9 +747,11 @@ def __init__(
743
747
self ._initializing = False
744
748
self .registry_type_map = {}
745
749
self .type_id_to_name = {}
750
+ self ._mock = _mock
746
751
747
752
async def __aenter__ (self ):
748
- await self .initialize ()
753
+ if not self ._mock :
754
+ await self .initialize ()
749
755
return self
750
756
751
757
async def initialize (self ):
Original file line number Diff line number Diff line change 3
3
import socket
4
4
from hashlib import blake2b
5
5
from typing import Optional , Union , Callable , Any
6
+ from unittest .mock import MagicMock
6
7
7
8
from bt_decode import MetadataV15 , PortableRegistry , decode as decode_by_type_string
8
9
from scalecodec import (
13
14
MultiAccountId ,
14
15
)
15
16
from scalecodec .base import RuntimeConfigurationObject , ScaleBytes , ScaleType
16
- from websockets .sync .client import connect
17
+ from websockets .sync .client import connect , ClientConnection
17
18
from websockets .exceptions import ConnectionClosed
18
19
19
20
from async_substrate_interface .const import SS58_FORMAT
@@ -522,14 +523,18 @@ def __init__(
522
523
)
523
524
self .metadata_version_hex = "0x0f000000" # v15
524
525
self .reload_type_registry ()
525
- self .ws = self .connect (init = True )
526
526
self .registry_type_map = {}
527
527
self .type_id_to_name = {}
528
+ self ._mock = _mock
528
529
if not _mock :
530
+ self .ws = self .connect (init = True )
529
531
self .initialize ()
532
+ else :
533
+ self .ws = MagicMock (spec = ClientConnection )
530
534
531
535
def __enter__ (self ):
532
- self .initialize ()
536
+ if not self ._mock :
537
+ self .initialize ()
533
538
return self
534
539
535
540
def __del__ (self ):
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments