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_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_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)