2009-01-14 20:49:49 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2009-01-21 16:52:43 +00:00
|
|
|
require 'digest/md5'
|
2009-01-14 20:49:49 +00:00
|
|
|
require 'mongo'
|
|
|
|
require 'test/unit'
|
|
|
|
|
2009-01-16 19:41:53 +00:00
|
|
|
class TestPKFactory
|
|
|
|
def create_pk(row)
|
|
|
|
row['_id'] ||= XGen::Mongo::Driver::ObjectID.new
|
|
|
|
row
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-14 20:49:49 +00:00
|
|
|
# NOTE: assumes Mongo is running
|
2009-01-16 14:46:47 +00:00
|
|
|
class DBTest < Test::Unit::TestCase
|
2009-01-14 20:49:49 +00:00
|
|
|
|
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
|
|
|
def setup
|
2009-01-14 23:37:28 +00:00
|
|
|
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
|
|
|
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
|
|
|
@db = Mongo.new(@host, @port).db('ruby-mongo-test')
|
2009-01-21 16:52:43 +00:00
|
|
|
|
2009-01-21 16:26:18 +00:00
|
|
|
@spongebob = 'spongebob'
|
|
|
|
@spongebob_password = 'squarepants'
|
2009-01-21 16:52:43 +00:00
|
|
|
@users = @db.collection('system.users')
|
|
|
|
@users.clear
|
2009-01-30 21:49:19 +00:00
|
|
|
@users.insert(:user => @spongebob, :pwd => @db.send(:hash_password, @spongebob, @spongebob_password))
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-01-23 16:47:22 +00:00
|
|
|
if @db && @db.connected?
|
2009-01-21 16:52:43 +00:00
|
|
|
@users.clear if @users
|
2009-01-14 23:37:28 +00:00
|
|
|
@db.close
|
|
|
|
end
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close
|
|
|
|
@db.close
|
2009-01-14 23:37:28 +00:00
|
|
|
assert !@db.connected?
|
2009-01-14 20:49:49 +00:00
|
|
|
begin
|
2009-01-14 23:37:28 +00:00
|
|
|
@db.collection('test').insert('a' => 1)
|
|
|
|
fail "expected 'NilClass' exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_match /NilClass/, ex.to_s
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_full_coll_name
|
2009-01-14 23:37:28 +00:00
|
|
|
coll = @db.collection('test')
|
|
|
|
assert_equal 'ruby-mongo-test.test', @db.full_coll_name(coll.name)
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
2009-01-23 18:30:59 +00:00
|
|
|
def test_pair
|
2009-01-14 23:37:28 +00:00
|
|
|
@db.close
|
2009-01-21 16:52:43 +00:00
|
|
|
@users = nil
|
2009-01-23 18:30:59 +00:00
|
|
|
@db = Mongo.new({:left => "nosuch.example.com", :right => [@host, @port]}).db('ruby-mongo-test')
|
2009-01-14 23:37:28 +00:00
|
|
|
assert @db.connected?
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
2009-01-16 19:41:53 +00:00
|
|
|
def test_pk_factory
|
|
|
|
db = Mongo.new(@host, @port).db('ruby-mongo-test', :pk => TestPKFactory.new)
|
|
|
|
coll = db.collection('test')
|
|
|
|
coll.clear
|
|
|
|
|
2009-01-28 20:42:42 +00:00
|
|
|
# new id gets added to returned object
|
2009-01-16 21:10:52 +00:00
|
|
|
obj = coll.insert('name' => 'Fred', 'age' => 42)
|
2009-01-16 19:41:53 +00:00
|
|
|
row = coll.find({'name' => 'Fred'}, :limit => 1).next_object
|
2009-01-28 20:42:42 +00:00
|
|
|
oid = row['_id']
|
2009-01-27 21:26:42 +00:00
|
|
|
assert_not_nil oid
|
2009-01-16 21:10:52 +00:00
|
|
|
assert_equal obj, row
|
2009-01-16 19:41:53 +00:00
|
|
|
|
|
|
|
oid = XGen::Mongo::Driver::ObjectID.new
|
2009-01-16 21:10:52 +00:00
|
|
|
obj = coll.insert('_id' => oid, 'name' => 'Barney', 'age' => 41)
|
2009-01-16 19:41:53 +00:00
|
|
|
row = coll.find({'name' => 'Barney'}, :limit => 1).next_object
|
2009-01-28 20:42:42 +00:00
|
|
|
db_oid = row['_id']
|
2009-01-27 21:26:42 +00:00
|
|
|
assert_equal oid, db_oid
|
2009-01-16 21:10:52 +00:00
|
|
|
assert_equal obj, row
|
2009-01-16 19:41:53 +00:00
|
|
|
|
|
|
|
coll.clear
|
|
|
|
end
|
|
|
|
|
2009-01-16 21:10:52 +00:00
|
|
|
def test_pk_factory_reset
|
|
|
|
@db.pk_factory = Object.new # first time
|
|
|
|
begin
|
|
|
|
@db.pk_factory = Object.new
|
|
|
|
fail "error: expected exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_match /can not change PK factory/, ex.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 16:26:18 +00:00
|
|
|
def test_authenticate
|
2009-01-21 16:52:43 +00:00
|
|
|
assert !@db.authenticate('nobody', 'nopassword')
|
|
|
|
assert !@db.authenticate(@spongebob, 'squareliederhosen')
|
|
|
|
assert @db.authenticate(@spongebob, @spongebob_password)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_logout
|
|
|
|
@db.logout # only testing that we don't throw exception
|
2009-01-21 16:26:18 +00:00
|
|
|
end
|
|
|
|
|
2009-01-23 16:47:22 +00:00
|
|
|
def test_auto_connect
|
|
|
|
@db.close
|
|
|
|
db = Mongo.new(@host, @port, :auto_reconnect => true).db('ruby-mongo-test')
|
|
|
|
assert db.connected?
|
|
|
|
assert db.auto_reconnect?
|
|
|
|
db.close
|
|
|
|
assert !db.connected?
|
|
|
|
assert db.auto_reconnect?
|
|
|
|
db.collection('test').insert('a' => 1)
|
|
|
|
assert db.connected?
|
|
|
|
end
|
|
|
|
|
2009-01-23 18:47:27 +00:00
|
|
|
def test_error
|
|
|
|
doc = @db.send(:db_command, :forceerror => 1)
|
|
|
|
assert @db.error?
|
|
|
|
err = @db.error
|
|
|
|
assert_match /forced error/, err
|
|
|
|
|
|
|
|
# ask again
|
|
|
|
assert @db.error?
|
|
|
|
err2 = @db.error
|
|
|
|
assert_equal err, err2
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:58:55 +00:00
|
|
|
def test_text_port_number
|
|
|
|
db = DB.new('ruby-mongo-test', [[@host, @port.to_s]])
|
|
|
|
# If there is no error, all is well
|
|
|
|
db.collection('users').clear
|
|
|
|
end
|
|
|
|
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|