Exemplo n.º 1
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon(binary=False, pack_type=KT_PACKER_PICKLE)
        self.kt_handle.open(port=11978)
        self.LARGE_KEY_LEN = 8000

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

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

        self.assertTrue(self.kt_handle.set('key', 'value'))

        self.assertTrue(self.kt_handle.set('k e y', 'v a l u e'))
        self.assertTrue(self.kt_handle.set('k\te\ty', 'tabbed'))

        self.assertEqual(self.kt_handle.get('key'), 'value')
        self.assertEqual(self.kt_handle.get('k e y'), 'v a l u e')
        self.assertEqual(self.kt_handle.get('k\te\ty'), 'tabbed')

        self.assertTrue(self.kt_handle.set('\\key', '\\xxx'))
        self.assertEqual(self.kt_handle.get('\\key'), '\\xxx')
        self.assertEqual(self.kt_handle.count(), 4)

        self.assertTrue(self.kt_handle.set('tabbed\tkey', 'tabbled\tvalue'))
        self.assertTrue(self.kt_handle.get('tabbed\tkey'))

        self.assertTrue(self.kt_handle.set('url1', 'http://github.com'))
        self.assertTrue(self.kt_handle.set('url2', 'https://github.com/'))
        self.assertTrue(self.kt_handle.set('url3', 'https://github.com/blog/'))

        self.assertTrue(self.kt_handle.set('http://github.com', 'url1'))
        self.assertTrue(self.kt_handle.set('https://github.com', 'url2'))
        self.assertTrue(self.kt_handle.set('https://github.com/blog/', 'url3'))

        self.assertEqual(self.kt_handle.get('non_existent'), None)

        self.assertTrue(self.kt_handle.set('cb', 1791))
        self.assertEqual(self.kt_handle.get('cb'), 1791)

        self.assertTrue(self.kt_handle.set('cb', 1791.1226))
        self.assertEqual(self.kt_handle.get('cb'), 1791.1226)

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

        self.assertTrue(self.kt_handle.set('key', 'xxx'))
        self.assertTrue(self.kt_handle.cas('key', old_val='xxx',
                                           new_val='yyy'))
        self.assertEqual(self.kt_handle.get('key'), 'yyy')

        self.assertTrue(self.kt_handle.cas('key', old_val='yyy'))
        assert self.kt_handle.get('key') is None
        self.assertTrue(self.kt_handle.cas('key', new_val='zzz'))
        self.assertEqual(self.kt_handle.get('key'), 'zzz')

        self.assertRaises(KyotoTycoonException,
                          self.kt_handle.cas,
                          'key',
                          old_val='foo',
                          new_val='zz')
        self.assertEqual(self.kt_handle.get('key'), 'zzz')

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

        self.assertFalse(self.kt_handle.remove('must fail key'))
        self.assertTrue(self.kt_handle.set('deleteable key', 'xxx'))
        self.assertTrue(self.kt_handle.remove('deleteable key'))

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

        # Must Fail - Can't replace something that doesn't exist.
        self.assertFalse(self.kt_handle.replace('xxxxxx', 'some value'))

        # Popuate then Replace.
        self.assertTrue(self.kt_handle.set('apple', 'ringo'))
        self.assertTrue(self.kt_handle.replace('apple', 'apfel'))
        self.assertEqual(self.kt_handle.get('apple'), 'apfel')

        self.assertTrue(self.kt_handle.replace('apple', 212))
        self.assertEqual(self.kt_handle.get('apple'), 212)
        self.assertTrue(self.kt_handle.replace('apple', 121))
        self.assertEqual(self.kt_handle.get('apple'), 121)

    def test_append(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('k1', 'abc'))
        self.assertTrue(self.kt_handle.append('k1', 'def'))
        self.assertEqual(self.kt_handle.get('k1'), 'abcdef')

        self.assertTrue(self.kt_handle.append('k2', 'new val'))
        self.assertEqual(self.kt_handle.get('k2'), 'new val')

        self.assertTrue(self.kt_handle.set('k3', 777))

        self.assertTrue(self.kt_handle.set('k4', b'abc'))
        self.assertTrue(self.kt_handle.append('k4', 'abc'))
        self.assertEqual(self.kt_handle.get('k4'), b'abcabc')

        self.assertTrue(self.kt_handle.set('k5', 'abc'))
        self.assertTrue(self.kt_handle.append('k5', b'abc'))
        self.assertEqual(self.kt_handle.get('k5'), 'abcabc')

    def test_add(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('stewie', 'griffin'))

        # Must Fail - Stewie exists
        self.assertRaises(KyotoTycoonException, self.kt_handle.add, 'stewie',
                          'hopkin')

        # New records
        self.assertTrue(self.kt_handle.add('peter', 'griffin'))
        self.assertTrue(self.kt_handle.add('lois', 'griffin'))
        self.assertTrue(self.kt_handle.add('seth', 'green'))
        self.assertTrue(self.kt_handle.add('nyc', 'new york city'))

        self.assertTrue(self.kt_handle.add('number', 111))
        self.assertEqual(self.kt_handle.get('number'), 111)

    def test_check(self):
        self.assertTrue(self.kt_handle.set('check1', 'abc'))
        self.assertTrue(self.kt_handle.check('check1'))

        self.assertTrue(self.kt_handle.remove('check1'))
        self.assertFalse(self.kt_handle.check('check1'))

    def test_seize(self):
        self.assertTrue(self.kt_handle.set('seize1', 'abc'))
        self.assertEqual(self.kt_handle.get('seize1'), 'abc')
        self.assertEqual(self.kt_handle.seize('seize1'), 'abc')
        self.assertEqual(self.kt_handle.get('seize1'), None)
        self.assertEqual(self.kt_handle.seize('seize2'), None)

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

        dict = {
            'k1': 'one',
            'k2': 'two',
            'k3': 'three',
            'k4': 'four',
            'k\n5': 'five',
            'k\t6': 'six',
            'k7': 111
        }

        n = self.kt_handle.set_bulk(dict)
        self.assertEqual(len(dict), n)
        self.assertEqual(self.kt_handle.get('k1'), 'one')
        self.assertEqual(self.kt_handle.get('k2'), 'two')
        self.assertEqual(self.kt_handle.get('k3'), 'three')
        self.assertEqual(self.kt_handle.get('k4'), 'four')
        self.assertEqual(self.kt_handle.get('k\n5'), 'five')
        self.assertEqual(self.kt_handle.get('k\t6'), 'six')
        self.assertEqual(self.kt_handle.get('k7'), 111)

        d = self.kt_handle.get_bulk(
            ['k1', 'k2', 'k3', 'k4', 'k\n5', 'k\t6', 'k7'])

        self.assertEqual(len(d), len(dict))
        self.assertEqual(d, dict)

        self.assertEqual(self.kt_handle.count(), 7)
        n = self.kt_handle.remove_bulk(['k1', 'k2', 'k\t6'])
        self.assertEqual(self.kt_handle.count(), 4)
        n = self.kt_handle.remove_bulk(['k3'], atomic=True)
        self.assertEqual(self.kt_handle.count(), 3)

    def test_get_bulk(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('a', 'one'))
        self.assertTrue(self.kt_handle.set('b', 'two'))
        self.assertTrue(self.kt_handle.set('c', 'three'))
        self.assertTrue(self.kt_handle.set('d', 'four'))

        d = self.kt_handle.get_bulk(['a', 'b', 'c', 'd'])
        assert d is not None

        self.assertEqual(d['a'], 'one')
        self.assertEqual(d['b'], 'two')
        self.assertEqual(d['c'], 'three')
        self.assertEqual(d['d'], 'four')
        self.assertEqual(len(d), 4)

        d = self.kt_handle.get_bulk(['a', 'x', 'y', 'd'])
        self.assertEqual(len(d), 2)
        d = self.kt_handle.get_bulk(['w', 'x', 'y', 'z'])
        self.assertEqual(len(d), 0)
        d = self.kt_handle.get_bulk([])
        self.assertEqual(d, {})

    def test_large_key(self):
        large_key = 'x' * self.LARGE_KEY_LEN
        self.assertTrue(self.kt_handle.set(large_key, 'value'))
        self.assertEqual(self.kt_handle.get(large_key), 'value')

    def test_report(self):
        report = None
        report = self.kt_handle.report()
        assert report is not None

        self.assertTrue(int(report['serv_conn_count']) > 0)

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

        self.assertTrue(status['count'], 0)
        self.kt_handle.set('red', 'apple')
        self.kt_handle.set('yellow', 'banana')
        self.kt_handle.set('pink', 'peach')
        self.assertTrue(status['count'], 3)

    def test_vacuum(self):
        self.assertTrue(self.kt_handle.vacuum())

    def test_match_prefix(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))
        self.assertTrue(self.kt_handle.set('abcdef', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefg', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefgh', 'val'))

        list = self.kt_handle.match_prefix('abc')
        self.assertEqual(len(list), 6)
        list = self.kt_handle.match_prefix('abcd')
        self.assertEqual(len(list), 5)
        list = self.kt_handle.match_prefix('abc', 1)
        self.assertEqual(list[0][:3], 'abc')
        list = self.kt_handle.match_prefix('abc', 3)
        self.assertEqual(len(list), 3)

    def test_match_regex(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))

        list = self.kt_handle.match_regex('^abc')
        self.assertEqual(len(list), 3)
        list = self.kt_handle.match_regex('^abcd')
        self.assertEqual(len(list), 2)
        list = self.kt_handle.match_regex('e$')
        self.assertEqual(len(list), 1)
        list = self.kt_handle.match_regex('^abc', 2)
        self.assertEqual(len(list), 2)

    def test_match_similar(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('potatoes', 'val'))
        self.assertTrue(self.kt_handle.set('potataes', 'val'))
        self.assertTrue(self.kt_handle.set('patataes', 'val'))

        list = self.kt_handle.match_similar('potatoes', 0)
        self.assertEqual(len(list), 1)
        list = self.kt_handle.match_similar('potatoes', 1)
        self.assertEqual(len(list), 2)
        list = self.kt_handle.match_similar('potatoes', 2)
        self.assertEqual(len(list), 3)
        list = self.kt_handle.match_similar('potatoes', 2, 1)
        self.assertEqual(len(list), 1)

        self.assertTrue(self.kt_handle.set('cafe', 'val'))
        self.assertTrue(
            self.kt_handle.set(b'caf\xc3'.decode('iso8859-1'), 'val'))
        list = self.kt_handle.match_similar('cafe', 1)
        self.assertEqual(len(list), 2)

    def test_cursor(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertEqual(self.kt_handle.count(), 0)
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))
        self.assertEqual(self.kt_handle.count(), 3)

        cur = self.kt_handle.cursor()
        self.assertTrue(cur.jump())
        while True:
            self.assertEqual(cur.get_key()[:3], 'abc')
            self.assertEqual(cur.get_value(), 'val')

            pair = cur.get()
            self.assertEqual(len(pair), 2)
            self.assertEqual(pair[0][:3], 'abc')
            self.assertEqual(pair[1], 'val')

            self.assertTrue(cur.set_value('foo'))
            self.assertEqual(cur.get_value(), 'foo')

            if pair[0] == 'abcd':
                self.assertTrue(cur.remove())
                break

            if not cur.step():
                break

        self.assertTrue(cur.delete())
        self.assertEqual(self.kt_handle.count(), 2)

        self.assertTrue(self.kt_handle.set('zabc', 'val'))
        self.assertTrue(self.kt_handle.set('zabcd', 'val'))
        self.assertTrue(self.kt_handle.set('zabcde', 'val'))
        self.assertEqual(self.kt_handle.count(), 5)

        cur = self.kt_handle.cursor()
        self.assertTrue(cur.jump(key='zabc'))
        while True:
            pair = cur.get()

            if pair[0] == 'zabc':
                dict = cur.seize()
                self.assertEqual(len(dict), 2)
                self.assertEqual(dict['key'][:4], 'zabc')
                self.assertEqual(dict['value'], 'val')

            if not cur.step():
                break

        self.assertTrue(cur.delete())
        self.assertEqual(self.kt_handle.count(), 4)
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 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.º 3
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
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.º 5
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()
        self.LARGE_KEY_LEN = 8000

    def test_set(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        self.assertTrue(self.kt_handle.set('key', 'value'))
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertTrue(self.kt_handle.set('k e y', 'v a l u e'))
        self.assertTrue(self.kt_handle.set('k\te\ty', 'tabbed'))

        self.assertEqual(self.kt_handle.get('key'), 'value')
        self.assertEqual(self.kt_handle.get('k e y'), 'v a l u e')
        self.assertEqual(self.kt_handle.get('k\te\ty'), 'tabbed')

        self.assertTrue(self.kt_handle.set('\\key', '\\xxx'))
        self.assertEqual(self.kt_handle.get('\\key'), '\\xxx')
        self.assertEqual(self.kt_handle.count(), 4)

        self.assertTrue(self.kt_handle.set('tabbed\tkey', 'tabbled\tvalue'))
        self.assertTrue(self.kt_handle.get('tabbed\tkey'))

        self.assertTrue(self.kt_handle.set('url1', 'http://github.com'))
        self.assertTrue(self.kt_handle.set('url2', 'https://github.com/'))
        self.assertTrue(self.kt_handle.set('url3', 'https://github.com/blog/'))

        self.assertTrue(self.kt_handle.set('http://github.com', 'url1'))
        self.assertTrue(self.kt_handle.set('https://github.com', 'url2'))
        self.assertTrue(self.kt_handle.set('https://github.com/blog/', 'url3'))

        self.assertFalse(self.kt_handle.set(None, 'value'))
        self.assertEqual(error.code(), error.LOGIC)

        self.assertFalse(self.kt_handle.get(None))
        self.assertEqual(error.code(), error.LOGIC)

        self.assertEqual(self.kt_handle.get('non_existent'), None)
        self.assertEqual(error.code(), error.NOTFOUND)

        self.assertTrue(self.kt_handle.set('cb', 1791))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('cb'), 1791)
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertTrue(self.kt_handle.set('cb', 1791.1226))
        self.assertEqual(self.kt_handle.get('cb'), 1791.1226)

    def test_cas(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        self.assertTrue(self.kt_handle.set('key', 'xxx'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.cas('key', old_val='xxx', new_val='yyy'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('key'), 'yyy')
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertTrue(self.kt_handle.cas('key', old_val='yyy'))
        assert self.kt_handle.get('key') is None
        self.assertEqual(error.code(), error.NOTFOUND)
        self.assertTrue(self.kt_handle.cas('key', new_val='zzz'))
        self.assertEqual(self.kt_handle.get('key'), 'zzz')
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertFalse(self.kt_handle.cas('key', old_val='foo', new_val='zz'))
        self.assertEqual(error.code(), error.EMISC)
        self.assertEqual(self.kt_handle.get('key'), 'zzz')
        self.assertEqual(error.code(), error.SUCCESS)

    def test_remove(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        self.assertFalse(self.kt_handle.remove('must fail key'))
        self.assertEqual(error.code(), error.NOTFOUND)
        self.assertTrue(self.kt_handle.set('deleteable key', 'xxx'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.remove('deleteable key'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertFalse(self.kt_handle.remove(None))
        self.assertEqual(error.code(), error.LOGIC)

    def test_replace(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        # Must Fail - Can't replace something that doesn't exist.
        self.assertFalse(self.kt_handle.replace('xxxxxx', 'some value'))
        self.assertEqual(error.code(), error.NOTFOUND)

        # Popuate then Replace.
        self.assertTrue(self.kt_handle.set('apple', 'ringo'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.replace('apple', 'apfel'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('apple'), 'apfel')
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertFalse(self.kt_handle.replace(None, 'value'))
        self.assertEqual(error.code(), error.LOGIC)

        self.assertTrue(self.kt_handle.replace('apple', 212))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('apple'), 212)
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.replace('apple', 121))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('apple'), 121)
        self.assertEqual(error.code(), error.SUCCESS)

    def test_append(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('k1', 'abc'))
        self.assertTrue(self.kt_handle.append('k1', 'def'))
        self.assertEqual(self.kt_handle.get('k1'), 'abcdef')

        self.assertTrue(self.kt_handle.append('k2', 'new val'))
        self.assertEqual(self.kt_handle.get('k2'), 'new val')

        self.assertTrue(self.kt_handle.set('k3', 777))
        self.assertFalse(self.kt_handle.append('k3', 111))

    def test_add(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('stewie', 'griffin'))

        # Must Fail - Stewie exists
        self.assertFalse(self.kt_handle.add('stewie', 'hopkin'))

        # New records
        self.assertTrue(self.kt_handle.add('peter', 'griffin'))
        self.assertTrue(self.kt_handle.add('lois', 'griffin'))
        self.assertTrue(self.kt_handle.add('seth', 'green'))
        self.assertTrue(self.kt_handle.add('nyc', 'new york city'))
        self.assertFalse(self.kt_handle.add(None, 'value'))

        self.assertTrue(self.kt_handle.add('number', 111))
        self.assertEqual(self.kt_handle.get('number'), 111)

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

        dict = {
            'k1': 'one',
            'k2': 'two',
            'k3': 'three',
            'k4': 'four',
            'k\n5': 'five',
            'k\t6': 'six',
            'k7': 111
        }

        n = self.kt_handle.set_bulk(dict)
        self.assertEqual(len(dict), n)
        self.assertEqual(self.kt_handle.get('k1'), 'one')
        self.assertEqual(self.kt_handle.get('k2'), 'two')
        self.assertEqual(self.kt_handle.get('k3'), 'three')
        self.assertEqual(self.kt_handle.get('k4'), 'four')
        self.assertEqual(self.kt_handle.get('k\n5'), 'five')
        self.assertEqual(self.kt_handle.get('k\t6'), 'six')
        self.assertEqual(self.kt_handle.get('k7'), 111)

        d = self.kt_handle.get_bulk(['k1', 'k2', 'k3', 'k4',
                                     'k\n5', 'k\t6', 'k7'])

        self.assertEqual(len(d), len(dict))
        self.assertEqual(d, dict)

        self.assertEqual(self.kt_handle.count(), 7)
        n = self.kt_handle.remove_bulk(['k1', 'k2', 'k\t6'])
        self.assertEqual(self.kt_handle.count(), 4)
        n = self.kt_handle.remove_bulk(['k3'], atomic=True)
        self.assertEqual(self.kt_handle.count(), 3)

    def test_get_bulk(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('a', 'one'))
        self.assertTrue(self.kt_handle.set('b', 'two'))
        self.assertTrue(self.kt_handle.set('c', 'three'))
        self.assertTrue(self.kt_handle.set('d', 'four'))

        d = self.kt_handle.get_bulk(['a','b','c','d'])
        assert d is not None

        self.assertEqual(d['a'], 'one')
        self.assertEqual(d['b'], 'two')
        self.assertEqual(d['c'], 'three')
        self.assertEqual(d['d'], 'four')
        self.assertEqual(len(d), 4)

        d = self.kt_handle.get_bulk(['a','x','y','d'])
        self.assertEqual(len(d), 2)
        d = self.kt_handle.get_bulk(['w','x','y','z'])
        self.assertEqual(len(d), 0)
        d = self.kt_handle.get_bulk([])
        self.assertEqual(d, {})

    def test_large_key(self):
        large_key = 'x' * self.LARGE_KEY_LEN
        self.assertTrue(self.kt_handle.set(large_key, 'value'))
        self.assertEqual(self.kt_handle.get(large_key), 'value')

    def test_report(self):
        report = None
        report = self.kt_handle.report()
        assert report is not None

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

        self.assertTrue(status['count'], 0)
        self.kt_handle.set('red', 'apple')
        self.kt_handle.set('yellow', 'banana')
        self.kt_handle.set('pink', 'peach')
        self.assertTrue(status['count'], 3)

    def test_error(self):
        self.assertTrue(self.kt_handle.clear())
        kt_error = self.kt_handle.error()
        assert kt_error is not None
        self.assertEqual(kt_error.code(), kt_error.SUCCESS)

    def test_vacuum(self):
        self.assertTrue(self.kt_handle.vacuum())

    def test_match_prefix(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))
        self.assertTrue(self.kt_handle.set('abcdef', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefg', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefgh', 'val'))

        list = self.kt_handle.match_prefix('abc')
        self.assertEqual(len(list), 6)
        list = self.kt_handle.match_prefix('abcd')
        self.assertEqual(len(list), 5)
        list = self.kt_handle.match_prefix('abc', 1)
        self.assertEqual(list[0], 'abc')

    def test_match_regex(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))

        list = self.kt_handle.match_regex('^abc')
        self.assertEqual(len(list), 3)
        list = self.kt_handle.match_regex('^abcd')
        self.assertEqual(len(list), 2)
        list = self.kt_handle.match_regex('e$')
        self.assertEqual(len(list), 1)
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon()
        self.kt_handle.open()
        self.LARGE_KEY_LEN = 8000

    def test_set(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        self.assertTrue(self.kt_handle.set('key', 'value'))
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertTrue(self.kt_handle.set('k e y', 'v a l u e'))
        self.assertTrue(self.kt_handle.set('k\te\ty', 'tabbed'))

        self.assertEqual(self.kt_handle.get('key'), 'value')
        self.assertEqual(self.kt_handle.get('k e y'), 'v a l u e')
        self.assertEqual(self.kt_handle.get('k\te\ty'), 'tabbed')

        self.assertTrue(self.kt_handle.set('\\key', '\\xxx'))
        self.assertEqual(self.kt_handle.get('\\key'), '\\xxx')
        self.assertEqual(self.kt_handle.count(), 4)

        self.assertTrue(self.kt_handle.set('tabbed\tkey', 'tabbled\tvalue'))
        self.assertTrue(self.kt_handle.get('tabbed\tkey'))

        self.assertTrue(self.kt_handle.set('url1', 'http://github.com'))
        self.assertTrue(self.kt_handle.set('url2', 'https://github.com/'))
        self.assertTrue(self.kt_handle.set('url3', 'https://github.com/blog/'))

        self.assertTrue(self.kt_handle.set('http://github.com', 'url1'))
        self.assertTrue(self.kt_handle.set('https://github.com', 'url2'))
        self.assertTrue(self.kt_handle.set('https://github.com/blog/', 'url3'))

        self.assertFalse(self.kt_handle.set(None, 'value'))
        self.assertEqual(error.code(), error.LOGIC)

        self.assertFalse(self.kt_handle.get(None))
        self.assertEqual(error.code(), error.LOGIC)

        self.assertEqual(self.kt_handle.get('non_existent'), None)
        self.assertEqual(error.code(), error.NOTFOUND)

        self.assertTrue(self.kt_handle.set('cb', 1791))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('cb'), 1791)
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertTrue(self.kt_handle.set('cb', 1791.1226))
        self.assertEqual(self.kt_handle.get('cb'), 1791.1226)

    def test_cas(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        self.assertTrue(self.kt_handle.set('key', 'xxx'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.cas('key', old_val='xxx',
                                           new_val='yyy'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('key'), 'yyy')
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertTrue(self.kt_handle.cas('key', old_val='yyy'))
        assert self.kt_handle.get('key') is None
        self.assertEqual(error.code(), error.NOTFOUND)
        self.assertTrue(self.kt_handle.cas('key', new_val='zzz'))
        self.assertEqual(self.kt_handle.get('key'), 'zzz')
        self.assertEqual(error.code(), error.SUCCESS)

        self.assertFalse(self.kt_handle.cas('key', old_val='foo',
                                            new_val='zz'))
        self.assertEqual(error.code(), error.EMISC)
        self.assertEqual(self.kt_handle.get('key'), 'zzz')
        self.assertEqual(error.code(), error.SUCCESS)

    def test_remove(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        self.assertFalse(self.kt_handle.remove('must fail key'))
        self.assertEqual(error.code(), error.NOTFOUND)
        self.assertTrue(self.kt_handle.set('deleteable key', 'xxx'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.remove('deleteable key'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertFalse(self.kt_handle.remove(None))
        self.assertEqual(error.code(), error.LOGIC)

    def test_replace(self):
        self.assertTrue(self.kt_handle.clear())
        error = self.kt_handle.error()

        # Must Fail - Can't replace something that doesn't exist.
        self.assertFalse(self.kt_handle.replace('xxxxxx', 'some value'))
        self.assertEqual(error.code(), error.NOTFOUND)

        # Popuate then Replace.
        self.assertTrue(self.kt_handle.set('apple', 'ringo'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.replace('apple', 'apfel'))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('apple'), 'apfel')
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertFalse(self.kt_handle.replace(None, 'value'))
        self.assertEqual(error.code(), error.LOGIC)

        self.assertTrue(self.kt_handle.replace('apple', 212))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('apple'), 212)
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertTrue(self.kt_handle.replace('apple', 121))
        self.assertEqual(error.code(), error.SUCCESS)
        self.assertEqual(self.kt_handle.get('apple'), 121)
        self.assertEqual(error.code(), error.SUCCESS)

    def test_append(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('k1', 'abc'))
        self.assertTrue(self.kt_handle.append('k1', 'def'))
        self.assertEqual(self.kt_handle.get('k1'), 'abcdef')

        self.assertTrue(self.kt_handle.append('k2', 'new val'))
        self.assertEqual(self.kt_handle.get('k2'), 'new val')

        self.assertTrue(self.kt_handle.set('k3', 777))
        self.assertFalse(self.kt_handle.append('k3', 111))

    def test_add(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('stewie', 'griffin'))

        # Must Fail - Stewie exists
        self.assertFalse(self.kt_handle.add('stewie', 'hopkin'))

        # New records
        self.assertTrue(self.kt_handle.add('peter', 'griffin'))
        self.assertTrue(self.kt_handle.add('lois', 'griffin'))
        self.assertTrue(self.kt_handle.add('seth', 'green'))
        self.assertTrue(self.kt_handle.add('nyc', 'new york city'))
        self.assertFalse(self.kt_handle.add(None, 'value'))

        self.assertTrue(self.kt_handle.add('number', 111))
        self.assertEqual(self.kt_handle.get('number'), 111)

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

        dict = {
            'k1': 'one',
            'k2': 'two',
            'k3': 'three',
            'k4': 'four',
            'k\n5': 'five',
            'k\t6': 'six',
            'k7': 111
        }

        n = self.kt_handle.set_bulk(dict)
        self.assertEqual(len(dict), n)
        self.assertEqual(self.kt_handle.get('k1'), 'one')
        self.assertEqual(self.kt_handle.get('k2'), 'two')
        self.assertEqual(self.kt_handle.get('k3'), 'three')
        self.assertEqual(self.kt_handle.get('k4'), 'four')
        self.assertEqual(self.kt_handle.get('k\n5'), 'five')
        self.assertEqual(self.kt_handle.get('k\t6'), 'six')
        self.assertEqual(self.kt_handle.get('k7'), 111)

        d = self.kt_handle.get_bulk(
            ['k1', 'k2', 'k3', 'k4', 'k\n5', 'k\t6', 'k7'])

        self.assertEqual(len(d), len(dict))
        self.assertEqual(d, dict)

        self.assertEqual(self.kt_handle.count(), 7)
        n = self.kt_handle.remove_bulk(['k1', 'k2', 'k\t6'])
        self.assertEqual(self.kt_handle.count(), 4)
        n = self.kt_handle.remove_bulk(['k3'], atomic=True)
        self.assertEqual(self.kt_handle.count(), 3)

    def test_get_bulk(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('a', 'one'))
        self.assertTrue(self.kt_handle.set('b', 'two'))
        self.assertTrue(self.kt_handle.set('c', 'three'))
        self.assertTrue(self.kt_handle.set('d', 'four'))

        d = self.kt_handle.get_bulk(['a', 'b', 'c', 'd'])
        assert d is not None

        self.assertEqual(d['a'], 'one')
        self.assertEqual(d['b'], 'two')
        self.assertEqual(d['c'], 'three')
        self.assertEqual(d['d'], 'four')
        self.assertEqual(len(d), 4)

        d = self.kt_handle.get_bulk(['a', 'x', 'y', 'd'])
        self.assertEqual(len(d), 2)
        d = self.kt_handle.get_bulk(['w', 'x', 'y', 'z'])
        self.assertEqual(len(d), 0)
        d = self.kt_handle.get_bulk([])
        self.assertEqual(d, {})

    def test_large_key(self):
        large_key = 'x' * self.LARGE_KEY_LEN
        self.assertTrue(self.kt_handle.set(large_key, 'value'))
        self.assertEqual(self.kt_handle.get(large_key), 'value')

    def test_report(self):
        report = None
        report = self.kt_handle.report()
        assert report is not None

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

        self.assertTrue(status['count'], 0)
        self.kt_handle.set('red', 'apple')
        self.kt_handle.set('yellow', 'banana')
        self.kt_handle.set('pink', 'peach')
        self.assertTrue(status['count'], 3)

    def test_error(self):
        self.assertTrue(self.kt_handle.clear())
        kt_error = self.kt_handle.error()
        assert kt_error is not None
        self.assertEqual(kt_error.code(), kt_error.SUCCESS)

    def test_vacuum(self):
        self.assertTrue(self.kt_handle.vacuum())

    def test_match_prefix(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))
        self.assertTrue(self.kt_handle.set('abcdef', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefg', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefgh', 'val'))

        list = self.kt_handle.match_prefix('abc')
        self.assertEqual(len(list), 6)
        list = self.kt_handle.match_prefix('abcd')
        self.assertEqual(len(list), 5)
        list = self.kt_handle.match_prefix('abc', 1)
        self.assertEqual(list[0], 'abc')

    def test_match_regex(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))

        list = self.kt_handle.match_regex('^abc')
        self.assertEqual(len(list), 3)
        list = self.kt_handle.match_regex('^abcd')
        self.assertEqual(len(list), 2)
        list = self.kt_handle.match_regex('e$')
        self.assertEqual(len(list), 1)
Exemplo n.º 7
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
Exemplo n.º 8
0
class UnitTest(unittest.TestCase):
    def setUp(self):
        self.kt_handle = KyotoTycoon(binary=False, pack_type=KT_PACKER_PICKLE)
        self.kt_handle.open(port=11978)
        self.LARGE_KEY_LEN = 8000

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

        self.assertTrue(self.kt_handle.set('key', 'value'))

        self.assertTrue(self.kt_handle.set('k e y', 'v a l u e'))
        self.assertTrue(self.kt_handle.set('k\te\ty', 'tabbed'))

        self.assertEqual(self.kt_handle.get('key'), 'value')
        self.assertEqual(self.kt_handle.get('k e y'), 'v a l u e')
        self.assertEqual(self.kt_handle.get('k\te\ty'), 'tabbed')

        self.assertTrue(self.kt_handle.set('\\key', '\\xxx'))
        self.assertEqual(self.kt_handle.get('\\key'), '\\xxx')
        self.assertEqual(self.kt_handle.count(), 4)

        self.assertTrue(self.kt_handle.set('tabbed\tkey', 'tabbled\tvalue'))
        self.assertTrue(self.kt_handle.get('tabbed\tkey'))

        self.assertTrue(self.kt_handle.set('url1', 'http://github.com'))
        self.assertTrue(self.kt_handle.set('url2', 'https://github.com/'))
        self.assertTrue(self.kt_handle.set('url3', 'https://github.com/blog/'))

        self.assertTrue(self.kt_handle.set('http://github.com', 'url1'))
        self.assertTrue(self.kt_handle.set('https://github.com', 'url2'))
        self.assertTrue(self.kt_handle.set('https://github.com/blog/', 'url3'))

        self.assertEqual(self.kt_handle.get('non_existent'), None)

        self.assertTrue(self.kt_handle.set('cb', 1791))
        self.assertEqual(self.kt_handle.get('cb'), 1791)

        self.assertTrue(self.kt_handle.set('cb', 1791.1226))
        self.assertEqual(self.kt_handle.get('cb'), 1791.1226)

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

        self.assertTrue(self.kt_handle.set('key', 'xxx'))
        self.assertTrue(self.kt_handle.cas('key', old_val='xxx', new_val='yyy'))
        self.assertEqual(self.kt_handle.get('key'), 'yyy')

        self.assertTrue(self.kt_handle.cas('key', old_val='yyy'))
        assert self.kt_handle.get('key') is None
        self.assertTrue(self.kt_handle.cas('key', new_val='zzz'))
        self.assertEqual(self.kt_handle.get('key'), 'zzz')

        self.assertRaises(KyotoTycoonException, self.kt_handle.cas, 'key', old_val='foo', new_val='zz')
        self.assertEqual(self.kt_handle.get('key'), 'zzz')

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

        self.assertFalse(self.kt_handle.remove('must fail key'))
        self.assertTrue(self.kt_handle.set('deleteable key', 'xxx'))
        self.assertTrue(self.kt_handle.remove('deleteable key'))

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

        # Must Fail - Can't replace something that doesn't exist.
        self.assertFalse(self.kt_handle.replace('xxxxxx', 'some value'))

        # Popuate then Replace.
        self.assertTrue(self.kt_handle.set('apple', 'ringo'))
        self.assertTrue(self.kt_handle.replace('apple', 'apfel'))
        self.assertEqual(self.kt_handle.get('apple'), 'apfel')

        self.assertTrue(self.kt_handle.replace('apple', 212))
        self.assertEqual(self.kt_handle.get('apple'), 212)
        self.assertTrue(self.kt_handle.replace('apple', 121))
        self.assertEqual(self.kt_handle.get('apple'), 121)

    def test_append(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('k1', 'abc'))
        self.assertTrue(self.kt_handle.append('k1', 'def'))
        self.assertEqual(self.kt_handle.get('k1'), 'abcdef')

        self.assertTrue(self.kt_handle.append('k2', 'new val'))
        self.assertEqual(self.kt_handle.get('k2'), 'new val')

        self.assertTrue(self.kt_handle.set('k3', 777))

        self.assertTrue(self.kt_handle.set('k4', b'abc'))
        self.assertTrue(self.kt_handle.append('k4', 'abc'))
        self.assertEqual(self.kt_handle.get('k4'), b'abcabc')

        self.assertTrue(self.kt_handle.set('k5', 'abc'))
        self.assertTrue(self.kt_handle.append('k5', b'abc'))
        self.assertEqual(self.kt_handle.get('k5'), 'abcabc')

    def test_add(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('stewie', 'griffin'))

        # Must Fail - Stewie exists
        self.assertRaises(KyotoTycoonException, self.kt_handle.add, 'stewie', 'hopkin')

        # New records
        self.assertTrue(self.kt_handle.add('peter', 'griffin'))
        self.assertTrue(self.kt_handle.add('lois', 'griffin'))
        self.assertTrue(self.kt_handle.add('seth', 'green'))
        self.assertTrue(self.kt_handle.add('nyc', 'new york city'))

        self.assertTrue(self.kt_handle.add('number', 111))
        self.assertEqual(self.kt_handle.get('number'), 111)

    def test_check(self):
        self.assertTrue(self.kt_handle.set('check1', 'abc'))
        self.assertTrue(self.kt_handle.check('check1'))

        self.assertTrue(self.kt_handle.remove('check1'))
        self.assertFalse(self.kt_handle.check('check1'))

    def test_seize(self):
        self.assertTrue(self.kt_handle.set('seize1', 'abc'))
        self.assertEqual(self.kt_handle.get('seize1'), 'abc')
        self.assertEqual(self.kt_handle.seize('seize1'), 'abc')
        self.assertEqual(self.kt_handle.get('seize1'), None)
        self.assertEqual(self.kt_handle.seize('seize2'), None)

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

        dict = {
            'k1': 'one',
            'k2': 'two',
            'k3': 'three',
            'k4': 'four',
            'k\n5': 'five',
            'k\t6': 'six',
            'k7': 111
        }

        n = self.kt_handle.set_bulk(dict)
        self.assertEqual(len(dict), n)
        self.assertEqual(self.kt_handle.get('k1'), 'one')
        self.assertEqual(self.kt_handle.get('k2'), 'two')
        self.assertEqual(self.kt_handle.get('k3'), 'three')
        self.assertEqual(self.kt_handle.get('k4'), 'four')
        self.assertEqual(self.kt_handle.get('k\n5'), 'five')
        self.assertEqual(self.kt_handle.get('k\t6'), 'six')
        self.assertEqual(self.kt_handle.get('k7'), 111)

        d = self.kt_handle.get_bulk(['k1', 'k2', 'k3', 'k4',
                                     'k\n5', 'k\t6', 'k7'])

        self.assertEqual(len(d), len(dict))
        self.assertEqual(d, dict)

        self.assertEqual(self.kt_handle.count(), 7)
        n = self.kt_handle.remove_bulk(['k1', 'k2', 'k\t6'])
        self.assertEqual(self.kt_handle.count(), 4)
        n = self.kt_handle.remove_bulk(['k3'], atomic=True)
        self.assertEqual(self.kt_handle.count(), 3)

    def test_get_bulk(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('a', 'one'))
        self.assertTrue(self.kt_handle.set('b', 'two'))
        self.assertTrue(self.kt_handle.set('c', 'three'))
        self.assertTrue(self.kt_handle.set('d', 'four'))

        d = self.kt_handle.get_bulk(['a','b','c','d'])
        assert d is not None

        self.assertEqual(d['a'], 'one')
        self.assertEqual(d['b'], 'two')
        self.assertEqual(d['c'], 'three')
        self.assertEqual(d['d'], 'four')
        self.assertEqual(len(d), 4)

        d = self.kt_handle.get_bulk(['a','x','y','d'])
        self.assertEqual(len(d), 2)
        d = self.kt_handle.get_bulk(['w','x','y','z'])
        self.assertEqual(len(d), 0)
        d = self.kt_handle.get_bulk([])
        self.assertEqual(d, {})

    def test_large_key(self):
        large_key = 'x' * self.LARGE_KEY_LEN
        self.assertTrue(self.kt_handle.set(large_key, 'value'))
        self.assertEqual(self.kt_handle.get(large_key), 'value')

    def test_report(self):
        report = None
        report = self.kt_handle.report()
        assert report is not None

        self.assertTrue(int(report['serv_conn_count']) > 0)

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

        self.assertTrue(status['count'], 0)
        self.kt_handle.set('red', 'apple')
        self.kt_handle.set('yellow', 'banana')
        self.kt_handle.set('pink', 'peach')
        self.assertTrue(status['count'], 3)

    def test_vacuum(self):
        self.assertTrue(self.kt_handle.vacuum())

    def test_match_prefix(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))
        self.assertTrue(self.kt_handle.set('abcdef', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefg', 'val'))
        self.assertTrue(self.kt_handle.set('abcdefgh', 'val'))

        list = self.kt_handle.match_prefix('abc')
        self.assertEqual(len(list), 6)
        list = self.kt_handle.match_prefix('abcd')
        self.assertEqual(len(list), 5)
        list = self.kt_handle.match_prefix('abc', 1)
        self.assertEqual(list[0][:3], 'abc')
        list = self.kt_handle.match_prefix('abc', 3)
        self.assertEqual(len(list), 3)

    def test_match_regex(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))

        list = self.kt_handle.match_regex('^abc')
        self.assertEqual(len(list), 3)
        list = self.kt_handle.match_regex('^abcd')
        self.assertEqual(len(list), 2)
        list = self.kt_handle.match_regex('e$')
        self.assertEqual(len(list), 1)
        list = self.kt_handle.match_regex('^abc', 2)
        self.assertEqual(len(list), 2)

    def test_match_similar(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertTrue(self.kt_handle.set('potatoes', 'val'))
        self.assertTrue(self.kt_handle.set('potataes', 'val'))
        self.assertTrue(self.kt_handle.set('patataes', 'val'))

        list = self.kt_handle.match_similar('potatoes', 0)
        self.assertEqual(len(list), 1)
        list = self.kt_handle.match_similar('potatoes', 1)
        self.assertEqual(len(list), 2)
        list = self.kt_handle.match_similar('potatoes', 2)
        self.assertEqual(len(list), 3)
        list = self.kt_handle.match_similar('potatoes', 2, 1)
        self.assertEqual(len(list), 1)

        self.assertTrue(self.kt_handle.set('cafe', 'val'))
        self.assertTrue(self.kt_handle.set(b'caf\xc3'.decode('iso8859-1'), 'val'))
        list = self.kt_handle.match_similar('cafe', 1)
        self.assertEqual(len(list), 2)


    def test_cursor(self):
        self.assertTrue(self.kt_handle.clear())
        self.assertEqual(self.kt_handle.count(), 0)
        self.assertTrue(self.kt_handle.set('abc', 'val'))
        self.assertTrue(self.kt_handle.set('abcd', 'val'))
        self.assertTrue(self.kt_handle.set('abcde', 'val'))
        self.assertEqual(self.kt_handle.count(), 3)

        cur = self.kt_handle.cursor()
        self.assertTrue(cur.jump())
        while True:
            self.assertEqual(cur.get_key()[:3], 'abc')
            self.assertEqual(cur.get_value(), 'val')

            pair = cur.get()
            self.assertEqual(len(pair), 2)
            self.assertEqual(pair[0][:3], 'abc')
            self.assertEqual(pair[1], 'val')

            self.assertTrue(cur.set_value('foo'))
            self.assertEqual(cur.get_value(), 'foo')

            if pair[0] == 'abcd':
                self.assertTrue(cur.remove())
                break

            if not cur.step():
                break

        self.assertTrue(cur.delete())
        self.assertEqual(self.kt_handle.count(), 2)

        self.assertTrue(self.kt_handle.set('zabc', 'val'))
        self.assertTrue(self.kt_handle.set('zabcd', 'val'))
        self.assertTrue(self.kt_handle.set('zabcde', 'val'))
        self.assertEqual(self.kt_handle.count(), 5)

        cur = self.kt_handle.cursor()
        self.assertTrue(cur.jump(key='zabc'))
        while True:
            pair = cur.get()

            if pair[0] == 'zabc':
                dict = cur.seize()
                self.assertEqual(len(dict), 2)
                self.assertEqual(dict['key'][:4], 'zabc')
                self.assertEqual(dict['value'], 'val')

            if not cur.step():
                break

        self.assertTrue(cur.delete())
        self.assertEqual(self.kt_handle.count(), 4)