Skip to content

Instantly share code, notes, and snippets.

@rhzs
Forked from suprememoocow/busted-output.txt
Created July 26, 2023 20:54
Show Gist options
  • Save rhzs/1eee5ccd85f412bc13654956f83cb6f6 to your computer and use it in GitHub Desktop.
Save rhzs/1eee5ccd85f412bc13654956f83cb6f6 to your computer and use it in GitHub Desktop.
Testing Redis Lua Scripts
●●
2 successes / 0 failures / 0 errors / 0 pending : 0.008108 seconds
local link_id = redis.call("INCR", KEYS[1])
redis.call("HSET", KEYS[2], link_id, ARGV[1])
return link_id
redis = require 'redis'
KEYS = {}
ARGV = {}
-- If you have some different host/port change it here
local host = "127.0.0.1"
local port = 6379
client = redis.connect(host, port)
-- Workaround for absence of redis.call
redis.call = function(cmd, ...)
cmd = string.lower(cmd)
local result = assert(loadstring('return client:'.. cmd ..'(...)'))(...)
-- The redis-lua library returns some values differently to how `redis.call` works inside redis.
-- this makes the responses look like those from the builtin redis
local response_lookup = {
type = function() return { ["ok"]= result } end,
sadd = function() return tonumber(result) end,
zrange = function()
if type(result) == "table" and type(result[1]) == "table" then
-- Deal with WITHSCORES...
local new_result = {}
for k,v in pairs(result) do
table.insert(new_result, v[1])
table.insert(new_result, v[2])
end
return new_result;
end
return result;
end
}
if response_lookup[cmd] then
return response_lookup[cmd]()
end
return result;
end
function call_redis_script(script, keys, argv)
-- This may not be strictly necessary
for k,v in pairs(ARGV) do ARGV[k] = nil end
for k,v in pairs(KEYS) do KEYS[k] = nil end
for k,v in pairs(keys) do table.insert(KEYS, v) end
for k,v in pairs(argv) do table.insert(ARGV, v) end
return assert(dofile(script))
end
return call_redis_script;
==============================================================================
incr-and-stor.lua
==============================================================================
3 local link_id = redis.call("INCR", KEYS[1])
3 redis.call("HSET", KEYS[2], link_id, ARGV[1])
3 return link_id
==============================================================================
Summary
==============================================================================
3 0 100.00% incr-and-stor.lua
------------------------
3 0 100.00%
local call_redis_script = require "./harness";
describe("incr-and-stor", function()
-- Flush the database before running the tests
before_each(function()
redis.call('FLUSHDB')
end)
it("should add single items", function()
-- Call the incr-and-stor.lua script with the given keys and values
local result = call_redis_script('incr-and-stor.lua', { "counter", "hash" }, { "link1" });
assert.are.equals(1, result)
local hash = redis.call("HGETALL", "hash")
assert.are.same({ ["1"] = "link1" }, hash)
end)
it("should add multiple items", function()
-- Call the incr-and-stor.lua script with the given keys and values
local result = call_redis_script('incr-and-stor.lua', { "counter", "hash" }, { "link1" });
assert.are.equals(1, result)
result = call_redis_script('incr-and-stor.lua', { "counter", "hash" }, { "link2" });
assert.are.equals(2, result)
local hash = redis.call("HGETALL", "hash")
assert.are.same({ ["1"] = "link1", ["2"] = "link2" }, hash)
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment