mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
Tweak features/steps for multiple orm, and fixed a bug in the couch example models
This commit is contained in:
parent
b723ed8bda
commit
d2da58156e
11 changed files with 66 additions and 38 deletions
|
|
@ -1,7 +1,10 @@
|
||||||
Feature: example
|
Feature: example
|
||||||
In order to test DataBase Cleaner
|
In order to test DataBase Cleaner
|
||||||
Here are some scenarios that rely on the DB being clean!
|
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
|
Scenario: dirty the db
|
||||||
When I create a widget in one db
|
When I create a widget in one db
|
||||||
And I create a widget in another db
|
And I create a widget in another db
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@ Feature: example
|
||||||
In order to test DataBase Cleaner
|
In order to test DataBase Cleaner
|
||||||
Here are some scenarios that rely on the DB being clean!
|
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
|
Scenario: dirty the db
|
||||||
When I create a widget in one orm
|
When I create a widget in one orm
|
||||||
And I create a widget in another orm
|
And I create a widget in another orm
|
||||||
|
|
|
||||||
|
|
@ -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
|
When /^I create a widget in one db$/ do
|
||||||
|
Widget.establish_connection_one
|
||||||
Widget.create!
|
Widget.create!
|
||||||
end
|
end
|
||||||
|
|
||||||
When /^I create a widget in another db$/ do
|
When /^I create a widget in another db$/ do
|
||||||
TwinWidget.create!
|
Widget.establish_connection_two
|
||||||
|
Widget.create!
|
||||||
end
|
end
|
||||||
|
|
||||||
Then /^I should see ([\d]+) widget in one db $/ do |widget_count|
|
Then /^I should see ([\d]+) widget in one db$/ do |widget_count|
|
||||||
Widget.count.should == widget_count
|
Widget.establish_connection_one
|
||||||
|
Widget.count.should == widget_count.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
Then /^I should see ([\d]+) widget in another db$/ do |widget_count|
|
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
|
end
|
||||||
|
|
@ -6,10 +6,10 @@ When /^I create a widget in another orm$/ do
|
||||||
AnotherWidget.create!
|
AnotherWidget.create!
|
||||||
end
|
end
|
||||||
|
|
||||||
Then /^I should see ([\d]+) widget in one orm $/ do |widget_count|
|
Then /^I should see ([\d]+) widget in one orm$/ do |widget_count|
|
||||||
Widget.count.should == widget_count
|
Widget.count.should == widget_count.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
Then /^I should see ([\d]+) widget in another orm$/ do |widget_count|
|
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
|
end
|
||||||
|
|
@ -3,12 +3,17 @@ Bundler.setup
|
||||||
require 'spec/expectations'
|
require 'spec/expectations'
|
||||||
require 'ruby-debug'
|
require 'ruby-debug'
|
||||||
|
|
||||||
|
DB_DIR = "#{File.dirname(__FILE__)}/../../db"
|
||||||
|
|
||||||
orm = ENV['ORM']
|
orm = ENV['ORM']
|
||||||
another_orm = ENV['ANOTHER_ORM']
|
another_orm = ENV['ANOTHER_ORM']
|
||||||
strategy = ENV['STRATEGY']
|
strategy = ENV['STRATEGY']
|
||||||
|
|
||||||
if orm && strategy
|
if orm && strategy
|
||||||
|
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
|
||||||
|
require 'database_cleaner'
|
||||||
|
require 'database_cleaner/cucumber'
|
||||||
|
|
||||||
begin
|
begin
|
||||||
require "#{File.dirname(__FILE__)}/../../lib/#{orm}_models"
|
require "#{File.dirname(__FILE__)}/../../lib/#{orm}_models"
|
||||||
rescue LoadError => e
|
rescue LoadError => e
|
||||||
|
|
@ -21,14 +26,20 @@ if orm && strategy
|
||||||
rescue LoadError => e
|
rescue LoadError => e
|
||||||
raise "You don't have the #{another_orm} ORM installed"
|
raise "You don't have the #{another_orm} ORM installed"
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
|
||||||
end
|
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
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,37 @@
|
||||||
require 'active_record'
|
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")
|
["two","one"].each do |db|
|
||||||
class SchemeInfo < ActiveRecord::Base
|
ActiveRecord::Base.establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_#{db}.db")
|
||||||
end
|
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "widgets"')
|
||||||
|
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "another_widgets"')
|
||||||
# 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
|
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 1) do
|
ActiveRecord::Schema.define(:version => 1) do
|
||||||
create_table :widgets do |t|
|
create_table :widgets do |t|
|
||||||
t.string :name
|
t.string :name
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :another_widgets do |t|
|
create_table :another_widgets do |t|
|
||||||
t.string :name
|
t.string :name
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
unless defined? Widget
|
unless defined? Widget
|
||||||
class Widget < ActiveRecord::Base
|
class Widget < ActiveRecordWidgetBase
|
||||||
|
set_table_name 'widgets'
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
class AnotherWidget < ActiveRecord::Base
|
class AnotherWidget < ActiveRecordWidgetBase
|
||||||
end
|
set_table_name 'another_widgets'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class CouchWidget
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.count
|
def self.count
|
||||||
CouchPotato.database.view(::Widget.by_name).size
|
CouchPotato.database.view(self.by_name).size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,7 @@ require "dm-core"
|
||||||
require "dm-validations"
|
require "dm-validations"
|
||||||
require "dm-aggregates"
|
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
|
class MapperWidget
|
||||||
include DataMapper::Resource
|
include DataMapper::Resource
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ When "I run my scenarios that rely on a clean database" do
|
||||||
end
|
end
|
||||||
|
|
||||||
When "I run my scenarios that rely on clean databases" do
|
When "I run my scenarios that rely on clean databases" do
|
||||||
|
@feature_runner.multiple_databases = true
|
||||||
@feature_runner.go 'example_multiple_db'
|
@feature_runner.go 'example_multiple_db'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
class FeatureRunner
|
class FeatureRunner
|
||||||
attr_accessor :orm
|
attr_accessor :orm
|
||||||
attr_accessor :another_orm
|
attr_accessor :another_orm
|
||||||
|
attr_accessor :multiple_databases
|
||||||
attr_accessor :strategy
|
attr_accessor :strategy
|
||||||
attr_accessor :exit_status
|
attr_accessor :exit_status
|
||||||
attr_accessor :output
|
attr_accessor :output
|
||||||
|
|
@ -13,9 +14,10 @@ class FeatureRunner
|
||||||
full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
|
full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
|
||||||
Dir.chdir(full_dir) do
|
Dir.chdir(full_dir) do
|
||||||
|
|
||||||
ENV['ORM'] = orm.downcase
|
ENV['ORM'] = orm#.downcase
|
||||||
ENV['ANOTHER_ORM'] = another_orm.downcase if another_orm
|
ENV['ANOTHER_ORM'] = another_orm if another_orm#.downcase if another_orm
|
||||||
ENV['STRATEGY'] = strategy
|
ENV['MULTIPLE_DBS'] = "true" if multiple_databases
|
||||||
|
ENV['STRATEGY'] = strategy
|
||||||
|
|
||||||
self.output = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features/#{feature}.feature`
|
self.output = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features/#{feature}.feature`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
Before do
|
Before do
|
||||||
DatabaseCleaner.start
|
DatabaseCleaner.start
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue