chainsyncer/tests/test_database.py

158 lines
4.2 KiB
Python
Raw Normal View History

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-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)
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)
(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)
s.complete_filter(0)
s.complete_filter(2)
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)
if __name__ == '__main__':
unittest.main()