2014-07-01 06:40:56 +04:00
|
|
|
# coding: utf-8
|
|
|
|
# Test Cases for list commands
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import sys
|
|
|
|
sys.path.append('..')
|
|
|
|
|
|
|
|
import ledis
|
2014-07-01 17:25:59 +04:00
|
|
|
from ledis._compat import b
|
2014-08-16 11:02:29 +04:00
|
|
|
from util import expire_at, expire_at_seconds
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
2014-08-16 11:02:29 +04:00
|
|
|
l = ledis.Ledis(port=6380)
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCmdList(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
pass
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2014-09-04 08:53:19 +04:00
|
|
|
l.flushdb()
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lindex(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1', '2', '3')
|
|
|
|
assert l.lindex('mylist', 0) == b('1')
|
|
|
|
assert l.lindex('mylist', 1) == b('2')
|
|
|
|
assert l.lindex('mylist', 2) == b('3')
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_llen(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1', '2', '3')
|
|
|
|
assert l.llen('mylist') == 3
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lpop(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1', '2', '3')
|
|
|
|
assert l.lpop('mylist') == b('1')
|
|
|
|
assert l.lpop('mylist') == b('2')
|
|
|
|
assert l.lpop('mylist') == b('3')
|
|
|
|
assert l.lpop('mylist') is None
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lpush(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.lpush('mylist', '1') == 1
|
|
|
|
assert l.lpush('mylist', '2') == 2
|
|
|
|
assert l.lpush('mylist', '3', '4', '5') == 5
|
|
|
|
assert l.lrange('mylist', 0, -1) == ['5', '4', '3', '2', '1']
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lrange(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1', '2', '3', '4', '5')
|
|
|
|
assert l.lrange('mylist', 0, 2) == ['1', '2', '3']
|
|
|
|
assert l.lrange('mylist', 2, 10) == ['3', '4', '5']
|
|
|
|
assert l.lrange('mylist', 0, -1) == ['1', '2', '3', '4', '5']
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_rpush(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.rpush('mylist', '1') == 1
|
|
|
|
assert l.rpush('mylist', '2') == 2
|
|
|
|
assert l.rpush('mylist', '3', '4') == 4
|
|
|
|
assert l.lrange('mylist', 0, -1) == ['1', '2', '3', '4']
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_rpop(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1', '2', '3')
|
|
|
|
assert l.rpop('mylist') == b('3')
|
|
|
|
assert l.rpop('mylist') == b('2')
|
|
|
|
assert l.rpop('mylist') == b('1')
|
|
|
|
assert l.rpop('mylist') is None
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lclear(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1', '2', '3')
|
|
|
|
assert l.lclear('mylist') == 3
|
|
|
|
assert l.lclear('mylist') == 0
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lmclear(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist1', '1', '2', '3')
|
|
|
|
l.rpush('mylist2', '1', '2', '3')
|
|
|
|
assert l.lmclear('mylist1', 'mylist2') == 2
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lexpire(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert not l.lexpire('mylist', 100)
|
|
|
|
l.rpush('mylist', '1')
|
|
|
|
assert l.lexpire('mylist', 100)
|
|
|
|
assert 0 < l.lttl('mylist') <= 100
|
|
|
|
assert l.lpersist('mylist')
|
|
|
|
assert l.lttl('mylist') == -1
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lexpireat_datetime(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1')
|
2014-08-16 11:02:29 +04:00
|
|
|
assert l.lexpireat('mylist', expire_at())
|
2014-07-01 17:25:59 +04:00
|
|
|
assert 0 < l.lttl('mylist') <= 61
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lexpireat_unixtime(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1')
|
2014-08-16 11:02:29 +04:00
|
|
|
assert l.lexpireat('mylist', expire_at_seconds())
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.lttl('mylist') <= 61
|
|
|
|
|
2014-07-01 06:40:56 +04:00
|
|
|
def test_lexpireat_no_key(self):
|
2014-08-16 11:02:29 +04:00
|
|
|
assert not l.lexpireat('mylist', expire_at())
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_lttl_and_lpersist(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.rpush('mylist', '1')
|
|
|
|
l.lexpire('mylist', 100)
|
|
|
|
assert 0 < l.lttl('mylist') <= 100
|
|
|
|
assert l.lpersist('mylist')
|
|
|
|
assert l.lttl('mylist') == -1
|
2014-07-01 06:40:56 +04:00
|
|
|
|