def test_default_val_db(self): test_dict = {1: 'a', 2: ['a', 'b', ['c', 'd']], 3: {'a': 1}} ContainerUtil.put_to_db(self.db, 'test_dict', test_dict) # dict_db self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 3, 'b', value_type=int), 0) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 3, 'c', value_type=str), "") self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 3, 'c', value_type=bytes), None) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 3, 'c', value_type=Address), None) # list_db self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 3, value_type=str), '') self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 3, value_type=int), 0) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 3, value_type=bytes), None) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 3, value_type=Address), None) # var_db test_var = VarDB('test_var', self.db, value_type=int) self.assertEqual(test_var.get(), 0) test_var2 = VarDB('test_var2', self.db, value_type=str) self.assertEqual(test_var2.get(), "") test_var3 = VarDB('test_var3', self.db, value_type=bytes) self.assertEqual(test_var3.get(), None) test_var4 = VarDB('test_var4', self.db, value_type=Address) self.assertEqual(test_var4.get(), None)
def test_var_db(self, context, key_value_db, old_value, new_value, value_type): context_db = ContextDatabase(key_value_db, is_shared=False) address = Address(AddressPrefix.CONTRACT, os.urandom(20)) score_db = IconScoreDatabase(address, context_db) self._init_context(context, address) self._set_revision(context, Revision.USE_RLP.value - 1) name = "var_db" var_db = VarDB(name, score_db, value_type) var_db.set(old_value) assert var_db.get() == old_value key = _get_final_key(address, ContainerTag.VAR, name.encode(), use_rlp=False) assert key_value_db.get(key) == ContainerUtil.encode_value(old_value) self._set_revision(context, Revision.USE_RLP.value) assert key_value_db.get(key) == ContainerUtil.encode_value(old_value) var_db.set(new_value) assert var_db.get() == new_value assert var_db.get() != old_value key = _get_final_key(address, ContainerTag.VAR, name.encode(), use_rlp=True) assert key_value_db.get(key) == ContainerUtil.encode_value(new_value) var_db.remove() assert var_db.get() == get_default_value(value_type) assert len(key_value_db) == 0
def test_var_db(self, score_db, value_type, expected_value): test_var = VarDB('test_var', score_db, value_type=value_type) assert test_var._db != score_db test_var.set(expected_value) assert test_var.get() == expected_value
def test_success_variable(self): test_var = VarDB('test_var', self.db, value_type=int) self.assertNotEqual(test_var._db, self.db) self.assertEqual(test_var._db._prefix, b'\x02') test_var.set(10**19 + 1) self.assertEqual(test_var.get(), 10**19 + 1) test_var2 = VarDB(2, self.db, value_type=Address) address = create_address(AddressPrefix.CONTRACT) test_var2.set(address) data = test_var2.get() self.assertEqual(data, address) test_var4 = VarDB(4, self.db, value_type=Address) address3 = create_address(AddressPrefix.CONTRACT) test_var4.set(address3) self.assertEqual(test_var4.get(), address3)
def test_default_value_of_var_db(self, score_db, value_type, expected_value): # var_db test_var = VarDB('test_var', score_db, value_type=value_type) assert test_var.get() == expected_value