Prepare integration test

This commit is contained in:
lash
2022-03-12 13:48:40 +00:00
parent 69ad3711cd
commit 0c9b42d086
8 changed files with 128 additions and 168 deletions

View File

@@ -4,6 +4,7 @@ import unittest
# external imports
from shep.store.file import SimpleFileStoreFactory
from chainlib.chain import ChainSpec
# local imports
from chainqueue import (
@@ -11,6 +12,10 @@ from chainqueue import (
Status,
)
# test imports
from tests.common import MockCounter
class MockContentStore:
@@ -26,18 +31,6 @@ class MockContentStore:
return self.store.get(k)
class MockCounter:
def __init__(self):
self.c = 0
def next(self):
c = self.c
self.c += 1
return c
class TestShepBase(unittest.TestCase):
def setUp(self):
@@ -46,4 +39,5 @@ class TestShepBase(unittest.TestCase):
self.state = Status(factory)
content_store = MockContentStore()
counter = MockCounter()
self.store = Store(self.state, content_store, counter)
chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
self.store = Store(chain_spec, self.state, content_store, counter)

40
tests/common.py Normal file
View File

@@ -0,0 +1,40 @@
# local imports
from chainqueue.cache import Cache
class MockCounter:
def __init__(self):
self.c = 0
def next(self):
c = self.c
self.c += 1
return c
class MockTokenCache(Cache):
def __init__(self):
self.db = {}
self.last_filter = None
def put(self, chain_spec, cache_tx):
self.db[cache_tx.tx_hash] = cache_tx
def get(self, chain_spec, tx_hash):
return self.db[tx_hash]
def by_nonce(self, cache_filter):
self.last_filter = cache_filter
def by_date(self, cache_filter=None):
self.last_filter = cache_filter
def count(self, cache_filter):
self.last_filter = cache_filter

View File

@@ -13,12 +13,12 @@ from chainlib.chain import ChainSpec
from chainqueue import QueueEntry
from chainqueue.cache import (
CacheTokenTx,
Cache,
CacheFilter,
)
# test imports
from tests.base_shep import TestShepBase
from tests.common import MockTokenCache
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
@@ -65,32 +65,6 @@ class MockCacheTokenTx(CacheTokenTx):
return self
class MockTokenCache(Cache):
def __init__(self):
self.db = {}
self.last_filter = None
def put(self, chain_spec, cache_tx):
self.db[cache_tx.tx_hash] = cache_tx
def get(self, chain_spec, tx_hash):
return self.db[tx_hash]
def by_nonce(self, cache_filter):
self.last_filter = cache_filter
def by_date(self, cache_filter=None):
self.last_filter = cache_filter
def count(self, cache_filter):
self.last_filter = cache_filter
class MockNormalizer:
def address(self, v):

View File

@@ -31,31 +31,31 @@ class TestEntry(TestShepBase):
entry = QueueEntry(self.store, tx_hash_two)
entry.create(signed_tx)
txs = self.store.list()
txs = self.store.by_state()
self.assertEqual(len(txs), 2)
entry = QueueEntry(self.store, tx_hash_one)
entry.load()
entry.sent()
txs = self.store.list()
txs = self.store.by_state()
self.assertEqual(len(txs), 1)
txs = self.store.list(state=self.store.IN_NETWORK)
txs = self.store.by_state(state=self.store.IN_NETWORK)
self.assertEqual(len(txs), 1)
entry.succeed(0)
txs = self.store.list()
txs = self.store.by_state()
self.assertEqual(len(txs), 1)
entry = QueueEntry(self.store, tx_hash_two)
entry.load()
entry.sent()
txs = self.store.list(state=self.store.IN_NETWORK)
txs = self.store.by_state(state=self.store.IN_NETWORK)
self.assertEqual(len(txs), 2)
txs = self.store.list(state=self.store.IN_NETWORK, strict=True)
txs = self.store.by_state(state=self.store.IN_NETWORK, strict=True)
self.assertEqual(len(txs), 1)

54
tests/test_integrate.py Normal file
View File

@@ -0,0 +1,54 @@
# standard imports
import tempfile
import unittest
# external imports
from shep.store.file import SimpleFileStoreFactory
from chainlib.chain import ChainSpec
# local imports
from chainqueue import (
Store,
Status,
)
# test imports
from tests.common import (
MockCounter,
MockTokenCache
)
class MockContentStore:
def __init__(self):
self.store = {}
def put(self, k, v):
self.store[k] = v
def get(self, k):
return self.store.get(k)
class TestShepBase(unittest.TestCase):
def setUp(self):
self.path = tempfile.mkdtemp()
factory = SimpleFileStoreFactory(self.path).add
self.state = Status(factory)
content_store = MockContentStore()
counter = MockCounter()
chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
self.cache = MockTokenCache()
self.store = Store(chain_spec, self.state, content_store, counter, cache=self.cache)
def test_basic(self):
pass
if __name__ == '__main__':
unittest.main()

View File

@@ -49,7 +49,11 @@ class TestShep(TestShepBase):
self.state.set('foo', self.state.FINAL)
with self.assertRaises(StateTransitionInvalid):
self.state.move('foo', self.state.INSUFFICIENT_FUNDS)
def test_shep_cache(self):
self.store.put('foo', 'bar')
if __name__ == '__main__':
unittest.main()