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/disconnected_test.rb
eileencodes eeacc03454 Merge ConnectionSpecification + Role -> Role
In order to move schema_cache off of DatabaseConfiguration we needed to
make role accessible on pool.

While looking at `ConnectionSpecification`, `Role`, and `DatabaseConfig` John
and I noticed that this could be achieved by merging `ConnectionSpecification`
and `Role` into one `Role` class. This allows us to eliminate the `spec`
concept which is confusing. `spec` is a private method so renaming
to `resolve_role` is ok.

In the `Role` class we took `name` (renamed to `connection_specification_name`
for clarity since it's not a `role` name) and `db_config` from
`ConnectionSpecification` and the `pool` methods from `Role` and combined
them into one `Role` class.

This feels a lot cleaner to us because it clarifies the purposes of the
classes/methods/variables, and makes it easier to drop
`connection_specification_name` keyed on the parent class in the future.

There are a lot of changes in here but the majority of them are find and
replace `spec` -> `role`, `spec.name` ->
`role.connection_specification_name`, `Resolver#spec` ->
`Resolver#resolve_role`.

This PR also moves the `schema_cache` from `DatabaseConfig` to the new
combined `Role` class.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-10-18 10:26:08 -05:00

32 lines
761 B
Ruby

# frozen_string_literal: true
require "cases/helper"
class TestRecord < ActiveRecord::Base
end
class TestDisconnectedAdapter < ActiveRecord::TestCase
self.use_transactional_tests = false
def setup
@connection = ActiveRecord::Base.connection
end
teardown do
return if in_memory_db?
role = ActiveRecord::Base.connection_config
ActiveRecord::Base.establish_connection(role)
end
unless in_memory_db?
test "can't execute statements while disconnected" do
@connection.execute "SELECT count(*) from products"
@connection.disconnect!
assert_raises(ActiveRecord::StatementInvalid) do
silence_warnings do
@connection.execute "SELECT count(*) from products"
end
end
end
end
end