Compare commits

..

2 Commits

2 changed files with 48 additions and 1 deletions

View File

@ -34,6 +34,7 @@ module Mongo
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 27017
DEFAULT_DB_NAME = 'test'
GENERIC_OPTS = [:ssl, :auths, :pool_size, :pool_timeout, :timeout, :op_timeout, :connect_timeout, :safe, :logger, :connect]
CONNECTION_OPTS = [:slave_ok]
@ -312,7 +313,13 @@ module Mongo
# @return [Mongo::DB]
#
# @core databases db-instance_method
def db(db_name, opts={})
def db(db_name=nil, opts={})
if !db_name && uri = ENV['MONGODB_URI']
db_name = uri[%r{/([^/\?]+)(\?|$)}, 1]
end
db_name ||= DEFAULT_DB_NAME
DB.new(db_name, self, opts)
end

View File

@ -82,6 +82,46 @@ class TestConnection < Test::Unit::TestCase
end
end
def test_db_from_uri_exists_no_options
begin
db_name = "_database"
old_mongodb_uri = ENV['MONGODB_URI']
ENV['MONGODB_URI'] = "mongodb://#{host_port}/#{db_name}"
con = Connection.from_uri
db = con.db
assert_equal db.name, db_name
ensure
ENV['MONGODB_URI'] = old_mongodb_uri
end
end
def test_db_from_uri_exists_options
begin
db_name = "_database"
old_mongodb_uri = ENV['MONGODB_URI']
ENV['MONGODB_URI'] = "mongodb://#{host_port}/#{db_name}?"
con = Connection.from_uri
db = con.db
assert_equal db.name, db_name
ensure
ENV['MONGODB_URI'] = old_mongodb_uri
end
end
def test_db_from_uri_exists_no_db_name
begin
old_mongodb_uri = ENV['MONGODB_URI']
ENV['MONGODB_URI'] = "mongodb://#{host_port}/"
con = Connection.from_uri
db = con.db
assert_equal db.name, Mongo::Connection::DEFAULT_DB_NAME
ensure
ENV['MONGODB_URI'] = old_mongodb_uri
end
end
def test_server_version
assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
end