Tweak features/steps for multiple orm, and fixed a bug in the couch example models

This commit is contained in:
Jon Rowe 2010-05-25 18:21:45 +01:00
parent b723ed8bda
commit d2da58156e
11 changed files with 66 additions and 38 deletions

View file

@ -1,7 +1,10 @@
Feature: example
In order to test DataBase Cleaner
Here are some scenarios that rely on the DB being clean!
# Background:
# Given I have setup DatabaseCleaner to clean multiple databases
#
Scenario: dirty the db
When I create a widget in one db
And I create a widget in another db

View file

@ -2,6 +2,9 @@ Feature: example
In order to test DataBase Cleaner
Here are some scenarios that rely on the DB being clean!
# Background:
# Given I have setup DatabaseCleaner to clean multiple orms
Scenario: dirty the db
When I create a widget in one orm
And I create a widget in another orm

View file

@ -1,15 +1,23 @@
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
TwinWidget.create!
Widget.establish_connection_two
Widget.create!
end
Then /^I should see ([\d]+) widget in one db $/ do |widget_count|
Widget.count.should == widget_count
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|
TwinWidget.count.should == widget_count
Widget.establish_connection_two
Widget.count.should == widget_count.to_i
end

View file

@ -6,10 +6,10 @@ 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
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
AnotherWidget.count.should == widget_count.to_i
end

View file

@ -3,12 +3,17 @@ Bundler.setup
require 'spec/expectations'
require 'ruby-debug'
DB_DIR = "#{File.dirname(__FILE__)}/../../db"
orm = ENV['ORM']
another_orm = ENV['ANOTHER_ORM']
strategy = ENV['STRATEGY']
if orm && strategy
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
require 'database_cleaner'
require 'database_cleaner/cucumber'
begin
require "#{File.dirname(__FILE__)}/../../lib/#{orm}_models"
rescue LoadError => e
@ -21,14 +26,20 @@ if orm && strategy
rescue LoadError => e
raise "You don't have the #{another_orm} ORM installed"
end
else
end
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
require 'database_cleaner'
require 'database_cleaner/cucumber'
DatabaseCleaner.strategy = strategy.to_sym
unless another_orm
DatabaseCleaner.strategy = strategy.to_sym
else
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
end
else
raise "Run 'ORM=activerecord|datamapper|mongomapper|couchpotato [ANOTHER_ORM=activerecord|datamapper|mongomapper|couchpotato] STRATEGY=transaction|truncation cucumber examples/features'"
raise "Run 'ORM=activerecord|datamapper|mongomapper|couchpotato [ANOTHER_ORM=activerecord|datamapper|mongomapper|couchpotato] [MULTIPLE_DBS=true] STRATEGY=transaction|truncation cucumber examples/features'"
end

View file

@ -1,34 +1,37 @@
require 'active_record'
db_dir = "#{File.dirname(__FILE__)}/../db"
ActiveRecord::Base.establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{db_dir}/activerecord_one.db")
class SchemeInfo < ActiveRecord::Base
end
# check to see if the schema needs resetting (when using file based db)
begin
result = ActiveRecord::Base.connection.execute("SELECT version FROM schema_migrations")
raise StandardError unless result.first["version"] == "1"
rescue
["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::Schema.define(:version => 1) do
create_table :widgets do |t|
t.string :name
end
create_table :another_widgets 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
end
unless defined? Widget
class Widget < ActiveRecord::Base
class Widget < ActiveRecordWidgetBase
set_table_name 'widgets'
end
else
class AnotherWidget < ActiveRecord::Base
end
class AnotherWidget < ActiveRecordWidgetBase
set_table_name 'another_widgets'
end
end

View file

@ -16,7 +16,7 @@ class CouchWidget
end
def self.count
CouchPotato.database.view(::Widget.by_name).size
CouchPotato.database.view(self.by_name).size
end
end

View file

@ -5,9 +5,7 @@ require "dm-core"
require "dm-validations"
require "dm-aggregates"
db_dir = "#{File.dirname(__FILE__)}/../db"
DataMapper.setup(:default, "sqlite3:#{db_dir}/datamapper_one.db")
DataMapper.setup(:default, "sqlite3:#{DB_DIR}/datamapper_one.db")
class MapperWidget
include DataMapper::Resource

View file

@ -19,6 +19,7 @@ When "I run my scenarios that rely on a clean database" do
end
When "I run my scenarios that rely on clean databases" do
@feature_runner.multiple_databases = true
@feature_runner.go 'example_multiple_db'
end

View file

@ -1,6 +1,7 @@
class FeatureRunner
attr_accessor :orm
attr_accessor :another_orm
attr_accessor :multiple_databases
attr_accessor :strategy
attr_accessor :exit_status
attr_accessor :output
@ -13,9 +14,10 @@ class FeatureRunner
full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
Dir.chdir(full_dir) do
ENV['ORM'] = orm.downcase
ENV['ANOTHER_ORM'] = another_orm.downcase if another_orm
ENV['STRATEGY'] = strategy
ENV['ORM'] = orm#.downcase
ENV['ANOTHER_ORM'] = another_orm if another_orm#.downcase if another_orm
ENV['MULTIPLE_DBS'] = "true" if multiple_databases
ENV['STRATEGY'] = strategy
self.output = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features/#{feature}.feature`

View file

@ -1,4 +1,3 @@
Before do
DatabaseCleaner.start
end