chainqueue/tests/chainqueue_base.py
2021-08-26 08:05:56 +00:00

86 lines
2.3 KiB
Python

# standard imports
import logging
import unittest
import tempfile
import os
#import pysqlite
# external imports
from chainqueue.db.models.otx import Otx
from chainqueue.db.models.tx import TxCache
from chainlib.chain import ChainSpec
from hexathon import (
add_0x,
strip_0x,
)
# local imports
from chainqueue.sql.tx import create
from chainqueue.unittest.db import ChainQueueDb
from chainqueue.sql.backend import SQLBackend
script_dir = os.path.realpath(os.path.dirname(__file__))
logg = logging.getLogger(__name__)
class TestBase(unittest.TestCase):
def setUp(self):
debug = bool(os.environ.get('DATABASE_DEBUG', False))
self.db = ChainQueueDb(debug=debug)
self.session = self.db.bind_session()
self.chain_spec = ChainSpec('evm', 'foo', 42, 'bar')
def tearDown(self):
self.session.commit()
self.db.release_session(self.session)
class TestOtxBase(TestBase):
def setUp(self):
super(TestOtxBase, self).setUp()
self.tx_hash = os.urandom(32).hex()
self.tx = os.urandom(128).hex()
self.nonce = 42
self.alice = add_0x(os.urandom(20).hex())
tx_hash = create(self.chain_spec, self.nonce, self.alice, self.tx_hash, self.tx, session=self.session)
self.assertEqual(tx_hash, self.tx_hash)
self.session.commit()
logg.info('using tx hash {}'.format(self.tx_hash))
class TestTxBase(TestOtxBase):
def setUp(self):
super(TestTxBase, self).setUp()
self.bob = add_0x(os.urandom(20).hex())
self.carol = add_0x(os.urandom(20).hex())
self.foo_token = add_0x(os.urandom(20).hex())
self.bar_token = add_0x(os.urandom(20).hex())
self.from_value = 42
self.to_value = 13
backend = SQLBackend(self.db.dsn)
tx = {
'hash': self.tx_hash,
'from': self.alice,
'to': self.bob,
'source_token': self.foo_token,
'destination_token': self.bar_token,
'from_value': self.from_value,
'to_value': self.to_value,
}
backend.cache(tx, session=self.session)
self.session.commit()
otx = Otx.load(self.tx_hash)
txc = TxCache.load(self.tx_hash)
self.assertEqual(txc.otx_id, otx.id)