Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tests and database_cleaner for multiple databases

We have a Mailbox model which uses a secondary DB to which another mail app also connects. This is called using

establish_connection :mail_database

Now, I suppose I could attempt to wrap that in an if statement so it uses a different connection for the test environment, but I'm wondering how I could keep this database clean, preferably using database_cleaner which we already use for the 'regular' database.

I hope someone can nudge me in the right direction.

like image 809
HannesFostie Avatar asked Sep 12 '12 14:09

HannesFostie


2 Answers

I have a rails3.2.10 application tested with rspec (2.12.0) and using database_cleaner (0.9.1 f4b44bb) having two database connections for mysql.

These are set in the database.yml something like:

test:
  ...
  database: my_app_test

test_my_second_connection:
  ...
  database: my_second_connection_test

The second database is connected in the model class with establish connection.

I was able to use the following settings in my spec/spec_helper.rb file according to the manual of database_cleaner :

require 'database_cleaner'

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :transaction
  end

  config.before(:each) do
    DatabaseCleaner.start
    DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].start
  end

  config.after(:each) do
    DatabaseCleaner.clean
    DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].clean
  end

end

Additionally I have to use for some parts of the test suite non-transactional fixtures. So I had to add the meta information :db_truncate => true in my specs and the settings like this in order to change the strategy in test run:

config.before(:suite) do
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :transaction
end

config.before(:each) do
  if example.metadata[:db_truncation]
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :truncation
  else
    DatabaseCleaner.start
    DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].start
  end
end

config.after(:each) do
  DatabaseCleaner.clean
  DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].clean
  if example.metadata[:db_truncation]
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :transaction
  end
end
like image 118
Lars Schirrmeister Avatar answered Sep 17 '22 12:09

Lars Schirrmeister


****set database to clean by database cleaner in rails:-****
config.before(:suite) do
   DatabaseCleaner[:active_record, :connection => :test].clean_with(:truncation)
end

config.before(:each) do
  DatabaseCleaner[:active_record, :connection => :test].strategy = :transaction
end

config.before(:each) do
  DatabaseCleaner[:active_record, :connection => :test].start
end

config.after(:each) do
  DatabaseCleaner[:active_record, :connection => :test].clean
end 
like image 42
Ganesh Shrivas Avatar answered Sep 17 '22 12:09

Ganesh Shrivas