Skip to content
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

Redis operation that returns dict now converted to Lua table when called inside eval operation #209

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,12 @@ def eval(self, script, numkeys, *keys_and_args):

def _convert_redis_result(self, lua_runtime, result):
if isinstance(result, dict):
return [
converted = [
i
for item in result.items()
for i in item
]
return lua_runtime.table_from(converted)
elif isinstance(result, set):
converted = sorted(
self._convert_redis_result(lua_runtime, item)
Expand Down
16 changes: 16 additions & 0 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,22 @@ def test_eval_hgetall(self):
[[b'k1', b'bar'], [b'k2', b'baz']]
)

def test_eval_hgetall_iterate(self):
self.redis.hset('foo', 'k1', 'bar')
self.redis.hset('foo', 'k2', 'baz')
lua = """
local result = redis.call("hgetall", "foo")
for i, v in ipairs(result) do
end
return result
"""
val = self.redis.eval(lua, 0, 'foo')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1, not 0 (number of keys passed).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are totally right.
Fixed it.

sorted_val = sorted([val[:2], val[2:]])
self.assertEqual(
sorted_val,
[[b'k1', b'bar'], [b'k2', b'baz']]
)

def test_eval_list_with_nil(self):
self.redis.lpush('foo', 'bar')
self.redis.lpush('foo', None)
Expand Down