mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
truncation support for CouchPotato / CouchDB
This commit is contained in:
parent
6ef3085bd4
commit
efe6bfc9a7
9 changed files with 103 additions and 4 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
|
is a small amount of code but is code that is usually needed in any ruby app
|
||||||
that is testing with a database.
|
that is testing with a database.
|
||||||
|
|
||||||
ActiveRecord, DataMapper and MongoMapper are supported.
|
ActiveRecord, DataMapper, MongoMapper and CouchPotato are supported.
|
||||||
|
|
||||||
h2. How to use
|
h2. How to use
|
||||||
|
|
||||||
|
|
|
@ -19,5 +19,5 @@ if orm && strategy
|
||||||
DatabaseCleaner.strategy = strategy.to_sym
|
DatabaseCleaner.strategy = strategy.to_sym
|
||||||
|
|
||||||
else
|
else
|
||||||
raise "Run 'ORM=activerecord|datamapper|mongomapper STRATEGY=transaction|truncation cucumber examples/features'"
|
raise "Run 'ORM=activerecord|datamapper|mongomapper|couchpotato STRATEGY=transaction|truncation cucumber examples/features'"
|
||||||
end
|
end
|
||||||
|
|
21
examples/lib/couchpotato.rb
Normal file
21
examples/lib/couchpotato.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
require 'couch_potato'
|
||||||
|
|
||||||
|
::CouchPotato::Config.database_name = 'couch_potato_test'
|
||||||
|
|
||||||
|
class Widget
|
||||||
|
include CouchPotato::Persistence
|
||||||
|
|
||||||
|
property :name
|
||||||
|
view :by_name, :key => :name
|
||||||
|
|
||||||
|
|
||||||
|
# mimic the AR interface used in example_steps
|
||||||
|
|
||||||
|
def self.create!(attrs = {})
|
||||||
|
CouchPotato.database.save(self.new)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.count
|
||||||
|
CouchPotato.database.view(::Widget.by_name).size
|
||||||
|
end
|
||||||
|
end
|
|
@ -17,3 +17,4 @@ Feature: database cleaning
|
||||||
| DataMapper | transaction |
|
| DataMapper | transaction |
|
||||||
| DataMapper | truncation |
|
| DataMapper | truncation |
|
||||||
| MongoMapper | truncation |
|
| MongoMapper | truncation |
|
||||||
|
| CouchPotato | truncation |
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Given /^I am using (ActiveRecord|DataMapper|MongoMapper)$/ do |orm|
|
Given /^I am using (ActiveRecord|DataMapper|MongoMapper|CouchPotato)$/ do |orm|
|
||||||
@orm = orm
|
@orm = orm
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,12 @@ module DatabaseCleaner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module CouchPotato
|
||||||
|
def self.available_strategies
|
||||||
|
%w[truncation]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def create_strategy(*args)
|
def create_strategy(*args)
|
||||||
|
@ -81,8 +87,10 @@ module DatabaseCleaner
|
||||||
'data_mapper'
|
'data_mapper'
|
||||||
elsif defined? ::MongoMapper
|
elsif defined? ::MongoMapper
|
||||||
'mongo_mapper'
|
'mongo_mapper'
|
||||||
|
elsif defined? ::CouchPotato
|
||||||
|
'couch_potato'
|
||||||
else
|
else
|
||||||
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, or MongoMapper loaded?"
|
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper or CouchPotato loaded?"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -96,6 +104,8 @@ module DatabaseCleaner
|
||||||
DatabaseCleaner::DataMapper
|
DatabaseCleaner::DataMapper
|
||||||
when 'mongo_mapper'
|
when 'mongo_mapper'
|
||||||
DatabaseCleaner::MongoMapper
|
DatabaseCleaner::MongoMapper
|
||||||
|
when 'couch_potato'
|
||||||
|
DatabaseCleaner::CouchPotato
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
26
lib/database_cleaner/couch_potato/truncation.rb
Normal file
26
lib/database_cleaner/couch_potato/truncation.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
require 'database_cleaner/truncation_base'
|
||||||
|
|
||||||
|
module DatabaseCleaner
|
||||||
|
module CouchPotato
|
||||||
|
class Truncation < DatabaseCleaner::TruncationBase
|
||||||
|
def initialize(options = {})
|
||||||
|
if options.has_key?(:only) || options.has_key?(:except)
|
||||||
|
raise ArgumentError, "The :only and :except options are not available for use with CouchPotato/CouchDB."
|
||||||
|
elsif !options.empty?
|
||||||
|
raise ArgumentError, "Unsupported option. You specified #{options.keys.join(',')}."
|
||||||
|
end
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def clean
|
||||||
|
database.recreate!
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def database
|
||||||
|
::CouchPotato.couchrest_database
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -55,6 +55,7 @@ describe DatabaseCleaner do
|
||||||
it "should raise an error when no ORM is detected" do
|
it "should raise an error when no ORM is detected" do
|
||||||
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
|
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
|
||||||
Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
|
Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
|
||||||
|
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
|
||||||
|
|
||||||
running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected)
|
running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected)
|
||||||
end
|
end
|
||||||
|
|
40
spec/database_cleaner/couch_potato/truncation_spec.rb
Normal file
40
spec/database_cleaner/couch_potato/truncation_spec.rb
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
||||||
|
require 'database_cleaner/couch_potato/truncation'
|
||||||
|
require 'couch_potato'
|
||||||
|
|
||||||
|
module DatabaseCleaner
|
||||||
|
module CouchPotato
|
||||||
|
|
||||||
|
describe Truncation do
|
||||||
|
before(:each) do
|
||||||
|
@database = mock('database')
|
||||||
|
::CouchPotato.stub!(:couchrest_database).and_return(@database)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should re-create the database" do
|
||||||
|
@database.should_receive(:recreate!)
|
||||||
|
|
||||||
|
Truncation.new.clean
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an error when the :only option is used" do
|
||||||
|
running {
|
||||||
|
Truncation.new(:only => ['document-type'])
|
||||||
|
}.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an error when the :except option is used" do
|
||||||
|
running {
|
||||||
|
Truncation.new(:except => ['document-type'])
|
||||||
|
}.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an error when invalid options are provided" do
|
||||||
|
running {
|
||||||
|
Truncation.new(:foo => 'bar')
|
||||||
|
}.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue