def test_success_array4(self, score_db):
        test_array = ArrayDB('test_array', score_db, value_type=int)

        test_array.put(1)
        test_array.put(2)

        with pytest.raises(InvalidParamsException):
            var = test_array[2]
            print(var)
示例#2
0
    def test_success_array4(self):
        test_array = ArrayDB('test_array', self.db, value_type=int)

        test_array.put(1)
        test_array.put(2)

        with self.assertRaises(InvalidParamsException):
            var = test_array[2]
            print(var)
示例#3
0
    def test_negative_index_access_in_array_db(self):
        array = ArrayDB('array', self.db, value_type=int)

        size = 10
        for i in range(size):
            array.put(i)

        negative_index = -1
        for _ in range(size):
            index = size + negative_index
            self.assertEqual(array[index], array[negative_index])
            negative_index -= 1
示例#4
0
    def test_success_array2(self):
        test_array = ArrayDB('test_array', self.db, value_type=int)

        range_size = 3
        expect_array = []

        for i in range(range_size):
            expect_array.append(i)
            test_array.put(i)

        for index, e in enumerate(test_array):
            self.assertEqual(e, expect_array[index])
    def test_negative_index_access_in_array_db(self, score_db):
        array = ArrayDB('array', score_db, value_type=int)

        size = 10
        for i in range(size):
            array.put(i)

        negative_index = -1
        for _ in range(size):
            index = size + negative_index
            assert array[index] == array[negative_index]
            negative_index -= 1
    def test_success_array2(self, score_db):
        test_array = ArrayDB('test_array', score_db, value_type=int)

        range_size = 3
        expect_array = []

        for i in range(range_size):
            expect_array.append(i)
            test_array.put(i)

        for index, e in enumerate(test_array):
            assert e == expect_array[index]
    def test_array_db(self, score_db):
        name = "TEST"
        test_array = ArrayDB(name, score_db, value_type=int)
        assert test_array._db != score_db

        test_array.put(1)
        test_array.put(3)
        test_array.put(5)
        test_array.put(7)
        assert len(test_array) == 4
        assert test_array.pop() == 7
        assert test_array.pop() == 5
        assert len(test_array) == 2
示例#8
0
    def test_array_db(self):
        name = "TEST"
        testarray = ArrayDB(name, self.db, value_type=int)
        self.assertNotEqual(testarray._db, self.db)
        self.assertEqual(testarray._db._prefix,
                         ContainerUtil.create_db_prefix(ArrayDB, name))

        testarray.put(1)
        testarray.put(3)
        testarray.put(5)
        testarray.put(7)
        self.assertEqual(4, len(testarray))
        self.assertEqual(7, testarray.pop())
        self.assertEqual(5, testarray.pop())
        self.assertEqual(2, len(testarray))
    def test_array_db(self, score_db):
        name = "TEST"
        testarray = ArrayDB(name, score_db, value_type=int)
        assert testarray._db != score_db
        assert testarray._db._prefix == ContainerUtil.create_db_prefix(
            ArrayDB, name)

        testarray.put(1)
        testarray.put(3)
        testarray.put(5)
        testarray.put(7)
        assert len(testarray) == 4
        assert testarray.pop() == 7
        assert testarray.pop() == 5
        assert len(testarray) == 2
    def test_array_db2(self):
        name = "TEST"
        testarray = ArrayDB(name, self.db, value_type=int)
        self.assertNotEqual(testarray._db, self.db)
        self.assertEqual(
            testarray._db._prefix,
            ContainerUtil.create_db_prefix(ArrayDB, name))

        testarray.put(1)
        testarray.put(2)
        testarray.put(3)
        testarray.put(4)

        self.assertEqual(1, testarray[0])
        self.assertEqual(2, testarray[1])
        self.assertEqual(3, testarray[2])
        self.assertEqual(4, testarray[3])

        self.assertEqual(4, testarray[-1])
        self.assertEqual(3, testarray[-2])
        self.assertEqual(2, testarray[-3])
        self.assertEqual(1, testarray[-4])

        testarray[0] = 5
        testarray[1] = 6
        testarray[2] = 7
        testarray[3] = 8

        self.assertEqual(5, testarray[0])
        self.assertEqual(6, testarray[1])
        self.assertEqual(7, testarray[2])
        self.assertEqual(8, testarray[3])

        testarray[-1] = 4
        testarray[-2] = 3
        testarray[-3] = 2
        testarray[-4] = 1

        self.assertEqual(4, testarray[-1])
        self.assertEqual(3, testarray[-2])
        self.assertEqual(2, testarray[-3])
        self.assertEqual(1, testarray[-4])

        with self.assertRaises(ContainerDBException):
            testarray[5] = 1
            a = testarray[5]
    def test_array_db2(self, score_db):
        name = "TEST"
        testarray = ArrayDB(name, score_db, value_type=int)
        assert testarray._db != score_db
        assert testarray._db._prefix == ContainerUtil.create_db_prefix(
            ArrayDB, name)

        testarray.put(1)
        testarray.put(2)
        testarray.put(3)
        testarray.put(4)

        assert testarray[0] == 1
        assert testarray[1] == 2
        assert testarray[2] == 3
        assert testarray[3] == 4

        assert testarray[-1] == 4
        assert testarray[-2] == 3
        assert testarray[-3] == 2
        assert testarray[-4] == 1

        testarray[0] = 5
        testarray[1] = 6
        testarray[2] = 7
        testarray[3] = 8

        assert testarray[0] == 5
        assert testarray[1] == 6
        assert testarray[2] == 7
        assert testarray[3] == 8

        testarray[-1] = 4
        testarray[-2] = 3
        testarray[-3] = 2
        testarray[-4] = 1

        assert testarray[-1] == 4
        assert testarray[-2] == 3
        assert testarray[-3] == 2
        assert testarray[-4] == 1

        with pytest.raises(InvalidParamsException):
            testarray[5] = 1
            a = testarray[5]
