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_fail_container(self): testlist = [[]] ContainerUtil.put_to_db(self.db, 'test_list', testlist) self.assertEqual( ContainerUtil.get_from_db(self.db, 'test_list', 1, value_type=int), 0)
def test_tuple(self, score_db, args, value_type, expected_value): test_tuple = tuple([1, 2, 3, self.ADDRESS]) ContainerUtil.put_to_db(score_db, 'test_tuple', test_tuple) assert ContainerUtil.get_from_db( score_db, 'test_tuple', args, value_type=value_type) == expected_value
def test_success_tuple(self): addr1 = create_address(AddressPrefix.CONTRACT) test_tuple = tuple([1, 2, 3, addr1]) ContainerUtil.put_to_db(self.db, 'test_tuple', test_tuple) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_tuple', 0, value_type=int), 1) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_tuple', 1, value_type=int), 2) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_tuple', 2, value_type=int), 3) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_tuple', 3, value_type=Address), addr1)
def test_default_value_of_container_db(self, score_db, value_type, expected_value, collection, key_or_index): # TEST: Check the default value of collection object (dict, list) ContainerUtil.put_to_db(score_db, 'test_collection', collection) actual_value = ContainerUtil.get_from_db(score_db, 'test_collection', key_or_index, value_type=value_type) assert actual_value == expected_value
def test_success_dict(self): addr1 = create_address(AddressPrefix.CONTRACT) test_dict = {1: 'a', 2: ['a', 'b', ['c', 'd']], 3: {'a': 1}, 4: addr1} ContainerUtil.put_to_db(self.db, 'test_dict', test_dict) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 1, value_type=str), 'a') self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 0, value_type=str), 'a') self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 1, value_type=str), 'b') self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 2, 0, value_type=str), 'c') self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 2, 2, 1, value_type=str), 'd') self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 3, 'a', value_type=int), 1) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_dict', 4, value_type=Address), addr1)
def test_nested_list(self, score_db, args, value_type, expected_value): test_list = [1, 2, 3, [4, 5, 6], [7, 8, 9, [10, 11, 12]], self.ADDRESS] ContainerUtil.put_to_db(score_db, 'test_list', test_list) if isinstance(args, tuple): assert ContainerUtil.get_from_db( score_db, 'test_list', *args, value_type=value_type) == expected_value else: assert ContainerUtil.get_from_db( score_db, 'test_list', args, value_type=value_type) == expected_value
def test_success_list(self): addr1 = create_address(AddressPrefix.CONTRACT) test_list = [1, 2, 3, [4, 5, 6], [7, 8, 9, [10, 11, 12]], addr1] ContainerUtil.put_to_db(self.db, 'test_list', test_list) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 0, value_type=int), 1) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 1, value_type=int), 2) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 2, value_type=int), 3) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 3, 0, value_type=int), 4) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 3, 1, value_type=int), 5) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 3, 2, value_type=int), 6) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 4, 0, value_type=int), 7) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 4, 1, value_type=int), 8) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 4, 2, value_type=int), 9) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 4, 3, 0, value_type=int), 10) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 4, 3, 1, value_type=int), 11) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 4, 3, 2, value_type=int), 12) self.assertEqual(ContainerUtil.get_from_db(self.db, 'test_list', 5, value_type=Address), addr1)
def test_nested_dict(self, score_db, args, value_type, expected_value): test_dict = { 1: 'a', 2: ['a', 'b', ['c', 'd']], 3: { 'a': 1 }, 4: self.ADDRESS } ContainerUtil.put_to_db(score_db, 'test_dict', test_dict) if isinstance(args, tuple): assert ContainerUtil.get_from_db( score_db, 'test_dict', *args, value_type=value_type) == expected_value else: assert ContainerUtil.get_from_db( score_db, 'test_dict', args, value_type=value_type) == expected_value