2021-04-02 10:31:50 +02:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
import unittest
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
#import pysqlite
|
|
|
|
|
|
|
|
# external imports
|
2021-04-02 14:02:22 +02:00
|
|
|
from chainqueue.db.models.otx import Otx
|
|
|
|
from chainqueue.db.models.tx import TxCache
|
2021-04-02 10:31:50 +02:00
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
import alembic
|
|
|
|
import alembic.config
|
2021-04-02 14:02:22 +02:00
|
|
|
from hexathon import add_0x
|
2021-04-02 10:31:50 +02:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from chainqueue.db import dsn_from_config
|
|
|
|
from chainqueue.db.models.base import SessionBase
|
2021-04-02 14:02:22 +02:00
|
|
|
from chainqueue.tx import create
|
2021-04-02 10:31:50 +02:00
|
|
|
|
|
|
|
script_dir = os.path.realpath(os.path.dirname(__file__))
|
|
|
|
|
2021-04-02 14:45:05 +02:00
|
|
|
logging.basicConfig(level=logging.WARNING)
|
2021-04-02 10:31:50 +02:00
|
|
|
logg = logging.getLogger().getChild(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TestBase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
rootdir = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
dbdir = os.path.join(rootdir, 'chainqueue', 'db')
|
|
|
|
#migrationsdir = os.path.join(dbdir, 'migrations', load_config.get('DATABASE_ENGINE'))
|
|
|
|
#if not os.path.isdir(migrationsdir):
|
|
|
|
migrationsdir = os.path.join(dbdir, 'migrations', 'default')
|
|
|
|
logg.info('using migrations directory {}'.format(migrationsdir))
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DATABASE_ENGINE': 'sqlite',
|
|
|
|
'DATABASE_DRIVER': 'pysqlite',
|
|
|
|
'DATABASE_NAME': 'chainqueue.sqlite',
|
|
|
|
}
|
|
|
|
logg.debug('config {}'.format(config))
|
|
|
|
|
|
|
|
dsn = dsn_from_config(config)
|
|
|
|
SessionBase.poolable = False
|
|
|
|
SessionBase.transactional = False
|
|
|
|
SessionBase.procedural = False
|
2021-04-02 14:45:05 +02:00
|
|
|
SessionBase.connect(dsn, debug=True)
|
2021-04-02 10:31:50 +02:00
|
|
|
|
|
|
|
ac = alembic.config.Config(os.path.join(migrationsdir, 'alembic.ini'))
|
|
|
|
ac.set_main_option('sqlalchemy.url', dsn)
|
|
|
|
ac.set_main_option('script_location', migrationsdir)
|
|
|
|
|
|
|
|
alembic.command.downgrade(ac, 'base')
|
|
|
|
alembic.command.upgrade(ac, 'head')
|
|
|
|
|
|
|
|
self.session = SessionBase.create_session()
|
2021-04-02 10:41:38 +02:00
|
|
|
|
2021-04-02 10:31:50 +02:00
|
|
|
self.chain_spec = ChainSpec('evm', 'foo', 42, 'bar')
|
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.session.commit()
|
|
|
|
self.session.close()
|
2021-04-02 14:02:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestOtxBase(TestBase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestOtxBase, self).setUp()
|
|
|
|
self.tx_hash = add_0x(os.urandom(32).hex())
|
|
|
|
self.tx = add_0x(os.urandom(128).hex())
|
|
|
|
self.nonce = 42
|
|
|
|
self.alice = add_0x(os.urandom(20).hex())
|
|
|
|
|
2021-04-02 19:48:13 +02:00
|
|
|
tx_hash = create(self.chain_spec, self.nonce, self.alice, self.tx_hash, self.tx, session=self.session)
|
2021-04-02 14:02:22 +02:00
|
|
|
self.assertEqual(tx_hash, self.tx_hash)
|
|
|
|
|
|
|
|
|
|
|
|
class TestTxBase(TestOtxBase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestTxBase, self).setUp()
|
|
|
|
self.bob = 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
|
|
|
|
|
|
|
|
txc = TxCache(
|
|
|
|
self.tx_hash,
|
|
|
|
self.alice,
|
|
|
|
self.bob,
|
|
|
|
self.foo_token,
|
|
|
|
self.bar_token,
|
|
|
|
self.from_value,
|
|
|
|
self.to_value,
|
|
|
|
session=self.session,
|
|
|
|
)
|
|
|
|
self.session.add(txc)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
otx = Otx.load(self.tx_hash)
|
|
|
|
self.assertEqual(txc.otx_id, otx.id)
|
|
|
|
|