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/generic/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation_mixin'
module DatabaseCleaner
module Mongo
class Truncation
include ::DatabaseCleaner::Generic::Base
include ::DatabaseCleaner::Generic::Truncation
include TruncationMixin
include Base

View file

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

View file

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

View file

@ -35,6 +35,13 @@ RSpec.describe DatabaseCleaner::Configuration do
expect(subject.cleaners.values).to eq [cleaner]
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
cleaner = subject[:mongo_mapper]
expect(cleaner).to be_a(DatabaseCleaner::Base)

View file

@ -19,6 +19,7 @@ RSpec.describe DatabaseCleaner::Mongo::Truncation do
MongoTest::Gadget.new(name: 'some gadget').save!
end
describe "#clean" do
context "by default" do
it "truncates all collections" do
expect { subject.clean }.to change {
@ -48,3 +49,34 @@ RSpec.describe DatabaseCleaner::Mongo::Truncation do
end
end
describe "#cleaning" do
context "by default" do
it "truncates all collections" do
expect { subject.cleaning {} }.to change {
[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.cleaning {} }.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.cleaning {} }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([1,0])
end
end
end
end