2021-04-09 21:40:28 +02:00
|
|
|
# standard imports
|
2021-04-10 00:11:10 +02:00
|
|
|
import logging
|
2021-04-09 21:40:28 +02:00
|
|
|
import uuid
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
|
|
|
|
# local imports
|
2021-04-15 17:19:05 +02:00
|
|
|
from chainsyncer.backend.file import FileBackend
|
2021-04-09 21:40:28 +02:00
|
|
|
|
2021-04-10 00:11:10 +02:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger().getChild(__name__)
|
|
|
|
|
2021-04-09 21:40:28 +02:00
|
|
|
script_dir = os.path.dirname(__file__)
|
|
|
|
tmp_test_dir = os.path.join(script_dir, 'testdata', 'tmp')
|
|
|
|
chainsyncer_test_dir = os.path.join(tmp_test_dir, 'chainsyncer')
|
|
|
|
os.makedirs(tmp_test_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
class TestFile(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
|
2021-04-15 17:19:05 +02:00
|
|
|
self.uu = FileBackend.create_object(self.chain_spec, None, base_dir=tmp_test_dir)
|
2021-04-10 00:11:10 +02:00
|
|
|
logg.debug('made uu {} for {}'.format(self.uu, self.chain_spec))
|
2021-04-09 21:40:28 +02:00
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
self.o = FileBackend(self.chain_spec, self.uu, base_dir=tmp_test_dir)
|
2021-04-09 21:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.o.purge()
|
|
|
|
shutil.rmtree(chainsyncer_test_dir)
|
|
|
|
|
|
|
|
|
2021-04-10 00:11:10 +02:00
|
|
|
@unittest.skip('foo')
|
2021-04-09 21:40:28 +02:00
|
|
|
def test_set(self):
|
|
|
|
self.o.set(42, 13)
|
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
o = FileBackend(self.chain_spec, self.o.object_id, base_dir=tmp_test_dir)
|
2021-04-09 21:40:28 +02:00
|
|
|
|
|
|
|
state = o.get()
|
|
|
|
|
|
|
|
self.assertEqual(state[0], 42)
|
|
|
|
self.assertEqual(state[1], 13)
|
|
|
|
|
|
|
|
|
2021-04-10 00:11:10 +02:00
|
|
|
@unittest.skip('foo')
|
2021-04-09 21:40:28 +02:00
|
|
|
def test_initial(self):
|
2021-04-15 17:19:05 +02:00
|
|
|
local_uu = FileBackend.initial(self.chain_spec, 1337, start_block_height=666, base_dir=tmp_test_dir)
|
2021-04-09 21:40:28 +02:00
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
o = FileBackend(self.chain_spec, local_uu, base_dir=tmp_test_dir)
|
2021-04-09 21:40:28 +02:00
|
|
|
|
|
|
|
(pair, filter_stats) = o.target()
|
|
|
|
self.assertEqual(pair[0], 1337)
|
|
|
|
self.assertEqual(pair[1], 0)
|
|
|
|
|
|
|
|
(pair, filter_stats) = o.start()
|
|
|
|
self.assertEqual(pair[0], 666)
|
|
|
|
self.assertEqual(pair[1], 0)
|
|
|
|
|
|
|
|
|
2021-04-10 00:11:10 +02:00
|
|
|
@unittest.skip('foo')
|
2021-04-09 22:14:58 +02:00
|
|
|
def test_resume(self):
|
|
|
|
for i in range(1, 10):
|
2021-04-15 17:19:05 +02:00
|
|
|
local_uu = FileBackend.initial(self.chain_spec, 666, start_block_height=i, base_dir=tmp_test_dir)
|
2021-04-09 22:14:58 +02:00
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
entries = FileBackend.resume(self.chain_spec, base_dir=tmp_test_dir)
|
2021-04-09 22:14:58 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(entries), 10)
|
|
|
|
|
|
|
|
last = -1
|
|
|
|
for o in entries:
|
|
|
|
self.assertLess(last, o.block_height_offset)
|
|
|
|
last = o.block_height_offset
|
|
|
|
|
|
|
|
|
2021-04-10 00:11:10 +02:00
|
|
|
@unittest.skip('foo')
|
2021-04-09 22:14:58 +02:00
|
|
|
def test_first(self):
|
|
|
|
for i in range(1, 10):
|
2021-04-15 17:19:05 +02:00
|
|
|
local_uu = FileBackend.initial(self.chain_spec, 666, start_block_height=i, base_dir=tmp_test_dir)
|
2021-04-09 22:14:58 +02:00
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
first_entry = FileBackend.first(self.chain_spec, base_dir=tmp_test_dir)
|
2021-04-09 22:14:58 +02:00
|
|
|
|
|
|
|
self.assertEqual(first_entry.block_height_offset, 9)
|
|
|
|
|
2021-04-09 21:40:28 +02:00
|
|
|
|
2021-04-10 00:11:10 +02:00
|
|
|
def test_filter(self):
|
|
|
|
|
|
|
|
self.assertEqual(len(self.o.filter), 1)
|
|
|
|
|
|
|
|
self.o.register_filter('foo')
|
|
|
|
self.o.register_filter('bar')
|
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
o = FileBackend(self.chain_spec, self.uu, base_dir=tmp_test_dir)
|
2021-04-10 00:11:10 +02:00
|
|
|
|
|
|
|
self.assertEqual(o.filter_count, 2)
|
|
|
|
self.assertEqual(o.filter_names, ['foo', 'bar'])
|
|
|
|
self.assertEqual(len(o.filter), 1)
|
|
|
|
|
|
|
|
self.o.complete_filter(1)
|
|
|
|
self.assertEqual(self.o.filter, b'\x40')
|
|
|
|
|
|
|
|
self.o.complete_filter(0)
|
|
|
|
self.assertEqual(self.o.filter, b'\xc0')
|
|
|
|
|
2021-04-15 17:19:05 +02:00
|
|
|
o = FileBackend(self.chain_spec, self.uu, base_dir=tmp_test_dir)
|
2021-04-10 00:11:10 +02:00
|
|
|
self.assertEqual(o.filter, b'\xc0')
|
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
self.o.complete_filter(2)
|
|
|
|
|
|
|
|
self.o.register_filter('baz')
|
|
|
|
self.o.complete_filter(2)
|
|
|
|
|
|
|
|
|
2021-04-09 21:40:28 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|