truncation support for CouchPotato / CouchDB

This commit is contained in:
Martin Rehfeld 2010-02-04 02:17:37 +01:00
parent 6ef3085bd4
commit efe6bfc9a7
9 changed files with 103 additions and 4 deletions

View File

@ -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.
ActiveRecord, DataMapper and MongoMapper are supported.
ActiveRecord, DataMapper, MongoMapper and CouchPotato are supported.
h2. How to use

View File

@ -19,5 +19,5 @@ if orm && strategy
DatabaseCleaner.strategy = strategy.to_sym
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

View 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

View File

@ -17,3 +17,4 @@ Feature: database cleaning
| DataMapper | transaction |
| DataMapper | truncation |
| MongoMapper | truncation |
| CouchPotato | truncation |

View File

@ -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
end

View File

@ -22,6 +22,12 @@ module DatabaseCleaner
end
end
module CouchPotato
def self.available_strategies
%w[truncation]
end
end
class << self
def create_strategy(*args)
@ -81,8 +87,10 @@ module DatabaseCleaner
'data_mapper'
elsif defined? ::MongoMapper
'mongo_mapper'
elsif defined? ::CouchPotato
'couch_potato'
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
@ -96,6 +104,8 @@ module DatabaseCleaner
DatabaseCleaner::DataMapper
when 'mongo_mapper'
DatabaseCleaner::MongoMapper
when 'couch_potato'
DatabaseCleaner::CouchPotato
end
end

View 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

View File

@ -55,6 +55,7 @@ describe DatabaseCleaner do
it "should raise an error when no ORM is detected" do
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
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)
end

View 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