Fix :mongo strategy

Currently, the following code doesn't work:

```ruby
DatabaseCleaner[:mongo].strategy = :truncation

RSpec.configure do |config|
  config.around do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end
```

The causes are:

1. `DatabaseCleaner::ORMAutodetector` doesn't consider `:mongo` option
2. `DatabaseCleaner::Mongo::Truncation` doesn't respond to `#cleaning`
This commit is contained in:
Fumiaki MATSUSHIMA 2020-05-04 09:53:56 +00:00 committed by Micah Geisel
parent 663678e37e
commit 13db3bd3f0
5 changed files with 85 additions and 26 deletions

View file

@ -1,9 +1,11 @@
require 'database_cleaner/mongo/base' require 'database_cleaner/mongo/base'
require 'database_cleaner/generic/base'
require 'database_cleaner/generic/truncation' require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation_mixin' require 'database_cleaner/mongo/truncation_mixin'
module DatabaseCleaner module DatabaseCleaner
module Mongo module Mongo
class Truncation class Truncation
include ::DatabaseCleaner::Generic::Base
include ::DatabaseCleaner::Generic::Truncation include ::DatabaseCleaner::Generic::Truncation
include TruncationMixin include TruncationMixin
include Base include Base

View file

@ -5,6 +5,7 @@ module DatabaseCleaner
data_mapper: "DataMapper", data_mapper: "DataMapper",
mongo_mapper: "MongoMapper", mongo_mapper: "MongoMapper",
mongoid: "Mongoid", mongoid: "Mongoid",
mongo: "Mongo",
couch_potato: "CouchPotato", couch_potato: "CouchPotato",
sequel: "Sequel", sequel: "Sequel",
moped: "Moped", moped: "Moped",

View file

@ -1,5 +1,6 @@
require 'active_record' require 'active_record'
require 'data_mapper' require 'data_mapper'
require 'mongo'
require 'mongo_mapper' require 'mongo_mapper'
require 'mongoid' require 'mongoid'
require 'couch_potato' require 'couch_potato'
@ -18,6 +19,7 @@ module DatabaseCleaner
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
hide_const "Mongo"
hide_const "CouchPotato" hide_const "CouchPotato"
hide_const "Sequel" hide_const "Sequel"
hide_const "Moped" hide_const "Moped"
@ -26,7 +28,7 @@ module DatabaseCleaner
hide_const "Neo4j" hide_const "Neo4j"
expect { subject }.to raise_error(DatabaseCleaner::NoORMDetected, <<-ERROR.chomp) expect { subject }.to raise_error(DatabaseCleaner::NoORMDetected, <<-ERROR.chomp)
No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, CouchPotato, Sequel, Moped, Ohm, Redis, or Neo4j loaded? No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, Mongo, CouchPotato, Sequel, Moped, Ohm, Redis, or Neo4j loaded?
ERROR ERROR
end end
@ -56,41 +58,54 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
expect(subject).to be_auto_detected expect(subject).to be_auto_detected
end end
it "should detect CouchPotato fifth" do it "should detect Mongo fifth" do
hide_const "ActiveRecord" hide_const "ActiveRecord"
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
expect(subject.orm).to eq :mongo
expect(subject).to be_auto_detected
end
it "should detect CouchPotato sixth" do
hide_const "ActiveRecord"
hide_const "DataMapper"
hide_const "MongoMapper"
hide_const "Mongoid"
hide_const "Mongo"
expect(subject.orm).to eq :couch_potato expect(subject.orm).to eq :couch_potato
expect(subject).to be_auto_detected expect(subject).to be_auto_detected
end end
it "should detect Sequel sixth" do it "should detect Sequel seventh" do
hide_const "ActiveRecord" hide_const "ActiveRecord"
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
hide_const "Mongo"
hide_const "CouchPotato" hide_const "CouchPotato"
expect(subject.orm).to eq :sequel expect(subject.orm).to eq :sequel
expect(subject).to be_auto_detected expect(subject).to be_auto_detected
end end
it 'detects Moped seventh' do it 'detects Moped eighth' do
hide_const "ActiveRecord" hide_const "ActiveRecord"
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
hide_const "Mongo"
hide_const "CouchPotato" hide_const "CouchPotato"
hide_const "Sequel" hide_const "Sequel"
expect(subject.orm).to eq :moped expect(subject.orm).to eq :moped
expect(subject).to be_auto_detected expect(subject).to be_auto_detected
end end
it 'detects Ohm eighth' do it 'detects Ohm ninth' do
hide_const "ActiveRecord" hide_const "ActiveRecord"
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
hide_const "Mongo"
hide_const "CouchPotato" hide_const "CouchPotato"
hide_const "Sequel" hide_const "Sequel"
hide_const "Moped" hide_const "Moped"
@ -98,11 +113,12 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
expect(subject).to be_auto_detected expect(subject).to be_auto_detected
end end
it 'detects Redis ninth' do it 'detects Redis tenth' do
hide_const "ActiveRecord" hide_const "ActiveRecord"
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
hide_const "Mongo"
hide_const "CouchPotato" hide_const "CouchPotato"
hide_const "Sequel" hide_const "Sequel"
hide_const "Moped" hide_const "Moped"
@ -111,11 +127,12 @@ No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, C
expect(subject).to be_auto_detected expect(subject).to be_auto_detected
end end
it 'detects Neo4j tenth' do it 'detects Neo4j eleventh' do
hide_const "ActiveRecord" hide_const "ActiveRecord"
hide_const "DataMapper" hide_const "DataMapper"
hide_const "MongoMapper" hide_const "MongoMapper"
hide_const "Mongoid" hide_const "Mongoid"
hide_const "Mongo"
hide_const "CouchPotato" hide_const "CouchPotato"
hide_const "Sequel" hide_const "Sequel"
hide_const "Moped" hide_const "Moped"

