diff --git a/mockredis/script.py b/mockredis/script.py index c0a3d48..a661151 100644 --- a/mockredis/script.py +++ b/mockredis/script.py @@ -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() @@ -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) diff --git a/mockredis/tests/test_script.py b/mockredis/tests/test_script.py index bb000b6..8495c6c 100644 --- a/mockredis/tests/test_script.py +++ b/mockredis/tests/test_script.py @@ -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