2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../../test_helper", __FILE__)
|
2009-11-02 18:22:46 +00:00
|
|
|
|
2011-02-20 15:00:06 +00:00
|
|
|
def insert_message(db, documents)
|
|
|
|
documents = [documents] unless documents.is_a?(Array)
|
|
|
|
message = ByteBuffer.new
|
|
|
|
message.put_int(0)
|
|
|
|
Mongo::BSON_CODER.serialize_cstr(message, "#{db.name}.test")
|
|
|
|
documents.each { |doc| message.put_array(Mongo::BSON_CODER.new.serialize(doc, true).to_a) }
|
|
|
|
message = db.add_message_headers(Mongo::Constants::OP_INSERT, message)
|
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
class DBTest < Test::Unit::TestCase
|
|
|
|
context "DBTest: " do
|
|
|
|
context "DB commands" do
|
|
|
|
setup do
|
|
|
|
@conn = stub()
|
2010-11-03 21:36:08 +00:00
|
|
|
@conn.stubs(:safe)
|
2011-08-29 21:49:58 +00:00
|
|
|
@conn.stubs(:read_preference)
|
2010-02-22 20:49:04 +00:00
|
|
|
@db = DB.new("testing", @conn)
|
2010-11-03 21:36:08 +00:00
|
|
|
@db.stubs(:safe)
|
2011-08-29 21:49:58 +00:00
|
|
|
@db.stubs(:read_preference)
|
2010-02-22 20:49:04 +00:00
|
|
|
@collection = mock()
|
|
|
|
@db.stubs(:system_command_collection).returns(@collection)
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error if given a hash with more than one key" do
|
2010-05-18 20:17:17 +00:00
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
@db.command(:buildinfo => 1, :somekey => 1)
|
|
|
|
end
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error if the selector is omitted" do
|
|
|
|
assert_raise MongoArgumentError do
|
2010-07-12 18:31:12 +00:00
|
|
|
@db.command({}, :check_response => true)
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:22:46 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "create the proper cursor" do
|
|
|
|
@cursor = mock(:next_document => {"ok" => 1})
|
2010-07-12 18:31:12 +00:00
|
|
|
Cursor.expects(:new).with(@collection,
|
2010-02-22 20:49:04 +00:00
|
|
|
:limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
|
2009-11-02 18:22:46 +00:00
|
|
|
command = {:buildinfo => 1}
|
2010-07-12 18:31:12 +00:00
|
|
|
@db.command(command, :check_response => true)
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error when the command fails" do
|
|
|
|
@cursor = mock(:next_document => {"ok" => 0})
|
2010-07-12 18:31:12 +00:00
|
|
|
Cursor.expects(:new).with(@collection,
|
2010-02-22 20:49:04 +00:00
|
|
|
:limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
command = {:buildinfo => 1}
|
2010-07-12 18:31:12 +00:00
|
|
|
@db.command(command, :check_response => true)
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error if logging out fails" do
|
|
|
|
@db.expects(:command).returns({})
|
2011-01-31 19:47:05 +00:00
|
|
|
@conn.expects(:pool_size).returns(1)
|
2010-04-05 14:39:55 +00:00
|
|
|
assert_raise Mongo::MongoDBError do
|
2010-02-22 20:49:04 +00:00
|
|
|
@db.logout
|
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error if collection creation fails" do
|
|
|
|
@db.expects(:collection_names).returns([])
|
2010-06-01 02:52:03 +00:00
|
|
|
@db.expects(:command).returns({'ok' => 0})
|
2010-04-05 14:39:55 +00:00
|
|
|
assert_raise Mongo::MongoDBError do
|
2010-02-22 20:49:04 +00:00
|
|
|
@db.create_collection("foo")
|
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error if getlasterror fails" do
|
|
|
|
@db.expects(:command).returns({})
|
2010-04-05 14:39:55 +00:00
|
|
|
assert_raise Mongo::MongoDBError do
|
2010-09-07 12:47:43 +00:00
|
|
|
@db.get_last_error
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error if drop_index fails" do
|
|
|
|
@db.expects(:command).returns({})
|
2010-04-05 14:39:55 +00:00
|
|
|
assert_raise Mongo::MongoDBError do
|
2010-02-22 20:49:04 +00:00
|
|
|
@db.drop_index("foo", "bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if set_profiling_level fails" do
|
|
|
|
@db.expects(:command).returns({})
|
2010-04-05 14:39:55 +00:00
|
|
|
assert_raise Mongo::MongoDBError do
|
2010-02-22 20:49:04 +00:00
|
|
|
@db.profiling_level = :slow_only
|
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
|
|
|
end
|