From efe6bfc9a73efd89a467cc331cf384b80f968786 Mon Sep 17 00:00:00 2001 From: Martin Rehfeld Date: Thu, 4 Feb 2010 02:17:37 +0100 Subject: [PATCH] truncation support for CouchPotato / CouchDB --- README.textile | 2 +- examples/features/support/env.rb | 2 +- examples/lib/couchpotato.rb | 21 ++++++++++ features/cleaning.feature | 1 + .../database_cleaner_steps.rb | 2 +- lib/database_cleaner/configuration.rb | 12 +++++- .../couch_potato/truncation.rb | 26 ++++++++++++ spec/database_cleaner/configuration_spec.rb | 1 + .../couch_potato/truncation_spec.rb | 40 +++++++++++++++++++ 9 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 examples/lib/couchpotato.rb create mode 100644 lib/database_cleaner/couch_potato/truncation.rb create mode 100644 spec/database_cleaner/couch_potato/truncation_spec.rb diff --git a/README.textile b/README.textile index d2a4b97..7742f28 100644 --- a/README.textile +++ b/README.textile @@ -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 diff --git a/examples/features/support/env.rb b/examples/features/support/env.rb index d9fd88a..ea9fe57 100644 --- a/examples/features/support/env.rb +++ b/examples/features/support/env.rb @@ -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 diff --git a/examples/lib/couchpotato.rb b/examples/lib/couchpotato.rb new file mode 100644 index 0000000..665c86a --- /dev/null +++ b/examples/lib/couchpotato.rb @@ -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 diff --git a/features/cleaning.feature b/features/cleaning.feature index 6c4088c..d527d4a 100644 --- a/features/cleaning.feature +++ b/features/cleaning.feature @@ -17,3 +17,4 @@ Feature: database cleaning | DataMapper | transaction | | DataMapper | truncation | | MongoMapper | truncation | + | CouchPotato | truncation | diff --git a/features/step_definitions/database_cleaner_steps.rb b/features/step_definitions/database_cleaner_steps.rb index ce2ce7a..59ed8ab 100644 --- a/features/step_definitions/database_cleaner_steps.rb +++ b/features/step_definitions/database_cleaner_steps.rb @@ -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 diff --git a/lib/database_cleaner/configuration.rb b/lib/database_cleaner/configuration.rb index ba892c9..078b3e9 100644 --- a/lib/database_cleaner/configuration.rb +++ b/lib/database_cleaner/configuration.rb @@ -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 diff --git a/lib/database_cleaner/couch_potato/truncation.rb b/lib/database_cleaner/couch_potato/truncation.rb new file mode 100644 index 0000000..6b61bdc --- /dev/null +++ b/lib/database_cleaner/couch_potato/truncation.rb @@ -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 diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index 90d4624..76b6fca 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -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 diff --git a/spec/database_cleaner/couch_potato/truncation_spec.rb b/spec/database_cleaner/couch_potato/truncation_spec.rb new file mode 100644 index 0000000..0a6d175 --- /dev/null +++ b/spec/database_cleaner/couch_potato/truncation_spec.rb @@ -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