2014-07-01 06:40:56 +04:00
|
|
|
# coding: utf-8
|
2014-07-01 17:25:59 +04:00
|
|
|
# Test Cases for hash commands
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import sys
|
|
|
|
import datetime, time
|
|
|
|
sys.path.append('..')
|
|
|
|
|
|
|
|
import ledis
|
|
|
|
from ledis._compat import b, iteritems, itervalues
|
|
|
|
from ledis import ResponseError
|
|
|
|
|
|
|
|
|
2014-07-01 17:25:59 +04:00
|
|
|
l = ledis.Ledis(port=6380)
|
|
|
|
|
2014-07-01 06:40:56 +04:00
|
|
|
def current_time():
|
|
|
|
return datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
class TestCmdHash(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-07-01 17:25:59 +04:00
|
|
|
l.hmclear('myhash', 'a')
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_hdel(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
assert l.hdel('myhash', 'field1') == 1
|
|
|
|
assert l.hdel('myhash', 'field1') == 0
|
|
|
|
assert l.hdel('myhash', 'field1', 'field2') == 0
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hexists(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
l.hdel('myhash', 'field2')
|
|
|
|
assert l.hexists('myhash', 'field1') == 1
|
|
|
|
assert l.hexists('myhash', 'field2') == 0
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hget(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
assert l.hget('myhash', 'field1') == 'foo'
|
|
|
|
assert (l.hget('myhash', 'field2')) is None
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hgetall(self):
|
|
|
|
h = {'field1': 'foo', 'field2': 'bar'}
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hmset('myhash', h)
|
|
|
|
assert l.hgetall('myhash') == h
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hincrby(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.hincrby('myhash', 'field1') == 1
|
|
|
|
l.hclear('myhash')
|
|
|
|
assert l.hincrby('myhash', 'field1', 1) == 1
|
|
|
|
assert l.hincrby('myhash', 'field1', 5) == 6
|
|
|
|
assert l.hincrby('myhash', 'field1', -10) == -4
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hkeys(self):
|
|
|
|
h = {'field1': 'foo', 'field2': 'bar'}
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hmset('myhash', h)
|
|
|
|
assert l.hkeys('myhash') == ['field1', 'field2']
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hlen(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
assert l.hlen('myhash') == 1
|
|
|
|
l.hset('myhash', 'field2', 'bar')
|
|
|
|
assert l.hlen('myhash') == 2
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_hmget(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.hmset('myhash', {'a': '1', 'b': '2', 'c': '3'})
|
|
|
|
assert l.hmget('myhash', 'a', 'b', 'c') == ['1', '2', '3']
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_hmset(self):
|
|
|
|
h = {'a': '1', 'b': '2', 'c': '3'}
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.hmset('myhash', h)
|
|
|
|
assert l.hgetall('myhash') == h
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hset(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hclear('myhash')
|
|
|
|
assert int(l.hset('myhash', 'field1', 'foo')) == 1
|
|
|
|
assert l.hset('myhash', 'field1', 'foo') == 0
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hvals(self):
|
|
|
|
h = {'a': '1', 'b': '2', 'c': '3'}
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hmset('myhash', h)
|
2014-07-01 06:40:56 +04:00
|
|
|
local_vals = list(itervalues(h))
|
2014-07-01 17:25:59 +04:00
|
|
|
remote_vals = l.hvals('myhash')
|
2014-07-01 06:40:56 +04:00
|
|
|
assert sorted(local_vals) == sorted(remote_vals)
|
|
|
|
|
|
|
|
|
|
|
|
def test_hclear(self):
|
|
|
|
h = {'a': '1', 'b': '2', 'c': '3'}
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hmset('myhash', h)
|
|
|
|
assert l.hclear('myhash') == 3
|
|
|
|
assert l.hclear('myhash') == 0
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_hmclear(self):
|
|
|
|
h = {'a': '1', 'b': '2', 'c': '3'}
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hmset('myhash1', h)
|
|
|
|
l.hmset('myhash2', h)
|
|
|
|
assert l.hmclear('myhash1', 'myhash2') == 2
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_hexpire(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.hexpire('myhash', 100) == 0
|
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
assert l.hexpire('myhash', 100) == 1
|
|
|
|
assert l.httl('myhash') <= 100
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hexpireat_datetime(self):
|
|
|
|
expire_at = current_time() + datetime.timedelta(minutes=1)
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('a', 'f', 'foo')
|
|
|
|
assert l.hexpireat('a', expire_at)
|
|
|
|
assert 0 < l.httl('a') <= 61
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hexpireat_unixtime(self):
|
|
|
|
expire_at = current_time() + datetime.timedelta(minutes=1)
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('a', 'f', 'foo')
|
2014-07-01 06:40:56 +04:00
|
|
|
expire_at_seconds = int(time.mktime(expire_at.timetuple()))
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.hexpireat('a', expire_at_seconds)
|
|
|
|
assert 0 < l.httl('a') <= 61
|
2014-07-01 06:40:56 +04:00
|
|
|
|
2014-07-14 18:48:33 +04:00
|
|
|
def test_hexpireat_no_key(self):
|
2014-07-01 06:40:56 +04:00
|
|
|
expire_at = current_time() + datetime.timedelta(minutes=1)
|
2014-07-01 17:25:59 +04:00
|
|
|
assert not l.hexpireat('a', expire_at)
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hexpireat(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
assert l.hexpireat('myhash', 1577808000) == 0
|
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
assert l.hexpireat('myhash', 1577808000) == 1
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_httl(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
assert l.hexpire('myhash', 100)
|
|
|
|
assert l.httl('myhash') <= 100
|
2014-07-01 06:40:56 +04:00
|
|
|
|
|
|
|
def test_hpersist(self):
|
2014-07-01 17:25:59 +04:00
|
|
|
l.hset('myhash', 'field1', 'foo')
|
|
|
|
l.hexpire('myhash', 100)
|
|
|
|
assert l.httl('myhash') <= 100
|
|
|
|
assert l.hpersist('myhash')
|
|
|
|
assert l.httl('myhash') == -1
|
2014-07-01 06:40:56 +04:00
|
|
|
|