View file

@ -35,6 +35,13 @@ RSpec.describe DatabaseCleaner::Configuration do
expect(subject.cleaners.values).to eq [cleaner] expect(subject.cleaners.values).to eq [cleaner]
end end
it "should accept :mongo" do
cleaner = subject[:mongo]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :mongo
expect(subject.cleaners.values).to eq [cleaner]
end
it "should accept :mongo_mapper" do it "should accept :mongo_mapper" do
cleaner = subject[:mongo_mapper] cleaner = subject[:mongo_mapper]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)

View file

@ -19,31 +19,63 @@ RSpec.describe DatabaseCleaner::Mongo::Truncation do
MongoTest::Gadget.new(name: 'some gadget').save! MongoTest::Gadget.new(name: 'some gadget').save!
end end
context "by default" do describe "#clean" do
it "truncates all collections" do context "by default" do
expect { subject.clean }.to change { it "truncates all collections" do
[MongoTest::Widget.count, MongoTest::Gadget.count] expect { subject.clean }.to change {
}.from([1,1]).to([0,0]) [MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([0,0])
end
end
context "when collections are provided to the :only option" do
subject { described_class.new(only: ['MongoTest::Widget']) }
it "only truncates the specified collections" do
expect { subject.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([0,1])
end
end
context "when collections are provided to the :except option" do
subject { described_class.new(except: ['MongoTest::Widget']) }
it "truncates all but the specified collections" do
expect { subject.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([1,0])
end
end end
end end
context "when collections are provided to the :only option" do describe "#cleaning" do
subject { described_class.new(only: ['MongoTest::Widget']) } context "by default" do
it "truncates all collections" do
it "only truncates the specified collections" do expect { subject.cleaning {} }.to change {
expect { subject.clean }.to change { [MongoTest::Widget.count, MongoTest::Gadget.count]
[MongoTest::Widget.count, MongoTest::Gadget.count] }.from([1,1]).to([0,0])
}.from([1,1]).to([0,1]) end
end end
end
context "when collections are provided to the :except option" do context "when collections are provided to the :only option" do
subject { described_class.new(except: ['MongoTest::Widget']) } subject { described_class.new(only: ['MongoTest::Widget']) }
it "truncates all but the specified collections" do it "only truncates the specified collections" do
expect { subject.clean }.to change { expect { subject.cleaning {} }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count] [MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([1,0]) }.from([1,1]).to([0,1])
end
end
context "when collections are provided to the :except option" do
subject { described_class.new(except: ['MongoTest::Widget']) }
it "truncates all but the specified collections" do
expect { subject.cleaning {} }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([1,0])
end
end end
end end
end end