mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
add mongo_mapper support
This commit is contained in:
parent
0901eb2c64
commit
857c5cf93c
8 changed files with 54 additions and 11 deletions
|
@ -5,7 +5,7 @@ The original use case was to ensure a clean state during tests. Each strategy
|
|||
is a small amount of code but is code that is usually needed in any ruby app
|
||||
that is testing with a database.
|
||||
|
||||
Both ActiveRecord and DataMapper are supported.
|
||||
ActiveRecord, DataMapper and MongoMapper are supported.
|
||||
|
||||
h2. How to use
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Generated by jeweler
|
||||
# DO NOT EDIT THIS FILE DIRECTLY
|
||||
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
||||
# DO NOT EDIT THIS FILE
|
||||
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
|
@ -9,13 +9,12 @@ Gem::Specification.new do |s|
|
|||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["Ben Mabey"]
|
||||
s.date = %q{2009-12-20}
|
||||
s.date = %q{2009-12-23}
|
||||
s.description = %q{Strategies for cleaning databases. Can be used to ensure a clean state for testing.}
|
||||
s.email = %q{ben@benmabey.com}
|
||||
s.extra_rdoc_files = [
|
||||
"LICENSE",
|
||||
"README.textile",
|
||||
"TODO"
|
||||
"README.textile"
|
||||
]
|
||||
s.files = [
|
||||
"History.txt",
|
||||
|
@ -28,6 +27,7 @@ Gem::Specification.new do |s|
|
|||
"examples/features/support/env.rb",
|
||||
"examples/lib/activerecord.rb",
|
||||
"examples/lib/datamapper.rb",
|
||||
"examples/lib/mongomapper.rb",
|
||||
"features/cleaning.feature",
|
||||
"features/step_definitions/database_cleaner_steps.rb",
|
||||
"features/support/env.rb",
|
||||
|
@ -38,6 +38,7 @@ Gem::Specification.new do |s|
|
|||
"lib/database_cleaner/cucumber.rb",
|
||||
"lib/database_cleaner/data_mapper/transaction.rb",
|
||||
"lib/database_cleaner/data_mapper/truncation.rb",
|
||||
"lib/database_cleaner/mongo_mapper/truncation.rb",
|
||||
"lib/database_cleaner/truncation_base.rb",
|
||||
"spec/database_cleaner/active_record/truncation_spec.rb",
|
||||
"spec/database_cleaner/configuration_spec.rb",
|
||||
|
@ -56,7 +57,8 @@ Gem::Specification.new do |s|
|
|||
"examples/features/step_definitions/example_steps.rb",
|
||||
"examples/features/support/env.rb",
|
||||
"examples/lib/activerecord.rb",
|
||||
"examples/lib/datamapper.rb"
|
||||
"examples/lib/datamapper.rb",
|
||||
"examples/lib/mongomapper.rb"
|
||||
]
|
||||
|
||||
if s.respond_to? :specification_version then
|
||||
|
@ -69,4 +71,3 @@ Gem::Specification.new do |s|
|
|||
else
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -19,5 +19,5 @@ if orm && strategy
|
|||
DatabaseCleaner.strategy = strategy.to_sym
|
||||
|
||||
else
|
||||
raise "Run 'ORM=activerecord|datamapper STRATEGY=transaction|truncation cucumber examples/features'"
|
||||
raise "Run 'ORM=activerecord|datamapper|mongomapper STRATEGY=transaction|truncation cucumber examples/features'"
|
||||
end
|
||||
|
|
10
examples/lib/mongomapper.rb
Normal file
10
examples/lib/mongomapper.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require 'mongo_mapper'
|
||||
|
||||
::MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
|
||||
::MongoMapper.database = 'database_cleaner_test'
|
||||
|
||||
class Widget
|
||||
include MongoMapper::Document
|
||||
key :id, Integer
|
||||
key :name, String
|
||||
end
|
|
@ -15,4 +15,5 @@ Feature: database cleaning
|
|||
| ActiveRecord | transaction |
|
||||
| ActiveRecord | truncation |
|
||||
| DataMapper | transaction |
|
||||
| DataMapper | truncation |
|
||||
| DataMapper | truncation |
|
||||
| MongoMapper | truncation |
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
Given /^I am using (ActiveRecord|DataMapper)$/ do |orm|
|
||||
Given /^I am using (ActiveRecord|DataMapper|MongoMapper)$/ do |orm|
|
||||
@orm = orm
|
||||
end
|
||||
|
||||
|
|
|
@ -15,6 +15,12 @@ module DatabaseCleaner
|
|||
%w[truncation transaction]
|
||||
end
|
||||
end
|
||||
|
||||
module MongoMapper
|
||||
def self.available_strategies
|
||||
%w[truncation]
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
|
@ -73,6 +79,8 @@ module DatabaseCleaner
|
|||
'active_record'
|
||||
elsif defined? ::DataMapper
|
||||
'data_mapper'
|
||||
elsif defined? ::MongoMapper
|
||||
'mongo_mapper'
|
||||
else
|
||||
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord or DataMapper loaded?"
|
||||
end
|
||||
|
@ -86,6 +94,8 @@ module DatabaseCleaner
|
|||
DatabaseCleaner::ActiveRecord
|
||||
when 'data_mapper'
|
||||
DatabaseCleaner::DataMapper
|
||||
when 'mongo_mapper'
|
||||
DatabaseCleaner::MongoMapper
|
||||
end
|
||||
end
|
||||
|
||||
|
|
21
lib/database_cleaner/mongo_mapper/truncation.rb
Normal file
21
lib/database_cleaner/mongo_mapper/truncation.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
require 'database_cleaner/truncation_base'
|
||||
|
||||
module DatabaseCleaner
|
||||
module MongoMapper
|
||||
class Truncation < DatabaseCleaner::TruncationBase
|
||||
def clean
|
||||
connection.db(database).collections.each { |c| c.remove }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
::MongoMapper.connection
|
||||
end
|
||||
|
||||
def database
|
||||
::MongoMapper.database.name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue