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)
|
2009-08-20 14:50:48 +00:00
|
|
|
row['_id'] ||= Mongo::ObjectID.new
|
2009-01-16 19:41:53 +00:00
|
|
|
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
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-01-14 20:49:49 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2009-08-20 22:48:09 +00:00
|
|
|
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
|
|
|
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users = @@db.collection('system.users')
|
2009-01-21 16:52:43 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
def setup
|
2009-01-21 16:26:18 +00:00
|
|
|
@spongebob = 'spongebob'
|
|
|
|
@spongebob_password = 'squarepants'
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users.clear
|
|
|
|
@@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password))
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users.clear if @@users
|
2009-03-13 21:09:19 +00:00
|
|
|
@@db.error
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.close
|
|
|
|
assert !@@db.connected?
|
2009-01-14 20:49:49 +00:00
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.collection('test').insert('a' => 1)
|
2009-01-14 23:37:28 +00:00
|
|
|
fail "expected 'NilClass' exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_match /NilClass/, ex.to_s
|
2009-02-05 15:10:41 +00:00
|
|
|
ensure
|
2009-08-20 22:48:09 +00:00
|
|
|
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users = @@db.collection('system.users')
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_full_coll_name
|
2009-02-05 15:10:41 +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-08-06 19:52:07 +00:00
|
|
|
def test_collection_names
|
|
|
|
@@db.collection("test").insert("foo" => 5)
|
|
|
|
@@db.collection("test.mike").insert("bar" => 0)
|
|
|
|
|
|
|
|
colls = @@db.collection_names()
|
|
|
|
assert colls.include?("test")
|
|
|
|
assert colls.include?("test.mike")
|
|
|
|
colls.each { |name|
|
|
|
|
assert !name.include?("$")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-23 18:30:59 +00:00
|
|
|
def test_pair
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.close
|
|
|
|
@@users = nil
|
2009-08-20 22:48:09 +00:00
|
|
|
@@db = Connection.new({:left => "this-should-fail", :right => [@@host, @@port]}).db('ruby-mongo-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
assert @@db.connected?
|
|
|
|
ensure
|
2009-08-20 22:48:09 +00:00
|
|
|
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test') unless @@db.connected?
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users = @@db.collection('system.users')
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
2009-01-16 19:41:53 +00:00
|
|
|
def test_pk_factory
|
2009-08-20 22:48:09 +00:00
|
|
|
db = Connection.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
|
2009-01-16 19:41:53 +00:00
|
|
|
coll = db.collection('test')
|
|
|
|
coll.clear
|
|
|
|
|
2009-07-27 23:43:20 +00:00
|
|
|
insert_id = coll.insert('name' => 'Fred', 'age' => 42)
|
2009-01-28 20:42:42 +00:00
|
|
|
# new id gets added to returned object
|
2009-08-14 19:39:49 +00:00
|
|
|
row = coll.find_one({'name' => 'Fred'})
|
2009-01-28 20:42:42 +00:00
|
|
|
oid = row['_id']
|
2009-01-27 21:26:42 +00:00
|
|
|
assert_not_nil oid
|
2009-07-27 23:43:20 +00:00
|
|
|
assert_equal insert_id, oid
|
2009-01-16 19:41:53 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
oid = ObjectID.new
|
2009-07-27 23:43:20 +00:00
|
|
|
data = {'_id' => oid, 'name' => 'Barney', 'age' => 41}
|
|
|
|
coll.insert(data)
|
2009-08-14 19:39:49 +00:00
|
|
|
row = coll.find_one({'name' => data['name']})
|
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-07-27 23:43:20 +00:00
|
|
|
assert_equal data, 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
|
2009-08-20 22:48:09 +00:00
|
|
|
db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
db.pk_factory = Object.new # first time
|
2009-01-16 21:10:52 +00:00
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
db.pk_factory = Object.new
|
2009-01-16 21:10:52 +00:00
|
|
|
fail "error: expected exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_match /can not change PK factory/, ex.to_s
|
2009-02-05 15:10:41 +00:00
|
|
|
ensure
|
|
|
|
db.close
|
2009-01-16 21:10:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 16:26:18 +00:00
|
|
|
def test_authenticate
|
2009-02-05 15:10:41 +00:00
|
|
|
assert !@@db.authenticate('nobody', 'nopassword')
|
|
|
|
assert !@@db.authenticate(@spongebob, 'squareliederhosen')
|
|
|
|
assert @@db.authenticate(@spongebob, @spongebob_password)
|
2009-01-21 16:52:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_logout
|
2009-02-05 15:10:41 +00:00
|
|
|
@@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
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.close
|
2009-08-20 22:48:09 +00:00
|
|
|
db = Connection.new(@@host, @@port, :auto_reconnect => true).db('ruby-mongo-test')
|
2009-01-23 16:47:22 +00:00
|
|
|
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?
|
2009-02-05 15:10:41 +00:00
|
|
|
ensure
|
2009-08-20 22:48:09 +00:00
|
|
|
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users = @@db.collection('system.users')
|
2009-01-23 16:47:22 +00:00
|
|
|
end
|
|
|
|
|
2009-01-23 18:47:27 +00:00
|
|
|
def test_error
|
2009-05-26 19:26:20 +00:00
|
|
|
@@db.reset_error_history
|
|
|
|
assert_nil @@db.error
|
|
|
|
assert !@@db.error?
|
|
|
|
assert_nil @@db.previous_error
|
|
|
|
|
|
|
|
@@db.send(:db_command, :forceerror => 1)
|
2009-02-05 15:10:41 +00:00
|
|
|
assert @@db.error?
|
2009-05-26 19:26:20 +00:00
|
|
|
assert_not_nil @@db.error
|
|
|
|
assert_not_nil @@db.previous_error
|
2009-01-23 18:47:27 +00:00
|
|
|
|
2009-05-26 19:26:20 +00:00
|
|
|
@@db.send(:db_command, :forceerror => 1)
|
2009-02-05 15:10:41 +00:00
|
|
|
assert @@db.error?
|
2009-05-26 19:26:20 +00:00
|
|
|
assert @@db.error
|
|
|
|
prev_error = @@db.previous_error
|
|
|
|
assert_equal 1, prev_error['nPrev']
|
|
|
|
assert_equal prev_error["err"], @@db.error
|
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
@@db.collection('test').find_one
|
2009-05-26 19:26:20 +00:00
|
|
|
assert_nil @@db.error
|
|
|
|
assert !@@db.error?
|
|
|
|
assert @@db.previous_error
|
|
|
|
assert_equal 2, @@db.previous_error['nPrev']
|
|
|
|
|
|
|
|
@@db.reset_error_history
|
|
|
|
assert_nil @@db.error
|
|
|
|
assert !@@db.error?
|
|
|
|
assert_nil @@db.previous_error
|
2009-01-23 18:47:27 +00:00
|
|
|
end
|
|
|
|
|
2009-01-29 21:58:55 +00:00
|
|
|
def test_text_port_number
|
2009-02-05 15:10:41 +00:00
|
|
|
db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]])
|
2009-01-29 21:58:55 +00:00
|
|
|
# If there is no error, all is well
|
|
|
|
db.collection('users').clear
|
|
|
|
end
|
|
|
|
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|