示例#12
0
    def test_array_db(self, context, key_value_db, values, 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, score_db.address)
        self._set_revision(context, Revision.USE_RLP.value - 1)

        name = "array_db"
        array_db = ArrayDB(name, score_db, value_type)

        for i in range(2):
            array_db.put(values[i])
        assert len(array_db) == 2

        self._set_revision(context, Revision.USE_RLP.value)

        array_db.put(values[2])
        array_db.put(values[3])
        assert len(array_db) == 4

        for i, value in enumerate(array_db):
            assert value == values[i]

        final_key: bytes = _get_final_key(address, ContainerTag.ARRAY, name.encode(), use_rlp=True)
        assert key_value_db.get(final_key) == int_to_bytes(len(array_db))

        for i, use_rlp in enumerate((False, False, True, True)):
            key = _get_final_key(
                address.to_bytes(),
                ContainerTag.ARRAY.value,
                name.encode(),
                int_to_bytes(i),
                use_rlp=use_rlp,
            )
            assert key_value_db.get(key) == ContainerUtil.encode_value(values[i])

        for v in reversed(values):
            assert v == array_db.pop()
        assert len(array_db) == 0

        # 2 values for array_db size still remain
        # even though all items in array_db have been popped.
        assert len(key_value_db) == 2
示例#13
0
    def test_success_array3(self):
        test_array = ArrayDB('test_array', self.db, value_type=int)

        range_size = 3
        expect_array = []

        for i in range(range_size):
            expect_array.append(i)
            test_array.put(i)

        if 0 in test_array:
            pass
        else:
            raise Exception()

        if "a" in test_array:
            raise Exception()
        else:
            pass
示例#14
0
    def test_success_array1(self):
        test_array = ArrayDB('test_array', self.db, value_type=int)

        range_size = 3

        for i in range(range_size):
            test_array.put(i)

        for i in range(range_size):
            self.assertEqual(test_array[i], i)

        cant_find_value = range_size
        self.assertFalse(cant_find_value in test_array)
        self.assertEqual(range_size, len(test_array))

        for e, i in zip(test_array, range(range_size)):
            self.assertEqual(e, i)

        self.assertEqual(test_array[-1], range(range_size)[-1])
    def test_success_array1(self, score_db):
        test_array = ArrayDB('test_array', score_db, value_type=int)

        range_size = 3

        for i in range(range_size):
            test_array.put(i)

        for i in range(range_size):
            assert test_array[i] == i

        cant_find_value = range_size
        assert (cant_find_value in test_array) is False
        assert len(test_array) == range_size

        for e, i in zip(test_array, range(range_size)):
            assert e == i

        assert test_array[-1] == range(range_size)[-1]