database_cleaner 0.5.0 → 0.6.0.rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. data/Gemfile.lock +145 -0
  2. data/History.txt +43 -1
  3. data/README.textile +49 -1
  4. data/Rakefile +4 -0
  5. data/TODO +3 -0
  6. data/VERSION.yml +3 -2
  7. data/examples/Gemfile +46 -0
  8. data/examples/Gemfile.lock +145 -0
  9. data/examples/config/database.yml +7 -0
  10. data/examples/config/database.yml.example +8 -0
  11. data/examples/db/activerecord_one.db +0 -0
  12. data/examples/db/activerecord_two.db +0 -0
  13. data/examples/db/datamapper_default.db +0 -0
  14. data/examples/db/datamapper_one.db +0 -0
  15. data/examples/db/datamapper_two.db +0 -0
  16. data/examples/db/sqlite_databases_go_here +0 -0
  17. data/examples/features/example_multiple_db.feature +23 -0
  18. data/examples/features/example_multiple_orm.feature +22 -0
  19. data/examples/features/step_definitions/activerecord_steps.rb +31 -0
  20. data/examples/features/step_definitions/couchpotato_steps.rb +31 -0
  21. data/examples/features/step_definitions/datamapper_steps.rb +37 -0
  22. data/examples/features/step_definitions/mongoid_steps.rb +23 -0
  23. data/examples/features/step_definitions/mongomapper_steps.rb +31 -0
  24. data/examples/features/step_definitions/translation_steps.rb +55 -0
  25. data/examples/features/support/env.rb +49 -10
  26. data/examples/lib/activerecord_models.rb +34 -5
  27. data/examples/lib/couchpotato_models.rb +46 -6
  28. data/examples/lib/datamapper_models.rb +37 -3
  29. data/examples/lib/mongoid_models.rb +49 -0
  30. data/examples/lib/mongomapper_models.rb +36 -2
  31. data/features/cleaning.feature +1 -0
  32. data/features/cleaning_multiple_dbs.feature +20 -0
  33. data/features/cleaning_multiple_orms.feature +29 -0
  34. data/features/step_definitions/database_cleaner_steps.rb +21 -14
  35. data/features/support/feature_runner.rb +39 -0
  36. data/lib/database_cleaner/active_record/base.rb +46 -0
  37. data/lib/database_cleaner/active_record/transaction.rb +10 -10
  38. data/lib/database_cleaner/active_record/truncation.rb +42 -11
  39. data/lib/database_cleaner/base.rb +129 -0
  40. data/lib/database_cleaner/configuration.rb +45 -82
  41. data/lib/database_cleaner/couch_potato/base.rb +7 -0
  42. data/lib/database_cleaner/couch_potato/truncation.rb +4 -2
  43. data/lib/database_cleaner/cucumber.rb +0 -1
  44. data/lib/database_cleaner/data_mapper/base.rb +21 -0
  45. data/lib/database_cleaner/data_mapper/transaction.rb +10 -5
  46. data/lib/database_cleaner/data_mapper/truncation.rb +52 -19
  47. data/lib/database_cleaner/generic/base.rb +23 -0
  48. data/lib/database_cleaner/generic/truncation.rb +43 -0
  49. data/lib/database_cleaner/mongo_mapper/base.rb +20 -0
  50. data/lib/database_cleaner/mongo_mapper/truncation.rb +9 -3
  51. data/lib/database_cleaner/mongoid/base.rb +20 -0
  52. data/lib/database_cleaner/mongoid/truncation.rb +27 -0
  53. data/spec/database_cleaner/active_record/base_spec.rb +130 -0
  54. data/spec/database_cleaner/active_record/transaction_spec.rb +65 -0
  55. data/spec/database_cleaner/active_record/truncation_spec.rb +19 -18
  56. data/spec/database_cleaner/base_spec.rb +441 -0
  57. data/spec/database_cleaner/configuration_spec.rb +255 -65
  58. data/spec/database_cleaner/couch_potato/truncation_spec.rb +4 -3
  59. data/spec/database_cleaner/data_mapper/base_spec.rb +30 -0
  60. data/spec/database_cleaner/data_mapper/transaction_spec.rb +23 -0
  61. data/spec/database_cleaner/data_mapper/truncation_spec.rb +11 -0
  62. data/spec/database_cleaner/generic/base_spec.rb +22 -0
  63. data/spec/database_cleaner/generic/truncation_spec.rb +68 -0
  64. data/spec/database_cleaner/mongo_mapper/base_spec.rb +33 -0
  65. data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +8 -0
  66. data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +12 -19
  67. data/spec/database_cleaner/mongoid/truncation_spec.rb +84 -0
  68. data/spec/database_cleaner/shared_strategy_spec.rb +13 -0
  69. data/spec/rcov.opts +1 -0
  70. data/spec/spec.opts +1 -0
  71. data/spec/spec_helper.rb +10 -3
  72. metadata +83 -8
  73. data/examples/features/step_definitions/example_steps.rb +0 -8
  74. data/lib/database_cleaner/truncation_base.rb +0 -41
