Merged wpiekutowski's commits to enable warnings

on test suite and clean up some code.

Conflicts:
	test/cursor_test.rb
This commit is contained in:
Kyle Banker 2011-03-23 16:02:04 -04:00
commit 2bfe205c51
25 changed files with 125 additions and 132 deletions

View File

@ -77,41 +77,49 @@ namespace :test do
Rake::TestTask.new(:rs) do |t|
t.test_files = FileList['test/replica_sets/*_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:unit) do |t|
t.test_files = FileList['test/unit/*_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:functional) do |t|
t.test_files = FileList['test/*_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:pooled_threading) do |t|
t.test_files = FileList['test/threading/*_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:auto_reconnect) do |t|
t.test_files = FileList['test/auxillary/autoreconnect_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:authentication) do |t|
t.test_files = FileList['test/auxillary/authentication_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:new_features) do |t|
t.test_files = FileList['test/auxillary/1.4_features.rb']
t.verbose = true
t.ruby_opts << '-w'
end
Rake::TestTask.new(:bson) do |t|
t.test_files = FileList['test/bson/*_test.rb']
t.verbose = true
t.ruby_opts << '-w'
end
task :drop_databases do |t|

View File

@ -245,7 +245,6 @@ module BSON
end
def dump
i = 0
@str.each_byte do |c, i|
$stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}"
i += 1

View File

@ -98,9 +98,9 @@ module BSON
# Check equality of this object id with another.
#
# @param [Mongo::ObjectId] object_id
# @param [BSON::ObjectId] object_id
def eql?(object_id)
@data == object_id.instance_variable_get("@data")
object_id.kind_of?(BSON::ObjectId) and self.data == object_id.data
end
alias_method :==, :eql?
@ -119,13 +119,6 @@ module BSON
@data.dup
end
# Get the array representation without cloning.
#
# @return [Array]
def data
@data
end
# Given a string representation of an ObjectId, return a new ObjectId
# with that value.
#

View File

@ -34,6 +34,8 @@ module Mongo
#
# @core cursors constructor_details
def initialize(collection, opts={})
@cursor_id = nil
@db = collection.db
@collection = collection
@connection = @db.connection
@ -313,8 +315,8 @@ module Mongo
def query_options_hash
{ :selector => @selector,
:fields => @fields,
:skip => @skip_num,
:limit => @limit_num,
:skip => @skip,
:limit => @limit,
:order => @order,
:hint => @hint,
:snapshot => @snapshot,

View File

@ -335,8 +335,8 @@ module Mongo
@files_id = opts.delete(:_id) || BSON::ObjectId.new
@content_type = opts.delete(:content_type) || (defined? MIME) && get_content_type || DEFAULT_CONTENT_TYPE
@chunk_size = opts.delete(:chunk_size) || DEFAULT_CHUNK_SIZE
@metadata = opts.delete(:metadata) if opts[:metadata]
@aliases = opts.delete(:aliases) if opts[:aliases]
@metadata = opts.delete(:metadata)
@aliases = opts.delete(:aliases)
@file_length = 0
opts.each {|k, v| self[k] = v}
check_existing_file if @safe

View File

@ -168,10 +168,6 @@ module Mongo
return socket
else
# Otherwise, wait
if @logger
@logger.warn "MONGODB Waiting for available connection; " +
"#{@checked_out.size} of #{@size} connections checked out."
end
@queue.wait(@connection_mutex)
end
end

View File

@ -20,7 +20,7 @@ module Mongo
class URIParser
DEFAULT_PORT = 27017
MONGODB_URI_MATCHER = /(([-_.\w\d]+):([^@]+)@)?([-.\w\d]+)(:([\w\d]+))?(\/([-\d\w]+))?/
MONGODB_URI_MATCHER = /(([-.\w]+):([^@]+)@)?([-.\w]+)(:([\w]+))?(\/([-\w]+))?/
MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]"
SPEC_ATTRS = [:nodes, :auths]
OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync]
@ -142,7 +142,11 @@ module Mongo
# This method uses the lambdas defined in OPT_VALID and OPT_CONV to validate
# and convert the given options.
def parse_options(opts)
# initialize instance variables for available options
OPT_VALID.keys.each { |k| instance_variable_set("@#{k}", nil) }
return unless opts
separator = opts.include?('&') ? '&' : ';'
opts.split(separator).each do |attr|
key, value = attr.split('=')

View File

@ -1,8 +1,11 @@
# encoding:utf-8
require './test/test_helper'
require 'complex'
if RUBY_VERSION < '1.9'
require 'complex'
require 'rational'
end
require 'bigdecimal'
require 'rational'
begin
require 'active_support/core_ext'
@ -134,14 +137,14 @@ class BSONTest < Test::Unit::TestCase
str = "壁に耳あり、障子に目あり"
bson = BSON::BSON_CODER.serialize("x" => str)
Encoding.default_internal = 'EUC-JP'
silently { Encoding.default_internal = 'EUC-JP' }
out = BSON::BSON_CODER.deserialize(bson)["x"]
assert_equal Encoding.default_internal, out.encoding
assert_equal str.encode('EUC-JP'), out
assert_equal str, out.encode(str.encoding)
ensure
Encoding.default_internal = before_enc
silently { Encoding.default_internal = before_enc }
end
end
@ -161,12 +164,12 @@ class BSONTest < Test::Unit::TestCase
assert_doc_pass(doc)
end
def test_double
doc = {'doc' => 41.25}
assert_doc_pass(doc)
end
def test_double
doc = {'doc' => 41.25}
assert_doc_pass(doc)
end
def test_int
def test_int
doc = {'doc' => 42}
assert_doc_pass(doc)
@ -283,7 +286,7 @@ class BSONTest < Test::Unit::TestCase
ensure
if !invalid_date.is_a? Time
assert_equal InvalidDocument, e.class
assert_match /UTC Time/, e.message
assert_match(/UTC Time/, e.message)
end
end
end
@ -445,7 +448,7 @@ class BSONTest < Test::Unit::TestCase
rescue => e
ensure
assert_equal InvalidDocument, e.class
assert_match /Cannot serialize/, e.message
assert_match(/Cannot serialize/, e.message)
end
end
end

View File

@ -373,8 +373,8 @@ class TestCollection < Test::Unit::TestCase
docs = [{"hello" => "world"}, {"hello" => "world"}]
@@test.insert(docs)
docs.each do |doc|
assert(doc.include?(:_id))
docs.each do |d|
assert(d.include?(:_id))
end
end

View File

@ -40,7 +40,7 @@ class TestConnection < Test::Unit::TestCase
end
def test_server_version
assert_match /\d\.\d+(\.\d+)?/, @conn.server_version.to_s
assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
end
def test_invalid_database_names

View File

@ -40,11 +40,11 @@ class ConversionsTest < Test::Unit::TestCase
end
def test_sort_value_when_value_is_negative_one
assert_equal -1, sort_value(-1)
assert_equal(-1, sort_value(-1))
end
def test_sort_value_when_value_is_negative_one_as_a_string
assert_equal -1, sort_value("-1")
assert_equal(-1, sort_value("-1"))
end
def test_sort_value_when_value_is_ascending
@ -80,35 +80,35 @@ class ConversionsTest < Test::Unit::TestCase
end
def test_sort_value_when_value_is_descending
assert_equal -1, sort_value("descending")
assert_equal(-1, sort_value("descending"))
end
def test_sort_value_when_value_is_desc
assert_equal -1, sort_value("desc")
assert_equal(-1, sort_value("desc"))
end
def test_sort_value_when_value_is_uppercase_descending
assert_equal -1, sort_value("DESCENDING")
assert_equal(-1, sort_value("DESCENDING"))
end
def test_sort_value_when_value_is_uppercase_desc
assert_equal -1, sort_value("DESC")
assert_equal(-1, sort_value("DESC"))
end
def test_sort_value_when_value_is_symbol_descending
assert_equal -1, sort_value(:descending)
assert_equal(-1, sort_value(:descending))
end
def test_sort_value_when_value_is_symbol_desc
assert_equal -1, sort_value(:desc)
assert_equal(-1, sort_value(:desc))
end
def test_sort_value_when_value_is_uppercase_symbol_descending
assert_equal -1, sort_value(:DESCENDING)
assert_equal(-1, sort_value(:DESCENDING))
end
def test_sort_value_when_value_is_uppercase_symbol_desc
assert_equal -1, sort_value(:DESC)
assert_equal(-1, sort_value(:DESC))
end
def test_sort_value_when_value_is_invalid

View File

@ -1,7 +1,7 @@
require './test/test_helper'
require 'logger'
class CursorTest < Test::Unit::TestCase
class CursorFailTest < Test::Unit::TestCase
include Mongo

View File

@ -1,7 +1,7 @@
require './test/test_helper'
require 'logger'
class CursorTest < Test::Unit::TestCase
class CursorMessageTest < Test::Unit::TestCase
include Mongo

View File

@ -120,7 +120,7 @@ class CursorTest < Test::Unit::TestCase
results = @@coll.find.sort([:n, :asc]).to_a
assert_equal MinKey.new, results[0]['n']
assert_equal -1000000, results[1]['n']
assert_equal(-1000000, results[1]['n'])
assert_equal 1000000, results[2]['n']
assert_equal MaxKey.new, results[3]['n']
end
@ -171,8 +171,8 @@ class CursorTest < Test::Unit::TestCase
cursor = Cursor.new(@@coll, :timeout => false)
assert_equal false, cursor.timeout
@@coll.find({}, :timeout => false) do |cursor|
assert_equal false, cursor.timeout
@@coll.find({}, :timeout => false) do |c|
assert_equal false, c.timeout
end
end

View File

@ -239,7 +239,7 @@ class DBAPITest < Test::Unit::TestCase
names = @@db.collection_names
assert names.length >= 2
assert names.include?(@@coll.name)
assert names.include?('mongo-ruby-test.test2')
assert names.include?('test2')
ensure
@@db.drop_collection('test2')
end
@ -621,7 +621,7 @@ class DBAPITest < Test::Unit::TestCase
assert_equal("mike", @@coll.find_one()["hello"])
end
def test_collection_names
def test_collection_names_errors
assert_raise TypeError do
@@db.collection(5)
end

View File

@ -26,7 +26,7 @@ class DBTest < Test::Unit::TestCase
@@db.collection('test').insert('a' => 1)
fail "expected 'NilClass' exception"
rescue => ex
assert_match /NilClass/, ex.to_s
assert_match(/NilClass/, ex.to_s)
ensure
@@db = standard_connection.db(MONGO_TEST_DB)
@@users = @@db.collection('system.users')
@ -104,7 +104,7 @@ class DBTest < Test::Unit::TestCase
db.pk_factory = Object.new
fail "error: expected exception"
rescue => ex
assert_match /Cannot change/, ex.to_s
assert_match(/Cannot change/, ex.to_s)
ensure
conn.close
end
@ -280,7 +280,7 @@ class DBTest < Test::Unit::TestCase
assert_not_nil doc
result = doc['result']
assert_not_nil result
assert_match /firstExtent/, result
assert_match(/firstExtent/, result)
end
end

View File

@ -1,6 +1,22 @@
require './test/test_helper'
include Mongo
def read_and_write_stream(filename, read_length, opts={})
io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
id = @grid.put(io, opts.merge!(:filename => filename + read_length.to_s))
file = @grid.get(id)
io.rewind
data = io.read
if data.respond_to?(:force_encoding)
data.force_encoding("binary")
end
read_data = ""
while(chunk = file.read(read_length))
read_data << chunk
end
assert_equal data.length, read_data.length
end
class GridTest < Test::Unit::TestCase
context "Tests:" do
setup do
@ -161,22 +177,6 @@ class GridTest < Test::Unit::TestCase
context "Streaming: " do || {}
setup do
def read_and_write_stream(filename, read_length, opts={})
io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
id = @grid.put(io, opts.merge!(:filename => filename + read_length.to_s))
file = @grid.get(id)
io.rewind
data = io.read
if data.respond_to?(:force_encoding)
data.force_encoding("binary")
end
read_data = ""
while(chunk = file.read(read_length))
read_data << chunk
end
assert_equal data.length, read_data.length
end
@grid = Grid.new(@db, 'test-fs')
end

View File

@ -38,19 +38,6 @@ class Hash
alias_method :to_options, :symbolize_keys
#alias_method :to_options!, :symbolize_keys!
# Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
# Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
# as keys, this will fail.
#
# ==== Examples
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
def assert_valid_keys(*valid_keys)
unknown_keys = keys - [valid_keys].flatten
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end
end
module ActiveSupport

View File

@ -3,12 +3,20 @@ require 'rubygems' if ENV['C_EXT']
require 'mongo'
require 'test/unit'
def silently
warn_level = $VERBOSE
$VERBOSE = nil
result = yield
$VERBOSE = warn_level
result
end
begin
require 'rubygems'
require 'shoulda'
silently { require 'shoulda' }
require 'mocha'
rescue LoadError
puts <<MSG
rescue LoadError
puts <<MSG
This test suite requires shoulda and mocha.
You can install them as follows:
@ -16,7 +24,8 @@ You can install them as follows:
gem install mocha
MSG
exit
exit
end
require 'bson_ext/cbson' if !(RUBY_PLATFORM =~ /java/) && ENV['C_EXT']
@ -69,7 +78,17 @@ class Test::Unit::TestCase
self.class.mongo_port
end
def new_mock_socket(host='localhost', port=27017)
socket = Object.new
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
socket.stubs(:close)
socket
end
def new_mock_db
db = Object.new
end
def assert_raise_error(klass, message)
begin
yield

View File

@ -1,4 +1,4 @@
require File.expand_path('./test/test_helper.rb')
require './test/test_helper'
class CollectionTest < Test::Unit::TestCase

View File

@ -3,19 +3,6 @@ include Mongo
class ConnectionTest < Test::Unit::TestCase
context "Initialization: " do
setup do
def new_mock_socket(host='localhost', port=27017)
socket = Object.new
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
socket.stubs(:close)
socket
end
def new_mock_db
db = Object.new
end
end
context "given a single node" do
setup do
@conn = Connection.new('localhost', 27017, :connect => false)

View File

@ -12,28 +12,32 @@ class CursorTest < Test::Unit::TestCase
end
should "set timeout" do
assert_equal true, @cursor.timeout
assert @cursor.timeout
assert @cursor.query_options_hash[:timeout]
end
should "set selector" do
assert @cursor.selector == {}
assert_equal({}, @cursor.selector)
@cursor = Cursor.new(@collection, :selector => {:name => "Jones"})
assert @cursor.selector == {:name => "Jones"}
assert_equal({:name => "Jones"}, @cursor.selector)
assert_equal({:name => "Jones"}, @cursor.query_options_hash[:selector])
end
should "set fields" do
assert_nil @cursor.fields
@cursor = Cursor.new(@collection, :fields => [:name, :date])
assert @cursor.fields == {:name => 1, :date => 1}
assert_equal({:name => 1, :date => 1}, @cursor.fields)
assert_equal({:name => 1, :date => 1}, @cursor.query_options_hash[:fields])
end
should "set mix fields 0 and 1" do
assert_nil @cursor.fields
@cursor = Cursor.new(@collection, :fields => {:name => 1, :date => 0})
assert @cursor.fields == {:name => 1, :date => 0}
assert_equal({:name => 1, :date => 0}, @cursor.fields)
assert_equal({:name => 1, :date => 0}, @cursor.query_options_hash[:fields])
end
should "set limit" do
@ -41,6 +45,7 @@ class CursorTest < Test::Unit::TestCase
@cursor = Cursor.new(@collection, :limit => 10)
assert_equal 10, @cursor.limit
assert_equal 10, @cursor.query_options_hash[:limit]
end
@ -49,6 +54,7 @@ class CursorTest < Test::Unit::TestCase
@cursor = Cursor.new(@collection, :skip => 5)
assert_equal 5, @cursor.skip
assert_equal 5, @cursor.query_options_hash[:skip]
end
should "set sort order" do
@ -56,6 +62,7 @@ class CursorTest < Test::Unit::TestCase
@cursor = Cursor.new(@collection, :order => "last_name")
assert_equal "last_name", @cursor.order
assert_equal "last_name", @cursor.query_options_hash[:order]
end
should "set hint" do
@ -63,6 +70,7 @@ class CursorTest < Test::Unit::TestCase
@cursor = Cursor.new(@collection, :hint => "name")
assert_equal "name", @cursor.hint
assert_equal "name", @cursor.query_options_hash[:hint]
end
should "cache full collection name" do
@ -72,7 +80,9 @@ class CursorTest < Test::Unit::TestCase
context "Query fields" do
setup do
@connection = stub(:class => Collection, :logger => @logger)
@logger = mock()
@logger.stubs(:debug)
@connection = stub(:class => Connection, :logger => @logger)
@db = stub(:slave_ok? => true, :name => "testing", :connection => @connection)
@collection = stub(:db => @db, :name => "items")
end

View File

@ -1,18 +1,16 @@
require './test/test_helper'
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
class DBTest < Test::Unit::TestCase
context "DBTest: " do
setup do
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
end
context "DB commands" do
setup do
@conn = stub()

View File

@ -3,19 +3,6 @@ include Mongo
class ReplSetConnectionTest < Test::Unit::TestCase
context "Initialization: " do
setup do
def new_mock_socket(host='localhost', port=27017)
socket = Object.new
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
socket.stubs(:close)
socket
end
def new_mock_db
db = Object.new
end
end
context "connecting to a replica set" do
setup do
TCPSocket.stubs(:new).returns(new_mock_socket('localhost', 27017))

View File

@ -1,4 +1,4 @@
require File.expand_path('./test/test_helper.rb')
require './test/test_helper'
class SafeTest < Test::Unit::TestCase