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_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 _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 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 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_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_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_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_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_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_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_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)))