Added server version checking. Updated tests for compatibility > 1.1.3

This commit is contained in:
Kyle Banker 2009-10-26 14:54:33 -04:00
parent bffc59ffb4
commit 5c6f53e408
7 changed files with 131 additions and 20 deletions

View File

@ -6,6 +6,7 @@ require 'mongo/types/regexp_of_holding'
require 'mongo/util/conversions'
require 'mongo/util/support'
require 'mongo/util/server_version'
require 'mongo/errors'
require 'mongo/constants'

View File

@ -120,6 +120,17 @@ module Mongo
single_db_command(name, :dropDatabase => 1)
end
# Return the build information for the current connection.
def server_info
db("admin").db_command(:buildinfo => 1)
end
# Returns the build version of the current server, using
# a ServerVersion object for comparability.
def server_version
ServerVersion.new(server_info["version"])
end
protected
# Turns an array containing a host name string and a

View File

@ -0,0 +1,54 @@
module Mongo
# Simple class for comparing server versions.
class ServerVersion
include Comparable
def initialize(version)
@version = version
end
# Implements comparable.
def <=>(new)
local, new = self.to_a, to_array(new)
for n in 0...local.size do
break if elements_include_mods?(local[n], new[n])
if local[n] < new[n].to_i
result = -1
break;
elsif local[n] > new[n].to_i
result = 1
break;
end
end
result || 0
end
# Return an array representation of this server version.
def to_a
to_array(@version)
end
# Return a string representation of this server version.
def to_s
@version
end
private
# Returns true if any elements include mod symbols (-, +)
def elements_include_mods?(*elements)
elements.any? { |n| n =~ /[\-\+]/ }
end
# Converts argument to an array of integers,
# appending any mods as the final element.
def to_array(version)
array = version.split(".").map {|n| (n =~ /^\d+$/) ? n.to_i : n }
if array.last =~ /(\d+)([\-\+])/
array[array.length-1] = $1.to_i
array << $2
end
array
end
end
end

View File

@ -16,9 +16,10 @@
require 'test/test_helper'
class TestCollection < Test::Unit::TestCase
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
@@db = @@connection.db('ruby-mongo-test')
@@test = @@db.collection("test")
@@version = @@connection.server_version
def setup
@@test.drop()
@ -89,15 +90,33 @@ class TestCollection < Test::Unit::TestCase
assert_equal 2, @@test.find_one()["count"]
end
def test_safe_update
@@test.create_index("x")
@@test.insert("x" => 5)
if @@version < "1.1.3"
def test_safe_update
@@test.create_index("x")
@@test.insert("x" => 5)
@@test.update({}, {"$inc" => {"x" => 1}})
assert @@db.error?
@@test.update({}, {"$inc" => {"x" => 1}})
assert @@db.error?
assert_raise OperationFailure do
@@test.update({}, {"$inc" => {"x" => 1}}, :safe => true)
# Can't change an index.
assert_raise OperationFailure do
@@test.update({}, {"$inc" => {"x" => 1}}, :safe => true)
end
end
else
def test_safe_update
@@test.create_index("x", true)
@@test.insert("x" => 5)
@@test.insert("x" => 10)
# Can update an indexed collection.
@@test.update({}, {"$inc" => {"x" => 1}})
assert !@@db.error?
# Can't duplicate an index.
assert_raise OperationFailure do
@@test.update({}, {"x" => 10}, :safe => true, :upsert => true)
end
end
end

View File

@ -19,6 +19,17 @@ class TestConnection < Test::Unit::TestCase
@mongo.db('ruby-mongo-test').error
end
def test_server_info
server_info = @mongo.server_info
assert server_info.keys.include? "version"
assert server_info.keys.include? "bits"
assert_equal 1.0, server_info["ok"]
end
def test_server_version
assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s
end
def test_invalid_database_names
assert_raise TypeError do @mongo.db(4) end

View File

@ -7,9 +7,11 @@ class CursorTest < Test::Unit::TestCase
include Mongo
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
@@db = @@connection.db('ruby-mongo-test')
@@coll = @@db.collection('test')
@@version = @@connection.server_version
def setup
@@coll.remove
@ -321,9 +323,10 @@ class CursorTest < Test::Unit::TestCase
@@coll.remove
@@coll.save("x" => 1)
@@coll.find({}, :fields => ["a"]).each do |doc|
fail "shouldn't have any results here"
if @@version < "1.1.3"
assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
else
assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
end
assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
end
end

View File

@ -6,9 +6,11 @@ require 'test/unit'
class DBAPITest < Test::Unit::TestCase
include Mongo
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
@@db = @@connection.db("ruby-mongo-test")
@@coll = @@db.collection('test')
@@version = @@connection.server_version
def setup
@@coll.remove
@ -386,16 +388,26 @@ class DBAPITest < Test::Unit::TestCase
def test_array
@@coll << {'b' => [1, 2, 3]}
rows = @@coll.find({}, {:fields => ['b']}).to_a
assert_equal 1, rows.length
assert_equal [1, 2, 3], rows[0]['b']
if @@version < "1.1.3"
assert_equal 1, rows.length
assert_equal [1, 2, 3], rows[0]['b']
else
assert_equal 2, rows.length
assert_equal [1, 2, 3], rows[1]['b']
end
end
def test_regex
regex = /foobar/i
@@coll << {'b' => regex}
rows = @@coll.find({}, {:fields => ['b']}).to_a
assert_equal 1, rows.length
assert_equal regex, rows[0]['b']
if @@version < "1.1.3"
assert_equal 1, rows.length
assert_equal regex, rows[0]['b']
else
assert_equal 2, rows.length
assert_equal regex, rows[1]['b']
end
end
def test_non_oid_id