2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/entrant'
|
2010-01-03 15:35:18 -05:00
|
|
|
require 'models/bird'
|
2012-08-10 12:42:48 -04:00
|
|
|
require 'models/course'
|
2005-11-24 02:01:43 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class MultipleDbTest < ActiveRecord::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def setup
|
|
|
|
@courses = create_fixtures("courses") { Course.retrieve_connection }
|
2012-02-10 17:35:22 -05:00
|
|
|
@colleges = create_fixtures("colleges") { College.retrieve_connection }
|
2004-11-23 20:04:44 -05:00
|
|
|
@entrants = create_fixtures("entrants")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connected
|
|
|
|
assert_not_nil Entrant.connection
|
|
|
|
assert_not_nil Course.connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proper_connection
|
|
|
|
assert_not_equal(Entrant.connection, Course.connection)
|
|
|
|
assert_equal(Entrant.connection, Entrant.retrieve_connection)
|
|
|
|
assert_equal(Course.connection, Course.retrieve_connection)
|
|
|
|
assert_equal(ActiveRecord::Base.connection, Entrant.connection)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find
|
|
|
|
c1 = Course.find(1)
|
|
|
|
assert_equal "Ruby Development", c1.name
|
|
|
|
c2 = Course.find(2)
|
|
|
|
assert_equal "Java Development", c2.name
|
|
|
|
e1 = Entrant.find(1)
|
|
|
|
assert_equal "Ruby Developer", e1.name
|
|
|
|
e2 = Entrant.find(2)
|
|
|
|
assert_equal "Ruby Guru", e2.name
|
|
|
|
e3 = Entrant.find(3)
|
|
|
|
assert_equal "Java Lover", e3.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_associations
|
|
|
|
c1 = Course.find(1)
|
2006-09-15 03:02:05 -04:00
|
|
|
assert_equal 2, c1.entrants.count
|
2004-11-23 20:04:44 -05:00
|
|
|
e1 = Entrant.find(1)
|
|
|
|
assert_equal e1.course.id, c1.id
|
|
|
|
c2 = Course.find(2)
|
2006-09-15 03:02:05 -04:00
|
|
|
assert_equal 1, c2.entrants.count
|
2004-11-23 20:04:44 -05:00
|
|
|
e3 = Entrant.find(3)
|
|
|
|
assert_equal e3.course.id, c2.id
|
|
|
|
end
|
2005-11-24 02:01:43 -05:00
|
|
|
|
|
|
|
def test_course_connection_should_survive_dependency_reload
|
|
|
|
assert Course.connection
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.clear
|
2005-11-24 02:01:43 -05:00
|
|
|
Object.send(:remove_const, :Course)
|
2008-01-18 02:31:37 -05:00
|
|
|
require_dependency 'models/course'
|
2005-11-24 02:01:43 -05:00
|
|
|
|
|
|
|
assert Course.connection
|
|
|
|
end
|
2008-07-02 00:01:26 -04:00
|
|
|
|
|
|
|
def test_transactions_across_databases
|
|
|
|
c1 = Course.find(1)
|
|
|
|
e1 = Entrant.find(1)
|
|
|
|
|
|
|
|
begin
|
|
|
|
Course.transaction do
|
|
|
|
Entrant.transaction do
|
|
|
|
c1.name = "Typo"
|
|
|
|
e1.name = "Typo"
|
|
|
|
c1.save
|
|
|
|
e1.save
|
|
|
|
raise "No I messed up."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# Yup caught it
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Typo", c1.name
|
|
|
|
assert_equal "Typo", e1.name
|
|
|
|
|
|
|
|
assert_equal "Ruby Development", Course.find(1).name
|
|
|
|
assert_equal "Ruby Developer", Entrant.find(1).name
|
|
|
|
end
|
2010-01-03 15:35:18 -05:00
|
|
|
|
|
|
|
def test_arel_table_engines
|
2012-03-13 13:11:40 -04:00
|
|
|
assert_not_equal Entrant.arel_engine, Bird.arel_engine
|
|
|
|
assert_not_equal Entrant.arel_engine, Course.arel_engine
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connection
|
|
|
|
assert_equal Entrant.arel_engine.connection, Bird.arel_engine.connection
|
|
|
|
assert_not_equal Entrant.arel_engine.connection, Course.arel_engine.connection
|
2010-01-03 15:35:18 -05:00
|
|
|
end
|
2012-02-10 17:35:22 -05:00
|
|
|
|
2012-03-04 07:15:11 -05:00
|
|
|
unless in_memory_db?
|
|
|
|
def test_associations_should_work_when_model_has_no_connection
|
|
|
|
begin
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.remove_connection
|
2012-03-04 07:15:11 -05:00
|
|
|
assert_nothing_raised ActiveRecord::ConnectionNotEstablished do
|
|
|
|
College.first.courses.first
|
|
|
|
end
|
|
|
|
ensure
|
2013-12-24 04:18:54 -05:00
|
|
|
ActiveRecord::Base.establish_connection :arunit
|
2012-02-10 17:35:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|