Skip to content
Browse files

Allow selecting DB for set/get ops. Added tests.

  • Loading branch information...
1 parent f415a4b commit 3cc9ae4f4bb8762eca6092013c4690f33f91b24d Toru Maesaka committed May 15, 2011
Showing with 42 additions and 13 deletions.
  1. +13 −6 kyototycoon/kt_http.py
  2. +4 −4 kyototycoon/kyototycoon.py
  3. +25 −3 tests/t_multi.py
View
19 kyototycoon/kt_http.py
@@ -58,12 +58,16 @@ def echo(self):
body = res.read()
return True if res.status == 200 else False
- def get(self, key):
+ def get(self, key, db=None):
if key is None:
return False
- key = urllib.quote(key.encode('UTF-8'))
- self.conn.request('GET', key)
+ 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()
body = rv.read()
@@ -90,13 +94,16 @@ def vacuum(self):
body = res.read()
return res.status == 200
- def set(self, key, value, expire):
+ def set(self, key, value, expire, db=None):
if key is None:
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 = self.pack(value)
- return self._rest_put(key, value, expire) == 201
+ return self._rest_put(path, value, expire) == 201
def set_int(self, key, value, expire):
if key is None:
View
8 kyototycoon/kyototycoon.py
@@ -43,8 +43,8 @@ def count(self, db=None):
def size(self, db=None):
return self.core.size(db)
- def set(self, key, value, expire=None):
- return self.core.set(key, value, expire)
+ 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)
@@ -64,8 +64,8 @@ def increment(self, key, delta, expire=None):
def remove(self, key):
return self.core.remove(key)
- def get(self, key):
- return self.core.get(key)
+ def get(self, key, db=None):
+ return self.core.get(key, db)
def get_int(self, key):
return self.core.get_int(key)
View
28 tests/t_multi.py
@@ -15,7 +15,7 @@
DB_1 = 'one.kch'
DB_2 = 'two.kch'
-DB_3 = 'three.kch'
+DB_INVALID = 'three.kch'
class UnitTest(unittest.TestCase):
def setUp(self):
@@ -30,11 +30,33 @@ def test_status(self):
status = self.kt_handle.status(DB_2)
assert status is not None
- status = self.kt_handle.status(DB_3)
- 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.kt_handle.clear())
+
+ 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
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 3cc9ae4

Please sign in to comment.
Something went wrong with that request. Please try again.