2009-11-02 18:22:46 +00:00
|
|
|
require 'test/test_helper'
|
|
|
|
|
2010-02-20 00:17:38 +00:00
|
|
|
context "DBTest: " do
|
|
|
|
setup do
|
|
|
|
def insert_message(db, documents)
|
|
|
|
documents = [documents] unless documents.is_a?(Array)
|
|
|
|
message = ByteBuffer.new
|
|
|
|
message.put_int(0)
|
|
|
|
BSON.serialize_cstr(message, "#{db.name}.test")
|
|
|
|
documents.each { |doc| message.put_array(BSON.new.serialize(doc, true).to_a) }
|
|
|
|
message = db.add_message_headers(Mongo::Constants::OP_INSERT, message)
|
|
|
|
end
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
context "DB commands" do
|
|
|
|
setup do
|
2009-11-23 20:20:05 +00:00
|
|
|
@conn = stub()
|
|
|
|
@db = DB.new("testing", @conn)
|
2009-11-02 18:22:46 +00:00
|
|
|
@collection = mock()
|
|
|
|
@db.stubs(:system_command_collection).returns(@collection)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if given a hash with more than one key" do
|
2009-12-16 19:03:15 +00:00
|
|
|
assert_raise MongoArgumentError do
|
2009-11-02 18:22:46 +00:00
|
|
|
@db.command(:buildinfo => 1, :somekey => 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
should "raise an error if the selector is omitted" do
|
|
|
|
assert_raise MongoArgumentError do
|
2009-11-02 18:22:46 +00:00
|
|
|
@db.command({}, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
should "create the proper cursor" do
|
|
|
|
@cursor = mock(:next_document => {"ok" => 1})
|
2009-11-02 18:22:46 +00:00
|
|
|
Cursor.expects(:new).with(@collection, :admin => true,
|
2009-11-23 20:20:05 +00:00
|
|
|
:limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
|
2009-11-02 18:22:46 +00:00
|
|
|
command = {:buildinfo => 1}
|
|
|
|
@db.command(command, true)
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
should "raise an error when the command fails" do
|
|
|
|
@cursor = mock(:next_document => {"ok" => 0})
|
2009-11-02 18:22:46 +00:00
|
|
|
Cursor.expects(:new).with(@collection, :admin => true,
|
2009-11-23 20:20:05 +00:00
|
|
|
:limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
|
2009-12-16 19:03:15 +00:00
|
|
|
assert_raise OperationFailure do
|
2009-11-02 18:22:46 +00:00
|
|
|
command = {:buildinfo => 1}
|
|
|
|
@db.command(command, true, true)
|
|
|
|
end
|
|
|
|
end
|
2010-01-11 23:11:38 +00:00
|
|
|
|
|
|
|
should "raise an error if logging out fails" do
|
|
|
|
@db.expects(:command).returns({})
|
|
|
|
assert_raise MongoDBError do
|
|
|
|
@db.logout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if collection creation fails" do
|
|
|
|
@db.expects(:collection_names).returns([])
|
|
|
|
@db.expects(:command).returns({})
|
|
|
|
assert_raise MongoDBError do
|
|
|
|
@db.create_collection("foo")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if getlasterror fails" do
|
|
|
|
@db.expects(:command).returns({})
|
|
|
|
assert_raise MongoDBError do
|
|
|
|
@db.error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if rename fails" do
|
|
|
|
@db.expects(:command).returns({})
|
|
|
|
assert_raise MongoDBError do
|
|
|
|
@db.rename_collection("foo", "bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if drop_index fails" do
|
|
|
|
@db.expects(:command).returns({})
|
|
|
|
assert_raise MongoDBError do
|
|
|
|
@db.drop_index("foo", "bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "raise an error if set_profiling_level fails" do
|
|
|
|
@db.expects(:command).returns({})
|
|
|
|
assert_raise MongoDBError do
|
|
|
|
@db.profiling_level = :slow_only
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
|