class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open(port=11978)
        self.LARGE_KEY_LEN = 8000

    def tearDown(self):
        self.kt_handle.close()

    def test_increment(self):
        self.assertTrue(self.kt_handle.clear())

        key = 'incrkey'
        self.assertEqual(self.kt_handle.increment(key, 10), 10)
        self.assertEqual(self.kt_handle.increment(key, 10), 20)
        self.assertEqual(self.kt_handle.increment(key, 10), 30)
        self.assertEqual(self.kt_handle.increment(key, 10), 40)
        self.assertEqual(self.kt_handle.increment(key, 10), 50)

        # Incrementing against a non numeric value. Must fail.
        self.assertTrue(self.kt_handle.set(key, 'foo'))
        self.assertRaises(KyotoTycoonException, self.kt_handle.increment, key,
                          10)

    def test_increment_double(self):
        self.assertTrue(self.kt_handle.clear())

        key = 'incrkey'
        self.assertEqual(self.kt_handle.increment_double(key, 1.23), 1.23)
        self.assertEqual(self.kt_handle.increment_double(key, 1.11), 2.34)
        self.assertEqual(self.kt_handle.increment_double(key, 0.16), 2.50)
Exemplo n.º 2
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()
        self.LARGE_KEY_LEN = 8000

    def test_increment(self):
        self.assertTrue(self.kt_handle.clear())

        key = 'incrkey'
        self.assertEqual(self.kt_handle.increment(key, 10), 10)
        self.assertEqual(self.kt_handle.increment(key, 10), 20)
        self.assertEqual(self.kt_handle.increment(key, 10), 30)
        self.assertEqual(self.kt_handle.increment(key, 10), 40)
        self.assertEqual(self.kt_handle.increment(key, 10), 50)

        # Incrementing against a non numeric value. Must fail.
        self.assertTrue(self.kt_handle.set(key, 'foo'))
        self.assertEqual(self.kt_handle.increment(key, 10), None)
        self.assertEqual(self.kt_handle.increment(key, 10), None)

    def test_increment_double(self):
        self.assertTrue(self.kt_handle.clear())

        key = 'incrkey'
        self.assertEqual(self.kt_handle.increment_double(key, 1.23), 1.23)
        self.assertEqual(self.kt_handle.increment_double(key, 1.11), 2.34)
        self.assertEqual(self.kt_handle.increment_double(key, 0.16), 2.50)
Exemplo n.º 3
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()

    def test_tsv_rpc(self):
        key = 'tabbed\tkey'
        value = 'tabs\tin\tvalue'

        self.assertTrue(self.kt_handle.clear())
        self.assertEqual(self.kt_handle.increment(key, 1024), 1024)
Exemplo n.º 4
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()

    def test_tsv_rpc(self):
        key = 'tabbed\tkey'
        value = 'tabs\tin\tvalue'

        self.assertTrue(self.kt_handle.clear())
        self.assertEqual(self.kt_handle.increment(key, 1024), 1024)
