mongo-ruby-driver/test/unit/db_test.rb

97 lines
3.0 KiB
Ruby
Raw Normal View History

2010-09-09 19:58:51 +00:00
require './test/test_helper'
2010-02-22 20:49:04 +00:00
class DBTest < Test::Unit::TestCase
context "DBTest: " do
2009-12-16 19:03:15 +00:00
setup do
2010-02-22 20:49:04 +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) }
2010-02-22 20:49:04 +00:00
message = db.add_message_headers(Mongo::Constants::OP_INSERT, message)
end
end
2010-02-22 20:49:04 +00:00
context "DB commands" do
setup do
@conn = stub()
@conn.stubs(:safe)
2010-02-22 20:49:04 +00:00
@db = DB.new("testing", @conn)
@db.stubs(:safe)
2010-02-22 20:49:04 +00:00
@collection = mock()
@db.stubs(:system_command_collection).returns(@collection)
end
2010-02-22 20:49:04 +00:00
should "raise an error if given a hash with more than one key" do
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
end
2010-02-22 20:49:04 +00:00
should "raise an error if the selector is omitted" do
assert_raise MongoArgumentError do
@db.command({}, :check_response => true)
2010-02-22 20:49:04 +00:00
end
end
2010-02-22 20:49:04 +00:00
should "create the proper cursor" do
@cursor = mock(:next_document => {"ok" => 1})
Cursor.expects(:new).with(@collection,
2010-02-22 20:49:04 +00:00
:limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
command = {:buildinfo => 1}
@db.command(command, :check_response => true)
end
2010-02-22 20:49:04 +00:00
should "raise an error when the command fails" do
@cursor = mock(:next_document => {"ok" => 0})
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}
@db.command(command, :check_response => true)
2010-02-22 20:49:04 +00:00
end
end
2010-02-22 20:49:04 +00:00
should "raise an error if logging out fails" do
@db.expects(:command).returns({})
@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
end
2010-02-22 20:49:04 +00:00
should "raise an error if collection creation fails" do
@db.expects(:collection_names).returns([])
@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
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
@db.get_last_error
2010-02-22 20:49:04 +00:00
end
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
end
end
end
end