@@ -0,0 +1,31 @@
1
+ Given /^I have setup database cleaner to clean multiple databases using mongomapper$/ do
2
+ #DatabaseCleaner
3
+ # require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
4
+ #
5
+ # DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
6
+ # DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
7
+ end
8
+
9
+ When /^I create a widget using mongomapper$/ do
10
+ MongoMapperWidget.create!
11
+ end
12
+
13
+ Then /^I should see ([\d]+) widget using mongomapper$/ do |widget_count|
14
+ MongoMapperWidget.count.should == widget_count.to_i
15
+ end
16
+
17
+ When /^I create a widget in one db using mongomapper$/ do
18
+ MongoMapperWidgetUsingDatabaseOne.create!
19
+ end
20
+
21
+ When /^I create a widget in another db using mongomapper$/ do
22
+ MongoMapperWidgetUsingDatabaseTwo.create!
23
+ end
24
+
25
+ Then /^I should see ([\d]+) widget in one db using mongomapper$/ do |widget_count|
26
+ MongoMapperWidgetUsingDatabaseOne.count.should == widget_count.to_i
27
+ end
28
+
29
+ Then /^I should see ([\d]+) widget in another db using mongomapper$/ do |widget_count|
30
+ MongoMapperWidgetUsingDatabaseTwo.count.should == widget_count.to_i
31
+ end
@@ -0,0 +1,55 @@
1
+ When /^I create a widget$/ do
2
+ When "I create a widget using #{ENV['ORM'].downcase}"
3
+ end
4
+
5
+ Then /^I should see 1 widget$/ do
6
+ Then "I should see 1 widget using #{ENV['ORM'].downcase}"
7
+ end
8
+
9
+ When /^I create a widget in one orm$/ do
10
+ When "I create a widget using #{ENV['ORM'].downcase}"
11
+ end
12
+
13
+ When /^I create a widget in another orm$/ do
14
+ When "I create a widget using #{ENV['ANOTHER_ORM'].downcase}"
15
+ end
16
+
17
+ Then /^I should see 1 widget in one orm$/ do
18
+ When "I should see 1 widget using #{ENV['ORM'].downcase}"
19
+ end
20
+
21
+ Then /^I should see 1 widget in another orm$/ do
22
+ When "I should see 1 widget using #{ENV['ANOTHER_ORM'].downcase}"
23
+ end
24
+
25
+ Then /^I should see 0 widget in another orm$/ do
26
+ When "I should see 0 widget using #{ENV['ANOTHER_ORM'].downcase}"
27
+ end
28
+
29
+ Then /^I should see 0 widget in one orm$/ do
30
+ When "I should see 0 widget using #{ENV['ORM'].downcase}"
31
+ end
32
+
33
+ When /^I create a widget in one db$/ do
34
+ When "I create a widget in one db using #{ENV['ORM'].downcase}"
35
+ end
36
+
37
+ When /^I create a widget in another db$/ do
38
+ When "I create a widget in another db using #{ENV['ORM'].downcase}"
39
+ end
40
+
41
+ Then /^I should see 1 widget in one db$/ do
42
+ When "I should see 1 widget in one db using #{ENV['ORM'].downcase}"
43
+ end
44
+
45
+ Then /^I should see 1 widget in another db$/ do
46
+ When "I should see 1 widget in another db using #{ENV['ORM'].downcase}"
47
+ end
48
+
49
+ Then /^I should see 0 widget in another db$/ do
50
+ When "I should see 0 widget in another db using #{ENV['ORM'].downcase}"
51
+ end
52
+
53
+ Then /^I should see 0 widget in one db$/ do
54
+ When "I should see 0 widget in one db using #{ENV['ORM'].downcase}"
55
+ end
@@ -1,23 +1,62 @@
1
+ #Hilarious as it seems, this is necessary so bundle exec cucumber works for mongoid cukeage (I'm assuming mongomapper is automatically present because its a git repo)
2
+ Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
3
+
1
4
  require 'rubygems'
