def test_python_to_lua_float(self): pval = 10.1 lval = MockredisScript._python_to_lua(pval) lval_expected = self.lua.eval('10.1') self.assertEqual("number", self.lua_globals.type(lval)) self.assertTrue( MockredisScript._lua_to_python( self.lua_compare_val(lval_expected, lval)))
def test_python_to_lua_float(self): pval = 10.1 lval = MockRedisScript._python_to_lua(pval) lval_expected = self.lua.eval('10.1') eq_("number", self.lua_globals.type(lval)) ok_( MockRedisScript._lua_to_python( self.lua_compare_val(lval_expected, lval)))
def test_python_to_lua_none(self): pval = None lval = MockRedisScript._python_to_lua(pval) is_null = """ function is_null(var1) return var1 == nil end return is_null """ lua_is_null = self.lua.execute(is_null) ok_(MockRedisScript._lua_to_python(lua_is_null(lval)))
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 evalsha(self, sha, numkeys, *keys_and_args): """Emulates evalsha""" if not self.script_exists(sha)[0]: raise RedisError("Sha not registered") script_callable = Script(self, self.shas[sha], self.load_lua_dependencies) numkeys = max(numkeys, 0) keys = keys_and_args[:numkeys] args = keys_and_args[numkeys:] return script_callable(keys, args)
def _python_to_lua(pval): """ Patch MockRedis+Lua for Python 3 compatibility """ # noinspection PyUnresolvedReferences import lua if pval is None: # Python None --> Lua None return lua.eval('') if isinstance(pval, (list, tuple, set)): # Python list --> Lua table # e.g.: in lrange # in Python returns: [v1, v2, v3] # in Lua returns: {v1, v2, v3} lua_list = lua.eval('{}') lua_table = lua.eval('table') for item in pval: lua_table.insert(lua_list, Script._python_to_lua(item)) return lua_list elif isinstance(pval, dict): # Python dict --> Lua dict # e.g.: in hgetall # in Python returns: {k1:v1, k2:v2, k3:v3} # in Lua returns: {k1, v1, k2, v2, k3, v3} lua_dict = lua.eval('{}') lua_table = lua.eval('table') for k, v in six.iteritems(pval): lua_table.insert(lua_dict, Script._python_to_lua(k)) lua_table.insert(lua_dict, Script._python_to_lua(v)) return lua_dict elif isinstance(pval, tuple(set(six.string_types + (six.binary_type, )))): # type: ignore # Python string --> Lua userdata return pval elif isinstance(pval, bool): # Python bool--> Lua boolean return lua.eval(str(pval).lower()) elif isinstance(pval, six.integer_types + (float, )): # type: ignore # Python int --> Lua number lua_globals = lua.globals() return lua_globals.tonumber(str(pval)) raise RuntimeError('Invalid Python type: ' + str(type(pval)))
def _lua_to_python(lval, return_status=False): """ Patch MockRedis+Lua for Python 3 compatibility """ # noinspection PyUnresolvedReferences import lua lua_globals = lua.globals() if lval is None: # Lua None --> Python None return None if lua_globals.type(lval) == 'table': # Lua table --> Python list pval = [] for i in lval: if return_status: if i == 'ok': return lval[i] if i == 'err': raise ResponseError(lval[i]) pval.append(Script._lua_to_python(lval[i])) return pval elif isinstance(lval, six.integer_types): # Lua number --> Python long return six.integer_types[-1](lval) elif isinstance(lval, float): # Lua number --> Python float return float(lval) elif lua_globals.type(lval) == 'userdata': # Lua userdata --> Python string return str(lval) elif lua_globals.type(lval) == 'string': # Lua string --> Python string return lval elif lua_globals.type(lval) == 'boolean': # Lua boolean --> Python bool return bool(lval) raise RuntimeError('Invalid Lua type: ' + str(lua_globals.type(lval)))
def test_lua_to_python_list(self): lval = self.lua.eval('{"val1", "val2"}') pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, list)) eq_(["val1", "val2"], pval)
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)
def register_script(self, script): """Emulate register_script""" return Script(self, script)
def register_script(self, script): """Emulate register_script""" return Script(self, script, self.load_lua_dependencies)
def test_python_to_lua_long(self): pval = long(10) lval = MockRedisScript._python_to_lua(pval) lval_expected = self.lua.eval('10') eq_("number", self.lua_globals.type(lval)) ok_(MockRedisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))
def test_lua_to_python_string(self): lval = self.lua.eval('"somestring"') pval = MockredisScript._lua_to_python(lval) self.assertIsInstance(pval, str) self.assertEqual("somestring", pval)
def test_lua_to_python_string(self): lval = self.lua.eval('"somestring"') pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, str)) eq_("somestring", pval)
def test_python_to_lua_list(self): pval = ["abc", "xyz"] lval = MockRedisScript._python_to_lua(pval) lval_expected = self.lua.eval('{"abc", "xyz"}') self.lua_assert_equal_list(lval_expected, lval)
def test_lua_to_python_long(self): lval = self.lua.eval("22") pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, long)) eq_(22, pval)
def test_lua_to_python_flota(self): lval = self.lua.eval("22.2") pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, float)) eq_(22.2, pval)
def test_lua_to_python_none(self): lval = self.lua.eval("") pval = MockRedisScript._lua_to_python(lval) ok_(pval is None)
def test_lua_to_python_bool(self): lval = self.lua.eval("true") pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, bool)) eq_(True, pval)
def test_lua_to_python_long(self): lval = self.lua.eval('22') pval = MockredisScript._lua_to_python(lval) self.assertIsInstance(pval, long) self.assertEqual(22, pval)
def test_python_to_lua_float(self): pval = 10.1 lval = MockredisScript._python_to_lua(pval) lval_expected = self.lua.eval('10.1') self.assertEqual("number", self.lua_globals.type(lval)) self.assertTrue(MockredisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))
def test_python_to_lua_boolean(self): pval = True lval = MockRedisScript._python_to_lua(pval) eq_("boolean", self.lua_globals.type(lval)) ok_(MockRedisScript._lua_to_python(lval))
def test_python_to_lua_string(self): pval = "somestring" lval = MockRedisScript._python_to_lua(pval) lval_expected = self.lua.eval('"somestring"') eq_("string", self.lua_globals.type(lval)) eq_(lval_expected, lval)
def test_lua_to_python_bool(self): lval = self.lua.eval('true') pval = MockredisScript._lua_to_python(lval) self.assertIsInstance(pval, bool) self.assertEqual(True, pval)
def test_python_to_lua_boolean(self): pval = True lval = MockredisScript._python_to_lua(pval) self.assertEqual("boolean", self.lua_globals.type(lval)) self.assertTrue(MockredisScript._lua_to_python(lval))
def test_python_to_lua_dict(self): pval = {"k1": "v1", "k2": "v2"} lval = MockRedisScript._python_to_lua(pval) lval_expected = self.lua.eval('{"k1", "v1", "k2", "v2"}') self.lua_assert_equal_list_with_pairs(lval_expected, lval)
def test_lua_to_python_flota(self): lval = self.lua.eval('22.2') pval = MockredisScript._lua_to_python(lval) self.assertIsInstance(pval, float) self.assertEqual(22.2, pval)
def test_python_to_lua_float(self): pval = 10.1 lval = MockRedisScript._python_to_lua(pval) lval_expected = self.lua.eval("10.1") eq_("number", self.lua_globals.type(lval)) ok_(MockRedisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))
def test_lua_to_python_long(self): lval = self.lua.eval('22') pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, long)) eq_(22, pval)
def test_lua_to_python_flota(self): lval = self.lua.eval('22.2') pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, float)) eq_(22.2, pval)
def test_lua_to_python_list(self): lval = self.lua.eval('{"val1", "val2"}') pval = MockredisScript._lua_to_python(lval) self.assertIsInstance(pval, list) self.assertEqual(["val1", "val2"], pval)
def test_lua_to_python_bool(self): lval = self.lua.eval('true') pval = MockRedisScript._lua_to_python(lval) ok_(isinstance(pval, bool)) eq_(True, pval)
def test_lua_to_python_none(self): lval = self.lua.eval("") pval = MockredisScript._lua_to_python(lval) self.assertTrue(pval is None)