mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
refactor mongo specs.
This commit is contained in:
parent
0c44b931a2
commit
ec82904231
6 changed files with 133 additions and 176 deletions
|
@ -1,13 +1,13 @@
|
|||
module MongoTest
|
||||
class ThingBase
|
||||
class Base
|
||||
def self.collection
|
||||
@connection ||= ::Mongo::Connection.new('127.0.0.1')
|
||||
@connection ||= Mongo::Connection.new('127.0.0.1')
|
||||
@db ||= @connection.db('database_cleaner_specs')
|
||||
@mongo ||= @db.collection(name) || @db.create_collection(name)
|
||||
@collection ||= @db.collection(name) || @db.create_collection(name)
|
||||
end
|
||||
|
||||
def self.count
|
||||
@mongo.count
|
||||
@collection.count
|
||||
end
|
||||
|
||||
def initialize(attrs={})
|
||||
|
@ -19,8 +19,8 @@ module MongoTest
|
|||
end
|
||||
end
|
||||
|
||||
class Widget < ThingBase
|
||||
class Widget < Base
|
||||
end
|
||||
class Gadget < ThingBase
|
||||
class Gadget < Base
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,62 +2,49 @@ require 'mongo'
|
|||
require 'database_cleaner/mongo/truncation'
|
||||
require File.dirname(__FILE__) + '/mongo_examples'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Mongo
|
||||
describe DatabaseCleaner::Mongo::Truncation do
|
||||
around do |example|
|
||||
connection = Mongo::Connection.new('127.0.0.1')
|
||||
db_name = 'database_cleaner_specs'
|
||||
db = connection.db(db_name)
|
||||
subject.db = db
|
||||
|
||||
describe Truncation do
|
||||
let(:args) {{}}
|
||||
let(:truncation) { described_class.new(args).tap { |t| t.db=@db } }
|
||||
#doing this in the file root breaks autospec, doing it before(:all) just fails the specs
|
||||
before(:all) do
|
||||
@connection = ::Mongo::Connection.new('127.0.0.1')
|
||||
@test_db = 'database_cleaner_specs'
|
||||
@db = @connection.db(@test_db)
|
||||
example.run
|
||||
|
||||
connection.drop_database(db_name)
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@connection.drop_database(@test_db)
|
||||
before do
|
||||
MongoTest::Widget.new(name: 'some widget').save!
|
||||
MongoTest::Gadget.new(name: 'some gadget').save!
|
||||
end
|
||||
|
||||
def create_widget(attrs={})
|
||||
MongoTest::Widget.new({:name => 'some widget'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
def create_gadget(attrs={})
|
||||
MongoTest::Gadget.new({:name => 'some gadget'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
it "truncates all collections by default" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { truncation.clean }.to change {
|
||||
context "by default" do
|
||||
it "truncates all collections" do
|
||||
expect { subject.clean }.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
|
||||
let(:args) {{:only => ['MongoTest::Widget']}}
|
||||
subject { described_class.new(only: ['MongoTest::Widget']) }
|
||||
|
||||
it "only truncates the specified collections" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { truncation.clean }.to change {
|
||||
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
|
||||
let(:args) {{:except => ['MongoTest::Widget']}}
|
||||
subject { described_class.new(except: ['MongoTest::Widget']) }
|
||||
|
||||
it "truncates all but the specified collections" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { truncation.clean }.to change {
|
||||
expect { subject.clean }.to change {
|
||||
[MongoTest::Widget.count, MongoTest::Gadget.count]
|
||||
}.from([1,1]).to([1,0])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
class Widget
|
||||
module MongoMapperTest
|
||||
class Widget
|
||||
include ::MongoMapper::Document
|
||||
key :name, String
|
||||
end
|
||||
class Gadget
|
||||
end
|
||||
class Gadget
|
||||
include ::MongoMapper::Document
|
||||
key :name, String
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,59 +2,48 @@ require 'mongo_mapper'
|
|||
require 'database_cleaner/mongo_mapper/truncation'
|
||||
require File.dirname(__FILE__) + '/mongo_examples'
|
||||
|
||||
module DatabaseCleaner
|
||||
module MongoMapper
|
||||
describe DatabaseCleaner::MongoMapper::Truncation do
|
||||
around do |example|
|
||||
MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
|
||||
db_name = 'database_cleaner_specs'
|
||||
MongoMapper.database = db_name
|
||||
|
||||
describe Truncation do
|
||||
example.run
|
||||
|
||||
#doing this in the file root breaks autospec, doing it before(:all) just fails the specs
|
||||
before(:all) do
|
||||
::MongoMapper.connection = ::Mongo::Connection.new('127.0.0.1')
|
||||
@test_db = 'database_cleaner_specs'
|
||||
::MongoMapper.database = @test_db
|
||||
MongoMapper.connection.drop_database(db_name)
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
::MongoMapper.connection.drop_database(@test_db)
|
||||
before do
|
||||
MongoMapperTest::Widget.new(name: 'some widget').save!
|
||||
MongoMapperTest::Gadget.new(name: 'some gadget').save!
|
||||
end
|
||||
|
||||
def create_widget(attrs={})
|
||||
Widget.new({:name => 'some widget'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
def create_gadget(attrs={})
|
||||
Gadget.new({:name => 'some gadget'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
it "truncates all collections by default" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { Truncation.new.clean }.to change {
|
||||
[Widget.count, Gadget.count]
|
||||
context "by default" do
|
||||
it "truncates all collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MongoMapperTest::Widget.count, MongoMapperTest::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: ['mongo_mapper_test.widgets']) }
|
||||
|
||||
it "only truncates the specified collections" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { Truncation.new(only: ['widgets']).clean }.to change {
|
||||
[Widget.count, Gadget.count]
|
||||
expect { subject.clean }.to change {
|
||||
[MongoMapperTest::Widget.count, MongoMapperTest::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: ['mongo_mapper_test.widgets']) }
|
||||
|
||||
it "truncates all but the specified collections" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { Truncation.new(except: ['widgets']).clean }.to change {
|
||||
[Widget.count, Gadget.count]
|
||||
expect { subject.clean }.to change {
|
||||
[MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count]
|
||||
}.from([1,1]).to([1,0])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module MopedTest
|
||||
class ThingBase
|
||||
class Base
|
||||
def self.collection
|
||||
@db ||= 'database_cleaner_specs'
|
||||
@session ||= ::Moped::Session.new(['127.0.0.1:27017'], database: @db)
|
||||
|
@ -19,11 +19,11 @@ module MopedTest
|
|||
end
|
||||
end
|
||||
|
||||
class Widget < ThingBase
|
||||
class Widget < Base
|
||||
end
|
||||
class Gadget < ThingBase
|
||||
class Gadget < Base
|
||||
end
|
||||
class System < ThingBase
|
||||
class System < Base
|
||||
def self.collection
|
||||
super
|
||||
@collection = @session['system_logs']
|
||||
|
|
|
@ -2,71 +2,50 @@ require 'moped'
|
|||
require 'database_cleaner/moped/truncation'
|
||||
require File.dirname(__FILE__) + '/moped_examples'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Moped
|
||||
describe DatabaseCleaner::Moped::Truncation do
|
||||
around do |example|
|
||||
db_name = 'database_cleaner_specs'
|
||||
session = ::Moped::Session.new(['127.0.0.1:27017'], database: db_name)
|
||||
subject.db = db_name
|
||||
|
||||
describe Truncation do
|
||||
let(:args) {{}}
|
||||
let(:truncation) { described_class.new(args) }
|
||||
#doing this in the file root breaks autospec, doing it before(:all) just fails the specs
|
||||
before(:all) do
|
||||
@test_db = 'database_cleaner_specs'
|
||||
@session = ::Moped::Session.new(['127.0.0.1:27017'], database: @test_db)
|
||||
example.run
|
||||
|
||||
session.drop
|
||||
session.command(getlasterror: 1)
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
truncation.db = @test_db
|
||||
before do
|
||||
MopedTest::Widget.new(name: 'some widget').save!
|
||||
MopedTest::Gadget.new(name: 'some gadget').save!
|
||||
MopedTest::System.new(name: 'some system').save!
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@session.drop
|
||||
@session.command(getlasterror: 1)
|
||||
end
|
||||
|
||||
def create_widget(attrs={})
|
||||
MopedTest::Widget.new({:name => 'some widget'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
def create_gadget(attrs={})
|
||||
MopedTest::Gadget.new({:name => 'some gadget'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
def create_system(attrs={})
|
||||
MopedTest::System.new({:name => 'some system'}.merge(attrs)).save!
|
||||
end
|
||||
|
||||
it "truncates all collections by default" do
|
||||
create_widget
|
||||
create_gadget
|
||||
create_system
|
||||
expect { truncation.clean }.to change {
|
||||
context "by default" do
|
||||
it "truncates all collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
|
||||
}.from([1,1,1]).to([0,0,0])
|
||||
end
|
||||
end
|
||||
|
||||
context "when collections are provided to the :only option" do
|
||||
let(:args) {{:only => ['MopedTest::Widget']}}
|
||||
subject { described_class.new(only: ['MopedTest::Widget']) }
|
||||
|
||||
it "only truncates the specified collections" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { truncation.clean }.to change {
|
||||
expect { subject.clean }.to change {
|
||||
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
|
||||
}.from([1,1,0]).to([0,1,0])
|
||||
}.from([1,1,1]).to([0,1,1])
|
||||
end
|
||||
end
|
||||
|
||||
context "when collections are provided to the :except option" do
|
||||
let(:args) {{:except => ['MopedTest::Widget']}}
|
||||
subject { described_class.new(except: ['MopedTest::Widget']) }
|
||||
|
||||
it "truncates all but the specified collections" do
|
||||
create_widget
|
||||
create_gadget
|
||||
expect { truncation.clean }.to change {
|
||||
expect { subject.clean }.to change {
|
||||
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
|
||||
}.from([1,1,0]).to([1,0,0])
|
||||
}.from([1,1,1]).to([1,0,0])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue