1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/test_fixtures_test.rb
Jean Boussier 8459c1c22f Make legacy_connection_handling a module instance variable
Ref: https://github.com/rails/rails/pull/42237
Ref: https://bugs.ruby-lang.org/issues/17763

Ruby class variables are slow, and get slower the more ancestors the class has.
And it doesn't seem likely to get faster any time soon. Overall I'm under the
impression that ruby-core consider them more or less deprecated.

But in many cases where `cattr_accessor` is used, a class instance variable
could work just fine, and would be much faster.

For Active Record this means most of the `cattr_accessor` on `ActiveRecord::Base`
could actually be `attr_accessor` on `ActiveRecord`.

I started with `legacy_connection_handling` as a proof of concept.
2021-06-10 15:00:20 +02:00

98 lines
2.8 KiB
Ruby

# frozen_string_literal: true
require "cases/helper"
require "tempfile"
require "fileutils"
require "models/zine"
class TestFixturesTest < ActiveRecord::TestCase
setup do
@klass = Class.new
@klass.include(ActiveRecord::TestFixtures)
end
def test_use_transactional_tests_defaults_to_true
assert_equal true, @klass.use_transactional_tests
end
def test_use_transactional_tests_can_be_overridden
@klass.use_transactional_tests = "foobar"
assert_equal "foobar", @klass.use_transactional_tests
end
unless in_memory_db?
def test_doesnt_rely_on_active_support_test_case_specific_methods_with_legacy_connection_handling
old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true
tmp_dir = Dir.mktmpdir
File.write(File.join(tmp_dir, "zines.yml"), <<~YML)
going_out:
title: Hello
YML
klass = Class.new(Minitest::Test) do
include ActiveRecord::TestFixtures
self.fixture_path = tmp_dir
self.use_transactional_tests = true
fixtures :all
def test_run_successfully
assert_equal("Hello", Zine.first.title)
assert_equal("Hello", zines(:going_out).title)
end
end
old_handler = ActiveRecord::Base.connection_handler
ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
assert_deprecated do
ActiveRecord::Base.connection_handlers = {}
end
ActiveRecord::Base.establish_connection(:arunit)
test_result = klass.new("test_run_successfully").run
assert_predicate(test_result, :passed?)
ensure
clean_up_legacy_connection_handlers
ActiveRecord::Base.connection_handler = old_handler
FileUtils.rm_r(tmp_dir)
ActiveRecord.legacy_connection_handling = old_value
end
def test_doesnt_rely_on_active_support_test_case_specific_methods
tmp_dir = Dir.mktmpdir
File.write(File.join(tmp_dir, "zines.yml"), <<~YML)
going_out:
title: Hello
YML
klass = Class.new(Minitest::Test) do
include ActiveRecord::TestFixtures
self.fixture_path = tmp_dir
self.use_transactional_tests = true
fixtures :all
def test_run_successfully
assert_equal("Hello", Zine.first.title)
assert_equal("Hello", zines(:going_out).title)
end
end
old_handler = ActiveRecord::Base.connection_handler
ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
ActiveRecord::Base.establish_connection(:arunit)
test_result = klass.new("test_run_successfully").run
assert_predicate(test_result, :passed?)
ensure
ActiveRecord::Base.connection_handler = old_handler
clean_up_connection_handler
FileUtils.rm_r(tmp_dir)
end
end
end