def test_lua_evaluation(): r = fresh_redis(port=PROFILE_PORT) before_eval = time.time() for score in range(LARGE_NUMBER): assert r.eval("return 1", 0) == 1 after_eval = time.time() print '\nLua EVAL time = {:.5f}s'.format(after_eval - before_eval)
def test_ttl_command(): r = fresh_redis() r.set('str', 'test') assert r.ttl('str') == -1 assert r.ttl('another-str') == -2
def test_scard(): r = fresh_redis() r.sadd('myset', 'myvalue1') r.sadd('myset', 'myvalue2') assert r.scard('myset') == 2 assert r.scard('notfound') == 0
def test_rename_when_new_key_already_exists(): r = fresh_redis() r.set('mystr1', 'testvalue') r.set('mystr2', 'another testvalue') assert r.rename('mystr1', 'mystr2') assert r.get('mystr2') == 'testvalue'
def test_sismember(): r = fresh_redis() r.sadd('myset', 'myvalue1') r.sadd('myset', 'myvalue2') assert r.sismember('myset', 'myvalue1') is True assert r.sismember('myset', 'myvalue2') is True assert r.sismember('myset', 'myvalue3') is False
def test_dump(): r = fresh_redis() r.set('str', 'test') assert r.dump('str') in [ b'\x00\x04test\x07\x00~\xa2zSd;e_', # RDB 7 (dredis & redis 3.x) b'\x00\x04test\t\x00Qb\xfel8w\xd3\xf4', # RDB 9 (redis 5.x) (`make fulltests-real-redis`) ]
def test_hgetall(): r = fresh_redis() r.hset('myhash', 'key1', 'value1') r.hset('myhash', 'key2', 'value2') r.hset('myhash', 'key3', 'value3') assert r.hgetall('myhash') == {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
def test_zadd_should_not_accept_nx_and_xx_at_the_same_time(): r = fresh_redis() with pytest.raises(redis.ResponseError) as exc: # the current version of redis-py doesn't support r.zadd(..., nx=True, xx=True) r.execute_command('ZADD', 'myzset', 'XX', 'NX', 0, 'test1') assert str(exc.value) == "XX and NX options at the same time are not compatible"
def test_save_creates_an_rdb_file(): r = fresh_redis() root_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) # 2 directory levels up rdb_files_before = set(glob.glob(os.path.join(root_dir, 'dump*.rdb'))) r.set('test', 'value') assert r.save() assert len(set(glob.glob(os.path.join(root_dir, 'dump*.rdb'))) - rdb_files_before) == 1
def test_zcard(): r = fresh_redis(port=PROFILE_PORT) for score in range(LARGE_NUMBER): assert r.zadd('myzset', score, 'value{}'.format(score)) == 1 before_zcard = time.time() assert r.zcard('myzset') == LARGE_NUMBER after_zcard = time.time() print '\nzset ZCARD time = {:.5f}s'.format(after_zcard - before_zcard)
def test_zadd_with_newlines(): r = fresh_redis() r.zadd('myzset', 0, 'my\ntest\nstring') r.zadd('myzset', 0, 'my\nsecond\nstring') assert r.zcard('myzset') == 2 assert r.zrange('myzset', 0, 1) == ['my\nsecond\nstring', 'my\ntest\nstring']
def test_incr_integers(): r = fresh_redis() assert r.incr('a') == 1 assert r.incr('a') == 2 r.set('b', 10) assert r.incr('b') == 11 assert r.incr('b') == 12
def test_delete(): r = fresh_redis() r.set('test1', 'value1') r.set('test2', 'value2') assert r.delete('test1', 'test2', 'notfound') == 2 assert r.get('test1') is None assert r.get('test2') is None
def test_zcount(): r = fresh_redis(port=PROFILE_PORT) for score in range(LARGE_NUMBER): assert r.zadd('myzset', 0, 'value{}'.format(score)) == 1 before_zcount = time.time() assert r.zcount('myzset', '-inf', '+inf') == LARGE_NUMBER after_zcount = time.time() print '\nzset ZCOUNT time = {:.5f}s'.format(after_zcount - before_zcount)
def test_zrangebyscore_with_scores(): r = fresh_redis() r.zadd('myzset', 0, 'myvalue0') r.zadd('myzset', 100, 'myvalue1') r.zadd('myzset', 200, 'myvalue2') r.zadd('myzset', 300, 'myvalue3') assert r.zrangebyscore('myzset', 100, 200, withscores=True) == [('myvalue1', 100), ('myvalue2', 200)]
def test_hincrby(): r = fresh_redis() assert r.hincrby('myhash', 'key1', 0) == 0 assert r.hincrby('myhash', 'key1', 1) == 1 r.hset('myhash', 'key2', 10) assert r.hincrby('myhash', 'key2', 5) == 15 assert r.hget('myhash', 'key2') == '15'
def test_zset_zrange_with_negative_indexes(): r = fresh_redis() r.zadd('myzset', 0, 'myvalue1') r.zadd('myzset', 1, 'myvalue2') assert r.zrange('myzset', 0, -1) == ['myvalue1', 'myvalue2'] assert r.zrange('myzset', 0, -2) == ['myvalue1'] assert r.zrange('myzset', 0, -3) == [] assert r.zrange('myzset', -2, 1) == ['myvalue1', 'myvalue2']
def test_hset_should_accept_multiple_key_value_pairs(): r = fresh_redis() assert r.execute_command('HSET', 'myhash', 'k1', 'v1', 'k2', 'v2') == 2 assert r.hgetall('myhash') == {'k1': 'v1', 'k2': 'v2'} with pytest.raises(redis.ResponseError) as exc: r.execute_command('HSET', 'myhash', 'k1', 'v1', 'k2') assert str(exc.value) == 'wrong number of arguments for HMSET'
def test_zrange(): r = fresh_redis(port=PROFILE_PORT) for score in range(LARGE_NUMBER): assert r.zadd('myzset', 0, 'value{}'.format(score)) == 1 before_zrange = time.time() assert len(r.zrange('myzset', 0, LARGE_NUMBER)) == LARGE_NUMBER after_zrange = time.time() print '\nzset ZRANGE time = {:.5f}s'.format(after_zrange - before_zrange)
def test_zrangebyscore_with_limit(): r = fresh_redis() r.zadd('myzset', 0, 'myvalue0') r.zadd('myzset', 100, 'myvalue1') r.zadd('myzset', 200, 'myvalue2') r.zadd('myzset', 300, 'myvalue3') assert r.zrangebyscore('myzset', 0, 400, start=2, num=2, withscores=True) == [('myvalue2', 200), ('myvalue3', 300)]
def test_getrange(): r = fresh_redis() r.set('test', 'value') assert r.getrange('test', 0, -1) == 'value' assert r.getrange('test', 0, 10) == 'value' assert r.getrange('test', 0, 2) == 'val' assert r.getrange('test', 0, 2) == 'val' assert r.getrange('notfound', 0, -1) == ''
def test_hdel(): r = fresh_redis() r.hset('myhash', 'key1', 'value1') r.hset('myhash', 'key2', 'value2') assert r.hdel('myhash', 'key1', 'key2') == 2 assert r.hget('myhash', 'key1') is None assert r.hget('myhash', 'key2') is None assert r.hdel('myhash', 'notfound') == 0
def test_hvals(): r = fresh_redis() r.hset('myhash', 'key1', 'value1') r.hset('myhash', 'key2', 'value2') # order isn't guaranteed result = r.hvals('myhash') assert len(result) == 2 assert sorted(result) == sorted(['value1', 'value2'])
def test_hset_and_hget(): r = fresh_redis() assert r.hset('myhash', 'key1', 'value1') == 1 assert r.hset('myhash', 'key1', 'value1') == 0 assert r.hset('myhash', 'key2', 'value2') == 1 assert r.hget('myhash', 'key1') == 'value1' assert r.hget('myhash', 'key2') == 'value2' assert r.hget('myhash', 'notfound') is None
def test_zset_with_rescoring(): r = fresh_redis() assert r.zadd('myzset', 0, 'myvalue1') == 1 assert r.zadd('myzset', 1, 'myvalue2') == 1 assert r.zadd( 'myzset', 0, 'myvalue2') == 0 # now the score 0 has "myvalue1" & "myvalue2" assert r.zcard('myzset') == 2 assert r.zrange('myzset', 0, -1, withscores=True) == [('myvalue1', 0), ('myvalue2', 0)]
def test_deleting_a_zset_should_not_impact_other_zsets(): # this is a regression test r = fresh_redis() r.zadd('myzset1', 0, 'test1') r.zadd('myzset2', 0, 'test2') r.delete('myzset1') assert r.keys('*') == ['myzset2'] assert r.zrange('myzset2', 0, 10) == ['test2']
def test_zrangebyscore_should_validate_withscores_and_limit_extra_arguments(): r = fresh_redis() with pytest.raises(redis.ResponseError) as exc1: r.execute_command('ZRANGEBYSCORE', 'mykey', 0, 1, 'bleh', 'WITHSCORES', 'LIMIT', 0) # missing count assert str(exc1.value) == "syntax error" with pytest.raises(redis.ResponseError) as exc2: r.execute_command('ZRANGEBYSCORE', 'mykey', 0, 1, 'bleh', 'extraword') # unknown parameter assert str(exc2.value) == "syntax error"
def test_zset_zrange_with_positive_indexes(): r = fresh_redis() r.zadd('z1', 0, 'z1-v1') r.zadd('z1', 1, 'z1-v2') r.zadd('z2', 0, 'z2-v1') r.zadd('z2', 1, 'z2-v2') assert r.zrange('z1', 0, 1) == ['z1-v1', 'z1-v2'] assert r.zrange('z1', 0, 100) == ['z1-v1', 'z1-v2'] assert r.zrange('z2', 0, 1) == ['z2-v1', 'z2-v2'] assert r.zrange('z2', 0, 100) == ['z2-v1', 'z2-v2']
def test_zrangebyscore_should_validate_limit_values_as_integers(): r = fresh_redis() with pytest.raises(redis.ResponseError) as exc1: r.execute_command('ZRANGEBYSCORE', 'mykey', 0, 1, 'bleh', 'LIMIT', 0, 's') assert str(exc1.value) == "syntax error" with pytest.raises(redis.ResponseError) as exc2: r.execute_command('ZRANGEBYSCORE', 'mykey', 0, 1, 'bleh', 'LIMIT', 's', 1) assert str(exc2.value) == "syntax error"
def test_delete(): r = fresh_redis() r.set('mystr', 'test') r.sadd('myset', 'elem1') r.zadd('myzset', 0, 'elem1') r.hset('myhash', 'testkey', 'testvalue') assert r.delete('mystr', 'myset', 'myzset', 'myhash', 'notfound') == 4 assert r.keys('*') == []