2014-07-01 17:26:31 +04:00
|
|
|
# coding: utf-8
|
|
|
|
# Test Cases for other commands
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import sys
|
|
|
|
sys.path.append('..')
|
|
|
|
|
|
|
|
import ledis
|
|
|
|
from ledis._compat import b
|
|
|
|
from ledis import ResponseError
|
|
|
|
|
|
|
|
l = ledis.Ledis(port=6380)
|
2014-09-04 08:53:19 +04:00
|
|
|
dbs = ["leveldb", "rocksdb", "goleveldb", "hyperleveldb", "lmdb", "boltdb"]
|
2014-07-01 17:26:31 +04:00
|
|
|
|
|
|
|
class TestOtherCommands(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.flushdb()
|
2014-07-01 17:26:31 +04:00
|
|
|
|
|
|
|
# server information
|
|
|
|
def test_echo(self):
|
|
|
|
assert l.echo('foo bar') == b('foo bar')
|
|
|
|
|
|
|
|
def test_ping(self):
|
|
|
|
assert l.ping()
|
|
|
|
|
|
|
|
def test_select(self):
|
|
|
|
assert l.select('1')
|
|
|
|
assert l.select('15')
|
2014-09-04 08:53:19 +04:00
|
|
|
self.assertRaises(ResponseError, lambda: l.select('16'))
|
|
|
|
|
|
|
|
|
|
|
|
def test_info(self):
|
|
|
|
info1 = l.info()
|
|
|
|
assert info1.get("db_name") in dbs
|
|
|
|
info2 = l.info(section="server")
|
|
|
|
assert info2.get("os") in ["linux", "darwin"]
|
|
|
|
|
|
|
|
def test_flushdb(self):
|
|
|
|
l.set("a", 1)
|
|
|
|
assert l.flushdb() == "OK"
|
|
|
|
assert l.get("a") is None
|
|
|
|
|
|
|
|
def test_flushall(self):
|
|
|
|
l.select(1)
|
|
|
|
l.set("a", 1)
|
|
|
|
assert l.get("a") == b("1")
|
|
|
|
|
|
|
|
l.select(10)
|
|
|
|
l.set("a", 1)
|
|
|
|
assert l.get("a") == b("1")
|
|
|
|
|
|
|
|
assert l.flushall() == "OK"
|
|
|
|
|
|
|
|
assert l.get("a") is None
|
|
|
|
l.select(1)
|
|
|
|
assert l.get("a") is None
|
|
|
|
|
|
|
|
|
|
|
|
# test *scan commands
|
|
|
|
|
|
|
|
def check_keys(self, scan_type):
|
|
|
|
d = {
|
2014-09-09 05:53:26 +04:00
|
|
|
"xscan": l.xscan,
|
|
|
|
"sxscan": l.sxscan,
|
|
|
|
"lxscan": l.lxscan,
|
|
|
|
"hxscan": l.hxscan,
|
|
|
|
"zxscan": l.zxscan,
|
|
|
|
"bxscan": l.bxscan
|
2014-09-04 08:53:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
key, keys = d[scan_type]()
|
|
|
|
assert key == ""
|
|
|
|
assert set(keys) == set([b("a"), b("b"), b("c")])
|
|
|
|
|
|
|
|
_, keys = d[scan_type](match="a")
|
|
|
|
assert set(keys) == set([b("a")])
|
|
|
|
|
|
|
|
_, keys = d[scan_type](key="a")
|
|
|
|
assert set(keys) == set([b("b"), b("c")])
|
|
|
|
|
|
|
|
|
2014-09-09 05:53:26 +04:00
|
|
|
def test_xscan(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
d = {"a":1, "b":2, "c": 3}
|
|
|
|
l.mset(d)
|
2014-09-09 05:53:26 +04:00
|
|
|
self.check_keys("xscan")
|
2014-09-04 08:53:19 +04:00
|
|
|
|
|
|
|
|
2014-09-09 05:53:26 +04:00
|
|
|
def test_lxscan(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.rpush("a", 1)
|
|
|
|
l.rpush("b", 1)
|
|
|
|
l.rpush("c", 1)
|
2014-09-09 05:53:26 +04:00
|
|
|
self.check_keys("lxscan")
|
2014-09-04 08:53:19 +04:00
|
|
|
|
|
|
|
|
2014-09-09 05:53:26 +04:00
|
|
|
def test_hxscan(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.hset("a", "hello", "world")
|
|
|
|
l.hset("b", "hello", "world")
|
|
|
|
l.hset("c", "hello", "world")
|
2014-09-09 05:53:26 +04:00
|
|
|
self.check_keys("hxscan")
|
2014-09-04 08:53:19 +04:00
|
|
|
|
2014-09-09 05:53:26 +04:00
|
|
|
def test_sxscan(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.sadd("a", 1)
|
|
|
|
l.sadd("b", 2)
|
|
|
|
l.sadd("c", 3)
|
2014-09-09 05:53:26 +04:00
|
|
|
self.check_keys("sxscan")
|
2014-09-04 08:53:19 +04:00
|
|
|
|
2014-09-09 05:53:26 +04:00
|
|
|
def test_zxscan(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.zadd("a", 1, "a")
|
|
|
|
l.zadd("b", 1, "a")
|
|
|
|
l.zadd("c", 1, "a")
|
2014-09-09 05:53:26 +04:00
|
|
|
self.check_keys("zxscan")
|
2014-09-04 08:53:19 +04:00
|
|
|
|
2014-09-09 05:53:26 +04:00
|
|
|
def test_bxscan(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.bsetbit("a", 1, 1)
|
|
|
|
l.bsetbit("b", 1, 1)
|
|
|
|
l.bsetbit("c", 1, 1)
|
2014-09-09 05:53:26 +04:00
|
|
|
self.check_keys("bxscan")
|
2014-09-04 08:53:19 +04:00
|
|
|
|