update steps and features into a cleaner fashion

This commit is contained in:
Jon Rowe 2010-05-30 19:29:42 +01:00
parent b7fed145df
commit a2b97f3a5b
15 changed files with 320 additions and 88 deletions

View File

@ -0,0 +1,23 @@
Feature: example
In order to test DataBase Cleaner
Here are some scenarios that rely on the DB being clean!
Background:
Given I have setup database cleaner to clean multiple databases using datamapper
Scenario: dirty the db
When I create a widget in one db using datamapper
And I create a widget in another db using datamapper
Then I should see 1 widget in one db using datamapper
And I should see 1 widget in another db using datamapper
Scenario: assume a clean db
When I create a widget in one db using datamapper
Then I should see 1 widget in one db using datamapper
And I should see 0 widget in another db using datamapper
Scenario: assume a clean db
When I create a widget in another db using datamapper
Then I should see 0 widget in one db using datamapper
And I should see 1 widget in another db using datamapper

View File

@ -0,0 +1,31 @@
Given /^I have setup database cleaner to clean multiple databases using activerecord$/ do
#DatabaseCleaner
# require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
#
# DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
# DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
end
When /^I create a widget using activerecord$/ do
ActiveRecordWidget.create!
end
Then /^I should see ([\d]+) widget using activerecord$/ do |widget_count|
ActiveRecordWidget.count.should == widget_count.to_i
end
When /^I create a widget in one db using activerecord$/ do
ActiveRecordWidgetUsingDatabaseOne.create!
end
When /^I create a widget in another db using activerecord$/ do
ActiveRecordWidgetUsingDatabaseTwo.create!
end
Then /^I should see ([\d]+) widget in one db using activerecord$/ do |widget_count|
ActiveRecordWidgetUsingDatabaseOne.count.should == widget_count.to_i
end
Then /^I should see ([\d]+) widget in another db using activerecord$/ do |widget_count|
ActiveRecordWidgetUsingDatabaseTwo.count.should == widget_count.to_i
end

View File

@ -0,0 +1,31 @@
Given /^I have setup database cleaner to clean multiple databases using couchpotato$/ do
#DatabaseCleaner
# require "#{File.dirname(__FILE__)}/../../../lib/couchpotato_models"
#
# DatabaseCleaner[:couchpotato, {:connection => :one} ].strategy = :truncation
# DatabaseCleaner[:couchpotato, {:connection => :two} ].strategy = :truncation
end
When /^I create a widget using couchpotato$/ do
CouchPotatoWidget.create!
end
Then /^I should see ([\d]+) widget using couchpotato$/ do |widget_count|
CouchPotatoWidget.count.should == widget_count.to_i
end
When /^I create a widget in one db using couchpotato$/ do
CouchPotatoWidgetUsingDatabaseOne.create!
end
When /^I create a widget in another db using couchpotato$/ do
CouchPotatoWidgetUsingDatabaseTwo.create!
end
Then /^I should see ([\d]+) widget in one db using couchpotato$/ do |widget_count|
CouchPotatoWidgetUsingDatabaseOne.count.should == widget_count.to_i
end
Then /^I should see ([\d]+) widget in another db using couchpotato$/ do |widget_count|
CouchPotatoWidgetUsingDatabaseTwo.count.should == widget_count.to_i
end

View File

@ -0,0 +1,31 @@
Given /^I have setup database cleaner to clean multiple databases using datamapper$/ do
#DatabaseCleaner
# require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
#
# DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
# DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
end
When /^I create a widget using datamapper$/ do
DataMapperWidget.create!
end
Then /^I should see ([\d]+) widget using datamapper$/ do |widget_count|
DataMapperWidget.count.should == widget_count.to_i
end
When /^I create a widget in one db using datamapper$/ do
DataMapperWidgetUsingDatabaseOne.create!
end
When /^I create a widget in another db using datamapper$/ do
DataMapperWidgetUsingDatabaseTwo.create!
end
Then /^I should see ([\d]+) widget in one db using datamapper$/ do |widget_count|
DataMapperWidgetUsingDatabaseOne.count.should == widget_count.to_i
end
Then /^I should see ([\d]+) widget in another db using datamapper$/ do |widget_count|
DataMapperWidgetUsingDatabaseTwo.count.should == widget_count.to_i
end

View File

@ -1,23 +0,0 @@
Given /^I have setup databasecleaner to clean multiple dbs$/ do
DatabaseCleaner
end
When /^I create a widget in one db$/ do
Widget.establish_connection_one
Widget.create!
end
When /^I create a widget in another db$/ do
Widget.establish_connection_two
Widget.create!
end
Then /^I should see ([\d]+) widget in one db$/ do |widget_count|
Widget.establish_connection_one
Widget.count.should == widget_count.to_i
end
Then /^I should see ([\d]+) widget in another db$/ do |widget_count|
Widget.establish_connection_two
Widget.count.should == widget_count.to_i
end

