def _execute_lua(self, keys, args, client): """ Patch MockRedis+Lua for error_reply """ lua, lua_globals = Script._import_lua(self.load_dependencies) lua_globals.KEYS = self._python_to_lua(keys) lua_globals.ARGV = self._python_to_lua(args) 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': response = client.call( call_args[0], call_args[1], call_args[3], # "count", default is 0 call_args[2]) else: response = client.call(*call_args) return self._python_to_lua(response) def _reply_table(field, message): return lua.eval("{{{field}='{message}'}}".format(field=field, message=message)) lua_globals.redis = { 'call': _call, 'status_reply': lambda status: _reply_table('ok', status), 'error_reply': lambda error: _reply_table('err', error), } return self._lua_to_python(lua.execute(self.script), return_status=True)
def setup(self): self.redis = MockRedis(load_lua_dependencies=False) self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT.encode("utf-8")).hexdigest() try: lua, lua_globals = MockRedisScript._import_lua(load_dependencies=False) except RuntimeError: raise SkipTest("mockredispy was not installed with lua support") self.lua = lua self.lua_globals = lua_globals assert_equal_list = """ function compare_list(list1, list2) if #list1 ~= #list2 then return false end for i, item1 in ipairs(list1) do if item1 ~= list2[i] then return false end end return true end function assert_equal_list(list1, list2) assert(compare_list(list1, list2)) end return assert_equal_list """ self.lua_assert_equal_list = self.lua.execute(assert_equal_list) assert_equal_list_with_pairs = """ function pair_exists(list1, key, value) i = 1 for i, item1 in ipairs(list1) do if i%2 == 1 then if (list1[i] == key) and (list1[i + 1] == value) then return true end end end return false end function compare_list_with_pairs(list1, list2) if #list1 ~= #list2 or #list1 % 2 == 1 then return false end for i = 1, #list1, 2 do if not pair_exists(list2, list1[i], list1[i + 1]) then return false end end return true end function assert_equal_list_with_pairs(list1, list2) assert(compare_list_with_pairs(list1, list2)) end return assert_equal_list_with_pairs """ self.lua_assert_equal_list_with_pairs = self.lua.execute(assert_equal_list_with_pairs) compare_val = """ function compare_val(var1, var2) return var1 == var2 end return compare_val """ self.lua_compare_val = self.lua.execute(compare_val)