mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
0965863564
I’m renaming all instances of `use_transcational_fixtures` to `use_transactional_tests` and “transactional fixtures” to “transactional tests”. I’m deprecating `use_transactional_fixtures=`. So anyone who is explicitly setting this will get a warning telling them to use `use_transactional_tests=` instead. I’m maintaining backwards compatibility—both forms will work. `use_transactional_tests` will check to see if `use_transactional_fixtures` is set and use that, otherwise it will use itself. But because `use_transactional_tests` is a class attribute (created with `class_attribute`) this requires a little bit of hoop jumping. The writer method that `class_attribute` generates defines a new reader method that return the value being set. Which means we can’t set the default of `true` using `use_transactional_tests=` as was done previously because that won’t take into account anyone using `use_transactional_fixtures`. Instead I defined the reader method manually and it checks `use_transactional_fixtures`. If it was set then it should be used, otherwise it should return the default, which is `true`. If someone uses `use_transactional_tests=` then it will overwrite the backwards-compatible method with whatever they set.
115 lines
3 KiB
Ruby
115 lines
3 KiB
Ruby
require "cases/helper"
|
|
require 'models/entrant'
|
|
require 'models/bird'
|
|
require 'models/course'
|
|
|
|
class MultipleDbTest < ActiveRecord::TestCase
|
|
self.use_transactional_tests = false
|
|
|
|
def setup
|
|
@courses = create_fixtures("courses") { Course.retrieve_connection }
|
|
@colleges = create_fixtures("colleges") { College.retrieve_connection }
|
|
@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)
|
|
assert_equal 2, c1.entrants.count
|
|
e1 = Entrant.find(1)
|
|
assert_equal e1.course.id, c1.id
|
|
c2 = Course.find(2)
|
|
assert_equal 1, c2.entrants.count
|
|
e3 = Entrant.find(3)
|
|
assert_equal e3.course.id, c2.id
|
|
end
|
|
|
|
def test_course_connection_should_survive_dependency_reload
|
|
assert Course.connection
|
|
|
|
ActiveSupport::Dependencies.clear
|
|
Object.send(:remove_const, :Course)
|
|
require_dependency 'models/course'
|
|
|
|
assert Course.connection
|
|
end
|
|
|
|
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
|
|
|
|
def test_arel_table_engines
|
|
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
|
|
end
|
|
|
|
unless in_memory_db?
|
|
def test_count_on_custom_connection
|
|
ActiveRecord::Base.remove_connection
|
|
assert_equal 1, College.count
|
|
ensure
|
|
ActiveRecord::Base.establish_connection :arunit
|
|
end
|
|
|
|
def test_associations_should_work_when_model_has_no_connection
|
|
begin
|
|
ActiveRecord::Base.remove_connection
|
|
assert_nothing_raised ActiveRecord::ConnectionNotEstablished do
|
|
College.first.courses.first
|
|
end
|
|
ensure
|
|
ActiveRecord::Base.establish_connection :arunit
|
|
end
|
|
end
|
|
end
|
|
end
|