refactor mongo specs.

This commit is contained in:
Micah Geisel 2018-04-26 11:45:17 -07:00
parent 0c44b931a2
commit ec82904231
6 changed files with 133 additions and 176 deletions

View file

@ -1,13 +1,13 @@
module MongoTest module MongoTest
class ThingBase class Base
def self.collection 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') @db ||= @connection.db('database_cleaner_specs')
@mongo ||= @db.collection(name) || @db.create_collection(name) @collection ||= @db.collection(name) || @db.create_collection(name)
end end
def self.count def self.count
@mongo.count @collection.count
end end
def initialize(attrs={}) def initialize(attrs={})
@ -19,8 +19,8 @@ module MongoTest
end end
end end
class Widget < ThingBase class Widget < Base
end end
class Gadget < ThingBase class Gadget < Base
end end
end end

View file

@ -2,62 +2,49 @@ require 'mongo'
require 'database_cleaner/mongo/truncation' require 'database_cleaner/mongo/truncation'
require File.dirname(__FILE__) + '/mongo_examples' require File.dirname(__FILE__) + '/mongo_examples'
module DatabaseCleaner describe DatabaseCleaner::Mongo::Truncation do
module Mongo 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 example.run
let(:args) {{}}
let(:truncation) { described_class.new(args).tap { |t| t.db=@db } } connection.drop_database(db_name)
#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)
end end
after(:each) do before do
@connection.drop_database(@test_db) MongoTest::Widget.new(name: 'some widget').save!
MongoTest::Gadget.new(name: 'some gadget').save!
end end
def create_widget(attrs={}) context "by default" do
MongoTest::Widget.new({:name => 'some widget'}.merge(attrs)).save! it "truncates all collections" do
end expect { subject.clean }.to change {
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 {
[MongoTest::Widget.count, MongoTest::Gadget.count] [MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([0,0]) }.from([1,1]).to([0,0])
end end
end
context "when collections are provided to the :only option" do 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 it "only truncates the specified collections" do
create_widget expect { subject.clean }.to change {
create_gadget
expect { truncation.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count] [MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([0,1]) }.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 :except option" do
let(:args) {{:except => ['MongoTest::Widget']}} subject { described_class.new(except: ['MongoTest::Widget']) }
it "truncates all but the specified collections" do it "truncates all but the specified collections" do
create_widget expect { subject.clean }.to change {
create_gadget
expect { truncation.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count] [MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([1,0]) }.from([1,1]).to([1,0])
end end
end end
end end
end
end

View file

@ -1,3 +1,4 @@
module MongoMapperTest
class Widget class Widget
include ::MongoMapper::Document include ::MongoMapper::Document
key :name, String key :name, String
@ -6,3 +7,4 @@ class Gadget
include ::MongoMapper::Document include ::MongoMapper::Document
key :name, String key :name, String
end end
end

View file

@ -2,59 +2,48 @@ require 'mongo_mapper'
require 'database_cleaner/mongo_mapper/truncation' require 'database_cleaner/mongo_mapper/truncation'
require File.dirname(__FILE__) + '/mongo_examples' require File.dirname(__FILE__) + '/mongo_examples'
module DatabaseCleaner describe DatabaseCleaner::MongoMapper::Truncation do
module MongoMapper 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 MongoMapper.connection.drop_database(db_name)
before(:all) do
::MongoMapper.connection = ::Mongo::Connection.new('127.0.0.1')
@test_db = 'database_cleaner_specs'
::MongoMapper.database = @test_db
end end
before(:each) do before do
::MongoMapper.connection.drop_database(@test_db) MongoMapperTest::Widget.new(name: 'some widget').save!
MongoMapperTest::Gadget.new(name: 'some gadget').save!
end end
def create_widget(attrs={}) context "by default" do
Widget.new({:name => 'some widget'}.merge(attrs)).save! it "truncates all collections" do
end expect { subject.clean }.to change {
[MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count]
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]
}.from([1,1]).to([0,0]) }.from([1,1]).to([0,0])
end end
end
context "when collections are provided to the :only option" do 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 it "only truncates the specified collections" do
create_widget expect { subject.clean }.to change {
create_gadget [MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count]
expect { Truncation.new(only: ['widgets']).clean }.to change {
[Widget.count, Gadget.count]
}.from([1,1]).to([0,1]) }.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 :except option" do
subject { described_class.new(except: ['mongo_mapper_test.widgets']) }
it "truncates all but the specified collections" do it "truncates all but the specified collections" do
create_widget expect { subject.clean }.to change {
create_gadget [MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count]
expect { Truncation.new(except: ['widgets']).clean }.to change {
[Widget.count, Gadget.count]
}.from([1,1]).to([1,0]) }.from([1,1]).to([1,0])
end end
end end
end end
end
end

View file

@ -1,5 +1,5 @@
module MopedTest module MopedTest
class ThingBase class Base
def self.collection def self.collection
@db ||= 'database_cleaner_specs' @db ||= 'database_cleaner_specs'
@session ||= ::Moped::Session.new(['127.0.0.1:27017'], database: @db) @session ||= ::Moped::Session.new(['127.0.0.1:27017'], database: @db)
@ -19,11 +19,11 @@ module MopedTest
end end
end end
class Widget < ThingBase class Widget < Base
end end
class Gadget < ThingBase class Gadget < Base
end end
class System < ThingBase class System < Base
def self.collection def self.collection
super super
@collection = @session['system_logs'] @collection = @session['system_logs']

View file

@ -2,71 +2,50 @@ require 'moped'
require 'database_cleaner/moped/truncation' require 'database_cleaner/moped/truncation'
require File.dirname(__FILE__) + '/moped_examples' require File.dirname(__FILE__) + '/moped_examples'
module DatabaseCleaner describe DatabaseCleaner::Moped::Truncation do
module Moped 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 example.run
let(:args) {{}}
let(:truncation) { described_class.new(args) } session.drop
#doing this in the file root breaks autospec, doing it before(:all) just fails the specs session.command(getlasterror: 1)
before(:all) do
@test_db = 'database_cleaner_specs'
@session = ::Moped::Session.new(['127.0.0.1:27017'], database: @test_db)
end end
before(:each) do before do
truncation.db = @test_db MopedTest::Widget.new(name: 'some widget').save!
MopedTest::Gadget.new(name: 'some gadget').save!
MopedTest::System.new(name: 'some system').save!
end end
after(:each) do context "by default" do
@session.drop it "truncates all collections" do
@session.command(getlasterror: 1) expect { subject.clean }.to change {
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 {
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
}.from([1,1,1]).to([0,0,0]) }.from([1,1,1]).to([0,0,0])
end end
end
context "when collections are provided to the :only option" do 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 it "only truncates the specified collections" do
create_widget expect { subject.clean }.to change {
create_gadget
expect { truncation.clean }.to change {
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] [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
end end
context "when collections are provided to the :except option" do 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 it "truncates all but the specified collections" do
create_widget expect { subject.clean }.to change {
create_gadget
expect { truncation.clean }.to change {
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] [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
end
end
end