Move and update enlist_fixture_connections test

This moves the previous test into the old test and reuses the connection
that that test establishes rather than requiring we muck with temporary
connection pool.

The change here is more correct than the previous code since we're
establishing new connections we should be checking the newly established
reading and writing connections are the same, not checking against the
existing ActiveRecord::Base.connection.

The test here also most closely emulates a real application using
multiple databases.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
This commit is contained in:
eileencodes 2020-05-13 11:01:11 -04:00
parent d4ebba275e
commit e9d1c9a2fc
No known key found for this signature in database
GPG Key ID: BA5C575120BBE8DF

@ -1391,37 +1391,8 @@ def self.file_fixture_path
end
end
class MultipleDatabaseFixturesTest < ActiveRecord::TestCase
test "enlist_fixture_connections ensures multiple databases share a connection pool" do
old_handlers = ActiveRecord::Base.connection_handlers
ActiveRecord::Base.connection_handlers = {}
with_temporary_connection_pool do
ActiveRecord::Base.connects_to database: { writing: :arunit, reading: :arunit2 }
rw_conn = ActiveRecord::Base.connection
ro_conn = ActiveRecord::Base.connection_handlers[:reading].connection_pool_list.first.connection
assert_equal rw_conn, ro_conn
end
ensure
ActiveRecord::Base.connection_handlers = old_handlers
end
private
def with_temporary_connection_pool
pool_config = ActiveRecord::Base.connection_handler.send(:owner_to_pool_manager).fetch("ActiveRecord::Base").get_pool_config(:default)
new_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(pool_config)
pool_config.stub(:pool, new_pool) do
yield
end
end
end
class UsesWritingConnectionForFixtures < ActiveRecord::TestCase
class MultipleFixtureConnectionsTest < ActiveRecord::TestCase
include ActiveRecord::TestFixtures
self.use_transactional_tests = true
fixtures :dogs
@ -1454,6 +1425,13 @@ def test_uses_writing_connection_for_fixtures
end
end
def test_writing_and_reading_connections_are_the_same
rw_conn = ActiveRecord::Base.connection_handlers[:writing].connection_pool_list.first.connection
ro_conn = ActiveRecord::Base.connection_handlers[:reading].connection_pool_list.first.connection
assert_equal rw_conn, ro_conn
end
private
def config
{ "default" => default_config, "readonly" => readonly_config }