From 1a0b7ea3c903493dc44b2fd650cd8e7f3b234e46 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 21 Jul 2011 12:57:05 -0700 Subject: adding more tests around database uri parsing --- .../abstract/connection_specification.rb | 8 ++--- .../test/cases/connection_management_test.rb | 34 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) (limited to 'activerecord') 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) -- cgit v1.2.3