aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-07-21 12:57:05 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-07-21 12:57:05 -0700
commit1a0b7ea3c903493dc44b2fd650cd8e7f3b234e46 (patch)
treed993a48b4c3d94389d3d8d00afe1afc913c4e68e
parentf4ed9751251b2e979ee7bf38faf74371e78bbf42 (diff)
downloadrails-1a0b7ea3c903493dc44b2fd650cd8e7f3b234e46.tar.gz
rails-1a0b7ea3c903493dc44b2fd650cd8e7f3b234e46.tar.bz2
rails-1a0b7ea3c903493dc44b2fd650cd8e7f3b234e46.zip
adding more tests around database uri parsing
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb8
-rw-r--r--activerecord/test/cases/connection_management_test.rb34
2 files changed, 37 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
index ca83173d3a..ab07253e86 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
@@ -89,8 +89,8 @@ module ActiveRecord
end
end
- def self.connection_url_to_hash(url)
- config = URI.parse(url)
+ def self.connection_url_to_hash(url) # :nodoc:
+ config = URI.parse url
adapter = config.scheme
adapter = "postgresql" if adapter == "postgres"
spec = { :adapter => adapter,
@@ -99,14 +99,12 @@ module ActiveRecord
:port => config.port,
:database => config.path.sub(%r{^/},""),
:host => config.host }
- spec.reject!{ |key,value| value.nil? }
+ spec.reject!{ |_,value| !value }
if config.query
options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
spec.merge!(options)
end
spec
- rescue URI::InvalidURIError
- return nil
end
class << self
diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb
index a1d1177289..f554ceef35 100644
--- a/activerecord/test/cases/connection_management_test.rb
+++ b/activerecord/test/cases/connection_management_test.rb
@@ -25,6 +25,40 @@ module ActiveRecord
assert ActiveRecord::Base.connection_handler.active_connections?
end
+ class FakeBase < ActiveRecord::Base
+ def self.establish_connection spec
+ String === spec ? super : spec
+ end
+ end
+
+ def test_url_host_no_db
+ spec = FakeBase.establish_connection 'postgres://foo?encoding=utf8'
+ assert_equal({
+ :adapter => "postgresql",
+ :database => "",
+ :host => "foo",
+ :encoding => "utf8" }, spec)
+ end
+
+ def test_url_host_db
+ spec = FakeBase.establish_connection 'postgres://foo/bar?encoding=utf8'
+ assert_equal({
+ :adapter => "postgresql",
+ :database => "bar",
+ :host => "foo",
+ :encoding => "utf8" }, spec)
+ end
+
+ def test_url_port
+ spec = FakeBase.establish_connection 'postgres://foo:123?encoding=utf8'
+ assert_equal({
+ :adapter => "postgresql",
+ :database => "",
+ :port => 123,
+ :host => "foo",
+ :encoding => "utf8" }, spec)
+ end
+
def test_app_delegation
manager = ConnectionManagement.new(@app)