Merge pull request #138 from sidereel/master

Added support for Mongo truncation without an ORM
This commit is contained in:
Ben Mabey 2012-08-25 09:51:22 -07:00
commit ad3285fe81
8 changed files with 152 additions and 18 deletions

View file

@ -19,6 +19,9 @@ Here is an overview of the strategies supported for each library:
| Mongoid | **Yes** | No | No |
| Sequel | **Yes** | Yes | No |
|_. Driver |_. Truncation |_. Transaction |_. Deletion |
| Mongo | Yes | No | No |
(Default strategy for each library is denoted in bold)
Database Cleaner also includes a @null@ strategy (that does no cleaning at all) which can be used

View file

@ -0,0 +1,16 @@
module DatabaseCleaner
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 file

@ -1,22 +1,17 @@
require 'database_cleaner/mongo/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation_mixin'
module DatabaseCleaner
module Mongo
module Truncation
def clean
if @only
collections.each { |c| c.remove if @only.include?(c.name) }
else
collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
end
true
end
class Truncation
include ::DatabaseCleaner::Generic::Truncation
include TruncationMixin
include Base
private
def collections
database.collections.select { |c| c.name !~ /^system\./ }
def database
db
end
end
end
end

View file

@ -0,0 +1,22 @@
module DatabaseCleaner
module Mongo
module TruncationMixin
def clean
if @only
collections.each { |c| c.remove if @only.include?(c.name) }
else
collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
end
true
end
private
def collections
database.collections.select { |c| c.name !~ /^system\./ }
end
end
end
end

View file

@ -1,13 +1,13 @@
require 'database_cleaner/mongo_mapper/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation'
require 'database_cleaner/mongo/truncation_mixin'
module DatabaseCleaner
module MongoMapper
class Truncation
include ::DatabaseCleaner::MongoMapper::Base
include ::DatabaseCleaner::Generic::Truncation
include ::DatabaseCleaner::Mongo::Truncation
include ::DatabaseCleaner::Mongo::TruncationMixin
private

View file

@ -1,6 +1,6 @@
require 'database_cleaner/mongoid/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation'
require 'database_cleaner/mongo/truncation_mixin'
require 'database_cleaner/moped/truncation'
require 'mongoid/version'
@ -12,7 +12,7 @@ module DatabaseCleaner
if ::Mongoid::VERSION < '3'
include ::DatabaseCleaner::Mongo::Truncation
include ::DatabaseCleaner::Mongo::TruncationMixin
private

View file

@ -0,0 +1,26 @@
module MongoTest
class ThingBase
def self.collection
@connection ||= ::Mongo::Connection.new('127.0.0.1')
@db ||= @connection.db('database_cleaner_specs')
@mongo ||= @db.collection(name) || @db.create_collection(name)
end
def self.count
@mongo.count
end
def initialize(attrs={})
@attrs = attrs
end
def save!
self.class.collection.insert(@attrs)
end
end
class Widget < ThingBase
end
class Gadget < ThingBase
end
end

View file

@ -0,0 +1,72 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'mongo'
require 'database_cleaner/mongo/truncation'
require File.dirname(__FILE__) + '/mongo_examples'
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'
@db = @connection.db(@test_db)
end
after(:each) do
@connection.drop_database(@test_db)
end
def ensure_counts(expected_counts)
# I had to add this sanity_check garbage because I was getting non-determinisc results from mongo at times..
# very odd and disconcerting...
expected_counts.each do |model_class, expected_count|
model_class.count.should equal(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{model_class.count}"
end
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
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
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.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.clean
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 0)
end
end
end
end
end