Skip to content
This repository has been archived by the owner on Sep 11, 2019. It is now read-only.

Commit

Permalink
Sort sets when returned into Lua
Browse files Browse the repository at this point in the history
This is to match redis's documented behaviour of sorting some arrays
when Lua fetches them, to make scripts deterministic.
  • Loading branch information
bmerry committed Feb 19, 2018
1 parent c44ae5e commit 2788f83
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions fakenewsredis.py
Expand Up @@ -781,6 +781,12 @@ def _convert_redis_result(self, lua_runtime, result):
for item in result.items()
for i in item
]
elif isinstance(result, set):
converted = sorted(
self._convert_redis_result(lua_runtime, item)
for item in result
)
return lua_runtime.table_from(converted)
elif isinstance(result, (list, set, tuple)):
converted = [
self._convert_redis_result(lua_runtime, item)
Expand Down
5 changes: 3 additions & 2 deletions test_fakenewsredis.py
Expand Up @@ -3338,7 +3338,7 @@ def test_eval_lset(self):
self.assertEqual(self.redis.lrange('foo', 0, -1), [b'z', b'b'])

def test_eval_sdiff(self):
self.redis.sadd('foo', 'a', 'b', 'c')
self.redis.sadd('foo', 'a', 'b', 'c', 'f', 'e', 'd')
self.redis.sadd('bar', 'b')
val = self.redis.eval(
'''
Expand All @@ -3349,7 +3349,8 @@ def test_eval_sdiff(self):
return value;
end
''', 2, 'foo', 'bar')
self.assertEqual(set(val), set([b'a', b'c']))
# Lua must receive the set *sorted*
self.assertEqual(val, [b'a', b'c', b'd', b'e', b'f'])


class TestFakeRedis(unittest.TestCase):
Expand Down

0 comments on commit 2788f83

Please sign in to comment.