View File

@ -1,15 +0,0 @@
When /^I create a widget in one orm$/ do
Widget.create!
end
When /^I create a widget in another orm$/ do
AnotherWidget.create!
end
Then /^I should see ([\d]+) widget in one orm$/ do |widget_count|
Widget.count.should == widget_count.to_i
end
Then /^I should see ([\d]+) widget in another orm$/ do |widget_count|
AnotherWidget.count.should == widget_count.to_i
end

View File

@ -1,7 +0,0 @@
When /^I create a widget$/ do
Widget.create!
end
Then /^I should see 1 widget$/ do
Widget.count.should == 1
end

View File

@ -0,0 +1,31 @@
Given /^I have setup database cleaner to clean multiple databases using mongomapper$/ do
#DatabaseCleaner
# require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
#
# DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
# DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
end
When /^I create a widget using mongomapper$/ do
MongoMapperWidget.create!
end
Then /^I should see ([\d]+) widget using mongomapper$/ do |widget_count|
MongoMapperWidget.count.should == widget_count.to_i
end
When /^I create a widget in one db using mongomapper$/ do
MongoMapperWidgetUsingDatabaseOne.create!
end
When /^I create a widget in another db using mongomapper$/ do
MongoMapperWidgetUsingDatabaseTwo.create!
end
Then /^I should see ([\d]+) widget in one db using mongomapper$/ do |widget_count|
MongoMapperWidgetUsingDatabaseOne.count.should == widget_count.to_i
end
Then /^I should see ([\d]+) widget in another db using mongomapper$/ do |widget_count|
MongoMapperWidgetUsingDatabaseTwo.count.should == widget_count.to_i
end

View File

@ -0,0 +1,55 @@
When /^I create a widget$/ do
When "I create a widget using #{ENV['ORM'].downcase}"
end
Then /^I should see 1 widget$/ do
Then "I should see 1 widget using #{ENV['ORM'].downcase}"
end
When /^I create a widget in one orm$/ do
When "I create a widget using #{ENV['ORM'].downcase}"
end
When /^I create a widget in another orm$/ do
When "I create a widget using #{ENV['ANOTHER_ORM'].downcase}"
end
Then /^I should see 1 widget in one orm$/ do
When "I should see 1 widget using #{ENV['ORM'].downcase}"
end
Then /^I should see 1 widget in another orm$/ do
When "I should see 1 widget using #{ENV['ANOTHER_ORM'].downcase}"
end
Then /^I should see 0 widget in another orm$/ do
When "I should see 0 widget using #{ENV['ANOTHER_ORM'].downcase}"
end
Then /^I should see 0 widget in one orm$/ do
When "I should see 0 widget using #{ENV['ORM'].downcase}"
end
When /^I create a widget in one db$/ do
When "I create a widget in one db using #{ENV['ORM'].downcase}"
end
When /^I create a widget in another db$/ do
When "I create a widget in another db using #{ENV['ORM'].downcase}"
end
Then /^I should see 1 widget in one db$/ do
When "I should see 1 widget in one db using #{ENV['ORM'].downcase}"
end
Then /^I should see 1 widget in another db$/ do
When "I should see 1 widget in another db using #{ENV['ORM'].downcase}"
end
Then /^I should see 0 widget in another db$/ do
When "I should see 0 widget in another db using #{ENV['ORM'].downcase}"
end
Then /^I should see 0 widget in one db$/ do
When "I should see 0 widget in one db using #{ENV['ORM'].downcase}"
end

View File

@ -10,20 +10,22 @@ another_orm = ENV['ANOTHER_ORM']
strategy = ENV['STRATEGY']
multiple_db = ENV['MULTIPLE_DBS']
if orm && strategy
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
require 'database_cleaner'
require 'database_cleaner/cucumber'
begin
require "#{File.dirname(__FILE__)}/../../lib/#{orm}_models"
require "#{File.dirname(__FILE__)}/../../lib/#{orm.downcase}_models"
rescue LoadError => e
raise "You don't have the #{orm} ORM installed"
end
if another_orm
begin
require "#{File.dirname(__FILE__)}/../../lib/#{another_orm}_models"
require "#{File.dirname(__FILE__)}/../../lib/#{another_orm.downcase}_models"
rescue LoadError => e
raise "You don't have the #{another_orm} ORM installed"
end
@ -35,6 +37,9 @@ if orm && strategy
if multiple_db && another_orm
DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :one} ].strategy = strategy.to_sym
DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :two} ].strategy = strategy.to_sym
elsif multiple_db
DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :one} ].strategy = strategy.to_sym
DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :two} ].strategy = strategy.to_sym
elsif another_orm
DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym
DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym

