Add docstrings for unittest module
This commit is contained in:
parent
877dde8cf6
commit
4c4070b8dc
@ -14,35 +14,70 @@ logg = logging.getLogger().getChild(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class MockConn:
|
class MockConn:
|
||||||
|
"""Noop connection mocker.
|
||||||
|
|
||||||
|
:param o: Object to execute rpc call for
|
||||||
|
:type o: dict
|
||||||
|
"""
|
||||||
def do(self, o):
|
def do(self, o):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MockTx:
|
class MockTx:
|
||||||
|
"""Minimal mocked tx object.
|
||||||
|
|
||||||
|
:param index: Transaction index in block
|
||||||
|
:type index: int
|
||||||
|
:param tx_hash: Transaction hash
|
||||||
|
:type tx_hash: str
|
||||||
|
"""
|
||||||
def __init__(self, index, tx_hash):
|
def __init__(self, index, tx_hash):
|
||||||
self.hash = tx_hash
|
self.hash = tx_hash
|
||||||
self.index = index
|
self.index = index
|
||||||
|
|
||||||
|
|
||||||
def apply_receipt(self, rcpt):
|
def apply_receipt(self, rcpt):
|
||||||
|
"""Save receipt source in mock tx object.
|
||||||
|
|
||||||
|
:param rcpt: Transaction receipt
|
||||||
|
:type rcpt: dict
|
||||||
|
"""
|
||||||
self.rcpt = rcpt
|
self.rcpt = rcpt
|
||||||
|
|
||||||
|
|
||||||
class MockBlock:
|
class MockBlock:
|
||||||
|
|
||||||
def __init__(self, number, txs):
|
def __init__(self, number, txs):
|
||||||
|
"""Minimal mocked block object.
|
||||||
|
|
||||||
|
:param number: Block number
|
||||||
|
:type number: int
|
||||||
|
:param txs: Transaction list to include in block
|
||||||
|
:type txs: list
|
||||||
|
"""
|
||||||
self.number = number
|
self.number = number
|
||||||
self.txs = txs
|
self.txs = txs
|
||||||
|
|
||||||
|
|
||||||
def tx(self, i):
|
def tx(self, i):
|
||||||
|
"""Get block transaction at given index.
|
||||||
|
|
||||||
|
:param i: Transaction index
|
||||||
|
:type i: int
|
||||||
|
"""
|
||||||
return MockTx(i, self.txs[i])
|
return MockTx(i, self.txs[i])
|
||||||
|
|
||||||
|
|
||||||
class TestSyncer(HistorySyncer):
|
class TestSyncer(HistorySyncer):
|
||||||
|
"""Unittest extension of history syncer driver.
|
||||||
|
|
||||||
|
:param backend: Syncer backend
|
||||||
|
:type backend: chainsyncer.backend.base.Backend implementation
|
||||||
|
:param chain_interface: Chain interface
|
||||||
|
:type chain_interface: chainlib.interface.ChainInterface implementation
|
||||||
|
:param tx_counts: List of integer values defining how many mock transactions to generate per block. Mock blocks will be generated for each element in list.
|
||||||
|
:type tx_counts: list
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, backend, chain_interface, tx_counts=[]):
|
def __init__(self, backend, chain_interface, tx_counts=[]):
|
||||||
self.tx_counts = tx_counts
|
self.tx_counts = tx_counts
|
||||||
@ -50,6 +85,14 @@ class TestSyncer(HistorySyncer):
|
|||||||
|
|
||||||
|
|
||||||
def get(self, conn):
|
def get(self, conn):
|
||||||
|
"""Implements the block getter of chainsyncer.driver.base.Syncer.
|
||||||
|
|
||||||
|
:param conn: RPC connection
|
||||||
|
:type conn: chainlib.connection.RPCConnection
|
||||||
|
:raises NoBlockForYou: End of mocked block array reached
|
||||||
|
:rtype: chainsyncer.unittest.base.MockBlock
|
||||||
|
:returns: Mock block.
|
||||||
|
"""
|
||||||
(pair, fltr) = self.backend.get()
|
(pair, fltr) = self.backend.get()
|
||||||
(target_block, fltr) = self.backend.target()
|
(target_block, fltr) = self.backend.target()
|
||||||
block_height = pair[0]
|
block_height = pair[0]
|
||||||
@ -57,7 +100,6 @@ class TestSyncer(HistorySyncer):
|
|||||||
if block_height == target_block:
|
if block_height == target_block:
|
||||||
self.running = False
|
self.running = False
|
||||||
raise NoBlockForYou()
|
raise NoBlockForYou()
|
||||||
return []
|
|
||||||
|
|
||||||
block_txs = []
|
block_txs = []
|
||||||
if block_height < len(self.tx_counts):
|
if block_height < len(self.tx_counts):
|
||||||
|
@ -15,6 +15,11 @@ logg = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class ChainSyncerDb:
|
class ChainSyncerDb:
|
||||||
|
"""SQLITE database setup for unit tests
|
||||||
|
|
||||||
|
:param debug: Activate sql level debug (outputs sql statements)
|
||||||
|
:type debug: bool
|
||||||
|
"""
|
||||||
|
|
||||||
base = SessionBase
|
base = SessionBase
|
||||||
|
|
||||||
@ -48,8 +53,12 @@ class ChainSyncerDb:
|
|||||||
|
|
||||||
|
|
||||||
def bind_session(self, session=None):
|
def bind_session(self, session=None):
|
||||||
|
"""Create session using underlying session base
|
||||||
|
"""
|
||||||
return self.base.bind_session(session)
|
return self.base.bind_session(session)
|
||||||
|
|
||||||
|
|
||||||
def release_session(self, session=None):
|
def release_session(self, session=None):
|
||||||
|
"""Release session using underlying session base
|
||||||
|
"""
|
||||||
return self.base.release_session(session)
|
return self.base.release_session(session)
|
||||||
|
Loading…
Reference in New Issue
Block a user