Skip to content

Make unlink consistent with del when an empty keyset passed #1316

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

Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 0 deletions lib/redis/commands/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ def del(*keys)
# @param [String, Array<String>] keys
# @return [Integer] number of keys that were unlinked
def unlink(*keys)
keys.flatten!(1)
return 0 if keys.empty?

send_command([:unlink] + keys)
end

Expand Down
17 changes: 15 additions & 2 deletions test/redis/commands_on_value_types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def test_unlink

assert_equal ["bar", "baz", "foo"], r.keys("*").sort

assert_equal 0, r.unlink("")

assert_equal ["bar", "baz", "foo"], r.keys("*").sort

assert_equal 1, r.unlink("foo")

assert_equal ["bar", "baz"], r.keys("*").sort
Expand All @@ -62,15 +66,24 @@ def test_unlink_with_array_argument
r.set "foo", "s1"
r.set "bar", "s2"
r.set "baz", "s3"
r.set "bad", "s4"

assert_equal ["bar", "baz", "foo"], r.keys("*").sort
assert_equal ["bad", "bar", "baz", "foo"], r.keys("*").sort

assert_equal 0, r.unlink([])

assert_equal ["bad", "bar", "baz", "foo"], r.keys("*").sort

assert_equal 1, r.unlink(["foo"])

assert_equal ["bar", "baz"], r.keys("*").sort
assert_equal ["bad", "bar", "baz"], r.keys("*").sort

assert_equal 2, r.unlink(["bar", "baz"])

assert_equal ["bad"], r.keys("*").sort

assert_equal 1, r.unlink([["bad"]])

assert_equal [], r.keys("*").sort
end

Expand Down
Loading