View File

@ -2,36 +2,32 @@ require 'active_record'
["two","one"].each do |db|
ActiveRecord::Base.establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_#{db}.db")
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "widgets"')
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "another_widgets"')
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widgets"')
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widget_using_database_ones"')
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widget_using_database_twos"')
ActiveRecord::Schema.define(:version => 1) do
create_table :widgets do |t|
create_table :active_record_widgets do |t|
t.string :name
end
create_table :another_widgets do |t|
create_table :active_record_widget_usings_database_ones do |t|
t.string :name
end
create_table :active_record_widget_usings_database_twos do |t|
t.string :name
end
end
end
class ActiveRecordWidgetBase < ActiveRecord::Base
def self.establish_connection_one
establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_one.db")
end
def self.establish_connection_two
establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_two.db")
end
class ActiveRecordWidget < ActiveRecord::Base
end
unless defined? Widget
class Widget < ActiveRecordWidgetBase
set_table_name 'widgets'
end
else
class AnotherWidget < ActiveRecordWidgetBase
set_table_name 'another_widgets'
end
class ActiveRecordWidgetUsingDatabaseOne < ActiveRecord::Base
establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_one.db")
end
class ActiveRecordWidgetUsingDatabaseTwo < ActiveRecord::Base
establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_two.db")
end

View File

@ -2,7 +2,7 @@ require 'couch_potato'
require 'json/pure'
::CouchPotato::Config.database_name = 'couch_potato_test'
class CouchWidget
class CouchPotatoWidget
include CouchPotato::Persistence
property :name
@ -20,10 +20,42 @@ class CouchWidget
end
end
unless defined? Widget
class Widget < CouchWidget
class CouchPotatoWidgetUsingDatabaseOne
include CouchPotato::Persistence
database_name = 'couch_potato_test_one'
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
else
class AnotherWidget < CouchWidget
def self.count
CouchPotato.database.view(self.by_name).size
end
end
class CouchPotatoWidgetUsingDatabaseTwo
include CouchPotato::Persistence
database_name = 'couch_potato_test_two'
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(self.by_name).size
end
end

View File

@ -6,22 +6,40 @@ require "dm-validations"
require "dm-aggregates"
DataMapper.setup(:default, "sqlite3:#{DB_DIR}/datamapper_one.db")
DataMapper.setup(:one, "sqlite3:#{DB_DIR}/datamapper_one.db")
DataMapper.setup(:two, "sqlite3:#{DB_DIR}/datamapper_two.db")
class MapperWidget
class DataMapperWidget
include DataMapper::Resource
property :id, Serial
property :name, String
end
class DataMapperWidgetUsingDatabaseOne
include DataMapper::Resource
unless defined? Widget
class Widget < MapperWidget
def self.default_repository_name
:one
end
Widget.auto_migrate!
else
class AnotherWidget < MapperWidget
property :id, Serial
property :name, String
end
class DataMapperWidgetUsingDatabaseTwo
include DataMapper::Resource
def self.default_repository_name
:two
end
AnotherWidget.auto_migrate!
end
property :id, Serial
property :name, String
end
DataMapperWidget.auto_migrate!
DataMapperWidgetUsingDatabaseOne.auto_migrate!
DataMapperWidgetUsingDatabaseTwo.auto_migrate!

View File

@ -3,7 +3,7 @@ require 'mongo_mapper'
::MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
::MongoMapper.database = 'database_cleaner_test'
class MongoWidget
class MongoMapperWidget
include MongoMapper::Document
key :id, Integer
key :name, String
@ -16,10 +16,34 @@ class MongoWidget
end
end
unless defined? Widget
class Widget < MongoWidget
end
else
class AnotherWidget < MongoWidget
class MongoMapperWidgetUsingDatabaseOne
include MongoMapper::Document
database = 'database_cleaner_test_one'
key :id, Integer
key :name, String
class << self
#mongomapper doesn't seem to provide this...
def create!(*args)
new(*args).save!
end
end
end
class MongoMapperWidgetUsingDatabaseTwo
include MongoMapper::Document
database = 'database_cleaner_test_two'
key :id, Integer
key :name, String
class << self
#mongomapper doesn't seem to provide this...
def create!(*args)
new(*args).save!
end
end
end