Skip to content
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

Fix entire db flush when no "only" keys found #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions lib/database_cleaner/redis/deletion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down
56 changes: 56 additions & 0 deletions spec/database_cleaner/redis/deletion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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]
Expand Down