Compare commits
2 Commits
master
...
db_from_ur
Author | SHA1 | Date |
---|---|---|
John Bintz | 9d4429221e | |
John Bintz | 11a5d8bd53 |
|
@ -34,6 +34,7 @@ module Mongo
|
||||||
|
|
||||||
DEFAULT_HOST = 'localhost'
|
DEFAULT_HOST = 'localhost'
|
||||||
DEFAULT_PORT = 27017
|
DEFAULT_PORT = 27017
|
||||||
|
DEFAULT_DB_NAME = 'test'
|
||||||
GENERIC_OPTS = [:ssl, :auths, :pool_size, :pool_timeout, :timeout, :op_timeout, :connect_timeout, :safe, :logger, :connect]
|
GENERIC_OPTS = [:ssl, :auths, :pool_size, :pool_timeout, :timeout, :op_timeout, :connect_timeout, :safe, :logger, :connect]
|
||||||
CONNECTION_OPTS = [:slave_ok]
|
CONNECTION_OPTS = [:slave_ok]
|
||||||
|
|
||||||
|
@ -312,7 +313,13 @@ module Mongo
|
||||||
# @return [Mongo::DB]
|
# @return [Mongo::DB]
|
||||||
#
|
#
|
||||||
# @core databases db-instance_method
|
# @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)
|
DB.new(db_name, self, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -82,6 +82,46 @@ class TestConnection < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
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
|
def test_server_version
|
||||||
assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
|
assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue