Permalink
Browse files

adding tests to mongo strategy, removed reliance on monkeypatched mongo

  • Loading branch information...
1 parent 5bb6079 commit 4de28f6fc8a1aef61f0c508dcec3b64c8e3077e4 Christopher Darrell & Richard Luther committed Aug 10, 2012
View
9 lib/database_cleaner/mongo/base.rb
@@ -3,5 +3,14 @@ module Mongo
def self.available_strategies
%w[truncation]
end
+ module Base
+ def db=(desired_db)
+ @db = desired_db
+ end
+
+ def db
+ @db || raise("You have not specified a database. (see Mongo::Database)")
+ end
+ end
end
end
View
3 lib/database_cleaner/mongo/truncation.rb
@@ -6,10 +6,11 @@ module Mongo
class Truncation
include ::DatabaseCleaner::Generic::Truncation
include TruncationMixin
+ include Base
private
def database
- ::Mongo.connection
+ db
end
end
end
View
1 spec/database_cleaner/mongo/mongo_examples.rb
@@ -15,7 +15,6 @@ def initialize(attrs={})
end
def save!
- p self.class.collection
self.class.collection.insert(@attrs)
end
end
View
13 spec/database_cleaner/mongo/truncation_spec.rb
@@ -7,12 +7,13 @@ module DatabaseCleaner
module Mongo
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'
- @connection.db(@test_db)
+ @db = @connection.db(@test_db)
end
after(:each) do
@@ -39,26 +40,28 @@ def create_gadget(attrs={})
create_widget
create_gadget
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
- Truncation.new.clean
+ truncation.clean
ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 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
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
- Truncation.new(:only => ['widgets']).clean
+ truncation.clean
ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 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
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
- Truncation.new(:except => ['widgets']).clean
+ truncation.clean
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 0)
end
end

0 comments on commit 4de28f6

Please sign in to comment.