Exemplo n.º 5
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle_http = KyotoTycoon(binary=False)
        self.kt_handle_http.open(port=11978)

        self.kt_handle_bin = KyotoTycoon(binary=True)
        self.kt_handle_bin.open(port=11978)

        self.LARGE_KEY_LEN = 8000

    def clear_all(self):
        self.assertTrue(self.kt_handle_http.clear(db=DB_1))
        self.assertTrue(self.kt_handle_http.clear(db=DB_2))
        return True

    def test_status(self):
        status = self.kt_handle_http.status(DB_1)
        assert status is not None

        status = self.kt_handle_http.status(DB_2)
        assert status is not None

        self.assertRaises(KyotoTycoonException, self.kt_handle_http.status, DB_INVALID)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.status, 'non_existent')

    def test_set_get(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('ice', 'cream', db=DB_2))
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.set, 'palo', 'alto', db=DB_INVALID)

        self.assertEqual(self.kt_handle_http.get('ice'), None)
        self.assertEqual(self.kt_handle_http.get('ice', db=DB_1), None)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.get, 'ice', db=DB_INVALID)

        self.assertEqual(self.kt_handle_http.get('ice', db=DB_2), 'cream')
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

        self.assertTrue(self.kt_handle_http.set('frozen', 'yoghurt', db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 1)

        self.assertEqual(self.kt_handle_http.get('frozen'), 'yoghurt')
        self.assertEqual(self.kt_handle_http.get('frozen', db=DB_1), 'yoghurt')
        self.assertEqual(self.kt_handle_http.get('frozen', db=DB_2), None)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.get, 'frozen', db=DB_INVALID)

        self.assertTrue(self.kt_handle_http.clear(db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

    def test_get_multi(self):
        self.assertTrue(self.clear_all())

        self.assertTrue(self.kt_handle_http.set('a', 'xxxx', db=DB_1))
        self.assertTrue(self.kt_handle_http.set_bulk({'b': 'yyyy', 'c': 'zzzz'}, db=DB_1))
        self.assertTrue(self.kt_handle_http.set('a1', 'xxxx', db=DB_2))
        self.assertTrue(self.kt_handle_http.set_bulk({'b1': 'yyyy', 'c1': 'zzzz'}, db=DB_2))

        d = self.kt_handle_http.get_bulk(['a', 'b', 'c'], db=DB_1)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a'], 'xxxx')
        self.assertEqual(d['b'], 'yyyy')
        self.assertEqual(d['c'], 'zzzz')
        d = self.kt_handle_http.get_bulk(['a', 'b', 'c'], db=DB_2)
        self.assertEqual(len(d), 0)

        d = self.kt_handle_http.get_bulk(['a1', 'b1', 'c1'], db=DB_2)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a1'], 'xxxx')
        self.assertEqual(d['b1'], 'yyyy')
        self.assertEqual(d['c1'], 'zzzz')
        d = self.kt_handle_http.get_bulk(['a1', 'b1', 'c1'], db=DB_1)
        self.assertEqual(len(d), 0)

    def test_add(self):
        self.assertTrue(self.clear_all())

        # Should not conflict due to different databases.
        self.assertTrue(self.kt_handle_http.add('key1', 'val1', db=DB_1))
        self.assertTrue(self.kt_handle_http.add('key1', 'val1', db=DB_2))

        # Now they should.
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.add, 'key1', 'val1', db=DB_1)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.add, 'key1', 'val1', db=DB_2)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.add, 'key1', 'val1', db=DB_INVALID)

    def test_check(self):
        self.assertTrue(self.kt_handle_http.set('check1', 'abc', db=DB_2))
        self.assertTrue(self.kt_handle_http.check('check1', db=DB_2))
        self.assertFalse(self.kt_handle_http.check('check1', db=DB_1))

    def test_seize(self):
        self.assertTrue(self.kt_handle_http.set('seize1', 'abc', db=DB_2))
        self.assertEqual(self.kt_handle_http.get('seize1', db=DB_2), 'abc')
        self.assertEqual(self.kt_handle_http.get('seize1', db=DB_1), None)
        self.assertEqual(self.kt_handle_http.seize('seize1', db=DB_2), 'abc')
        self.assertEqual(self.kt_handle_http.get('seize1', db=DB_2), None)

    def test_replace(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.add('key1', 'val1', db=DB_1))
        self.assertFalse(self.kt_handle_http.replace('key1', 'val2', db=DB_2))
        self.assertTrue(self.kt_handle_http.replace('key1', 'val2', db=DB_1))
        self.assertFalse(self.kt_handle_http.replace('key1', 'val2', db=DB_INVALID))

        self.assertTrue(self.kt_handle_http.add('key2', 'aaa'))
        self.assertTrue(self.kt_handle_http.replace('key2', 'bbb'))
        self.assertTrue(self.kt_handle_http.replace('key1', 'zzz'))
        self.assertEqual(self.kt_handle_http.get('key2'), 'bbb')
        self.assertEqual(self.kt_handle_http.get('key1'), 'zzz')

    def test_cas(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('key', 'xxx'))
        self.assertEqual(self.kt_handle_http.get('key', db=DB_2), None)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.cas, 'key', old_val='xxx', new_val='yyy', db=DB_2)
        self.assertEqual(self.kt_handle_http.get('key', db=DB_1), 'xxx')
        self.assertTrue(self.kt_handle_http.cas('key', old_val='xxx', new_val='yyy', db=DB_1))
        self.assertTrue(self.kt_handle_http.cas('key', new_val='xxx', db=DB_2))

    def test_remove(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.add('key', 'value', db=DB_1))
        self.assertTrue(self.kt_handle_http.add('key', 'value', db=DB_2))

        self.assertTrue(self.kt_handle_http.remove('key', db=DB_1))
        self.assertEqual(self.kt_handle_http.get('key', db=DB_2), 'value')
        assert self.kt_handle_http.get('key', db=DB_1) is None

    def test_vacuum(self):
        self.assertTrue(self.kt_handle_http.vacuum())
        self.assertTrue(self.kt_handle_http.vacuum(db=DB_1))
        self.assertTrue(self.kt_handle_http.vacuum(db=DB_2))
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.vacuum, db=DB_INVALID)

    def test_append(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('key', 'xxx', db=DB_1))
        self.assertTrue(self.kt_handle_http.set('key', 'xxx', db=DB_2))
        self.assertTrue(self.kt_handle_http.append('key', 'xxx', db=DB_1))

        self.assertEqual(self.kt_handle_http.get('key', db=DB_1), 'xxxxxx')
        self.assertEqual(self.kt_handle_http.get('key', db=DB_2), 'xxx')

    def test_increment(self):
        self.assertTrue(self.clear_all())
        self.assertEqual(self.kt_handle_http.increment('key', 0, db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.increment('key', 0, db=DB_2), 0)

        self.assertEqual(self.kt_handle_http.increment('key', 100, db=DB_1), 100)
        self.assertEqual(self.kt_handle_http.increment('key', 200, db=DB_2), 200)
        self.assertEqual(self.kt_handle_http.increment('key', 100, db=DB_1), 200)
        self.assertEqual(self.kt_handle_http.increment('key', 200, db=DB_2), 400)
        self.assertEqual(self.kt_handle_http.get_int('key', db=DB_1), 200)
        self.assertEqual(self.kt_handle_http.get_int('key', db=DB_2), 400)

    def test_match_prefix(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('abcdef', 'val', db=DB_1))
        self.assertTrue(self.kt_handle_http.set('fedcba', 'val', db=DB_2))

        list = self.kt_handle_http.match_prefix('abc', db=DB_1)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'abcdef')
        list = self.kt_handle_http.match_prefix('abc', db=DB_2)
        self.assertEqual(len(list), 0)
        list = self.kt_handle_http.match_prefix('fed', db=DB_1)
        self.assertEqual(len(list), 0)
        list = self.kt_handle_http.match_prefix('fed', db=DB_2)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'fedcba')

    def test_set_get_bin(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_bin.set('ice', 'cream', db=DB_2))
        self.assertFalse(self.kt_handle_bin.set('palo', 'alto', db=DB_INVALID))

        self.assertEqual(self.kt_handle_bin.get('ice'), None)
        self.assertEqual(self.kt_handle_bin.get('ice', db=DB_1), None)
        self.assertFalse(self.kt_handle_bin.get('ice', db=DB_INVALID))

        self.assertEqual(self.kt_handle_bin.get('ice', db=DB_2), 'cream')
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

        self.assertTrue(self.kt_handle_bin.set('frozen', 'yoghurt', db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 1)

        self.assertEqual(self.kt_handle_bin.get('frozen'), 'yoghurt')
        self.assertEqual(self.kt_handle_bin.get('frozen', db=DB_1), 'yoghurt')
        self.assertEqual(self.kt_handle_bin.get('frozen', db=DB_2), None)
        self.assertFalse(self.kt_handle_bin.get('frozen', db=DB_INVALID), None)

        self.assertTrue(self.kt_handle_http.clear(db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

    def test_get_multi_bin(self):
        self.assertTrue(self.clear_all())

        self.assertTrue(self.kt_handle_bin.set('a', 'xxxx', db=DB_1))
        self.assertTrue(self.kt_handle_bin.set_bulk({'b': 'yyyy', 'c': 'zzzz'}, db=DB_1))
        self.assertTrue(self.kt_handle_bin.set('a1', 'xxxx', db=DB_2))
        self.assertTrue(self.kt_handle_bin.set_bulk({'b1': 'yyyy', 'c1': 'zzzz'}, db=DB_2))

        d = self.kt_handle_bin.get_bulk(['a', 'b', 'c'], db=DB_1, atomic=False)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a'], 'xxxx')
        self.assertEqual(d['b'], 'yyyy')
        self.assertEqual(d['c'], 'zzzz')
        d = self.kt_handle_bin.get_bulk(['a', 'b', 'c'], db=DB_2, atomic=False)
        self.assertEqual(len(d), 0)

        d = self.kt_handle_bin.get_bulk(['a1', 'b1', 'c1'], db=DB_2, atomic=False)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a1'], 'xxxx')
        self.assertEqual(d['b1'], 'yyyy')
        self.assertEqual(d['c1'], 'zzzz')
        d = self.kt_handle_bin.get_bulk(['a1', 'b1', 'c1'], db=DB_1, atomic=False)
        self.assertEqual(len(d), 0)

    def test_remove_bin(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_bin.set('key', 'value', db=DB_1))
        self.assertTrue(self.kt_handle_bin.set('key', 'value', db=DB_2))

        self.assertTrue(self.kt_handle_bin.remove('key', db=DB_1))
        self.assertEqual(self.kt_handle_bin.get('key', db=DB_2), 'value')
        assert self.kt_handle_bin.get('key', db=DB_1) is None
Exemplo n.º 6
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()
        self.LARGE_KEY_LEN = 8000

    def clear_all(self):
        self.assertTrue(self.kt_handle.clear(db=DB_1))
        self.assertTrue(self.kt_handle.clear(db=DB_2))
        return True

    def test_status(self):
        status = self.kt_handle.status(DB_1)
        assert status is not None

        status = self.kt_handle.status(DB_2)
        assert status is not None

        status = self.kt_handle.status(DB_INVALID)
        assert status is None

        status = self.kt_handle.status('non_existent')
        assert status is None

    def test_set_get(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('ice', 'cream', db=DB_2))
        self.assertFalse(self.kt_handle.set('palo', 'alto', db=DB_INVALID))

        assert self.kt_handle.get('ice') is None
        assert self.kt_handle.get('ice', db=DB_1) is None
        assert self.kt_handle.get('ice', db=DB_INVALID) is None

        self.assertEqual(self.kt_handle.get('ice', db='two.kch'), 'cream')
        self.assertEqual(self.kt_handle.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle.count(db=DB_2), 1)

        self.assertTrue(self.kt_handle.set('frozen', 'yoghurt', db=DB_1))
        self.assertEqual(self.kt_handle.count(db=DB_1), 1)

        self.assertEqual(self.kt_handle.get('frozen'), 'yoghurt')
        self.assertEqual(self.kt_handle.get('frozen', db=DB_1), 'yoghurt')
        assert self.kt_handle.get('frozen', db=DB_2) is None
        assert self.kt_handle.get('frozen', db=DB_INVALID) is None

        self.assertTrue(self.kt_handle.clear(db=DB_1))
        self.assertEqual(self.kt_handle.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle.count(db=DB_2), 1)

    def test_get_multi(self):
        self.assertTrue(self.clear_all())

        self.assertTrue(self.kt_handle.set('a', 'xxxx', db=DB_1))
        self.assertTrue(self.kt_handle.set('b', 'yyyy', db=DB_1))
        self.assertTrue(self.kt_handle.set('c', 'zzzz', db=DB_1))
        self.assertTrue(self.kt_handle.set('a1', 'xxxx', db=DB_2))
        self.assertTrue(self.kt_handle.set('b1', 'yyyy', db=DB_2))
        self.assertTrue(self.kt_handle.set('c1', 'zzzz', db=DB_2))

        d = self.kt_handle.get_bulk(['a', 'b', 'c'], db=DB_1)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a'], 'xxxx')
        self.assertEqual(d['b'], 'yyyy')
        self.assertEqual(d['c'], 'zzzz')
        d = self.kt_handle.get_bulk(['a', 'b', 'c'], db=DB_2)
        self.assertEqual(len(d), 0)

        d = self.kt_handle.get_bulk(['a1', 'b1', 'c1'], db=DB_2)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a1'], 'xxxx')
        self.assertEqual(d['b1'], 'yyyy')
        self.assertEqual(d['c1'], 'zzzz')
        d = self.kt_handle.get_bulk(['a1', 'b1', 'c1'], db=DB_1)
        self.assertEqual(len(d), 0)

    def test_add(self):
        self.assertTrue(self.clear_all())

        # Should not conflict due to different databases.
        self.assertTrue(self.kt_handle.add('key1', 'val1', db=DB_1))
        self.assertTrue(self.kt_handle.add('key1', 'val1', db=DB_2))

        # Now they should.
        self.assertFalse(self.kt_handle.add('key1', 'val1', db=DB_1))
        self.assertFalse(self.kt_handle.add('key1', 'val1', db=DB_2))
        self.assertFalse(self.kt_handle.add('key1', 'val1', db=DB_INVALID))

    def test_replace(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.add('key1', 'val1', db=DB_1))
        self.assertFalse(self.kt_handle.replace('key1', 'val2', db=DB_2))
        self.assertTrue(self.kt_handle.replace('key1', 'val2', db=DB_1))
        self.assertFalse(self.kt_handle.replace('key1', 'val2', db=DB_INVALID))

        self.assertTrue(self.kt_handle.add('key2', 'aaa'))
        self.assertTrue(self.kt_handle.replace('key2', 'bbb'))
        self.assertTrue(self.kt_handle.replace('key1', 'zzz'))
        self.assertEqual(self.kt_handle.get('key2'), 'bbb')
        self.assertEqual(self.kt_handle.get('key1'), 'zzz')

    def test_cas(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('key', 'xxx'))
        self.assertFalse(self.kt_handle.cas('key', old_val='xxx',
                                            new_val='yyy', db=DB_2))
        self.assertTrue(self.kt_handle.cas('key', old_val='xxx',
                                            new_val='yyy', db=DB_1))
        self.assertTrue(self.kt_handle.cas('key', new_val='xxx', db=DB_2))

    def test_remove(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.add('key', 'value', db=DB_1))
        self.assertTrue(self.kt_handle.add('key', 'value', db=DB_2))

        self.assertTrue(self.kt_handle.remove('key', db=DB_1))
        self.assertEqual(self.kt_handle.get('key', db=DB_2), 'value')
        assert self.kt_handle.get('key', db=DB_1) is None

    def test_vacuum(self):
        self.assertTrue(self.kt_handle.vacuum())
        self.assertTrue(self.kt_handle.vacuum(db=DB_1))
        self.assertTrue(self.kt_handle.vacuum(db=DB_2))
        self.assertFalse(self.kt_handle.vacuum(db=DB_INVALID))

    def test_append(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('key', 'xxx', db=DB_1))
        self.assertTrue(self.kt_handle.set('key', 'xxx', db=DB_2))
        self.assertTrue(self.kt_handle.append('key', 'xxx', db=DB_1))

        self.assertEqual(self.kt_handle.get('key', db=DB_1), 'xxxxxx')
        self.assertEqual(self.kt_handle.get('key', db=DB_2), 'xxx')

    def test_increment(self):
        self.assertTrue(self.clear_all())
        self.assertEqual(self.kt_handle.increment('key', 0, db=DB_1), 0)
        self.assertEqual(self.kt_handle.increment('key', 0, db=DB_2), 0)

        self.assertEqual(self.kt_handle.increment('key', 100, db=DB_1), 100)
        self.assertEqual(self.kt_handle.increment('key', 200, db=DB_2), 200)
        self.assertEqual(self.kt_handle.increment('key', 100, db=DB_1), 200)
        self.assertEqual(self.kt_handle.increment('key', 200, db=DB_2), 400)
        self.assertEqual(self.kt_handle.get_int('key', db=DB_1), 200)
        self.assertEqual(self.kt_handle.get_int('key', db=DB_2), 400)

    def test_match_prefix(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('abcdef', 'val', db=DB_1))
        self.assertTrue(self.kt_handle.set('fedcba', 'val', db=DB_2))

        list = self.kt_handle.match_prefix('abc', db=DB_1)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'abcdef')
        list = self.kt_handle.match_prefix('abc', db=DB_2)
        self.assertEqual(len(list), 0)
        list = self.kt_handle.match_prefix('fed', db=DB_1)
        self.assertEqual(len(list), 0)
        list = self.kt_handle.match_prefix('fed', db=DB_2)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'fedcba')
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()
        self.LARGE_KEY_LEN = 8000

    def clear_all(self):
        self.assertTrue(self.kt_handle.clear(db=DB_1))
        self.assertTrue(self.kt_handle.clear(db=DB_2))
        return True

    def test_status(self):
        status = self.kt_handle.status(DB_1)
        assert status is not None

        status = self.kt_handle.status(DB_2)
        assert status is not None

        status = self.kt_handle.status(DB_INVALID)
        assert status is None

        status = self.kt_handle.status('non_existent')
        assert status is None

    def test_set_get(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('ice', 'cream', db=DB_2))
        self.assertFalse(self.kt_handle.set('palo', 'alto', db=DB_INVALID))

        assert self.kt_handle.get('ice') is None
        assert self.kt_handle.get('ice', db=DB_1) is None
        assert self.kt_handle.get('ice', db=DB_INVALID) is None

        self.assertEqual(self.kt_handle.get('ice', db='two.kch'), 'cream')
        self.assertEqual(self.kt_handle.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle.count(db=DB_2), 1)

        self.assertTrue(self.kt_handle.set('frozen', 'yoghurt', db=DB_1))
        self.assertEqual(self.kt_handle.count(db=DB_1), 1)

        self.assertEqual(self.kt_handle.get('frozen'), 'yoghurt')
        self.assertEqual(self.kt_handle.get('frozen', db=DB_1), 'yoghurt')
        assert self.kt_handle.get('frozen', db=DB_2) is None
        assert self.kt_handle.get('frozen', db=DB_INVALID) is None

        self.assertTrue(self.kt_handle.clear(db=DB_1))
        self.assertEqual(self.kt_handle.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle.count(db=DB_2), 1)

    def test_get_multi(self):
        self.assertTrue(self.clear_all())

        self.assertTrue(self.kt_handle.set('a', 'xxxx', db=DB_1))
        self.assertTrue(self.kt_handle.set('b', 'yyyy', db=DB_1))
        self.assertTrue(self.kt_handle.set('c', 'zzzz', db=DB_1))
        self.assertTrue(self.kt_handle.set('a1', 'xxxx', db=DB_2))
        self.assertTrue(self.kt_handle.set('b1', 'yyyy', db=DB_2))
        self.assertTrue(self.kt_handle.set('c1', 'zzzz', db=DB_2))

        d = self.kt_handle.get_bulk(['a', 'b', 'c'], db=DB_1)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a'], 'xxxx')
        self.assertEqual(d['b'], 'yyyy')
        self.assertEqual(d['c'], 'zzzz')
        d = self.kt_handle.get_bulk(['a', 'b', 'c'], db=DB_2)
        self.assertEqual(len(d), 0)

        d = self.kt_handle.get_bulk(['a1', 'b1', 'c1'], db=DB_2)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a1'], 'xxxx')
        self.assertEqual(d['b1'], 'yyyy')
        self.assertEqual(d['c1'], 'zzzz')
        d = self.kt_handle.get_bulk(['a1', 'b1', 'c1'], db=DB_1)
        self.assertEqual(len(d), 0)

    def test_add(self):
        self.assertTrue(self.clear_all())

        # Should not conflict due to different databases.
        self.assertTrue(self.kt_handle.add('key1', 'val1', db=DB_1))
        self.assertTrue(self.kt_handle.add('key1', 'val1', db=DB_2))

        # Now they should.
        self.assertFalse(self.kt_handle.add('key1', 'val1', db=DB_1))
        self.assertFalse(self.kt_handle.add('key1', 'val1', db=DB_2))
        self.assertFalse(self.kt_handle.add('key1', 'val1', db=DB_INVALID))

    def test_replace(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.add('key1', 'val1', db=DB_1))
        self.assertFalse(self.kt_handle.replace('key1', 'val2', db=DB_2))
        self.assertTrue(self.kt_handle.replace('key1', 'val2', db=DB_1))
        self.assertFalse(self.kt_handle.replace('key1', 'val2', db=DB_INVALID))

        self.assertTrue(self.kt_handle.add('key2', 'aaa'))
        self.assertTrue(self.kt_handle.replace('key2', 'bbb'))
        self.assertTrue(self.kt_handle.replace('key1', 'zzz'))
        self.assertEqual(self.kt_handle.get('key2'), 'bbb')
        self.assertEqual(self.kt_handle.get('key1'), 'zzz')

    def test_cas(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('key', 'xxx'))
        self.assertFalse(
            self.kt_handle.cas('key', old_val='xxx', new_val='yyy', db=DB_2))
        self.assertTrue(
            self.kt_handle.cas('key', old_val='xxx', new_val='yyy', db=DB_1))
        self.assertTrue(self.kt_handle.cas('key', new_val='xxx', db=DB_2))

    def test_remove(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.add('key', 'value', db=DB_1))
        self.assertTrue(self.kt_handle.add('key', 'value', db=DB_2))

        self.assertTrue(self.kt_handle.remove('key', db=DB_1))
        self.assertEqual(self.kt_handle.get('key', db=DB_2), 'value')
        assert self.kt_handle.get('key', db=DB_1) is None

    def test_vacuum(self):
        self.assertTrue(self.kt_handle.vacuum())
        self.assertTrue(self.kt_handle.vacuum(db=DB_1))
        self.assertTrue(self.kt_handle.vacuum(db=DB_2))
        self.assertFalse(self.kt_handle.vacuum(db=DB_INVALID))

    def test_append(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('key', 'xxx', db=DB_1))
        self.assertTrue(self.kt_handle.set('key', 'xxx', db=DB_2))
        self.assertTrue(self.kt_handle.append('key', 'xxx', db=DB_1))

        self.assertEqual(self.kt_handle.get('key', db=DB_1), 'xxxxxx')
        self.assertEqual(self.kt_handle.get('key', db=DB_2), 'xxx')

    def test_increment(self):
        self.assertTrue(self.clear_all())
        self.assertEqual(self.kt_handle.increment('key', 0, db=DB_1), 0)
        self.assertEqual(self.kt_handle.increment('key', 0, db=DB_2), 0)

        self.assertEqual(self.kt_handle.increment('key', 100, db=DB_1), 100)
        self.assertEqual(self.kt_handle.increment('key', 200, db=DB_2), 200)
        self.assertEqual(self.kt_handle.increment('key', 100, db=DB_1), 200)
        self.assertEqual(self.kt_handle.increment('key', 200, db=DB_2), 400)
        self.assertEqual(self.kt_handle.get_int('key', db=DB_1), 200)
        self.assertEqual(self.kt_handle.get_int('key', db=DB_2), 400)

    def test_match_prefix(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle.set('abcdef', 'val', db=DB_1))
        self.assertTrue(self.kt_handle.set('fedcba', 'val', db=DB_2))

        list = self.kt_handle.match_prefix('abc', db=DB_1)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'abcdef')
        list = self.kt_handle.match_prefix('abc', db=DB_2)
        self.assertEqual(len(list), 0)
        list = self.kt_handle.match_prefix('fed', db=DB_1)
        self.assertEqual(len(list), 0)
        list = self.kt_handle.match_prefix('fed', db=DB_2)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'fedcba')
