2009-12-01 18:49:57 +00:00
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2012-02-19 00:24:27 +00:00
|
|
|
require 'rubygems' if RUBY_VERSION
|
2009-11-30 22:50:54 +00:00
|
|
|
require 'mongo'
|
2012-02-19 00:24:27 +00:00
|
|
|
gem 'test-unit'
|
2009-10-20 15:31:07 +00:00
|
|
|
require 'test/unit'
|
|
|
|
|
2011-02-20 14:38:21 +00:00
|
|
|
def silently
|
|
|
|
warn_level = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
result = yield
|
|
|
|
$VERBOSE = warn_level
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
begin
|
2011-03-29 14:39:01 +00:00
|
|
|
require 'rubygems' if RUBY_VERSION < "1.9.0" && !ENV['C_EXT']
|
2011-02-20 14:38:21 +00:00
|
|
|
silently { require 'shoulda' }
|
2011-03-29 14:39:01 +00:00
|
|
|
silently { require 'mocha' }
|
2011-02-20 14:39:09 +00:00
|
|
|
rescue LoadError
|
|
|
|
puts <<MSG
|
2009-10-20 15:31:07 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
This test suite requires shoulda and mocha.
|
|
|
|
You can install them as follows:
|
|
|
|
gem install shoulda
|
2009-10-20 15:31:07 +00:00
|
|
|
gem install mocha
|
|
|
|
|
|
|
|
MSG
|
2011-02-20 14:39:09 +00:00
|
|
|
|
|
|
|
exit
|
2009-10-20 15:31:07 +00:00
|
|
|
end
|
|
|
|
|
2010-10-14 20:11:30 +00:00
|
|
|
require 'bson_ext/cbson' if !(RUBY_PLATFORM =~ /java/) && ENV['C_EXT']
|
2009-12-01 18:49:57 +00:00
|
|
|
|
2010-10-07 21:05:45 +00:00
|
|
|
unless defined? MONGO_TEST_DB
|
2010-12-15 17:55:06 +00:00
|
|
|
MONGO_TEST_DB = 'ruby-test-db'
|
2010-10-07 21:05:45 +00:00
|
|
|
end
|
2010-04-05 19:48:35 +00:00
|
|
|
|
2010-12-10 16:12:18 +00:00
|
|
|
unless defined? TEST_PORT
|
2010-12-10 21:00:35 +00:00
|
|
|
TEST_PORT = ENV['MONGO_RUBY_DRIVER_PORT'] ? ENV['MONGO_RUBY_DRIVER_PORT'].to_i : Mongo::Connection::DEFAULT_PORT
|
2010-12-10 16:12:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? TEST_HOST
|
|
|
|
TEST_HOST = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
|
|
|
end
|
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
class Test::Unit::TestCase
|
|
|
|
include Mongo
|
2010-04-05 14:39:55 +00:00
|
|
|
include BSON
|
2009-11-23 18:13:14 +00:00
|
|
|
|
2010-10-13 21:09:23 +00:00
|
|
|
def self.standard_connection(options={})
|
2010-12-10 16:12:18 +00:00
|
|
|
Connection.new(TEST_HOST, TEST_PORT, options)
|
2010-10-13 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def standard_connection(options={})
|
|
|
|
self.class.standard_connection(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.host_port
|
|
|
|
"#{mongo_host}:#{mongo_port}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mongo_host
|
2010-12-10 16:12:18 +00:00
|
|
|
TEST_HOST
|
2010-10-13 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.mongo_port
|
2010-12-10 16:12:18 +00:00
|
|
|
TEST_PORT
|
2010-10-13 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def host_port
|
|
|
|
self.class.host_port
|
|
|
|
end
|
|
|
|
|
|
|
|
def mongo_host
|
|
|
|
self.class.mongo_host
|
|
|
|
end
|
|
|
|
|
|
|
|
def mongo_port
|
|
|
|
self.class.mongo_port
|
|
|
|
end
|
|
|
|
|
2011-02-20 15:00:06 +00:00
|
|
|
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)
|
2011-11-07 23:01:27 +00:00
|
|
|
socket.stubs(:closed?)
|
2012-03-06 22:06:42 +00:00
|
|
|
socket.stubs(:pool)
|
2011-02-20 15:00:06 +00:00
|
|
|
socket
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_mock_db
|
|
|
|
db = Object.new
|
|
|
|
end
|
|
|
|
|
2010-05-04 20:00:05 +00:00
|
|
|
def assert_raise_error(klass, message)
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue => e
|
2011-11-07 18:36:57 +00:00
|
|
|
if klass.to_s != e.class.to_s
|
|
|
|
flunk "Expected exception class #{klass} but got #{e.class}.\n #{e.backtrace}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if !e.message.include?(message)
|
|
|
|
p e.backtrace
|
|
|
|
flunk "#{e.message} does not include #{message}.\n#{e.backtrace}"
|
|
|
|
end
|
2010-08-02 22:19:54 +00:00
|
|
|
else
|
|
|
|
flunk "Expected assertion #{klass} but none was raised."
|
2010-05-04 20:00:05 +00:00
|
|
|
end
|
|
|
|
end
|
2009-10-20 15:31:07 +00:00
|
|
|
end
|