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
|
|
|
|
@db.add_user(@spongebob, @spongebob_password)
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-01-14 23:37:28 +00:00
|
|
|
if @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
|
|
|
|
|
|
|
|
def test_master
|
|
|
|
# Doesn't really test anything since we probably only have one database
|
|
|
|
# during this test.
|
|
|
|
@db.switch_to_master
|
2009-01-14 23:37:28 +00:00
|
|
|
assert @db.connected?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array
|
|
|
|
@db.close
|
2009-01-21 16:52:43 +00:00
|
|
|
@users = nil
|
2009-01-14 23:37:28 +00:00
|
|
|
@db = Mongo.new([["nosuch.example.com"], [@host, @port]]).db('ruby-mongo-test')
|
|
|
|
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-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-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-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_add_user
|
2009-01-21 16:52:43 +00:00
|
|
|
assert_equal 1, @users.count
|
|
|
|
doc = @users.find({}, :limit => 1).next_object
|
|
|
|
assert_equal @spongebob, doc['user']
|
|
|
|
assert_equal Digest::MD5.hexdigest("mongo#{@spongebob_password}"), doc['pwd']
|
2009-01-21 16:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_user
|
2009-01-21 16:52:43 +00:00
|
|
|
@db.delete_user(@spongebob)
|
|
|
|
assert_equal 0, @users.count
|
2009-01-21 16:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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-14 20:49:49 +00:00
|
|
|
end
|