views:

44

answers:

0

I have a rails project that uses multiple legacy databases.

I would like to perform tests that verify that the models are linked together correctly. This is being done so that the team can use TeamCity (by JetBrains) to perform automated testing.

database.yaml:

test:
  ...
  database: application_test
  ...

admin:
  ...
<% if RAILS_ENV == 'test' %>
  database: admin
<% end %>
  ...

in my models:

class AdminRecord < ActiveRecord::Base
  establish_connection 'admin'
  set_table_name 'record'
end

class NonAdminRecord < ActiveRecord::Base
  # uses default connection (in this case, test)
end

This works in the web application context, but when tests are run, we get ugly errors such as the following:

me@ubuntu:~/work/########$ rake test
(in ###############################)
/usr/bin/ruby1.8 -I"lib:test" "/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/#####_controller_test.rb" "test/functional/#####_controller_test.rb"
Loaded suite /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
EEEEEEE
Finished in 497.371838 seconds.

  1) Error:
test_###########(#####ControllerTest):
ActiveRecord::StatementInvalid: Mysql::Error: Table 'application_test.record' doesn't exist: DELETE FROM `record`


  2) Error:
...

As far as I can tell, this indicates that ActiveRecord is using the connection test instead of admin. How can this be fixed?

One possible fix I see is to prevent 'rake test' from loading models that do not pertain to the test. Is there a way to run 'rake test' without loading models until necessary?

Multiple databases is a must as many applications depend on these databases.

P.S. record, application_test and admin are all made-up database/table names.

Thanks all.