Skip to content

Commit

Permalink
Merge pull request #194 from iRonin/master
Browse files Browse the repository at this point in the history
Added support for Moped (when used without Mongoid)
  • Loading branch information
bmabey committed Mar 19, 2013
2 parents 8002b46 + 7762d2f commit e12f473
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 32 deletions.
11 changes: 11 additions & 0 deletions README.markdown
Expand Up @@ -72,6 +72,12 @@ Here is an overview of the strategies supported for each library:
<td> No</td>
<td> No</td>
</tr>
<tr>
<td> Moped</td>
<td> Yes</td>
<td> No</td>
<td> No</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -276,6 +282,11 @@ Usage beyond that remains the same with `DatabaseCleaner.start` calling any setu
<td> <code>DatabaseCleaner[:mongoid]</code></td>
<td> Multiple databases supported for Mongoid 3. Specify <code>DatabaseCleaner[:mongoid, {:connection =&gt; :db_name}]</code> </td>
</tr>
<tr>
<td> Moped</td>
<td> <code>DatabaseCleaner[:moped]</code></td>
<td> It is necessary to configure database name with <code>DatabaseCleaner[:moped].db = db_name</code> otherwise name `default` will be used.</td>
</tr>
<tr>
<td> Couch Potato</td>
<td> <code>DatabaseCleaner[:couch_potato]</code></td>
Expand Down
6 changes: 4 additions & 2 deletions lib/database_cleaner/base.rb
Expand Up @@ -120,8 +120,10 @@ def autodetect
:couch_potato
elsif defined? ::Sequel
:sequel
elsif defined? ::Moped
:moped
else
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, or CouchPotato loaded?"
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato loaded?"
end
end
end
Expand All @@ -130,7 +132,7 @@ def set_default_orm_strategy
case orm
when :active_record, :data_mapper, :sequel
self.strategy = :transaction
when :mongo_mapper, :mongoid, :couch_potato
when :mongo_mapper, :mongoid, :couch_potato, :moped
self.strategy = :truncation
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/database_cleaner/mongoid/truncation.rb
@@ -1,7 +1,7 @@
require 'database_cleaner/mongoid/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation_mixin'
require 'database_cleaner/moped/truncation'
require 'database_cleaner/moped/truncation_base'
require 'mongoid/version'

module DatabaseCleaner
Expand All @@ -22,7 +22,7 @@ def database

else

include ::DatabaseCleaner::Moped::Truncation
include ::DatabaseCleaner::Moped::TruncationBase

private

Expand Down
35 changes: 35 additions & 0 deletions lib/database_cleaner/moped/base.rb
@@ -0,0 +1,35 @@
require 'database_cleaner/generic/base'

module DatabaseCleaner
module Moped
def self.available_strategies
%w[truncation]
end

module Base
include ::DatabaseCleaner::Generic::Base

def db=(desired_db)
@db = desired_db
end

def db
@db || :default
end

def host_port=(desired_host)
@host = desired_host
end

def host
@host || '127.0.0.1:27017'
end

private

def session
::Moped::Session.new([host], database: db)
end
end
end
end
28 changes: 4 additions & 24 deletions lib/database_cleaner/moped/truncation.rb
@@ -1,29 +1,9 @@
require 'database_cleaner/moped/truncation_base'

module DatabaseCleaner
module Moped
module Truncation

def clean
if @only
collections.each { |c| session[c].find.remove_all if @only.include?(c) }
else
collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
end
true
end

private

def collections
if db != :default
session.use(db)
end

session['system.namespaces'].find(:name => { '$not' => /system|\$/ }).to_a.map do |collection|
_, name = collection['name'].split('.', 2)
name
end
end

class Truncation
include ::DatabaseCleaner::Moped::TruncationBase
end
end
end
34 changes: 34 additions & 0 deletions lib/database_cleaner/moped/truncation_base.rb
@@ -0,0 +1,34 @@
require 'database_cleaner/moped/base'
require 'database_cleaner/generic/truncation'

module DatabaseCleaner
module Moped
module TruncationBase
include ::DatabaseCleaner::Moped::Base
include ::DatabaseCleaner::Generic::Truncation

def clean
if @only
collections.each { |c| session[c].find.remove_all if @only.include?(c) }
else
collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
end
true
end

private

def collections
if db != :default
session.use(db)
end

session['system.namespaces'].find(:name => { '$not' => /system|\$/ }).to_a.map do |collection|
_, name = collection['name'].split('.', 2)
name
end
end

