Skip to content

Fixed HMSET argument handling on Lua redis.call. #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion mockredis/script.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sys
import threading
try:
# Python 2
from itertools import izip
except ImportError:
# Python 3
izip = zip
from mockredis.exceptions import ResponseError

LuaLock = threading.Lock()
Expand Down Expand Up @@ -38,11 +44,16 @@ def _execute_lua(self, keys, args, client):
def _call(*call_args):
# redis-py and native redis commands are mostly compatible argument
# wise, but some exceptions need to be handled here:
if str(call_args[0]).lower() == 'lrem':
nrm_cmd = str(call_args[0]).lower()
if nrm_cmd == 'lrem':
response = client.call(
call_args[0], call_args[1],
call_args[3], # "count", default is 0
call_args[2])
elif nrm_cmd == 'hmset':
# redis-py hmset takes key value pairs in a dictionary and not as a flat list of arguments.
call_iter = iter(call_args)
response = client.call(next(call_iter), next(call_iter), dict(izip(call_iter, call_iter)))
else:
response = client.call(*call_args)
return self._python_to_lua(response)
Expand Down
10 changes: 10 additions & 0 deletions mockredis/tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ def test_script_hgetall(self):
ok_(isinstance(item, list))
eq_(["k1", "v1"], item)

def test_script_hmset(self):
script_content = """
return redis.call('HMSET', KEYS[1], unpack(ARGV))
"""
script = self.redis.register_script(script_content)
script_args = ['a', 1, 'b', '2', 3, 'c', 4, 5]
hash_key = "myhash"
script(keys=[hash_key], args=script_args)
eq_([str(val) for val in script_args[1::2]], self.redis.hmget(hash_key, script_args[0::2]))

def test_evalsha(self):
self.redis.lpush(LIST1, VAL1)
script = LPOP_SCRIPT
Expand Down