def __init__(self, db_path: str): self._db: TbearsDB = TbearsDB(TbearsDB.make_db(db_path)) self._block_height = -1 self._prev_block_hash = None self._peer_id = str(uuid.uuid1()) self.load_block_info()
def __init__(self, db_path: str): self._db: TbearsDB = TbearsDB(TbearsDB.make_db(db_path)) self._block_height = -1 self._prev_block_hash = None self._peer_id: str = str( Address.from_string("hx6e1dd0d4432620778b54b2bbc21ac3df961adf89")) self.load_block_info()
def setUp(self): self.TBEARS_DB = TbearsDB(TbearsDB.make_db(DB_PATH)) self.test_key = b'test_key' self.test_value = b'test_value'
class TestTBearsDB(unittest.TestCase): def setUp(self): self.TBEARS_DB = TbearsDB(TbearsDB.make_db(DB_PATH)) self.test_key = b'test_key' self.test_value = b'test_value' def tearDown(self): self.TBEARS_DB.close() shutil.rmtree(DB_PATH) def test_put_and_get(self): # Put and get self.TBEARS_DB.put(self.test_key, self.test_value) ret = self.TBEARS_DB.get(self.test_key) self.assertEqual(ret, self.test_value) # overwrite overwrite_value = b'test_value_overwrite' self.TBEARS_DB.put(self.test_key, overwrite_value) ret = self.TBEARS_DB.get(self.test_key) self.assertEqual(ret, overwrite_value) # get invalid key ret = self.TBEARS_DB.get(b'invalid_key') self.assertIsNone(ret) # put invalid type self.assertRaises(TypeError, self.TBEARS_DB.put, 'test_key', self.test_value) self.assertRaises(TypeError, self.TBEARS_DB.put, self.test_key, 123) def test_delete(self): self.TBEARS_DB.put(self.test_key, self.test_value) ret = self.TBEARS_DB.get(self.test_key) self.assertEqual(ret, self.test_value) self.TBEARS_DB.delete(self.test_key) ret = self.TBEARS_DB.get(self.test_key) self.assertIsNone(ret) def test_iterator(self): self.TBEARS_DB.put(b'key1', b'value1') self.TBEARS_DB.put(b'key2', b'value2') self.TBEARS_DB.put(b'key3', b'value3') self.TBEARS_DB.put(b'key4', b'value4') i = 1 for _, actual_value in self.TBEARS_DB.iterator(): expected_value = ('value' + str(i)).encode() self.assertEqual(expected_value, actual_value) i += 1