diff --git a/spec/database_cleaner/mongo/mongo_examples.rb b/spec/database_cleaner/mongo/mongo_examples.rb index d6fc4de..0360294 100644 --- a/spec/database_cleaner/mongo/mongo_examples.rb +++ b/spec/database_cleaner/mongo/mongo_examples.rb @@ -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 diff --git a/spec/database_cleaner/mongo/truncation_spec.rb b/spec/database_cleaner/mongo/truncation_spec.rb index 1ac3ab5..922648d 100644 --- a/spec/database_cleaner/mongo/truncation_spec.rb +++ b/spec/database_cleaner/mongo/truncation_spec.rb @@ -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) - end + example.run - after(:each) do - @connection.drop_database(@test_db) - end + connection.drop_database(db_name) + 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 { - [MongoTest::Widget.count, MongoTest::Gadget.count] - }.from([1,1]).to([0,0]) - end - - context "when collections are provided to the :only option" do - let(:args) {{:only => ['MongoTest::Widget']}} - it "only truncates the specified collections" do - create_widget - create_gadget - expect { truncation.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']}} - it "truncates all but the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MongoTest::Widget.count, MongoTest::Gadget.count] - }.from([1,1]).to([1,0]) - end - end + before do + MongoTest::Widget.new(name: 'some widget').save! + MongoTest::Gadget.new(name: 'some gadget').save! + end + 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 + 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 + diff --git a/spec/database_cleaner/mongo_mapper/mongo_examples.rb b/spec/database_cleaner/mongo_mapper/mongo_examples.rb index 44637ea..f0b8a73 100644 --- a/spec/database_cleaner/mongo_mapper/mongo_examples.rb +++ b/spec/database_cleaner/mongo_mapper/mongo_examples.rb @@ -1,8 +1,10 @@ -class Widget - include ::MongoMapper::Document - key :name, String -end -class Gadget - include ::MongoMapper::Document - key :name, String +module MongoMapperTest + class Widget + include ::MongoMapper::Document + key :name, String + end + class Gadget + include ::MongoMapper::Document + key :name, String + end end diff --git a/spec/database_cleaner/mongo_mapper/truncation_spec.rb b/spec/database_cleaner/mongo_mapper/truncation_spec.rb index e65207b..211d3b6 100644 --- a/spec/database_cleaner/mongo_mapper/truncation_spec.rb +++ b/spec/database_cleaner/mongo_mapper/truncation_spec.rb @@ -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 - end + MongoMapper.connection.drop_database(db_name) + end - before(:each) do - ::MongoMapper.connection.drop_database(@test_db) - 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] - }.from([1,1]).to([0,0]) - end - - context "when collections are provided to the :only option" do - it "only truncates the specified collections" do - create_widget - create_gadget - expect { Truncation.new(only: ['widgets']).clean }.to change { - [Widget.count, Gadget.count] - }.from([1,1]).to([0,1]) - end - end - - context "when collections are provided to the :except option" do - it "truncates all but the specified collections" do - create_widget - create_gadget - expect { Truncation.new(except: ['widgets']).clean }.to change { - [Widget.count, Gadget.count] - }.from([1,1]).to([1,0]) - end - end + before do + MongoMapperTest::Widget.new(name: 'some widget').save! + MongoMapperTest::Gadget.new(name: 'some gadget').save! + end + 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 + 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 + expect { subject.clean }.to change { + [MongoMapperTest::Widget.count, MongoMapperTest::Gadget.count] + }.from([1,1]).to([1,0]) + end end end + diff --git a/spec/database_cleaner/moped/moped_examples.rb b/spec/database_cleaner/moped/moped_examples.rb index fe8a96d..a144850 100644 --- a/spec/database_cleaner/moped/moped_examples.rb +++ b/spec/database_cleaner/moped/moped_examples.rb @@ -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'] diff --git a/spec/database_cleaner/moped/truncation_spec.rb b/spec/database_cleaner/moped/truncation_spec.rb index 956fc67..08b1c58 100644 --- a/spec/database_cleaner/moped/truncation_spec.rb +++ b/spec/database_cleaner/moped/truncation_spec.rb @@ -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) - end + example.run - before(:each) do - truncation.db = @test_db - end + session.drop + session.command(getlasterror: 1) + 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 { - [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] - }.from([1,1,1]).to([0,0,0]) - end - - context "when collections are provided to the :only option" do - let(:args) {{:only => ['MopedTest::Widget']}} - it "only truncates the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] - }.from([1,1,0]).to([0,1,0]) - end - end - - context "when collections are provided to the :except option" do - let(:args) {{:except => ['MopedTest::Widget']}} - it "truncates all but the specified collections" do - create_widget - create_gadget - expect { truncation.clean }.to change { - [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] - }.from([1,1,0]).to([1,0,0]) - end - end + before do + MopedTest::Widget.new(name: 'some widget').save! + MopedTest::Gadget.new(name: 'some gadget').save! + MopedTest::System.new(name: 'some system').save! + end + 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 + subject { described_class.new(only: ['MopedTest::Widget']) } + + it "only truncates the specified collections" do + expect { subject.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,1]).to([0,1,1]) + end + end + + context "when collections are provided to the :except option" do + subject { described_class.new(except: ['MopedTest::Widget']) } + + it "truncates all but the specified collections" do + expect { subject.clean }.to change { + [MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count] + }.from([1,1,1]).to([1,0,0]) + end end end +