diff --git a/lib/redis/connection/memory.rb b/lib/redis/connection/memory.rb index 951eceb..f8b58bd 100644 --- a/lib/redis/connection/memory.rb +++ b/lib/redis/connection/memory.rb @@ -1442,7 +1442,7 @@ def punsubscribe(*patterns) def zscan(key, start_cursor, *args) data_type_check(key, ZSet) - return [] unless data[key] + return '0', [] unless data[key] # return cursor 0, indicating end of iteration match = "*" count = 10 diff --git a/spec/sorted_sets_spec.rb b/spec/sorted_sets_spec.rb index 0429388..1d344d7 100644 --- a/spec/sorted_sets_spec.rb +++ b/spec/sorted_sets_spec.rb @@ -497,30 +497,41 @@ module FakeRedis end describe "#zscan" do - before { 50.times { |x| @client.zadd("key", x, "key #{x}") } } - - it 'with no arguments should return 10 numbers in ascending order' do - result = @client.zscan("key", 0)[1] - expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] }) - expect(result.count).to eq(10) + context 'without data' do + it 'return cursor 0 with an empty array' do + result = @client.zscan("notexistkey", 0) + expect(result.count).to eq 2 + expect(result[0]).to eq '0' + expect(result[1]).to eq [] + end end - it 'with a count should return that number of members' do - expect(@client.zscan("key", 0, count: 2)).to eq(["2", [["key 0", 0.0], ["key 1", 1.0]]]) - end + context 'with data' do + before { 50.times { |x| @client.zadd("key", x, "key #{x}") } } - it 'with a count greater than the number of members, returns all the members in asc order' do - result = @client.zscan("key", 0, count: 1000)[1] - expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] }) - expect(result.size).to eq(50) - end + it 'with no arguments should return 10 numbers in ascending order' do + result = @client.zscan("key", 0)[1] + expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] }) + expect(result.count).to eq(10) + end - it 'with match, should return key-values where the key matches' do - @client.zadd("key", 1.0, "blah") - @client.zadd("key", 2.0, "bluh") - result = @client.zscan("key", 0, count: 100, match: "key*")[1] - expect(result).to_not include(["blah", 1.0]) - expect(result).to_not include(["bluh", 2.0]) + it 'with a count should return that number of members' do + expect(@client.zscan("key", 0, count: 2)).to eq(["2", [["key 0", 0.0], ["key 1", 1.0]]]) + end + + it 'with a count greater than the number of members, returns all the members in asc order' do + result = @client.zscan("key", 0, count: 1000)[1] + expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] }) + expect(result.size).to eq(50) + end + + it 'with match, should return key-values where the key matches' do + @client.zadd("key", 1.0, "blah") + @client.zadd("key", 2.0, "bluh") + result = @client.zscan("key", 0, count: 100, match: "key*")[1] + expect(result).to_not include(["blah", 1.0]) + expect(result).to_not include(["bluh", 2.0]) + end end end