Completed basic blockdb tests
This commit is contained in:
parent
9c3ed5bb10
commit
df686b3995
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import sys, os
|
import sys, os
|
||||||
|
import time
|
||||||
import dbm
|
import dbm
|
||||||
sys.path.append(".")
|
sys.path.append(".")
|
||||||
sys.path.append("src/")
|
sys.path.append("src/")
|
||||||
@ -18,8 +19,15 @@ import onionrblocks
|
|||||||
import blockdb
|
import blockdb
|
||||||
|
|
||||||
|
|
||||||
|
def _delete_db():
|
||||||
|
try:
|
||||||
|
os.remove(blockdb.block_db_path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
class TestBlockDB(unittest.TestCase):
|
class TestBlockDB(unittest.TestCase):
|
||||||
def test_store_vdf_block(self):
|
def test_store_vdf_block(self):
|
||||||
|
_delete_db()
|
||||||
bl: Block = onionrblocks.create_anonvdf_block(os.urandom(10), b'bin', 2500)
|
bl: Block = onionrblocks.create_anonvdf_block(os.urandom(10), b'bin', 2500)
|
||||||
blockdb.store_vdf_block(bl)
|
blockdb.store_vdf_block(bl)
|
||||||
|
|
||||||
@ -27,15 +35,32 @@ class TestBlockDB(unittest.TestCase):
|
|||||||
b_db[bl.id]
|
b_db[bl.id]
|
||||||
|
|
||||||
def test_get_blocks_by_type(self):
|
def test_get_blocks_by_type(self):
|
||||||
|
_delete_db()
|
||||||
with dbm.open(blockdb.block_db_path, 'c') as b_db:
|
with dbm.open(blockdb.block_db_path, 'c') as b_db:
|
||||||
bl: Block = onionrblocks.create_anonvdf_block('test', b'txt', 2500)
|
bl: Block = onionrblocks.create_anonvdf_block('test', b'txt', 2500)
|
||||||
b_db[bl.id] = bl.raw
|
b_db[bl.id] = bl.raw
|
||||||
|
bl2: Block = onionrblocks.create_anonvdf_block('test2', b'bin', 2500)
|
||||||
|
b_db[bl2.id] = bl2.raw
|
||||||
looped = False
|
looped = False
|
||||||
for block in blockdb.get_blocks_by_type('txt'):
|
for c, block in enumerate(blockdb.get_blocks_by_type('txt')):
|
||||||
looped = True
|
looped = True
|
||||||
self.assertEqual(bl.id, block.id)
|
self.assertEqual(bl.id, block.id)
|
||||||
|
self.assertTrue(c <= 1)
|
||||||
self.assertTrue(looped)
|
self.assertTrue(looped)
|
||||||
|
|
||||||
|
def test_get_blocks_after_timestamp(self):
|
||||||
|
_delete_db()
|
||||||
|
t = round(time.time())
|
||||||
|
with dbm.open(blockdb.block_db_path, 'c') as b_db:
|
||||||
|
bl: Block = onionrblocks.create_anonvdf_block('test', b'txt', 2500)
|
||||||
|
b_db[bl.id] = bl.raw
|
||||||
|
time.sleep(1)
|
||||||
|
t2 = round(time.time())
|
||||||
|
bl2: Block = onionrblocks.create_anonvdf_block('test2', b'bin', 2500)
|
||||||
|
b_db[bl2.id] = bl2.raw
|
||||||
|
|
||||||
|
bls = list(blockdb.get_blocks_after_timestamp(t + 10))
|
||||||
|
self.assertTrue(len(bls) == 0)
|
||||||
|
|
||||||
|
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user