update test code

This commit is contained in:
holys 2014-07-08 10:48:08 +08:00
parent e8cb0ba612
commit 3c8fd575d5
1 changed files with 26 additions and 88 deletions

View File

@ -1,5 +1,16 @@
--[[
Stupid test codes for ledis.lua. Running based on openresty configuration file.
Remember setting up your `lua_package_path`.
location = /test {
default_type "application/json";
content_by_lua_file /path/to/ledis_test.lua;
}
Then use `curl` or other http clients to see the output.
--]]
local ledis = require "ledis" local ledis = require "ledis"
local cjson = require "cjson"
local lds = ledis:new() local lds = ledis:new()
lds:set_timeout(1000) lds:set_timeout(1000)
@ -11,7 +22,6 @@ if not ok then
return return
end end
function cleanUp() function cleanUp()
lds:del("mykey", "key1", "key2", "key3", "non_exists_key") lds:del("mykey", "key1", "key2", "key3", "non_exists_key")
lds:hmclear("myhash", "myhash1", "myhash2") lds:hmclear("myhash", "myhash1", "myhash2")
@ -22,7 +32,6 @@ end
cleanUp() cleanUp()
ngx.say("======================= K/V =====================\n") ngx.say("======================= K/V =====================\n")
--[[KV]]-- --[[KV]]--
@ -39,7 +48,6 @@ ngx.say("DECR, should be: -1 <=> ", res)
lds:del("mykey") lds:del("mykey")
-- decrby -- decrby
local res, err = lds:decrby("mykey", 10) local res, err = lds:decrby("mykey", 10)
if not res then if not res then
ngx.say("failed to decrby:", err) ngx.say("failed to decrby:", err)
@ -50,7 +58,6 @@ ngx.say("DECRBY, should be: -10 <=> ", res)
lds:del("mykey") lds:del("mykey")
-- del -- del
lds:set("key1", "foo") lds:set("key1", "foo")
lds:set("key2", "bar") lds:set("key2", "bar")
local res, err = lds:del("key1", "key2") local res, err = lds:del("key1", "key2")
@ -62,7 +69,6 @@ end
ngx.say("DEL, should be: 2 <=> 2") ngx.say("DEL, should be: 2 <=> 2")
--exists --exists
lds:set("mykey", "foo") lds:set("mykey", "foo")
res, err = lds:exists("mykey") res, err = lds:exists("mykey")
if not res then if not res then
@ -81,9 +87,7 @@ end
ngx.say("EXISTS, should be 0 <=>", res) ngx.say("EXISTS, should be 0 <=>", res)
lds:del("non_exists_key") lds:del("non_exists_key")
-- get -- get
lds:set("mykey", "foo") lds:set("mykey", "foo")
res, err = lds:get("mykey") res, err = lds:get("mykey")
if not res then if not res then
@ -94,9 +98,7 @@ end
ngx.say("GET, should be foo <=> ", res) ngx.say("GET, should be foo <=> ", res)
lds:del("mykey") lds:del("mykey")
-- getset -- getset
lds:set("mykey", "foo") lds:set("mykey", "foo")
res, err = lds:getset("mykey", "bar") res, err = lds:getset("mykey", "bar")
if not res then if not res then
@ -110,7 +112,6 @@ ngx.say("GET, should be bar <=>", res)
lds:del("mykey") lds:del("mykey")
-- incr -- incr
lds:set("mykey", "10") lds:set("mykey", "10")
res, err = lds:incr("mykey") res, err = lds:incr("mykey")
if not res then if not res then
@ -122,7 +123,6 @@ ngx.say("INCR should be 11 <=>", res)
lds:del("mykey") lds:del("mykey")
-- incrby -- incrby
lds:set("mykey", "10") lds:set("mykey", "10")
res, err = lds:incrby("mykey", 10) res, err = lds:incrby("mykey", 10)
if not res then if not res then
@ -146,7 +146,6 @@ ngx.say("MGET should be foobar <=>", res)
lds:del("key1", "key2") lds:del("key1", "key2")
-- mset -- mset
res, err = lds:mset("key1", "foo", "key2", "bar") res, err = lds:mset("key1", "foo", "key2", "bar")
if not res then if not res then
ngx.say("failed to command ", err) ngx.say("failed to command ", err)
@ -181,7 +180,6 @@ ngx.say("setnx should be 0 <=>", res)
lds:del("mykey") lds:del("mykey")
-- expire -- expire
lds:set("mykey", "foo") lds:set("mykey", "foo")
res, err = lds:expire("mykey", 60) res, err = lds:expire("mykey", 60)
if not res then if not res then
@ -192,7 +190,6 @@ end
ngx.say("EXPIRE should be 1 <=> ", res) ngx.say("EXPIRE should be 1 <=> ", res)
lds:del("mykey") lds:del("mykey")
-- expireat -- expireat
lds:set("mykey", "foo") lds:set("mykey", "foo")
res, err = lds:expire("mykey", 14366666666) res, err = lds:expire("mykey", 14366666666)
@ -205,7 +202,6 @@ ngx.say("EXPIREAT 1 <=>", res)
lds:del("mykey") lds:del("mykey")
-- ttl -- ttl
lds:set("mykey", "foo") lds:set("mykey", "foo")
lds:expire("mykey", 100) lds:expire("mykey", 100)
res, err = lds:ttl("mykey") res, err = lds:ttl("mykey")
@ -222,7 +218,6 @@ ngx.say("TTL ", res)
lds:del("mykey") lds:del("mykey")
-- persist -- persist
lds:set("mykey", "foo") lds:set("mykey", "foo")
lds:expire("mykey", 100) lds:expire("mykey", 100)
res, err = lds:persist("mykey") res, err = lds:persist("mykey")
@ -235,12 +230,11 @@ end
ngx.say("PERSIST should be 1 <=>", res) ngx.say("PERSIST should be 1 <=>", res)
lds:del("mykey") lds:del("mykey")
-- [[ HASH ]]
ngx.say("\n=================== HASH =====================\n") ngx.say("\n=================== HASH =====================\n")
-- hdel -- [[ HASH ]]
-- hdel
res, err = lds:hset("myhash", "field", "foo") res, err = lds:hset("myhash", "field", "foo")
if not res then if not res then
ngx.say("failed to HDEL", err) ngx.say("failed to HDEL", err)
@ -294,7 +288,6 @@ ngx.say("HINCRBY should be 1 <=>", res)
lds:hclear("myhash") lds:hclear("myhash")
-- hkeys -- hkeys
lds:hmset("myhash", "field1", "foo", "field2", "bar") lds:hmset("myhash", "field1", "foo", "field2", "bar")
res, err = lds:hkeys("myhash") res, err = lds:hkeys("myhash")
if not res then if not res then
@ -305,9 +298,7 @@ end
ngx.say("HKEYS should be field1field2 <=> ", res) ngx.say("HKEYS should be field1field2 <=> ", res)
lds:hclear("myhash") lds:hclear("myhash")
-- hlen -- hlen
lds:hset("myhash", "field", "foo") lds:hset("myhash", "field", "foo")
res, err = lds:hlen("myhash") res, err = lds:hlen("myhash")
if not res then if not res then
@ -318,9 +309,7 @@ end
ngx.say("HLEN should be 1 <=>", res) ngx.say("HLEN should be 1 <=>", res)
lds:hclear("myhash") lds:hclear("myhash")
-- hmget -- hmget
lds:hmset("myhash", "field1", "foo", "field2", "bar") lds:hmset("myhash", "field1", "foo", "field2", "bar")
res, err = lds:hmget("myhash", "field1", "field2") res, err = lds:hmget("myhash", "field1", "field2")
if not res then if not res then
@ -331,10 +320,7 @@ end
ngx.say("HMGET should be foobar <=>", res) ngx.say("HMGET should be foobar <=>", res)
lds:hclear("myhash") lds:hclear("myhash")
-- hmset -- hmset
res, err = lds:hmset("myhash", "field1", "foo", "field2", "bar") res, err = lds:hmset("myhash", "field1", "foo", "field2", "bar")
if not res then if not res then
ngx.say("failed to HMSET ", err) ngx.say("failed to HMSET ", err)
@ -350,8 +336,7 @@ end
res, err = lds:hclear("myhash") res, err = lds:hclear("myhash")
-- hset
--hset
res, err = lds:hset("myhash", "field", "foo") res, err = lds:hset("myhash", "field", "foo")
if not res then if not res then
ngx.say("failed to HSET", err) ngx.say("failed to HSET", err)
@ -361,7 +346,7 @@ end
ngx.say("HSET should be 1 <=> ", res) ngx.say("HSET should be 1 <=> ", res)
lds:hclear("myhash") lds:hclear("myhash")
--hvals -- hvals
lds:hset("myhash", "field", "foo") lds:hset("myhash", "field", "foo")
res, err = lds:hvals("myhash") res, err = lds:hvals("myhash")
if not res then if not res then
@ -370,26 +355,19 @@ if not res then
end end
ngx.say("HVALS should be foo <=>", res) ngx.say("HVALS should be foo <=>", res)
lds:hvals("myhash") lds:hclear("myhash")
-- hclear -- hclear
lds:hmset("myhash", "field1", "foo", "field2", "bar")
--FIXME: why 3?
lds:hset("myhash", "field", "foo")
res, err = lds:hclear("myhash") res, err = lds:hclear("myhash")
if not res then if not res then
ngx.say("failed to HCLEAR", err) ngx.say("failed to HCLEAR", err)
return return
end end
ngx.say("HCLEAR should be 1 <=>", res) ngx.say("HCLEAR should be 2 <=>", res)
lds:hclear("myhash")
-- hmclear -- hmclear
lds:hset("myhash1", "field1", "foo") lds:hset("myhash1", "field1", "foo")
lds:hset("myhash2", "field2", "bar") lds:hset("myhash2", "field2", "bar")
res, err = lds:hmclear("myhash1", "myhash2") res, err = lds:hmclear("myhash1", "myhash2")
@ -400,7 +378,6 @@ end
ngx.say("HMCLEAR should be 2 <=>", res) ngx.say("HMCLEAR should be 2 <=>", res)
-- hexpire -- hexpire
lds:hset("myhash", "field", "foo") lds:hset("myhash", "field", "foo")
res, err = lds:hexpire("myhash", 100) res, err = lds:hexpire("myhash", 100)
@ -412,9 +389,7 @@ end
ngx.say("HEXPIRE should be 1 <=>", res) ngx.say("HEXPIRE should be 1 <=>", res)
lds:hclear("myhash") lds:hclear("myhash")
-- hexpireat -- hexpireat
lds:hset("myhash", "field", "foo") lds:hset("myhash", "field", "foo")
res, err = lds:hexpireat("myhash", 14366666666) res, err = lds:hexpireat("myhash", 14366666666)
if not res then if not res then
@ -425,7 +400,6 @@ end
ngx.say("HEXPIREAT should be 1 <=>", res) ngx.say("HEXPIREAT should be 1 <=>", res)
lds:hclear("myhash") lds:hclear("myhash")
-- hpersist -- hpersist
lds:hset("myhash", "field", "foo") lds:hset("myhash", "field", "foo")
lds:hexpire("myhash", 100) lds:hexpire("myhash", 100)
@ -505,10 +479,7 @@ end
ngx.say("LRANGE should be one <=>", res) ngx.say("LRANGE should be one <=>", res)
lds:lclear("mylist") lds:lclear("mylist")
-- lpush -- lpush
res, err = lds:lpush("mylist", "one", "two") res, err = lds:lpush("mylist", "one", "two")
if not res then if not res then
ngx.say("failed to LPUSH ", err) ngx.say("failed to LPUSH ", err)
@ -520,7 +491,6 @@ lds:lclear("mylist")
-- rpop -- rpop
lds:rpush("mylist", "one", "two") lds:rpush("mylist", "one", "two")
res, err = lds:rpop("mylist") res, err = lds:rpop("mylist")
if not res then if not res then
@ -563,9 +533,7 @@ end
ngx.say("LEXPIRE should be 1 <=>", res) ngx.say("LEXPIRE should be 1 <=>", res)
lds:lclear("mylist") lds:lclear("mylist")
-- lexpireat -- lexpireat
lds:rpush("mylist", "one") lds:rpush("mylist", "one")
res, err = lds:lexpireat("mylist", 14366666666) res, err = lds:lexpireat("mylist", 14366666666)
if not res then if not res then
@ -576,7 +544,6 @@ end
ngx.say("LEXPIREAT should be 1 <=>", res) ngx.say("LEXPIREAT should be 1 <=>", res)
lds:lclear("mylist") lds:lclear("mylist")
-- lpersist -- lpersist
lds:rpush("mylist", "one", "two") lds:rpush("mylist", "one", "two")
lds:lexpire("mylist", 100) lds:lexpire("mylist", 100)
@ -590,7 +557,6 @@ end
ngx.say("LPERSIST should be 1 <=>", res) ngx.say("LPERSIST should be 1 <=>", res)
lds:hclear("mylist") lds:hclear("mylist")
--lttl --lttl
lds:rpush("mylist", "field", "foo") lds:rpush("mylist", "field", "foo")
lds:lexpire("mylist", 100) lds:lexpire("mylist", 100)
@ -606,6 +572,7 @@ lds:lclear("mylist")
ngx.say("\n==================== ZSET =====================\n") ngx.say("\n==================== ZSET =====================\n")
-- [[ ZSET ]] -- [[ ZSET ]]
-- zadd -- zadd
@ -630,7 +597,6 @@ end
ngx.say("ZCARD should be 2 <=>", res) ngx.say("ZCARD should be 2 <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zcount -- zcount
lds:zadd("myset", 1, "one", 2, "two") lds:zadd("myset", 1, "one", 2, "two")
res, err = lds:zcount("myset", "-inf", "+inf") res, err = lds:zcount("myset", "-inf", "+inf")
@ -642,7 +608,7 @@ end
ngx.say("ZCOUNT should be 2 <=>", res) ngx.say("ZCOUNT should be 2 <=>", res)
lds:zclear("myset") lds:zclear("myset")
--zincrby -- zincrby
lds:zadd("myset", 1, "one") lds:zadd("myset", 1, "one")
res, err = lds:zincrby("myset", 2, "one") res, err = lds:zincrby("myset", 2, "one")
if not res then if not res then
@ -653,8 +619,7 @@ end
ngx.say("ZINCRBY should be 3 <=>", res) ngx.say("ZINCRBY should be 3 <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zrange
--zrange
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zrange("myset", 0, -1, "WITHSCORES") res, err = lds:zrange("myset", 0, -1, "WITHSCORES")
if not res then if not res then
@ -665,8 +630,7 @@ end
ngx.say("ZRANGE should be one1two2three3<=>", res) ngx.say("ZRANGE should be one1two2three3<=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zrangebyscore
--zrangebyscore
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zrangebyscore("myset", 1, 2) res, err = lds:zrangebyscore("myset", 1, 2)
if not res then if not res then
@ -677,7 +641,6 @@ end
ngx.say("ZRANGEBYSCORE should be onetwo <=>", res) ngx.say("ZRANGEBYSCORE should be onetwo <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zrank -- zrank
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zrank("myset", "three") res, err = lds:zrank("myset", "three")
@ -689,7 +652,7 @@ end
ngx.say("ZRANK should be 2 <=>", res) ngx.say("ZRANK should be 2 <=>", res)
lds:zclear("myset") lds:zclear("myset")
--zrem -- zrem
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zrem("myset", "two", "three") res, err = lds:zrem("myset", "two", "three")
if not res then if not res then
@ -700,8 +663,7 @@ end
ngx.say("ZREM should be 2 <=>", res) ngx.say("ZREM should be 2 <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zremrangebyrank
--zremrangebyrank
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err= lds:zremrangebyrank("myset", 0, 2) res, err= lds:zremrangebyrank("myset", 0, 2)
if not res then if not res then
@ -713,7 +675,7 @@ ngx.say("ZREMRANGEBYRANK should be 3 <=>", res)
lds:zclear("myset") lds:zclear("myset")
--zremrangebyscore -- zremrangebyscore
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zremrangebyscore("myset", 0, 2) res, err = lds:zremrangebyscore("myset", 0, 2)
if not res then if not res then
@ -736,9 +698,6 @@ end
ngx.say("ZREVRANGE should be threetwoone <=>", res) ngx.say("ZREVRANGE should be threetwoone <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zrevrangebyscore -- zrevrangebyscore
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zrevrangebyscore("myset", "+inf", "-inf") res, err = lds:zrevrangebyscore("myset", "+inf", "-inf")
@ -750,8 +709,6 @@ end
ngx.say("ZREVRANGEBYSCORE should be threetwoone <=>", res) ngx.say("ZREVRANGEBYSCORE should be threetwoone <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zscore -- zscore
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zscore("myset", "two") res, err = lds:zscore("myset", "two")
@ -763,7 +720,6 @@ end
ngx.say("ZSCORE should be 2 <=>", res) ngx.say("ZSCORE should be 2 <=>", res)
lds.zclear("myset") lds.zclear("myset")
-- zclear -- zclear
lds:zadd("myset", 1, "one", 2, "two", 3, "three") lds:zadd("myset", 1, "one", 2, "two", 3, "three")
res, err = lds:zclear("myset") res, err = lds:zclear("myset")
@ -774,7 +730,6 @@ end
ngx.say("ZCLEAR should be 3 <=>", res) ngx.say("ZCLEAR should be 3 <=>", res)
-- zmclear -- zmclear
lds:zadd("myset1", 1, "one", 2, "two", 3, "three") lds:zadd("myset1", 1, "one", 2, "two", 3, "three")
lds:zadd("myset2", 1, "one", 2, "two", 3, "three") lds:zadd("myset2", 1, "one", 2, "two", 3, "three")
@ -786,17 +741,7 @@ end
ngx.say("ZMCLEAR should be 2 <=>", res) ngx.say("ZMCLEAR should be 2 <=>", res)
--zexpire
--zexpireat
--zpersist
--zttl
-- zexpire -- zexpire
lds:zadd("myset", 1, "one") lds:zadd("myset", 1, "one")
res, err = lds:zexpire("myset", 60) res, err = lds:zexpire("myset", 60)
if not res then if not res then
@ -807,7 +752,6 @@ end
ngx.say("ZEXPIRE should be 1 <=> ", res) ngx.say("ZEXPIRE should be 1 <=> ", res)
lds:zclear("myset") lds:zclear("myset")
-- zexpireat -- zexpireat
lds:zadd("myset", 1, "one") lds:zadd("myset", 1, "one")
res, err = lds:zexpire("myset", 14366666666) res, err = lds:zexpire("myset", 14366666666)
@ -820,7 +764,6 @@ ngx.say("ZEXPIREAT 1 <=>", res)
lds:zclear("myset") lds:zclear("myset")
-- zttl -- zttl
lds:zadd("myset", 1, "one") lds:zadd("myset", 1, "one")
lds:zexpire("myset", 100) lds:zexpire("myset", 100)
res, err = lds:zttl("myset") res, err = lds:zttl("myset")
@ -837,7 +780,6 @@ ngx.say("ZTTL ", res)
lds:zclear("myset") lds:zclear("myset")
-- zpersist -- zpersist
lds:zadd("myset", 1, "one") lds:zadd("myset", 1, "one")
lds:zexpire("myset", 100) lds:zexpire("myset", 100)
res, err = lds:zpersist("myset") res, err = lds:zpersist("myset")
@ -873,9 +815,7 @@ end
ngx.say("ECHO should be hello, lua <=>", res) ngx.say("ECHO should be hello, lua <=>", res)
-- select -- select
res, err = lds:select(5) res, err = lds:select(5)
if not res then if not res then
ngx.say("failed to SELECT ", err) ngx.say("failed to SELECT ", err)
@ -883,5 +823,3 @@ if not res then
end end
ngx.say("SELECT should be OK <=>", res) ngx.say("SELECT should be OK <=>", res)