Exemplo n.º 8
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle_http = KyotoTycoon(binary=False)
        self.kt_handle_http.open(port=11978)

        self.kt_handle_bin = KyotoTycoon(binary=True)
        self.kt_handle_bin.open(port=11978)

        self.LARGE_KEY_LEN = 8000

    def tearDown(self):
        self.kt_handle_http.close()
        self.kt_handle_bin.close()

    def clear_all(self):
        self.assertTrue(self.kt_handle_http.clear(db=DB_1))
        self.assertTrue(self.kt_handle_http.clear(db=DB_2))
        return True

    def test_status(self):
        status = self.kt_handle_http.status(DB_1)
        assert status is not None

        status = self.kt_handle_http.status(DB_2)
        assert status is not None

        self.assertRaises(KyotoTycoonException, self.kt_handle_http.status,
                          DB_INVALID)
        self.assertRaises(KyotoTycoonException, self.kt_handle_http.status,
                          'non_existent')

    def test_set_get(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('ice', 'cream', db=DB_2))
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.set,
                          'palo',
                          'alto',
                          db=DB_INVALID)

        self.assertEqual(self.kt_handle_http.get('ice'), None)
        self.assertEqual(self.kt_handle_http.get('ice', db=DB_1), None)
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.get,
                          'ice',
                          db=DB_INVALID)

        self.assertEqual(self.kt_handle_http.get('ice', db=DB_2), 'cream')
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

        self.assertTrue(self.kt_handle_http.set('frozen', 'yoghurt', db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 1)

        self.assertEqual(self.kt_handle_http.get('frozen'), 'yoghurt')
        self.assertEqual(self.kt_handle_http.get('frozen', db=DB_1), 'yoghurt')
        self.assertEqual(self.kt_handle_http.get('frozen', db=DB_2), None)
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.get,
                          'frozen',
                          db=DB_INVALID)

        self.assertTrue(self.kt_handle_http.clear(db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

    def test_get_multi(self):
        self.assertTrue(self.clear_all())

        self.assertTrue(self.kt_handle_http.set('a', 'xxxx', db=DB_1))
        self.assertTrue(
            self.kt_handle_http.set_bulk({
                'b': 'yyyy',
                'c': 'zzzz'
            }, db=DB_1))
        self.assertTrue(self.kt_handle_http.set('a1', 'xxxx', db=DB_2))
        self.assertTrue(
            self.kt_handle_http.set_bulk({
                'b1': 'yyyy',
                'c1': 'zzzz'
            }, db=DB_2))

        d = self.kt_handle_http.get_bulk(['a', 'b', 'c'], db=DB_1)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a'], 'xxxx')
        self.assertEqual(d['b'], 'yyyy')
        self.assertEqual(d['c'], 'zzzz')
        d = self.kt_handle_http.get_bulk(['a', 'b', 'c'], db=DB_2)
        self.assertEqual(len(d), 0)

        d = self.kt_handle_http.get_bulk(['a1', 'b1', 'c1'], db=DB_2)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a1'], 'xxxx')
        self.assertEqual(d['b1'], 'yyyy')
        self.assertEqual(d['c1'], 'zzzz')
        d = self.kt_handle_http.get_bulk(['a1', 'b1', 'c1'], db=DB_1)
        self.assertEqual(len(d), 0)

    def test_add(self):
        self.assertTrue(self.clear_all())

        # Should not conflict due to different databases.
        self.assertTrue(self.kt_handle_http.add('key1', 'val1', db=DB_1))
        self.assertTrue(self.kt_handle_http.add('key1', 'val1', db=DB_2))

        # Now they should.
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.add,
                          'key1',
                          'val1',
                          db=DB_1)
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.add,
                          'key1',
                          'val1',
                          db=DB_2)
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.add,
                          'key1',
                          'val1',
                          db=DB_INVALID)

    def test_check(self):
        self.assertTrue(self.kt_handle_http.set('check1', 'abc', db=DB_2))
        self.assertTrue(self.kt_handle_http.check('check1', db=DB_2))
        self.assertFalse(self.kt_handle_http.check('check1', db=DB_1))

    def test_seize(self):
        self.assertTrue(self.kt_handle_http.set('seize1', 'abc', db=DB_2))
        self.assertEqual(self.kt_handle_http.get('seize1', db=DB_2), 'abc')
        self.assertEqual(self.kt_handle_http.get('seize1', db=DB_1), None)
        self.assertEqual(self.kt_handle_http.seize('seize1', db=DB_2), 'abc')
        self.assertEqual(self.kt_handle_http.get('seize1', db=DB_2), None)

    def test_replace(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.add('key1', 'val1', db=DB_1))
        self.assertFalse(self.kt_handle_http.replace('key1', 'val2', db=DB_2))
        self.assertTrue(self.kt_handle_http.replace('key1', 'val2', db=DB_1))
        self.assertFalse(
            self.kt_handle_http.replace('key1', 'val2', db=DB_INVALID))

        self.assertTrue(self.kt_handle_http.add('key2', 'aaa'))
        self.assertTrue(self.kt_handle_http.replace('key2', 'bbb'))
        self.assertTrue(self.kt_handle_http.replace('key1', 'zzz'))
        self.assertEqual(self.kt_handle_http.get('key2'), 'bbb')
        self.assertEqual(self.kt_handle_http.get('key1'), 'zzz')

    def test_cas(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('key', 'xxx'))
        self.assertEqual(self.kt_handle_http.get('key', db=DB_2), None)
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.cas,
                          'key',
                          old_val='xxx',
                          new_val='yyy',
                          db=DB_2)
        self.assertEqual(self.kt_handle_http.get('key', db=DB_1), 'xxx')
        self.assertTrue(
            self.kt_handle_http.cas('key',
                                    old_val='xxx',
                                    new_val='yyy',
                                    db=DB_1))
        self.assertTrue(self.kt_handle_http.cas('key', new_val='xxx', db=DB_2))

    def test_remove(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.add('key', 'value', db=DB_1))
        self.assertTrue(self.kt_handle_http.add('key', 'value', db=DB_2))

        self.assertTrue(self.kt_handle_http.remove('key', db=DB_1))
        self.assertEqual(self.kt_handle_http.get('key', db=DB_2), 'value')
        assert self.kt_handle_http.get('key', db=DB_1) is None

    def test_vacuum(self):
        self.assertTrue(self.kt_handle_http.vacuum())
        self.assertTrue(self.kt_handle_http.vacuum(db=DB_1))
        self.assertTrue(self.kt_handle_http.vacuum(db=DB_2))
        self.assertRaises(KyotoTycoonException,
                          self.kt_handle_http.vacuum,
                          db=DB_INVALID)

    def test_append(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('key', 'xxx', db=DB_1))
        self.assertTrue(self.kt_handle_http.set('key', 'xxx', db=DB_2))
        self.assertTrue(self.kt_handle_http.append('key', 'xxx', db=DB_1))

        self.assertEqual(self.kt_handle_http.get('key', db=DB_1), 'xxxxxx')
        self.assertEqual(self.kt_handle_http.get('key', db=DB_2), 'xxx')

    def test_increment(self):
        self.assertTrue(self.clear_all())
        self.assertEqual(self.kt_handle_http.increment('key', 0, db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.increment('key', 0, db=DB_2), 0)

        self.assertEqual(self.kt_handle_http.increment('key', 100, db=DB_1),
                         100)
        self.assertEqual(self.kt_handle_http.increment('key', 200, db=DB_2),
                         200)
        self.assertEqual(self.kt_handle_http.increment('key', 100, db=DB_1),
                         200)
        self.assertEqual(self.kt_handle_http.increment('key', 200, db=DB_2),
                         400)
        self.assertEqual(self.kt_handle_http.get_int('key', db=DB_1), 200)
        self.assertEqual(self.kt_handle_http.get_int('key', db=DB_2), 400)

    def test_match_prefix(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_http.set('abcdef', 'val', db=DB_1))
        self.assertTrue(self.kt_handle_http.set('fedcba', 'val', db=DB_2))

        list = self.kt_handle_http.match_prefix('abc', db=DB_1)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'abcdef')
        list = self.kt_handle_http.match_prefix('abc', db=DB_2)
        self.assertEqual(len(list), 0)
        list = self.kt_handle_http.match_prefix('fed', db=DB_1)
        self.assertEqual(len(list), 0)
        list = self.kt_handle_http.match_prefix('fed', db=DB_2)
        self.assertEqual(len(list), 1)
        self.assertEqual(list[0], 'fedcba')

    def test_set_get_bin(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_bin.set('ice', 'cream', db=DB_2))
        self.assertFalse(self.kt_handle_bin.set('palo', 'alto', db=DB_INVALID))

        self.assertEqual(self.kt_handle_bin.get('ice'), None)
        self.assertEqual(self.kt_handle_bin.get('ice', db=DB_1), None)
        self.assertFalse(self.kt_handle_bin.get('ice', db=DB_INVALID))

        self.assertEqual(self.kt_handle_bin.get('ice', db=DB_2), 'cream')
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

        self.assertTrue(self.kt_handle_bin.set('frozen', 'yoghurt', db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 1)

        self.assertEqual(self.kt_handle_bin.get('frozen'), 'yoghurt')
        self.assertEqual(self.kt_handle_bin.get('frozen', db=DB_1), 'yoghurt')
        self.assertEqual(self.kt_handle_bin.get('frozen', db=DB_2), None)
        self.assertFalse(self.kt_handle_bin.get('frozen', db=DB_INVALID), None)

        self.assertTrue(self.kt_handle_http.clear(db=DB_1))
        self.assertEqual(self.kt_handle_http.count(db=DB_1), 0)
        self.assertEqual(self.kt_handle_http.count(db=DB_2), 1)

    def test_get_multi_bin(self):
        self.assertTrue(self.clear_all())

        self.assertTrue(self.kt_handle_bin.set('a', 'xxxx', db=DB_1))
        self.assertTrue(
            self.kt_handle_bin.set_bulk({
                'b': 'yyyy',
                'c': 'zzzz'
            }, db=DB_1))
        self.assertTrue(self.kt_handle_bin.set('a1', 'xxxx', db=DB_2))
        self.assertTrue(
            self.kt_handle_bin.set_bulk({
                'b1': 'yyyy',
                'c1': 'zzzz'
            }, db=DB_2))

        d = self.kt_handle_bin.get_bulk(['a', 'b', 'c'], db=DB_1, atomic=False)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a'], 'xxxx')
        self.assertEqual(d['b'], 'yyyy')
        self.assertEqual(d['c'], 'zzzz')
        d = self.kt_handle_bin.get_bulk(['a', 'b', 'c'], db=DB_2, atomic=False)
        self.assertEqual(len(d), 0)

        d = self.kt_handle_bin.get_bulk(['a1', 'b1', 'c1'],
                                        db=DB_2,
                                        atomic=False)
        self.assertEqual(len(d), 3)
        self.assertEqual(d['a1'], 'xxxx')
        self.assertEqual(d['b1'], 'yyyy')
        self.assertEqual(d['c1'], 'zzzz')
        d = self.kt_handle_bin.get_bulk(['a1', 'b1', 'c1'],
                                        db=DB_1,
                                        atomic=False)
        self.assertEqual(len(d), 0)

    def test_remove_bin(self):
        self.assertTrue(self.clear_all())
        self.assertTrue(self.kt_handle_bin.set('key', 'value', db=DB_1))
        self.assertTrue(self.kt_handle_bin.set('key', 'value', db=DB_2))

        self.assertTrue(self.kt_handle_bin.remove('key', db=DB_1))
        self.assertEqual(self.kt_handle_bin.get('key', db=DB_2), 'value')
        assert self.kt_handle_bin.get('key', db=DB_1) is None