5
+ require 'bundler'
6
+
7
+ Bundler.setup
2
8
  require 'spec/expectations'
9
+ require 'ruby-debug'
10
+
11
+ DB_DIR = "#{File.dirname(__FILE__)}/../../db"
12
+
13
+ orm = ENV['ORM']
14
+ another_orm = ENV['ANOTHER_ORM']
15
+ strategy = ENV['STRATEGY']
16
+ multiple_db = ENV['MULTIPLE_DBS']
3
17
 
4
- orm = ENV['ORM']
5
- strategy = ENV['STRATEGY']
6
18
 
7
19
  if orm && strategy
20
+ $:.unshift(File.dirname(__FILE__) + '/../../../lib')
21
+ require 'database_cleaner'
22
+ require 'database_cleaner/cucumber'
8
23
 
9
24
  begin
10
- require "#{File.dirname(__FILE__)}/../../lib/#{orm}_models"
11
- rescue LoadError
25
+ require "#{File.dirname(__FILE__)}/../../lib/#{orm.downcase}_models"
26
+ rescue LoadError => e
12
27
  raise "You don't have the #{orm} ORM installed"
13
28
  end
14
29
 
15
- $:.unshift(File.dirname(__FILE__) + '/../../../lib')
16
- require 'database_cleaner'
17
- require 'database_cleaner/cucumber'
30
+ if another_orm
31
+ begin
32
+ require "#{File.dirname(__FILE__)}/../../lib/#{another_orm.downcase}_models"
33
+ rescue LoadError => e
34
+ raise "You don't have the #{another_orm} ORM installed"
35
+ end
36
+ end
37
+
38
+
39
+
40
+
41
+ if multiple_db
42
+ DatabaseCleaner.app_root = "#{File.dirname(__FILE__)}/../.."
43
+ orm_sym = orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym
44
+
45
+ if orm_sym == :mongo_mapper
46
+ DatabaseCleaner[ orm_sym, {:connection => 'database_cleaner_test_one'} ].strategy = strategy.to_sym
47
+ DatabaseCleaner[ orm_sym, {:connection => 'database_cleaner_test_two'} ].strategy = strategy.to_sym
48
+ else
49
+ DatabaseCleaner[ orm_sym, {:connection => :one} ].strategy = strategy.to_sym
50
+ DatabaseCleaner[ orm_sym, {:connection => :two} ].strategy = strategy.to_sym
51
+ end
52
+
53
+ elsif another_orm
54
+ DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym
55
+ DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym
56
+ else
57
+ DatabaseCleaner.strategy = strategy.to_sym
58
+ end
18
59
 
19
- DatabaseCleaner.strategy = strategy.to_sym
20
-
21
60
  else
22
- raise "Run 'ORM=activerecord|datamapper|mongomapper|couchpotato STRATEGY=transaction|truncation cucumber examples/features'"
61
+ raise "Run 'ORM=ActiveRecord|DataMapper|MongoMapper|CouchPotato [ANOTHER_ORM=...] [MULTIPLE_DBS=true] STRATEGY=transaction|truncation cucumber examples/features'"
23
62
  end
@@ -1,12 +1,41 @@
1
1
  require 'active_record'
