rails/activerecord/test/cases/unconnected_test.rb
eileencodes 3dffb0da78
Ensure connection_name is used everywhere
Looking at connection management we were using `spec_name`,
`spec`, `owner_name`, `owner` and `connection_specification_name` to all mean
the "string representation of the name we use to lookup the connection".

In most applications this is the string representation of the class that
established the connection. In some rarer cases this is represented by
either passing a string to `owner_name` on `establish_connection` or
passing a config as a symbol which gets turned into an `owner_name`.
This behavior is undocumented and legacy, in most cases no longer
necessary. However I know that it is still in use so I'm going to slowly
work on replacing it so the behavior is less confusing.

For now this PR simply renames all the interal words to mean "string
that established the connection" to `connection_name`. In my next PR
I'll address the public APIs around `connection_specification_name` on
`Base` and `owner_name` on `ConnectionHandler`.

Note that the instrumentation in `establish_connection` is private
(denoted by the !), so it is safe to change without warning. Everything
else is internal naming or part of a private API.
2022-07-11 13:44:57 -04:00

47 lines
1.2 KiB
Ruby

# frozen_string_literal: true
require "cases/helper"
class TestRecord < ActiveRecord::Base
end
class TestUnconnectedAdapter < ActiveRecord::TestCase
self.use_transactional_tests = false
def setup
@underlying = ActiveRecord::Base.connection
@connection_name = ActiveRecord::Base.remove_connection
# Clear out connection info from other pids (like a fork parent) too
ActiveRecord::ConnectionAdapters::PoolConfig.discard_pools!
end
teardown do
@underlying = nil
ActiveRecord::Base.establish_connection(@connection_name)
load_schema if in_memory_db?
end
def test_connection_no_longer_established
assert_raise(ActiveRecord::ConnectionNotEstablished) do
TestRecord.find(1)
end
assert_raise(ActiveRecord::ConnectionNotEstablished) do
TestRecord.new.save
end
end
def test_error_message_when_connection_not_established
error = assert_raise(ActiveRecord::ConnectionNotEstablished) do
TestRecord.find(1)
end
assert_equal "No connection pool for 'ActiveRecord::Base' found.", error.message
end
def test_underlying_adapter_no_longer_active
assert_not @underlying.active?, "Removed adapter should no longer be active"
end
end