diff --git a/chaind/__init__.py b/chaind/__init__.py deleted file mode 100644 index 5117a74..0000000 --- a/chaind/__init__.py +++ /dev/null @@ -1 +0,0 @@ -#from .setup import Environment diff --git a/chaind/adapters/base.py b/chaind/adapters/base.py index 4773b4a..6e5315d 100644 --- a/chaind/adapters/base.py +++ b/chaind/adapters/base.py @@ -4,7 +4,7 @@ from chainqueue import Store as QueueStore class ChaindAdapter: - def __init__(self, chain_spec, state_store, index_store, counter_store,deserializer, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0): - self.deserialize = deserializer + def __init__(self, chain_spec, state_store, index_store, counter_store, cache_adapter, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0): + self.cache_adapter = cache_adapter self.dispatcher = dispatcher self.store = QueueStore(chain_spec, state_store, index_store, counter_store, cache=cache) diff --git a/chaind/adapters/fs.py b/chaind/adapters/fs.py index 5967093..3c570e7 100644 --- a/chaind/adapters/fs.py +++ b/chaind/adapters/fs.py @@ -19,18 +19,18 @@ logg = logging.getLogger(__name__) class ChaindFsAdapter(ChaindAdapter): - def __init__(self, chain_spec, path, deserializer, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0, digest_bytes=32): + def __init__(self, chain_spec, path, cache_adapter, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0, digest_bytes=32): factory = SimpleFileStoreFactory(path).add state_store = Status(factory) index_store = IndexStore(path, digest_bytes=digest_bytes) counter_store = CounterStore(path) - super(ChaindFsAdapter, self).__init__(chain_spec, state_store, index_store, counter_store, deserializer, dispatcher, cache=cache, pending_retry_threshold=pending_retry_threshold, error_retry_threshold=error_retry_threshold) + super(ChaindFsAdapter, self).__init__(chain_spec, state_store, index_store, counter_store, cache_adapter, dispatcher, cache=cache, pending_retry_threshold=pending_retry_threshold, error_retry_threshold=error_retry_threshold) def put(self, signed_tx): - cache_tx = self.deserialize(signed_tx) - self.store.put(cache_tx.hash, signed_tx) - return cache_tx.hash + #cache_tx = self.deserialize(signed_tx) + (s, tx_hash,) = self.store.put(signed_tx, cache_adapter=self.cache_adapter) + return tx_hash def get(self, tx_hash): diff --git a/chaind/unittest/common.py b/chaind/unittest/common.py new file mode 100644 index 0000000..c6f8e70 --- /dev/null +++ b/chaind/unittest/common.py @@ -0,0 +1,55 @@ +# standard imports +import unittest +import hashlib +import tempfile + +# external imports +from chainqueue.cache import CacheTokenTx +from chainlib.status import Status as TxStatus +from chainlib.chain import ChainSpec +from chainlib.error import RPCException + +# local imports +from chaind.adapters.fs import ChaindFsAdapter + + +class MockCacheAdapter(CacheTokenTx): + + def deserialize(self, v): + h = hashlib.sha256() + h.update(v.encode('utf-8')) + z = h.digest() + self.hash = z.hex() + + +class MockDispatcher: + + def __init__(self): + self.fails = [] + + + def add_fail(self, v): + self.fails.append(v) + + + def send(self, v): + if v not in self.fails: + raise RPCException('{} is in fails'.format(v)) + pass + + +class MockTx: + + def __init__(self, tx_hash, status=TxStatus.SUCCESS): + self.hash = tx_hash + self.status = status + + +class TestChaindFsBase(unittest.TestCase): + + def setUp(self): + self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz') + self.path = tempfile.mkdtemp() + self.dispatcher = MockDispatcher() + self.adapter = ChaindFsAdapter(self.chain_spec, self.path, self.cache_adapter, self.dispatcher) + diff --git a/tests/test_fs.py b/tests/test_fs.py index 3cb7235..1024b19 100644 --- a/tests/test_fs.py +++ b/tests/test_fs.py @@ -1,69 +1,34 @@ # standard imports import os -import tempfile import unittest import shutil import logging -import hashlib # external imports -from chainlib.chain import ChainSpec -from chainqueue.cache import CacheTokenTx -from chainlib.error import RPCException from chainlib.status import Status as TxStatus # local imports -from chaind.adapters.fs import ChaindFsAdapter from chaind.driver import QueueDriver from chaind.filter import StateFilter +# test imports +from chaind.unittest.common import ( + MockTx, + MockCacheAdapter, + TestChaindFsBase, + ) + + logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() -class MockCacheAdapter(CacheTokenTx): - def deserialize(self, v): - tx = CacheTokenTx() - h = hashlib.sha256() - h.update(v.encode('utf-8')) - z = h.digest() - tx.hash = z.hex() - return tx - - -class MockDispatcher: - - def __init__(self): - self.fails = [] - - - def add_fail(self, v): - self.fails.append(v) - - - def send(self, v): - if v not in self.fails: - raise RPCException('{} is in fails'.format(v)) - pass - - -class MockTx: - - def __init__(self, tx_hash, status=TxStatus.SUCCESS): - self.hash = tx_hash - self.status = status - - - -class TestChaindFs(unittest.TestCase): +class TestChaindFs(TestChaindFsBase): def setUp(self): - self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz') - self.path = tempfile.mkdtemp() - self.dispatcher = MockDispatcher() - deserializer = MockCacheAdapter().deserialize - self.adapter = ChaindFsAdapter(self.chain_spec, self.path, deserializer, self.dispatcher) + self.cache_adapter = MockCacheAdapter + super(TestChaindFs, self).setUp() def tearDown(self):