2
+ databases_config = {
3
+ "one" => {"adapter" => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", "database" => "#{DB_DIR}/activerecord_one.db"},
4
+ "two" => {"adapter" => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", "database" => "#{DB_DIR}/activerecord_two.db"}
5
+ }
2
6
 
3
- ActiveRecord::Base.establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => ":memory:")
7
+ File.open("#{File.dirname(__FILE__)}/../config/database.yml", 'w') do |file|
8
+ file.write(YAML.dump(databases_config))
9
+ end
10
+
11
+ ["two","one"].each do |db|
12
+ ActiveRecord::Base.establish_connection(databases_config[db])
13
+ ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widgets"')
14
+ ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widget_using_database_ones"')
15
+ ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widget_using_database_twos"')
16
+
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+ create_table :active_record_widgets do |t|
19
+ t.string :name
20
+ end
21
+
22
+ create_table :active_record_widget_using_database_ones do |t|
23
+ t.string :name
24
+ end
4
25
 
5
- ActiveRecord::Schema.define(:version => 1) do
6
- create_table :widgets do |t|
7
- t.string :name
26
+ create_table :active_record_widget_using_database_twos do |t|
27
+ t.string :name
28
+ end
8
29
  end
9
30
  end
10
31
 
11
- class Widget < ActiveRecord::Base
32
+ class ActiveRecordWidget < ActiveRecord::Base
33
+ end
34
+
35
+ class ActiveRecordWidgetUsingDatabaseOne < ActiveRecord::Base
36
+ establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_one.db")
37
+ end
38
+
39
+ class ActiveRecordWidgetUsingDatabaseTwo < ActiveRecord::Base
40
+ establish_connection(:adapter => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", :database => "#{DB_DIR}/activerecord_two.db")
12
41
  end
@@ -1,21 +1,61 @@
1
1
  require 'couch_potato'
2
-
2
+ require 'json/pure' unless defined? ::JSON
3
3
  ::CouchPotato::Config.database_name = 'couch_potato_test'
4
4
 
5
- class Widget
5
+ class CouchPotatoWidget
6
+ include CouchPotato::Persistence
7
+
8
+ property :name
9
+ view :by_name, :key => :name
10
+
11
+
12
+ # mimic the AR interface used in example_steps
13
+
14
+ def self.create!(attrs = {})
15
+ CouchPotato.database.save(self.new)
16
+ end
17
+
18
+ def self.count
19
+ CouchPotato.database.view(self.by_name).size
20
+ end
21
+ end
22
+
23
+ class CouchPotatoWidgetUsingDatabaseOne
24
+ include CouchPotato::Persistence
25
+
26
+ database_name = 'couch_potato_test_one'
27
+
28
+ property :name
29
+ view :by_name, :key => :name
30
+
31
+
32
+ # mimic the AR interface used in example_steps
33
+
34
+ def self.create!(attrs = {})
35
+ CouchPotato.database.save(self.new)
36
+ end
37
+
38
+ def self.count
39
+ CouchPotato.database.view(self.by_name).size
40
+ end
41
+ end
42
+
43
+ class CouchPotatoWidgetUsingDatabaseTwo
6
44
  include CouchPotato::Persistence
7
-
45
+
46
+ database_name = 'couch_potato_test_two'
47
+
8
48
  property :name
9
49
  view :by_name, :key => :name
10
-
50
+
11
51
 
12
52
  # mimic the AR interface used in example_steps
13
53
 
14
54
  def self.create!(attrs = {})
15
55
  CouchPotato.database.save(self.new)
16
56
  end
17
-
57
+
18
58
  def self.count
19
- CouchPotato.database.view(::Widget.by_name).size
59
+ CouchPotato.database.view(self.by_name).size
20
60
  end
21
61
  end
@@ -1,16 +1,50 @@
1
1
  require "dm-core"
2
+ require "dm-transactions"
3
+
4
+ #Datamapper 1.0 requires you to require dm-migrations to automigrate
5
+ require "dm-migrations"
2
6
 
3
7
  # only to please activerecord API used in database_cleaner/examples/features/step_definitions
4
8
  # yes, i know that's lazy ...
9
+
5
10
  require "dm-validations"
6
11
  require "dm-aggregates"
7
12
 
8
- DataMapper.setup(:default, "sqlite3::memory:")
13
+ DataMapper.setup(:default, "sqlite3:#{DB_DIR}/datamapper_default.db")
14
+ DataMapper.setup(:one, "sqlite3:#{DB_DIR}/datamapper_one.db")
15
+ DataMapper.setup(:two, "sqlite3:#{DB_DIR}/datamapper_two.db")
16
+
17
+ class DataMapperWidget
18
+ include DataMapper::Resource
19
+
20
+ property :id, Serial
21
+ property :name, String
22
+ end
9
23
 
10
- class Widget
24
+ class DataMapperWidgetUsingDatabaseOne
11
25
  include DataMapper::Resource
26
+
27
+ def self.default_repository_name
28
+ :one
29
+ end
30
+
12
31
  property :id, Serial
13
32
  property :name, String
33
+
34
+ end
35
+
36
+ class DataMapperWidgetUsingDatabaseTwo
37
+ include DataMapper::Resource
38
+
39
+ def self.default_repository_name
40
+ :two
41
+ end
42
+
43
+ property :id, Serial
44
+ property :name, String
45
+
14
46
  end
15
47
 
16
- Widget.auto_migrate!
48
+ DataMapperWidget.auto_migrate!
49
+ DataMapperWidgetUsingDatabaseOne.auto_migrate!
50
+ DataMapperWidgetUsingDatabaseTwo.auto_migrate!
@@ -0,0 +1,49 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ name = 'database_cleaner_test'
5
+ config.master = Mongo::Connection.new.db(name)
6
+ end
7
+
8
+
9
+ #::MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
10
+ #::MongoMapper.database = 'database_cleaner_test'
11
+
12
+ class MongoidWidget
13
+ include Mongoid::Document
14
+ field :id, :type => Integer
15
+ field :name
16
+
17
+ class << self
18
+ #mongoid doesn't seem to provide this...
19
+ def create!(*args)
20
+ new(*args).save!
21
+ end
22
+ end
23
+ end
24
+
25
+ class MongoidWidgetUsingDatabaseOne
26
+ include Mongoid::Document
27
+ field :id, :type => Integer
28
+ field :name
29
+
30
+ class << self
31
+ #mongoid doesn't seem to provide this...
32
+ def create!(*args)
33
+ new(*args).save!
34
+ end
35
+ end
36
+ end
37
+
38
+ class MongoidWidgetUsingDatabaseTwo
39
+ include Mongoid::Document
40
+ field :id, :type => Integer
41
+ field :name
42
+
43
+ class << self
44
+ #mongoid doesn't seem to provide this...
45
+ def create!(*args)
46
+ new(*args).save!
47
+ end
48
+ end
49
+ end
@@ -1,9 +1,9 @@
1
- require 'mongomapper'
1
+ require 'mongo_mapper'
2
2
 
3
3
  ::MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
4
4
  ::MongoMapper.database = 'database_cleaner_test'
5
5
 
6
- class Widget
6
+ class MongoMapperWidget
7
7
  include MongoMapper::Document
8
8
  key :id, Integer
9
9
  key :name, String
@@ -15,3 +15,37 @@ class Widget
15
15
  end
16
16
  end
17
17
  end
18
+
19
+ class MongoMapperWidgetUsingDatabaseOne
20
+ include MongoMapper::Document
21
+
22
+ connection = Mongo::Connection.new('127.0.0.1')
23
+ set_database_name = 'database_cleaner_test_one'
24
+
25
+ key :id, Integer
26
+ key :name, String
27
+
28
+ class << self
29
+ #mongomapper doesn't seem to provide this...
30
+ def create!(*args)
31
+ new(*args).save!
32
+ end
33
+ end
34
+ end
35
+
36
+ class MongoMapperWidgetUsingDatabaseTwo
37
+ include MongoMapper::Document
38
+
39
+ connection = Mongo::Connection.new('127.0.0.1')
40
+ set_database_name = 'database_cleaner_test_two'
41
+
42
+ key :id, Integer
43
+ key :name, String
44
+
45
+ class << self
46
+ #mongomapper doesn't seem to provide this...
47
+ def create!(*args)
48
+ new(*args).save!
49
+ end
50
+ end
51
+ end
@@ -17,4 +17,5 @@ Feature: database cleaning
17
17
  | DataMapper | transaction |
18
18
  | DataMapper | truncation |
19
19
  | MongoMapper | truncation |
20
+ | Mongoid | truncation |
20
21
  | CouchPotato | truncation |
@@ -0,0 +1,20 @@
1
+ Feature: multiple database cleaning
2
+ In order to ease example and feature writing
3
+ As a developer
4
+ I want to have my databases in a clean state
5
+
6
+ Scenario Outline: ruby app
7
+ Given I am using <ORM>
8
+ And the <Strategy> cleaning strategy
9
+
10
+ When I run my scenarios that rely on clean databases
11
+ Then I should see all green
12
+
13
+ Examples:
14
+ | ORM | Strategy |
15
+ | ActiveRecord | truncation |
16
+ | DataMapper | truncation |
17
+ | MongoMapper | truncation |
18
+ | DataMapper | transaction |
19
+ # Not working...
20
+ #| ActiveRecord | transaction |
@@ -0,0 +1,29 @@
1
+ Feature: database cleaning using multiple ORMs
2
+ In order to ease example and feature writing
3
+ As a developer
4
+ I want to have my database in a clean state
5
+
6
+ Scenario Outline: ruby app
7
+ Given I am using <ORM1> and <ORM2>
8
+
9
+ When I run my scenarios that rely on clean databases using multiple orms
10
+ Then I should see all green
11
+
12
+ Examples:
13
+ | ORM1 | ORM2 |
14
+ | ActiveRecord | DataMapper |
15
+ | ActiveRecord | MongoMapper |
16
+ | ActiveRecord | Mongoid |
17
+ | ActiveRecord | CouchPotato |
18
+ | DataMapper | ActiveRecord |
19
+ | DataMapper | MongoMapper |
20
+ | DataMapper | Mongoid |
21
+ | DataMapper | CouchPotato |
22
+ | MongoMapper | ActiveRecord |
23
+ | MongoMapper | DataMapper |
24
+ | MongoMapper | Mongoid |
25
+ | MongoMapper | CouchPotato |
26
+ | CouchPotato | ActiveRecord |
27
+ | CouchPotato | DataMapper |
28
+ | CouchPotato | MongoMapper |
29
+ | CouchPotato | Mongoid |
@@ -1,25 +1,32 @@
1
- Given /^I am using (ActiveRecord|DataMapper|MongoMapper|CouchPotato)$/ do |orm|
2
- @orm = orm
1
+
2
+ Given /^I am using (ActiveRecord|DataMapper|MongoMapper|Mongoid|CouchPotato)$/ do |orm|
3
+ @feature_runner = FeatureRunner.new
4
+ @feature_runner.orm = orm
5
+ end
6
+
7
+ Given /^I am using (ActiveRecord|DataMapper|MongoMapper|CouchPotato|Mongoid) and (ActiveRecord|DataMapper|MongoMapper|CouchPotato|Mongoid)$/ do |orm1,orm2|
8
+ @feature_runner = FeatureRunner.new
9
+ @feature_runner.orm = orm1
10
+ @feature_runner.another_orm = orm2
3
11
  end
4
12
 
5
13
  Given /^the (.+) cleaning strategy$/ do |strategy|
6
- @strategy = strategy
14
+ @feature_runner.strategy = strategy
7
15
  end
8
16
 
9
17
  When "I run my scenarios that rely on a clean database" do
10
- full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
11
- Dir.chdir(full_dir) do
12
- ENV['ORM'] = @orm.downcase
13
- ENV['STRATEGY'] = @strategy
14
- @out = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features`
15
- @status = $?.exitstatus
16
- end
18
+ @feature_runner.go 'example'
17
19
  end
18
20
 
19
- Then "I should see all green" do
20
- unless @status == 0
21
- raise "Expected to see all green but we saw FAIL! Output:\n#{@out}"
22
- end
21
+ When "I run my scenarios that rely on clean databases" do
22
+ @feature_runner.multiple_databases = true
23
+ @feature_runner.go 'example_multiple_db'
23
24
  end
24
25
 
26
+ When "I run my scenarios that rely on clean databases using multiple orms" do
27
+ @feature_runner.go 'example_multiple_orm'
28
+ end
25
29
 
30
+ Then "I should see all green" do
31
+ fail "Feature failed with :#{@feature_runner.output}" unless @feature_runner.exit_status == 0
32
+ end
@@ -0,0 +1,39 @@
1
+ class FeatureRunner
2
+ attr_accessor :orm
3
+ attr_accessor :another_orm
4
+ attr_accessor :multiple_databases
5
+ attr_accessor :strategy
6
+ attr_accessor :exit_status
7
+ attr_accessor :output
8
+
9
+ def strategy
10
+ @strategy || 'truncation'
11
+ end
12
+
13
+ def go(feature)
14
+ full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
15
+ Dir.chdir(full_dir) do
16
+
17
+
18
+ ENV['ORM'] = orm
19
+ ENV['STRATEGY'] = strategy
20
+
21
+ if another_orm
22
+ ENV['ANOTHER_ORM'] = another_orm
23
+ else
24
+ ENV['ANOTHER_ORM'] = nil
25
+ end
26
+
27
+ if multiple_databases
28
+ ENV['MULTIPLE_DBS'] = "true"
29
+ else
30
+ ENV['MULTIPLE_DBS'] = nil
31
+ end
32
+
33
+ self.output = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features/#{feature}.feature`
34
+
35
+ self.exit_status = $?.exitstatus
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,46 @@
1
+ require 'database_cleaner/generic/base'
2
+ require 'active_record'
3
+
4
+ module DatabaseCleaner
5
+ module ActiveRecord
6
+
7
+ def self.available_strategies
8
+ %w[truncation transaction]
9
+ end
10
+
11
+ def self.config_file_location
12
+ "#{DatabaseCleaner.app_root}/config/database.yml"
13
+ end
14
+
15
+ module Base
16
+ include ::DatabaseCleaner::Generic::Base
17
+
18
+ attr_accessor :connection_hash
19
+
20
+ def db=(desired_db)
21
+ @db = desired_db
22
+ load_config
23
+ end
24
+
25
+ def db
26
+ @db || super
27
+ end
28
+
29
+ def load_config
30
+ connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
31
+ self.connection_hash = connection_details[self.db.to_s]
32
+ end
33
+
34
+ def create_connection_klass
35
+ Class.new(::ActiveRecord::Base)
36
+ end
37
+
38
+ def connection_klass
39
+ return ::ActiveRecord::Base if connection_hash.nil?
40
+ klass = create_connection_klass
41
+ klass.send :establish_connection, connection_hash
42
+ klass
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,26 +1,26 @@
1
+ require 'database_cleaner/active_record/base'
1
2
  module DatabaseCleaner::ActiveRecord
2
3
  class Transaction
4
+ include ::DatabaseCleaner::ActiveRecord::Base
3
5
 
4
6
  def start
5
- if ActiveRecord::Base.connection.respond_to?(:increment_open_transactions)
6
- ActiveRecord::Base.connection.increment_open_transactions
7
+ if connection_klass.connection.respond_to?(:increment_open_transactions)
8
+ connection_klass.connection.increment_open_transactions
7
9
  else
8
- ActiveRecord::Base.__send__(:increment_open_transactions)
10
+ connection_klass.__send__(:increment_open_transactions)
9
11
  end
10
-
11
- ActiveRecord::Base.connection.begin_db_transaction
12
+ connection_klass.connection.begin_db_transaction
12
13
  end
13
14
 
14
15
 
15
16
  def clean
16
- ActiveRecord::Base.connection.rollback_db_transaction
17
+ connection_klass.connection.rollback_db_transaction
17
18
 
18
- if ActiveRecord::Base.connection.respond_to?(:decrement_open_transactions)
19
- ActiveRecord::Base.connection.decrement_open_transactions
19
+ if connection_klass.connection.respond_to?(:decrement_open_transactions)
20
+ connection_klass.connection.decrement_open_transactions
20
21
  else
21
- ActiveRecord::Base.__send__(:decrement_open_transactions)
22
+ connection_klass.__send__(:decrement_open_transactions)
22
23
  end
23
24
  end
24
25
  end
25
-
26
26
  end