Example #1
0
    def test_container_util(self):
        prefix: bytes = ContainerUtil.create_db_prefix(ArrayDB, 'a')
        self.assertEqual(b'\x00|a', prefix)

        prefix: bytes = ContainerUtil.create_db_prefix(DictDB, 'dictdb')
        self.assertEqual(b'\x01|dictdb', prefix)

        with self.assertRaises(InvalidParamsException):
            prefix: bytes = ContainerUtil.create_db_prefix(VarDB, 'vardb')
Example #2
0
    def test_success_dict_depth1(self):
        name = 'test_dict'
        test_dict = DictDB(name, self.db, value_type=int)

        prefix: bytes = ContainerUtil.create_db_prefix(DictDB, name)
        self.assertEqual(b'\x01|' + name.encode(), prefix)

        test_dict['a'] = 1
        test_dict['b'] = 2

        self.assertEqual(test_dict['a'], 1)
Example #3
0
    def test_success_dict_other_Key(self):
        name = 'test_dict'
        test_dict = DictDB(name, self.db, depth=2, value_type=int)

        prefix: bytes = ContainerUtil.create_db_prefix(DictDB, name)
        self.assertEqual(b'\x01|' + name.encode(), prefix)

        addr1 = create_address(1)
        addr2 = create_address(0)
        test_dict['a'][addr1] = 1
        test_dict['a'][addr2] = 2

        self.assertEqual(test_dict['a'][addr1], 1)
        self.assertEqual(test_dict['a'][addr2], 2)
Example #4
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]
 def test_when_create_var_db_prefix_using_container_util_should_raise_error(
         self):
     with pytest.raises(InvalidParamsException):
         ContainerUtil.create_db_prefix(VarDB, 'vardb')
 def test_container_util(self, prefix, score_db_cls, expected_prefix):
     actual_prefix: bytes = ContainerUtil.create_db_prefix(
         score_db_cls, prefix)
     assert actual_prefix == expected_prefix
 def _check_the_db_prefix_format(name):
     prefix: bytes = ContainerUtil.create_db_prefix(DictDB, name)
     assert prefix == b'\x01|' + name.encode()