class UnitTest(unittest.TestCase): def setUp(self): self.kt_bin_handle = KyotoTycoon(binary=True, pack_type=kt_binary.KT_PACKER_CUSTOM, custom_packer=CustomPacker()) self.kt_bin_handle.open(port=11978) self.kt_http_handle = KyotoTycoon(binary=False, pack_type=kt_http.KT_PACKER_CUSTOM, custom_packer=CustomPacker()) self.kt_http_handle.open(port=11978) def tearDown(self): self.kt_http_handle.close() self.kt_bin_handle.close() def test_packer_bytes(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) self.assertTrue(self.kt_bin_handle.set('key1', 'abc')) self.assertEqual(self.kt_bin_handle.get('key1'), 'abc') self.assertTrue(self.kt_http_handle.set('key2', 'abcd')) self.assertEqual(self.kt_http_handle.get('key2'), 'abcd') self.assertTrue(self.kt_http_handle.append('key2', 'xyz')) self.assertEqual(self.kt_http_handle.get('key2'), 'abcdxyz') self.assertEqual(self.kt_http_handle.count(), 2) self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0)
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)
class UnitTest(unittest.TestCase): def setUp(self): memc_packer = MemcachePacker(gzip_enabled=True, gzip_threshold=10, gzip_flag=1) self.kt_bin_handle = KyotoTycoon(binary=True, pack_type=kt_binary.KT_PACKER_CUSTOM, custom_packer=memc_packer) self.kt_bin_handle.open(port=11978) self.kt_http_handle = KyotoTycoon(binary=False, pack_type=kt_http.KT_PACKER_CUSTOM, custom_packer=memc_packer) self.kt_http_handle.open(port=11978) def tearDown(self): self.kt_http_handle.close() self.kt_bin_handle.close() def test_packer_bin(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) # Uncompressed self.assertTrue(self.kt_bin_handle.set('key1-1', b'12345')) self.assertEqual(self.kt_bin_handle.get('key1-1'), b'12345') # Compressed self.assertTrue( self.kt_bin_handle.set('key1-2', b'12345678901234567890')) self.assertEqual(self.kt_bin_handle.get('key1-2'), b'12345678901234567890') self.assertEqual(self.kt_http_handle.count(), 2) self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) def test_packer_http(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) # Uncompressed self.assertTrue(self.kt_http_handle.set('key2-1', b'12345')) self.assertEqual(self.kt_http_handle.get('key2-1'), b'12345') # Compressed self.assertTrue( self.kt_http_handle.set('key2-2', b'12345678901234567890')) self.assertEqual(self.kt_http_handle.get('key2-2'), b'12345678901234567890') self.assertEqual(self.kt_http_handle.count(), 2) self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0)
class UnitTest(unittest.TestCase): def setUp(self): self.kt_handle = KyotoTycoon() self.kt_handle.open(port=11978) def tearDown(self): self.kt_handle.close() 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)
class UnitTest(unittest.TestCase): def setUp(self): memc_packer = SimpleMemcachePacker() self.kt_bin_handle = KyotoTycoon(binary=True, pack_type=kt_binary.KT_PACKER_CUSTOM, custom_packer=memc_packer) self.kt_bin_handle.open(port=11978) self.kt_http_handle = KyotoTycoon(binary=False, pack_type=kt_http.KT_PACKER_CUSTOM, custom_packer=memc_packer) self.kt_http_handle.open(port=11978) def tearDown(self): self.kt_http_handle.close() self.kt_bin_handle.close() def test_packer_bin(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) self.assertTrue(self.kt_bin_handle.set('key1-1', (b'12345', 1234))) self.assertEqual(self.kt_bin_handle.get('key1-1'), (b'12345', 1234)) self.assertRaises(ValueError, self.kt_bin_handle.set, 'key1-2', (b'12345', -1)) self.assertEqual(self.kt_http_handle.count(), 1) self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) def test_packer_http(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) self.assertTrue(self.kt_http_handle.set('key2-1', (b'12345', 1234))) self.assertEqual(self.kt_http_handle.get('key2-1'), (b'12345', 1234)) self.assertRaises(ValueError, self.kt_http_handle.set, 'key2-2', (b'12345', -1)) self.assertEqual(self.kt_http_handle.count(), 1) self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0)
class UnitTest(unittest.TestCase): def setUp(self): self.kt_http_handle = KyotoTycoon(binary=False) self.kt_http_handle.open(port=11978) self.kt_bin_handle = KyotoTycoon(binary=True) self.kt_bin_handle.open(port=11978) def tearDown(self): self.kt_http_handle.close() self.kt_bin_handle.close() def test_play_script(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) self.assertEqual( self.kt_bin_handle.play_script('echo', {'key1': b'abc'}), {'key1': b'abc'}) self.assertEqual( self.kt_bin_handle.play_script('set', { 'key': b'key2', 'value': b'abcd' }), {}) out = self.kt_bin_handle.play_script('get', {'key': b'key2'}) self.assertTrue('value' in out) self.assertEqual(out['value'], b'abcd') self.assertEqual( self.kt_http_handle.play_script('echo', {'key3': b'abc'}), {'key3': b'abc'}) self.assertEqual( self.kt_http_handle.play_script('set', { 'key': b'key4', 'value': b'abcd' }), {}) out = self.kt_http_handle.play_script('get', {'key': b'key4'}) self.assertTrue('value' in out) self.assertEqual(out['value'], b'abcd')
class UnitTest(unittest.TestCase): def setUp(self): self.kt_bin_handle = KyotoTycoon(binary=True, pack_type=kt_binary.KT_PACKER_BYTES) self.kt_bin_handle.open(port=11978) self.kt_http_handle = KyotoTycoon(binary=False, pack_type=kt_http.KT_PACKER_BYTES) self.kt_http_handle.open(port=11978) def tearDown(self): self.kt_http_handle.close() self.kt_bin_handle.close() def test_packer_bytes(self): self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0) value1 = bytes([0x00, 0x01, 0x02, 0xff]) self.assertTrue(self.kt_bin_handle.set('key1', value1)) value2 = self.kt_bin_handle.get('key1') self.assertEqual(value1, value2) self.assertEqual(type(value1), type(value2)) value3 = bytes([0x00, 0x01, 0x03, 0xff]) self.assertTrue(self.kt_http_handle.set('key2', value3)) value4 = self.kt_http_handle.get('key2') self.assertEqual(value3, value4) self.assertEqual(type(value3), type(value4)) self.assertTrue(self.kt_http_handle.append('key2', 'xyz')) self.assertEqual(self.kt_http_handle.get('key2'), value3 + b'xyz') self.assertEqual(self.kt_http_handle.count(), 2) self.assertTrue(self.kt_http_handle.clear()) self.assertEqual(self.kt_http_handle.count(), 0)
from kyototycoon import KyotoTycoon import sys,time # sample usage kt = KyotoTycoon() kt.open("localhost", 1978) kt.set("japan:sexy", "tokyo00000ooo") #build for masks form cid =0 for i in range(0,100): cid +=1 if not kt.set("mask:"+str(cid)+":maskid", str(cid)) or\ not kt.set("mask:"+str(cid)+":userid", str(cid)) or\ not kt.set("mask:"+str(cid)+":boardid", str(cid)) or\ not kt.set("mask:"+str(cid)+":fileid", str(cid)) or\ not kt.set("mask:"+str(cid)+":description", str("Mask的内容描述")) or\ not kt.set("mask:"+str(cid)+":content", str("哇。。阳光暖暖的,好惬意的。")) or\ not kt.set("mask:"+str(cid)+":ups",bytes(int("100"))) or\ not kt.set("mask:"+str(cid)+":downs",bytes(int("10"))) or\ not kt.set("mask:"+str(cid)+":hits",bytes(int("110"))) or\ not kt.set("mask:"+str(cid)+":order", bytes(cid)) or\ not kt.set("maks:"+str(cid)+":createdata",str(int(time.time()))) or\ not kt.set("maks:"+str(cid)+":commentcount","8") or\ not kt.set("mask:count", "3"): print >>sys.stderr, "set error: " + str(kt.error()) k=kt.get("mask:100:content") print(str(k,'UTF-8')) #kt.remove("japan:sexy") kt.close()
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)
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
class UnitTest(unittest.TestCase): def setUp(self): self.kt_http_handle = KyotoTycoon(binary=False) self.kt_http_handle.open(port=11978) self.kt_bin_handle = KyotoTycoon(binary=True) self.kt_bin_handle.open(port=11978) self.LARGE_KEY_LEN = 8000 def tearDown(self): self.kt_http_handle.close() self.kt_bin_handle.close() def test_set_expire(self): self.assertTrue(self.kt_http_handle.clear()) # Set record to be expired in 2 seconds. self.assertTrue(self.kt_http_handle.set('key1', 'value', 2)) self.assertEqual(self.kt_http_handle.get('key1'), 'value') self.assertEqual(self.kt_http_handle.count(), 1) time.sleep(4) self.assertEqual(self.kt_http_handle.count(), 0) # Bulk expire in 2 seconds. self.assertTrue( self.kt_http_handle.set_bulk( { 'key1-1': 'value1', 'key1-2': 'value2' }, 2)) self.assertEqual(self.kt_http_handle.get('key1-1'), 'value1') self.assertEqual(self.kt_http_handle.get('key1-2'), 'value2') self.assertEqual(self.kt_http_handle.count(), 2) time.sleep(4) self.assertEqual(self.kt_http_handle.count(), 0) # Set record to be expired in 2 seconds. self.assertTrue(self.kt_bin_handle.set('key2', 'value', 2)) self.assertEqual(self.kt_bin_handle.get('key2'), 'value') self.assertEqual(self.kt_http_handle.count(), 1) time.sleep(4) self.assertEqual(self.kt_http_handle.count(), 0) # Bulk expire in 2 seconds. self.assertTrue( self.kt_bin_handle.set_bulk( { 'key2-1': 'value1', 'key2-2': 'value2' }, 2)) self.assertEqual(self.kt_bin_handle.get('key2-1'), 'value1') self.assertEqual(self.kt_bin_handle.get('key2-2'), 'value2') self.assertEqual(self.kt_http_handle.count(), 2) time.sleep(4) self.assertEqual(self.kt_http_handle.count(), 0) def test_add_expire(self): self.assertTrue(self.kt_http_handle.clear()) self.assertTrue(self.kt_http_handle.add('hello', 'world', 2)) self.assertEqual(self.kt_http_handle.get('hello'), 'world') time.sleep(4) self.assertEqual(self.kt_http_handle.get('hello'), None)
class UnitTest(unittest.TestCase): def setUp(self): # For operations not supported by the binary protocol, but useful for testing it... self.kt_handle_http = KyotoTycoon(binary=False) self.kt_handle_http.open(port=11978) self.kt_handle = KyotoTycoon(binary=True, pack_type=KT_PACKER_PICKLE) self.kt_handle.open(port=11978) self.LARGE_KEY_LEN = 8000 def tearDown(self): self.kt_handle.close() self.kt_handle_http.close() def test_set(self): self.assertTrue(self.kt_handle_http.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_http.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_remove(self): self.assertTrue(self.kt_handle_http.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_set_bulk(self): self.assertTrue(self.kt_handle_http.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, atomic=False) 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'], atomic=False) self.assertEqual(len(d), len(dict)) self.assertEqual(d, dict) self.assertEqual(self.kt_handle_http.count(), 7) n = self.kt_handle.remove_bulk(['k1', 'k2', 'k\t6'], atomic=False) self.assertEqual(self.kt_handle_http.count(), 4) n = self.kt_handle.remove_bulk(['k3'], atomic=False) self.assertEqual(self.kt_handle_http.count(), 3) def test_get_bulk(self): self.assertTrue(self.kt_handle_http.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'], atomic=False) 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'], atomic=False) self.assertEqual(len(d), 2) d = self.kt_handle.get_bulk(['w', 'x', 'y', 'z'], atomic=False) self.assertEqual(len(d), 0) d = self.kt_handle.get_bulk([], atomic=False) 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')