2009-01-16 14:52:31 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'mongo'
|
|
|
|
require 'test/unit'
|
2009-09-11 16:12:24 +00:00
|
|
|
require 'logger'
|
|
|
|
require 'stringio'
|
2009-01-16 14:52:31 +00:00
|
|
|
|
|
|
|
# NOTE: assumes Mongo is running
|
2009-08-20 22:48:09 +00:00
|
|
|
class TestConnection < Test::Unit::TestCase
|
2009-01-16 14:52:31 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-01-16 14:52:31 +00:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2009-08-20 22:48:09 +00:00
|
|
|
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
|
|
|
@mongo = Connection.new(@host, @port)
|
2009-01-16 14:52:31 +00:00
|
|
|
end
|
|
|
|
|
2009-03-13 21:09:19 +00:00
|
|
|
def teardown
|
|
|
|
@mongo.db('ruby-mongo-test').error
|
|
|
|
end
|
|
|
|
|
2009-10-26 18:54:33 +00:00
|
|
|
def test_server_info
|
|
|
|
server_info = @mongo.server_info
|
2009-10-26 20:41:17 +00:00
|
|
|
assert server_info.keys.include?("version")
|
2009-10-26 18:54:33 +00:00
|
|
|
assert_equal 1.0, server_info["ok"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_server_version
|
|
|
|
assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s
|
|
|
|
end
|
|
|
|
|
2009-08-13 20:26:51 +00:00
|
|
|
def test_invalid_database_names
|
|
|
|
assert_raise TypeError do @mongo.db(4) end
|
|
|
|
|
|
|
|
assert_raise InvalidName do @mongo.db('') end
|
|
|
|
assert_raise InvalidName do @mongo.db('te$t') end
|
|
|
|
assert_raise InvalidName do @mongo.db('te.t') end
|
|
|
|
assert_raise InvalidName do @mongo.db('te\\t') end
|
|
|
|
assert_raise InvalidName do @mongo.db('te/t') end
|
|
|
|
assert_raise InvalidName do @mongo.db('te st') end
|
|
|
|
end
|
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
def test_database_info
|
2009-01-28 19:21:23 +00:00
|
|
|
@mongo.drop_database('ruby-mongo-info-test')
|
|
|
|
@mongo.db('ruby-mongo-info-test').collection('info-test').insert('a' => 1)
|
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
info = @mongo.database_info
|
|
|
|
assert_not_nil info
|
|
|
|
assert_kind_of Hash, info
|
2009-01-28 19:21:23 +00:00
|
|
|
assert_not_nil info['ruby-mongo-info-test']
|
|
|
|
assert info['ruby-mongo-info-test'] > 0
|
|
|
|
|
|
|
|
@mongo.drop_database('ruby-mongo-info-test')
|
2009-01-16 14:52:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_database_names
|
2009-01-28 19:21:23 +00:00
|
|
|
@mongo.drop_database('ruby-mongo-info-test')
|
|
|
|
@mongo.db('ruby-mongo-info-test').collection('info-test').insert('a' => 1)
|
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
names = @mongo.database_names
|
|
|
|
assert_not_nil names
|
|
|
|
assert_kind_of Array, names
|
|
|
|
assert names.length >= 1
|
2009-01-28 19:21:23 +00:00
|
|
|
assert names.include?('ruby-mongo-info-test')
|
|
|
|
|
|
|
|
@mongo.drop_database('ruby-mongo-info-test')
|
2009-01-16 14:52:31 +00:00
|
|
|
end
|
|
|
|
|
2009-09-11 16:12:24 +00:00
|
|
|
def test_logging
|
|
|
|
output = StringIO.new
|
|
|
|
logger = Logger.new(output)
|
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test')
|
|
|
|
|
2009-10-20 19:44:46 +00:00
|
|
|
assert output.string.include?("2004")
|
2009-09-11 16:12:24 +00:00
|
|
|
end
|
2009-10-07 23:39:36 +00:00
|
|
|
|
|
|
|
def test_connection_logger
|
|
|
|
output = StringIO.new
|
|
|
|
logger = Logger.new(output)
|
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
connection = Connection.new(@host, @port, :logger => logger)
|
|
|
|
assert_equal logger, connection.logger
|
|
|
|
|
|
|
|
connection.logger.debug 'testing'
|
|
|
|
assert output.string.include?('testing')
|
|
|
|
end
|
2009-09-11 16:12:24 +00:00
|
|
|
|
2009-01-20 14:21:19 +00:00
|
|
|
def test_drop_database
|
2009-01-28 19:21:23 +00:00
|
|
|
db = @mongo.db('ruby-mongo-will-be-deleted')
|
2009-01-20 14:21:19 +00:00
|
|
|
coll = db.collection('temp')
|
2009-10-20 15:31:07 +00:00
|
|
|
coll.remove
|
2009-01-20 14:21:19 +00:00
|
|
|
coll.insert(:name => 'temp')
|
|
|
|
assert_equal 1, coll.count()
|
2009-01-28 19:21:23 +00:00
|
|
|
assert @mongo.database_names.include?('ruby-mongo-will-be-deleted')
|
2009-01-20 14:21:19 +00:00
|
|
|
|
2009-01-28 19:21:23 +00:00
|
|
|
@mongo.drop_database('ruby-mongo-will-be-deleted')
|
|
|
|
assert !@mongo.database_names.include?('ruby-mongo-will-be-deleted')
|
2009-01-20 14:21:19 +00:00
|
|
|
end
|
|
|
|
|
2009-01-23 18:30:59 +00:00
|
|
|
def test_pair
|
2009-08-20 22:48:09 +00:00
|
|
|
db = Connection.new({:left => ['foo', 123]})
|
2009-01-23 18:30:59 +00:00
|
|
|
pair = db.instance_variable_get('@pair')
|
|
|
|
assert_equal 2, pair.length
|
|
|
|
assert_equal ['foo', 123], pair[0]
|
2009-08-20 22:48:09 +00:00
|
|
|
assert_equal ['localhost', Connection::DEFAULT_PORT], pair[1]
|
2009-01-23 18:30:59 +00:00
|
|
|
|
2009-08-20 22:48:09 +00:00
|
|
|
db = Connection.new({:right => 'bar'})
|
2009-01-23 18:30:59 +00:00
|
|
|
pair = db.instance_variable_get('@pair')
|
|
|
|
assert_equal 2, pair.length
|
2009-08-20 22:48:09 +00:00
|
|
|
assert_equal ['localhost', Connection::DEFAULT_PORT], pair[0]
|
|
|
|
assert_equal ['bar', Connection::DEFAULT_PORT], pair[1]
|
2009-01-23 18:30:59 +00:00
|
|
|
|
2009-08-20 22:48:09 +00:00
|
|
|
db = Connection.new({:right => ['foo', 123], :left => 'bar'})
|
2009-01-23 18:30:59 +00:00
|
|
|
pair = db.instance_variable_get('@pair')
|
|
|
|
assert_equal 2, pair.length
|
2009-08-20 22:48:09 +00:00
|
|
|
assert_equal ['bar', Connection::DEFAULT_PORT], pair[0]
|
2009-01-23 18:30:59 +00:00
|
|
|
assert_equal ['foo', 123], pair[1]
|
|
|
|
end
|
2009-08-20 22:48:09 +00:00
|
|
|
end
|