2021-04-04 15:03:58 +02:00
|
|
|
# standard imports
|
|
|
|
import unittest
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from chainsyncer.db.models.base import SessionBase
|
|
|
|
from chainsyncer.db.models.filter import BlockchainSyncFilter
|
2021-04-15 17:45:35 +02:00
|
|
|
from chainsyncer.backend.sql import SQLBackend
|
2021-09-26 19:32:08 +02:00
|
|
|
from chainsyncer.error import LockError
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
# testutil imports
|
2021-08-26 10:09:47 +02:00
|
|
|
from tests.chainsyncer_base import TestBase
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class TestDatabase(TestBase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_backend_live(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(s.object_id, 1)
|
2021-04-15 17:45:35 +02:00
|
|
|
backend = SQLBackend.first(self.chain_spec)
|
|
|
|
#SQLBackend(self.chain_spec, sync_id)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(backend.object_id, 1)
|
|
|
|
|
|
|
|
bogus_chain_spec = ChainSpec('bogus', 'foo', 13, 'baz')
|
2021-04-15 17:45:35 +02:00
|
|
|
sync_id = SQLBackend.first(bogus_chain_spec)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertIsNone(sync_id)
|
|
|
|
|
|
|
|
|
2021-09-26 19:32:08 +02:00
|
|
|
def test_backend_filter_lock(self):
|
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
|
|
|
|
|
|
|
s.connect()
|
|
|
|
filter_id = s.db_object_filter.id
|
|
|
|
s.disconnect()
|
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
o = session.query(BlockchainSyncFilter).get(filter_id)
|
|
|
|
self.assertEqual(len(o.flags), 0)
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
s.register_filter(str(0))
|
|
|
|
s.register_filter(str(1))
|
|
|
|
|
|
|
|
s.connect()
|
|
|
|
filter_id = s.db_object_filter.id
|
|
|
|
s.disconnect()
|
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
o = session.query(BlockchainSyncFilter).get(filter_id)
|
|
|
|
|
|
|
|
o.set(1)
|
|
|
|
with self.assertRaises(LockError):
|
|
|
|
o.set(2)
|
|
|
|
o.release()
|
|
|
|
o.set(2)
|
|
|
|
|
|
|
|
|
2021-04-04 15:03:58 +02:00
|
|
|
def test_backend_filter(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
s.connect()
|
|
|
|
filter_id = s.db_object_filter.id
|
|
|
|
s.disconnect()
|
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
o = session.query(BlockchainSyncFilter).get(filter_id)
|
|
|
|
self.assertEqual(len(o.flags), 0)
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
for i in range(9):
|
|
|
|
s.register_filter(str(i))
|
|
|
|
|
|
|
|
s.connect()
|
|
|
|
filter_id = s.db_object_filter.id
|
|
|
|
s.disconnect()
|
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
|
|
|
o = session.query(BlockchainSyncFilter).get(filter_id)
|
|
|
|
self.assertEqual(len(o.flags), 2)
|
|
|
|
|
|
|
|
(t, c, d) = o.target()
|
|
|
|
self.assertEqual(t, (1 << 9) - 1)
|
|
|
|
|
|
|
|
for i in range(9):
|
|
|
|
o.set(i)
|
2021-09-26 19:32:08 +02:00
|
|
|
o.release()
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
(f, c, d) = o.cursor()
|
|
|
|
self.assertEqual(f, t)
|
|
|
|
self.assertEqual(c, 9)
|
|
|
|
self.assertEqual(d, o.digest)
|
|
|
|
|
|
|
|
session.close()
|
|
|
|
|
2021-04-10 00:30:08 +02:00
|
|
|
|
2021-04-04 15:03:58 +02:00
|
|
|
def test_backend_retrieve(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
s.register_filter('foo')
|
|
|
|
s.register_filter('bar')
|
|
|
|
s.register_filter('baz')
|
|
|
|
|
|
|
|
s.set(42, 13)
|
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.first(self.chain_spec)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(s.get(), ((42,13), 0))
|
|
|
|
|
|
|
|
|
|
|
|
def test_backend_initial(self):
|
|
|
|
with self.assertRaises(ValueError):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.initial(self.chain_spec, 42, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.initial(self.chain_spec, 42, 43)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.initial(self.chain_spec, 42, 13)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
s.set(43, 13)
|
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.first(self.chain_spec)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(s.get(), ((43,13), 0))
|
|
|
|
self.assertEqual(s.start(), ((13,0), 0))
|
|
|
|
|
|
|
|
|
|
|
|
def test_backend_resume(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 666)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(len(s), 0)
|
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
original_id = s.object_id
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 666)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(len(s), 1)
|
|
|
|
resumed_id = s[0].object_id
|
|
|
|
self.assertEqual(resumed_id, original_id + 1)
|
|
|
|
self.assertEqual(s[0].get(), ((42, 0), 0))
|
|
|
|
|
|
|
|
|
|
|
|
def test_backend_resume_when_completed(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 666)
|
2021-04-04 15:03:58 +02:00
|
|
|
s[0].set(666, 0)
|
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 666)
|
2021-04-04 15:03:58 +02:00
|
|
|
self.assertEqual(len(s), 0)
|
|
|
|
|
|
|
|
|
|
|
|
def test_backend_resume_several(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
s.set(43, 13)
|
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 666)
|
|
|
|
SQLBackend.live(self.chain_spec, 666)
|
2021-04-04 15:03:58 +02:00
|
|
|
s[0].set(123, 2)
|
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 1024)
|
|
|
|
SQLBackend.live(self.chain_spec, 1024)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(s), 2)
|
|
|
|
self.assertEqual(s[0].target(), (666, 0))
|
|
|
|
self.assertEqual(s[0].get(), ((123, 2), 0))
|
|
|
|
self.assertEqual(s[1].target(), (1024, 0))
|
|
|
|
self.assertEqual(s[1].get(), ((666, 0), 0))
|
|
|
|
|
|
|
|
|
|
|
|
def test_backend_resume_filter(self):
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.live(self.chain_spec, 42)
|
2021-04-04 15:03:58 +02:00
|
|
|
s.register_filter('foo')
|
|
|
|
s.register_filter('bar')
|
|
|
|
s.register_filter('baz')
|
|
|
|
|
|
|
|
s.set(43, 13)
|
2021-09-26 19:32:08 +02:00
|
|
|
s.begin_filter(0)
|
|
|
|
s.begin_filter(2)
|
2021-04-04 15:03:58 +02:00
|
|
|
|
2021-04-15 17:45:35 +02:00
|
|
|
s = SQLBackend.resume(self.chain_spec, 666)
|
2021-04-04 15:03:58 +02:00
|
|
|
(pair, flags) = s[0].get()
|
|
|
|
|
|
|
|
self.assertEqual(flags, 5)
|
|
|
|
|
|
|
|
|
2021-09-27 20:37:13 +02:00
|
|
|
def test_backend_sql_custom(self):
|
|
|
|
chain_spec = ChainSpec('evm', 'bloxberg', 8996, 'foo')
|
|
|
|
flags = 5
|
|
|
|
flags_target = 1023
|
|
|
|
flag_count = 10
|
|
|
|
backend = SQLBackend.custom(chain_spec, 666, 42, 2, flags, flag_count)
|
|
|
|
self.assertEqual(((42, 2), flags), backend.start())
|
|
|
|
self.assertEqual(((42, 2), flags), backend.get())
|
|
|
|
self.assertEqual((666, flags_target), backend.target())
|
|
|
|
|
|
|
|
|
2021-04-04 15:03:58 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|