diff --git a/lib/database_cleaner/redis/deletion.rb b/lib/database_cleaner/redis/deletion.rb index b87a107..8a47f1a 100644 --- a/lib/database_cleaner/redis/deletion.rb +++ b/lib/database_cleaner/redis/deletion.rb @@ -3,13 +3,13 @@ module DatabaseCleaner module Redis class Deletion < Strategy - def initialize only: [], except: [] + def initialize only: nil, except: nil @only = only @except = except end def clean - if @only.none? && @except.none? + if @only.nil? && @except.nil? connection.flushdb else keys_to_delete.each do |key| @@ -23,9 +23,8 @@ def clean private def keys_to_delete - only = expand_keys(@only) - except = expand_keys(@except) - only = connection.keys if only.none? + only = @only.nil? ? connection.keys : expand_keys(@only) + except = @except.nil? ? [] : expand_keys(@except) (only - except) end diff --git a/spec/database_cleaner/redis/deletion_spec.rb b/spec/database_cleaner/redis/deletion_spec.rb index e14d5f4..5a892b8 100644 --- a/spec/database_cleaner/redis/deletion_spec.rb +++ b/spec/database_cleaner/redis/deletion_spec.rb @@ -31,6 +31,15 @@ expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1) expect(@redis.get('Gadget')).to eq '1' end + + context "but nothing matches in the database" do + subject { described_class.new(only: ['Foo']) } + + it "does not delete the database" do + expect { subject.clean }.not_to change { @redis.keys.size } + expect(@redis.keys).to match_array(["Widget", "Gadget"]) + end + end end context "with wildcard keys" do @@ -40,6 +49,15 @@ expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1) expect(@redis.get('Gadget')).to eq '1' end + + context "but nothing matches in the database" do + subject { described_class.new(only: ['Foo*']) } + + it "does not delete the database" do + expect { subject.clean }.not_to change { @redis.keys.size } + expect(@redis.keys).to match_array(["Widget", "Gadget"]) + end + end end end @@ -63,6 +81,44 @@ end end + context "with the :only and :except options" do + context "with concrete keys" do + subject { described_class.new(only: ['Widget', 'Gadget'], except: ['Gadget']) } + + it "only deletes the difference between the specified keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1) + expect(@redis.get('Gadget')).to eq '1' + end + + context "but :only does not match anything in the database" do + subject { described_class.new(only: ['Foo', 'Bar'], except: ['Gadget']) } + + it "does not delete anything in the database" do + expect { subject.clean }.not_to change { @redis.keys.size } + expect(@redis.keys).to match_array(["Widget", "Gadget"]) + end + end + end + + context "with wildcard keys" do + subject { described_class.new(only: ['Widge*', 'Gadge*'], except: ['Ga*']) } + + it "only deletes the difference between the specified keys" do + expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1) + expect(@redis.get('Gadget')).to eq '1' + end + + context "but :only does not match anything in the database" do + subject { described_class.new(only: ['F*', 'B*'], except: ['Ga*']) } + + it "does not delete the database" do + expect { subject.clean }.not_to change { @redis.keys.size } + expect(@redis.keys).to match_array(["Widget", "Gadget"]) + end + end + end + end + context "when passing url" do it "still works" do url = @redis.connection[:id]