Permalink
Browse files

Allow selecting DB for set_int/get_int ops. Added tests.

  • Loading branch information...
1 parent 3cc9ae4 commit 95a82017eba0f1b64045a3b425cf6f90a09f889f Toru Maesaka committed May 16, 2011
Showing with 49 additions and 14 deletions.
  1. +16 −7 kyototycoon/kt_http.py
  2. +4 −4 kyototycoon/kyototycoon.py
  3. +29 −3 tests/t_multi.py
View
@@ -76,10 +76,16 @@ def get(self, key, db=None):
return self.unpack(body)
- def get_int(self, key):
- if key is None: return False
- key = urllib.quote(key.encode('UTF-8'))
- self.conn.request('GET', key)
+ def get_int(self, key, db=None):
+ if key is None:
+ return False
+
+ path = key
+ if db:
+ path = '/%s/%s' % (db, key)
+ path = urllib.quote(path.encode('UTF-8'))
+
+ self.conn.request('GET', path)
rv = self.conn.getresponse()
buf = rv.read()
@@ -105,15 +111,18 @@ def set(self, key, value, expire, db=None):
value = self.pack(value)
return self._rest_put(path, value, expire) == 201
- def set_int(self, key, value, expire):
+ def set_int(self, key, value, expire, db=None):
if key is None:
return False
if not isinstance(value, int):
return False
- key = urllib.quote(key.encode('UTF-8'))
+ path = key
+ if db:
+ path = '/%s/%s' % (db, key)
+ path = urllib.quote(path.encode('UTF-8'))
value = struct.pack('>q', value)
- return self._rest_put(key, value, expire) == 201
+ return self._rest_put(path, value, expire) == 201
def add(self, key, value, expire):
if key is None:
@@ -46,8 +46,8 @@ def size(self, db=None):
def set(self, key, value, expire=None, db=None):
return self.core.set(key, value, expire, db)
- def set_int(self, key, value, expire=None):
- return self.core.set_int(key, value, expire)
+ def set_int(self, key, value, expire=None, db=None):
+ return self.core.set_int(key, value, expire, db)
def add(self, key, value, expire=None):
return self.core.add(key, value, expire)
@@ -67,8 +67,8 @@ def remove(self, key):
def get(self, key, db=None):
return self.core.get(key, db)
- def get_int(self, key):
- return self.core.get_int(key)
+ def get_int(self, key, db=None):
+ return self.core.get_int(key, db)
def vacuum(self):
return self.core.vacuum()
View
@@ -15,14 +15,19 @@
DB_1 = 'one.kch'
DB_2 = 'two.kch'
-DB_INVALID = 'three.kch'
+DB_INVALID = 'invalid.kch'
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
@@ -37,8 +42,7 @@ def test_status(self):
assert status is None
def test_set_get(self):
- self.assertTrue(self.kt_handle.clear())
-
+ 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))
@@ -58,5 +62,27 @@ def test_set_get(self):
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_set_get_int(self):
+ self.assertTrue(self.clear_all())
+ self.assertTrue(self.kt_handle.set_int('key1', 1, db=DB_1))
+ self.assertTrue(self.kt_handle.set_int('key2', 2, db=DB_2))
+ self.assertFalse(self.kt_handle.set_int('key3', 3, db=DB_INVALID))
+
+ self.assertEqual(self.kt_handle.get_int('key1'), 1)
+ self.assertEqual(self.kt_handle.get_int('key1', db=0), 1)
+ self.assertEqual(self.kt_handle.get_int('key1', db=DB_1), 1)
+ assert self.kt_handle.get('key1', db=DB_2) is None
+ assert self.kt_handle.get('key1', db=DB_INVALID) is None
+
+ self.assertEqual(self.kt_handle.get_int('key2', db=DB_2), 2)
+ self.assertEqual(self.kt_handle.get_int('key2', db=1), 2)
+ assert self.kt_handle.get('key2', db=DB_1) is None
+ assert self.kt_handle.get('key2', db=DB_INVALID) is None
+ assert self.kt_handle.get('key1', db=DB_2) is None
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 95a8201

Please sign in to comment.