fix for boolean command response in core server 1.5.2

This commit is contained in:
Kyle Banker 2010-05-31 22:52:03 -04:00
parent acf185fc33
commit a7e2991c31
8 changed files with 20 additions and 11 deletions

View File

@ -538,7 +538,7 @@ module Mongo
result = @db.command(group_command) result = @db.command(group_command)
if result["ok"] == 1 if Mongo::Support.ok?(result)
result["retval"] result["retval"]
else else
raise OperationFailure, "group command failed: #{result['errmsg']}" raise OperationFailure, "group command failed: #{result['errmsg']}"

View File

@ -423,7 +423,8 @@ module Mongo
# If we're connected to master, set the @host and @port # If we're connected to master, set the @host and @port
result = self['admin'].command({:ismaster => 1}, :check_response => false, :sock => socket) result = self['admin'].command({:ismaster => 1}, :check_response => false, :sock => socket)
if result['ok'] == 1 && ((is_master = result['ismaster'] == 1) || @slave_ok) if Mongo::Support.ok?(result) &&
((is_master = result['ismaster'] == 1) || @slave_ok)
@host, @port = host, port @host, @port = host, port
apply_saved_authentication apply_saved_authentication
end end

View File

@ -103,7 +103,7 @@ module Mongo
"query", @selector, "query", @selector,
"fields", @fields] "fields", @fields]
response = @db.command(command) response = @db.command(command)
return response['n'].to_i if response['ok'] == 1 return response['n'].to_i if Mongo::Support.ok?(response)
return 0 if response['errmsg'] == "ns missing" return 0 if response['errmsg'] == "ns missing"
raise OperationFailure, "Count failed: #{response['errmsg']}" raise OperationFailure, "Count failed: #{response['errmsg']}"
end end

View File

@ -217,8 +217,7 @@ module Mongo
oh = BSON::OrderedHash.new oh = BSON::OrderedHash.new
oh[:create] = name oh[:create] = name
doc = command(oh.merge(options || {})) doc = command(oh.merge(options || {}))
ok = doc['ok'] return Collection.new(self, name, @pk_factory) if ok?(doc)
return Collection.new(self, name, @pk_factory) if ok.kind_of?(Numeric) && (ok.to_i == 1 || ok.to_i == 0)
raise MongoDBError, "Error creating collection: #{doc.inspect}" raise MongoDBError, "Error creating collection: #{doc.inspect}"
end end
@ -427,8 +426,7 @@ module Mongo
# #
# @return [Boolean] # @return [Boolean]
def ok?(doc) def ok?(doc)
ok = doc['ok'] Mongo::Support.ok?(doc)
ok.kind_of?(Numeric) && ok.to_i == 1
end end
# Send a command to the database. # Send a command to the database.

View File

@ -68,5 +68,15 @@ module Mongo
"[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]" "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
end end
end end
# Determine if a database command has succeeded by
# checking the document response.
#
# @param [Hash] doc
#
# @return [Boolean] true if the 'ok' key is either 1 or *true*.
def ok?(doc)
doc['ok'] == 1.0 || doc['ok'] == true
end
end end
end end

View File

@ -21,7 +21,7 @@ class TestConnection < Test::Unit::TestCase
def test_server_info def test_server_info
server_info = @mongo.server_info server_info = @mongo.server_info
assert server_info.keys.include?("version") assert server_info.keys.include?("version")
assert_equal 1.0, server_info["ok"] assert Mongo::Support.ok?(server_info)
end end
def test_server_version def test_server_version
@ -71,7 +71,7 @@ class TestConnection < Test::Unit::TestCase
end end
result = @mongo.copy_database('old', 'new', 'localhost', 'bob', 'secret') result = @mongo.copy_database('old', 'new', 'localhost', 'bob', 'secret')
assert result['ok'].to_i == 1 assert Mongo::Support.ok?(result)
@mongo.drop_database('old') @mongo.drop_database('old')
@mongo.drop_database('new') @mongo.drop_database('new')

View File

@ -158,7 +158,7 @@ class DBTest < Test::Unit::TestCase
end end
result = @@db.command({:non_command => 1}, :check_response => false) result = @@db.command({:non_command => 1}, :check_response => false)
assert_equal 0, result['ok'].to_i assert !Mongo::Support.ok?(result)
end end
def test_error def test_error

View File

@ -62,7 +62,7 @@ class DBTest < Test::Unit::TestCase
should "raise an error if collection creation fails" do should "raise an error if collection creation fails" do
@db.expects(:collection_names).returns([]) @db.expects(:collection_names).returns([])
@db.expects(:command).returns({}) @db.expects(:command).returns({'ok' => 0})
assert_raise Mongo::MongoDBError do assert_raise Mongo::MongoDBError do
@db.create_collection("foo") @db.create_collection("foo")
end end