1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class MergeAndResolveDefaultUrlConfigTest < ActiveRecord::TestCase
def klass
ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig
end
def setup
@previous_database_url = ENV.delete("DATABASE_URL")
end
teardown do
ENV["DATABASE_URL"] = @previous_database_url
end
def resolve(spec, config)
ConnectionSpecification::Resolver.new(klass.new(config).resolve).resolve(spec)
end
def spec(spec, config)
ConnectionSpecification::Resolver.new(klass.new(config).resolve).spec(spec)
end
def test_resolver_with_database_uri_and_known_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve(:production, config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_known_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = assert_deprecated { resolve("production", config) }
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_unknown_symbol_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve(:production, config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_unknown_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
assert_raises AdapterNotSpecified do
spec("production", config)
end
end
def test_resolver_with_database_uri_and_supplied_url
ENV['DATABASE_URL'] = "not-postgres://not-localhost/not_foo"
config = { "production" => { "adapter" => "also_not_postgres", "database" => "also_not_foo" } }
actual = resolve("postgres://localhost/foo", config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_jdbc_url
config = { "production" => { "url" => "jdbc:postgres://localhost/foo" } }
actual = klass.new(config).resolve
assert_equal config, actual
end
def test_environment_does_not_exist_in_config_url_does_exist
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = klass.new(config).resolve
expect_prod = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expect_prod, actual["production"]
end
def test_string_connection
config = { "production" => "postgres://localhost/foo" }
actual = klass.new(config).resolve
expected = { "production" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_url_sub_key
config = { "production" => { "url" => "postgres://localhost/foo" } }
actual = klass.new(config).resolve
expected = { "production" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_hash
config = { "production" => { "adapter" => "postgres", "database" => "foo" } }
actual = klass.new(config).resolve
assert_equal config, actual
end
def test_blank
config = {}
actual = klass.new(config).resolve
assert_equal config, actual
end
def test_blank_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {}
actual = klass.new(config).resolve
expected = { "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost" }
assert_equal expected, actual["production"]
assert_equal expected, actual["development"]
assert_equal expected, actual["test"]
assert_equal nil, actual[:production]
assert_equal nil, actual[:development]
assert_equal nil, actual[:test]
end
def test_url_sub_key_with_database_url
ENV['DATABASE_URL'] = "NOT-POSTGRES://localhost/NOT_FOO"
config = { "production" => { "url" => "postgres://localhost/foo" } }
actual = klass.new(config).resolve
expected = { "production" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_merge_no_conflicts_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {"production" => { "pool" => "5" } }
actual = klass.new(config).resolve
expected = { "production" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost",
"pool" => "5"
}
}
assert_equal expected, actual
end
def test_merge_conflicts_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {"production" => { "adapter" => "NOT-POSTGRES", "database" => "NOT-FOO", "pool" => "5" } }
actual = klass.new(config).resolve
expected = { "production" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost",
"pool" => "5"
}
}
assert_equal expected, actual
end
end
class ConnectionHandlerTest < ActiveRecord::TestCase
def setup
@klass = Class.new(Base) { def self.name; 'klass'; end }
@subklass = Class.new(@klass) { def self.name; 'subklass'; end }
@handler = ConnectionHandler.new
@pool = @handler.establish_connection(@klass, Base.connection_pool.spec)
end
def test_retrieve_connection
assert @handler.retrieve_connection(@klass)
end
def test_active_connections?
assert !@handler.active_connections?
assert @handler.retrieve_connection(@klass)
assert @handler.active_connections?
@handler.clear_active_connections!
assert !@handler.active_connections?
end
def test_retrieve_connection_pool_with_ar_base
assert_nil @handler.retrieve_connection_pool(ActiveRecord::Base)
end
def test_retrieve_connection_pool
assert_not_nil @handler.retrieve_connection_pool(@klass)
end
def test_retrieve_connection_pool_uses_superclass_when_no_subclass_connection
assert_not_nil @handler.retrieve_connection_pool(@subklass)
end
def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove
sub_pool = @handler.establish_connection(@subklass, Base.connection_pool.spec)
assert_same sub_pool, @handler.retrieve_connection_pool(@subklass)
@handler.remove_connection @subklass
assert_same @pool, @handler.retrieve_connection_pool(@subklass)
end
def test_connection_pools
assert_deprecated do
assert_equal({ Base.connection_pool.spec => @pool }, @handler.connection_pools)
end
end
end
end
end
|