end
end
end
29 changes: 25 additions & 4 deletions spec/database_cleaner/base_spec.rb
Expand Up @@ -18,6 +18,7 @@ module DatabaseCleaner
Temp_MO = ::Mongoid if defined?(::Mongoid) and not defined?(Temp_MO)
Temp_CP = ::CouchPotato if defined?(::CouchPotato) and not defined?(Temp_CP)
Temp_SQ = ::Sequel if defined?(::Sequel) and not defined?(Temp_SQ)
Temp_MP = ::Moped if defined?(::Moped) and not defined?(Temp_MP)
end

#Remove all ORM mocks and restore from cache
Expand All @@ -28,6 +29,7 @@ module DatabaseCleaner
Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
Object.send(:remove_const, 'Moped') if defined?(::Moped)


# Restore ORMs
Expand All @@ -36,6 +38,7 @@ module DatabaseCleaner
::MongoMapper = Temp_MM if defined? Temp_MM
::Mongoid = Temp_MO if defined? Temp_MO
::CouchPotato = Temp_CP if defined? Temp_CP
::Moped = Temp_MP if defined? Temp_MP
end

#reset the orm mocks
Expand All @@ -46,6 +49,7 @@ module DatabaseCleaner
Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
Object.send(:remove_const, 'Moped') if defined?(::Moped)
end

let(:cleaner) { DatabaseCleaner::Base.new :autodetect }
Expand All @@ -61,6 +65,7 @@ module DatabaseCleaner
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :active_record
cleaner.should be_auto_detected
Expand All @@ -72,6 +77,7 @@ module DatabaseCleaner
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :data_mapper
cleaner.should be_auto_detected
Expand All @@ -82,6 +88,7 @@ module DatabaseCleaner
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :mongo_mapper
cleaner.should be_auto_detected
Expand All @@ -91,6 +98,7 @@ module DatabaseCleaner
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :mongoid
cleaner.should be_auto_detected
Expand All @@ -99,17 +107,26 @@ module DatabaseCleaner
it "should detect CouchPotato fifth" do
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :couch_potato
cleaner.should be_auto_detected
end
it "should detect Sequel last" do

it "should detect Sequel sixth" do
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :sequel
cleaner.should be_auto_detected
end

it "should detect Moped seventh" do
Object.const_set('Moped', 'Moped mock')

cleaner.orm.should == :moped
cleaner.should be_auto_detected
end
end

describe "orm_module" do
Expand Down Expand Up @@ -330,8 +347,7 @@ module DatabaseCleaner
describe "strategy" do
subject { ::DatabaseCleaner::Base.new :a_orm }

it "returns a null strategy when strategy no set and undetectable" do
subject.instance_values["@strategy"] = nil
it "returns a null strategy when strategy is not set and undetectable" do
subject.strategy.should == DatabaseCleaner::NullStrategy
end

Expand Down Expand Up @@ -487,6 +503,11 @@ module DatabaseCleaner
cleaner = DatabaseCleaner::Base.new(:couch_potato)
cleaner.strategy.should be_instance_of DatabaseCleaner::CouchPotato::Truncation
end

it 'sets strategy to :truncation for Moped' do
cleaner = DatabaseCleaner::Base.new(:moped)
cleaner.strategy.should be_instance_of DatabaseCleaner::Moped::Truncation
end
end

end
Expand Down
7 changes: 7 additions & 0 deletions spec/database_cleaner/configuration_spec.rb
Expand Up @@ -55,6 +55,13 @@ def connections_stub!(array)
cleaner.orm.should == :couch_potato
::DatabaseCleaner.connections.size.should == 1
end

it "should accept :moped" do
cleaner = ::DatabaseCleaner[:moped]
cleaner.should be_a(::DatabaseCleaner::Base)
cleaner.orm.should == :moped
::DatabaseCleaner.connections.size.should == 1
end
end

it "should accept multiple orm's" do
Expand Down
26 changes: 26 additions & 0 deletions spec/database_cleaner/moped/moped_examples.rb
@@ -0,0 +1,26 @@
module MopedTest
class ThingBase
def self.collection
@db ||= 'database_cleaner_specs'
@session ||= ::Moped::Session.new(['127.0.0.1:27017'], database: @db)
@collection ||= @session[name]
end

def self.count
@collection.find.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

0 comments on commit e12f473

Please sign in to comment.