aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorbogdanvlviv <bogdanvlviv@gmail.com>2018-11-15 00:39:29 +0200
committerbogdanvlviv <bogdanvlviv@gmail.com>2018-11-15 11:48:07 +0200
commitcf01da283e7ff6765b9ecb21215160650c87635a (patch)
treed47906eef999919546dd2cf81b24a37476ed4b93 /activerecord/test
parentf9ba12bd5382dc3251debce81f0c82566aee7581 (diff)
downloadrails-cf01da283e7ff6765b9ecb21215160650c87635a.tar.gz
rails-cf01da283e7ff6765b9ecb21215160650c87635a.tar.bz2
rails-cf01da283e7ff6765b9ecb21215160650c87635a.zip
Exercise `connected_to` and `connects_to` methods
Since both methods are public API I think it makes sense to add these tests in order to prevent any regression in the behavior of those methods after the 6.0 release. Exercise `connected_to` - Ensure that the method raises with both `database` and `role` arguments - Ensure that the method raises without `database` and `role` Exercise `connects_to` - Ensure that the method returns an array of established connections(as mentioned in the docs of the method) Related to #34052
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
index 0e4ea62113..a394430dfe 100644
--- a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
@@ -153,6 +153,20 @@ module ActiveRecord
ENV["RAILS_ENV"] = previous_env
end
+ def test_switching_connections_with_database_and_role_raises
+ error = assert_raises(ArgumentError) do
+ ActiveRecord::Base.connected_to(database: :readonly, role: :writing) { }
+ end
+ assert_equal "connected_to can only accept a `database` or a `role` argument, but not both arguments.", error.message
+ end
+
+ def test_switching_connections_without_database_and_role_raises
+ error = assert_raises(ArgumentError) do
+ ActiveRecord::Base.connected_to { }
+ end
+ assert_equal "must provide a `database` or a `role`.", error.message
+ end
+
def test_switching_connections_with_database_symbol
previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"
@@ -207,6 +221,27 @@ module ActiveRecord
ActiveRecord::Base.configurations = @prev_configs
ActiveRecord::Base.establish_connection(:arunit)
end
+
+ def test_connects_to_returns_array_of_established_connections
+ config = {
+ "development" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" },
+ "development_readonly" => { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" }
+ }
+ @prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config
+
+ result = ActiveRecord::Base.connects_to database: { writing: :development, reading: :development_readonly }
+
+ assert_equal(
+ [
+ ActiveRecord::Base.connection_handlers[:writing].retrieve_connection_pool("primary"),
+ ActiveRecord::Base.connection_handlers[:reading].retrieve_connection_pool("primary")
+ ],
+ result
+ )
+ ensure
+ ActiveRecord::Base.configurations = @prev_configs
+ ActiveRecord::Base.establish_connection(:arunit)
+ end
end
def test_connection_pools