@@ -20,6 +20,66 @@ def test_establish_connection_uses_spec_name
2020 @handler . remove_connection ( "readonly" )
2121 end
2222
23+ def test_establish_connection_using_3_levels_config
24+ previous_env , ENV [ "RAILS_ENV" ] = ENV [ "RAILS_ENV" ] , "default_env"
25+
26+ config = {
27+ "default_env" => {
28+ "readonly" => { "adapter" => "sqlite3" , "database" => "db/readonly.sqlite3" } ,
29+ "primary" => { "adapter" => "sqlite3" , "database" => "db/primary.sqlite3" }
30+ } ,
31+ "another_env" => {
32+ "readonly" => { "adapter" => "sqlite3" , "database" => "db/bad-readonly.sqlite3" } ,
33+ "primary" => { "adapter" => "sqlite3" , "database" => "db/bad-primary.sqlite3" }
34+ } ,
35+ "common" => { "adapter" => "sqlite3" , "database" => "db/common.sqlite3" }
36+ }
37+ @prev_configs , ActiveRecord ::Base . configurations = ActiveRecord ::Base . configurations , config
38+
39+ @handler . establish_connection ( :common )
40+ @handler . establish_connection ( :primary )
41+ @handler . establish_connection ( :readonly )
42+
43+ assert_not_nil pool = @handler . retrieve_connection_pool ( "readonly" )
44+ assert_equal "db/readonly.sqlite3" , pool . spec . config [ :database ]
45+
46+ assert_not_nil pool = @handler . retrieve_connection_pool ( "primary" )
47+ assert_equal "db/primary.sqlite3" , pool . spec . config [ :database ]
48+
49+ assert_not_nil pool = @handler . retrieve_connection_pool ( "common" )
50+ assert_equal "db/common.sqlite3" , pool . spec . config [ :database ]
51+ ensure
52+ ActiveRecord ::Base . configurations = @prev_configs
53+ ENV [ "RAILS_ENV" ] = previous_env
54+ end
55+
56+ def test_establish_connection_using_two_level_configurations
57+ config = { "development" => { "adapter" => "sqlite3" , "database" => "db/primary.sqlite3" } }
58+ @prev_configs , ActiveRecord ::Base . configurations = ActiveRecord ::Base . configurations , config
59+
60+ @handler . establish_connection ( :development )
61+
62+ assert_not_nil pool = @handler . retrieve_connection_pool ( "development" )
63+ assert_equal "db/primary.sqlite3" , pool . spec . config [ :database ]
64+ ensure
65+ ActiveRecord ::Base . configurations = @prev_configs
66+ end
67+
68+ def test_establish_connection_using_top_level_key_in_two_level_config
69+ config = {
70+ "development" => { "adapter" => "sqlite3" , "database" => "db/primary.sqlite3" } ,
71+ "development_readonly" => { "adapter" => "sqlite3" , "database" => "db/readonly.sqlite3" }
72+ }
73+ @prev_configs , ActiveRecord ::Base . configurations = ActiveRecord ::Base . configurations , config
74+
75+ @handler . establish_connection ( :development_readonly )
76+
77+ assert_not_nil pool = @handler . retrieve_connection_pool ( "development_readonly" )
78+ assert_equal "db/readonly.sqlite3" , pool . spec . config [ :database ]
79+ ensure
80+ ActiveRecord ::Base . configurations = @prev_configs
81+ end
82+
2383 def test_retrieve_connection
2484 assert @handler . retrieve_connection ( @spec_name )
2585 end
0 commit comments