From 8d1d36cd6692fb675cbc4ecb707cc58302cfe3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hi=E1=BA=BFu=20Nguy=E1=BB=85n?= Date: Fri, 13 Aug 2021 02:40:16 +0800 Subject: [PATCH] Raise error when hdel is called with empty array (#215) Close #212 --- lib/mock_redis/hash_methods.rb | 5 +++++ spec/commands/hdel_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 92398306..e79a7a01 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -10,6 +10,11 @@ def hdel(key, *fields) with_hash_at(key) do |hash| orig_size = hash.size fields = Array(fields).flatten.map(&:to_s) + + if fields.empty? + raise Redis::CommandError, "ERR wrong number of arguments for 'hdel' command" + end + hash.delete_if { |k, _v| fields.include?(k) } orig_size - hash.size end diff --git a/spec/commands/hdel_spec.rb b/spec/commands/hdel_spec.rb index d015569d..418a6ecd 100644 --- a/spec/commands/hdel_spec.rb +++ b/spec/commands/hdel_spec.rb @@ -66,5 +66,12 @@ @redises.get(@key).should be_nil end + it 'raises error if an empty array is passed' do + expect { @redises.hdel(@key, []) }.to raise_error( + Redis::CommandError, + "ERR wrong number of arguments for 'hdel' command" + ) + end + it_should_behave_like 'a hash-only command' end