2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../test_helper", __FILE__)
|
2009-01-21 16:52:43 +00:00
|
|
|
require 'digest/md5'
|
2009-10-07 23:39:36 +00:00
|
|
|
require 'stringio'
|
|
|
|
require 'logger'
|
2009-01-14 20:49:49 +00:00
|
|
|
|
2009-01-16 19:41:53 +00:00
|
|
|
class TestPKFactory
|
|
|
|
def create_pk(row)
|
2010-08-24 16:49:23 +00:00
|
|
|
row['_id'] ||= BSON::ObjectId.new
|
2009-01-16 19:41:53 +00:00
|
|
|
row
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2010-10-13 21:09:23 +00:00
|
|
|
@@conn = standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
@@db = @@conn.db(MONGO_TEST_DB)
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users = @@db.collection('system.users')
|
2010-04-06 22:29:39 +00:00
|
|
|
@@version = @@conn.server_version
|
2009-01-21 16:52:43 +00:00
|
|
|
|
2009-01-14 20:49:49 +00:00
|
|
|
def test_close
|
2009-11-23 20:20:05 +00:00
|
|
|
@@conn.close
|
|
|
|
assert !@@conn.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
|
2011-02-20 16:19:14 +00:00
|
|
|
assert_match(/NilClass/, ex.to_s)
|
2009-02-05 15:10:41 +00:00
|
|
|
ensure
|
2010-10-13 21:09:23 +00:00
|
|
|
@@db = standard_connection.db(MONGO_TEST_DB)
|
2009-02-05 15:10:41 +00:00
|
|
|
@@users = @@db.collection('system.users')
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
end
|
2011-05-09 16:28:07 +00:00
|
|
|
|
|
|
|
def test_create_collection
|
|
|
|
col = @@db.create_collection('foo')
|
|
|
|
assert_equal @@db['foo'].name, col.name
|
|
|
|
|
|
|
|
col = @@db.create_collection(:foo)
|
|
|
|
assert_equal @@db['foo'].name, col.name
|
|
|
|
|
|
|
|
@@db.drop_collection('foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_and_drop_collection
|
|
|
|
db = @@conn.db(MONGO_TEST_DB, :strict => true)
|
|
|
|
db.create_collection('foo')
|
|
|
|
assert db.collection('foo')
|
|
|
|
assert db.drop_collection('foo')
|
|
|
|
|
|
|
|
db.create_collection(:foo)
|
|
|
|
assert db.collection(:foo)
|
|
|
|
assert db.drop_collection(:foo)
|
|
|
|
end
|
|
|
|
|
2009-10-07 23:39:36 +00:00
|
|
|
def test_logger
|
|
|
|
output = StringIO.new
|
|
|
|
logger = Logger.new(output)
|
|
|
|
logger.level = Logger::DEBUG
|
2010-10-13 21:09:23 +00:00
|
|
|
conn = standard_connection(:logger => logger)
|
2009-11-23 20:20:05 +00:00
|
|
|
assert_equal logger, conn.logger
|
2011-05-09 16:28:07 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
conn.logger.debug 'testing'
|
2009-10-07 23:39:36 +00:00
|
|
|
assert output.string.include?('testing')
|
|
|
|
end
|
2009-01-14 20:49:49 +00:00
|
|
|
|
|
|
|
def test_full_coll_name
|
2009-02-05 15:10:41 +00:00
|
|
|
coll = @@db.collection('test')
|
2010-04-05 19:48:35 +00:00
|
|
|
assert_equal "#{MONGO_TEST_DB}.test", @@db.full_collection_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-08-27 20:29:41 +00:00
|
|
|
def test_collections
|
|
|
|
@@db.collection("test.durran").insert("foo" => 5)
|
|
|
|
@@db.collection("test.les").insert("bar" => 0)
|
|
|
|
|
|
|
|
colls = @@db.collections()
|
|
|
|
assert_not_nil colls.select { |coll| coll.name == "test.durran" }
|
|
|
|
assert_not_nil colls.select { |coll| coll.name == "test.les" }
|
|
|
|
assert_equal [], colls.select { |coll| coll.name == "does_not_exist" }
|
|
|
|
|
|
|
|
assert_kind_of Collection, colls[0]
|
|
|
|
end
|
|
|
|
|
2009-01-16 19:41:53 +00:00
|
|
|
def test_pk_factory
|
2010-10-13 21:09:23 +00:00
|
|
|
db = standard_connection.db(MONGO_TEST_DB, :pk => TestPKFactory.new)
|
2009-01-16 19:41:53 +00:00
|
|
|
coll = db.collection('test')
|
2009-10-20 15:31:07 +00:00
|
|
|
coll.remove
|
2009-01-16 19:41:53 +00:00
|
|
|
|
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
|
|
|
|
2010-08-24 16:49:23 +00:00
|
|
|
oid = BSON::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
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
coll.remove
|
2009-01-16 19:41:53 +00:00
|
|
|
end
|
|
|
|
|
2009-01-16 21:10:52 +00:00
|
|
|
def test_pk_factory_reset
|
2010-10-13 21:09:23 +00:00
|
|
|
conn = standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
db = conn.db(MONGO_TEST_DB)
|
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
|
2011-02-20 16:19:14 +00:00
|
|
|
assert_match(/Cannot change/, ex.to_s)
|
2009-02-05 15:10:41 +00:00
|
|
|
ensure
|
2009-11-23 20:20:05 +00:00
|
|
|
conn.close
|
2009-01-16 21:10:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 16:26:18 +00:00
|
|
|
def test_authenticate
|
2010-01-28 21:39:40 +00:00
|
|
|
@@db.add_user('spongebob', 'squarepants')
|
2010-02-24 18:05:45 +00:00
|
|
|
assert_raise Mongo::AuthenticationError do
|
|
|
|
assert !@@db.authenticate('nobody', 'nopassword')
|
|
|
|
end
|
|
|
|
assert_raise Mongo::AuthenticationError do
|
|
|
|
assert !@@db.authenticate('spongebob' , 'squareliederhosen')
|
|
|
|
end
|
2010-01-28 21:39:40 +00:00
|
|
|
assert @@db.authenticate('spongebob', 'squarepants')
|
|
|
|
@@db.logout
|
|
|
|
@@db.remove_user('spongebob')
|
2009-01-21 16:52:43 +00:00
|
|
|
end
|
|
|
|
|
2011-04-15 14:58:37 +00:00
|
|
|
def test_authenticate_with_special_characters
|
|
|
|
assert @@db.add_user('foo:bar', '@foo')
|
|
|
|
assert @@db.authenticate('foo:bar', '@foo')
|
|
|
|
@@db.logout
|
|
|
|
@@db.remove_user('foo:bar')
|
|
|
|
end
|
|
|
|
|
2012-02-12 21:14:18 +00:00
|
|
|
def test_authenticate_read_only
|
|
|
|
@@db.add_user('joebob', 'user', true) # read-only user
|
|
|
|
assert @@db.authenticate('joebob', 'user')
|
|
|
|
@@db.logout
|
|
|
|
@@db.remove_user('joebob')
|
|
|
|
end
|
|
|
|
|
2010-02-17 20:15:07 +00:00
|
|
|
def test_authenticate_with_connection_uri
|
|
|
|
@@db.add_user('spongebob', 'squarepants')
|
2010-10-13 21:09:23 +00:00
|
|
|
assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@#{host_port}/#{@@db.name}")
|
2010-02-17 20:15:07 +00:00
|
|
|
|
2010-02-24 18:05:45 +00:00
|
|
|
assert_raise Mongo::AuthenticationError do
|
2011-08-25 18:57:24 +00:00
|
|
|
con = Mongo::Connection.from_uri("mongodb://wrong:info@#{host_port}/#{@@db.name}")
|
|
|
|
con['test']['foo'].find_one
|
2010-02-17 20:15:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 16:52:43 +00:00
|
|
|
def test_logout
|
2010-01-09 00:11:49 +00:00
|
|
|
assert @@db.logout
|
2009-01-21 16:26:18 +00:00
|
|
|
end
|
|
|
|
|
2010-05-18 20:17:17 +00:00
|
|
|
def test_command
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@db.command({:non_command => 1}, :check_response => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
result = @@db.command({:non_command => 1}, :check_response => false)
|
2010-06-01 02:52:03 +00:00
|
|
|
assert !Mongo::Support.ok?(result)
|
2010-05-18 20:17:17 +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
|
2010-08-04 20:47:13 +00:00
|
|
|
assert_nil @@db.get_last_error['err']
|
2009-05-26 19:26:20 +00:00
|
|
|
assert !@@db.error?
|
|
|
|
assert_nil @@db.previous_error
|
|
|
|
|
2010-05-18 20:17:17 +00:00
|
|
|
@@db.command({:forceerror => 1}, :check_response => false)
|
2009-02-05 15:10:41 +00:00
|
|
|
assert @@db.error?
|
2010-08-04 20:47:13 +00:00
|
|
|
assert_not_nil @@db.get_last_error['err']
|
2009-05-26 19:26:20 +00:00
|
|
|
assert_not_nil @@db.previous_error
|
2009-01-23 18:47:27 +00:00
|
|
|
|
2010-05-18 20:17:17 +00:00
|
|
|
@@db.command({:forceerror => 1}, :check_response => false)
|
2009-02-05 15:10:41 +00:00
|
|
|
assert @@db.error?
|
2010-08-04 20:47:13 +00:00
|
|
|
assert @@db.get_last_error['err']
|
2009-05-26 19:26:20 +00:00
|
|
|
prev_error = @@db.previous_error
|
|
|
|
assert_equal 1, prev_error['nPrev']
|
2010-08-04 20:47:13 +00:00
|
|
|
assert_equal prev_error["err"], @@db.get_last_error['err']
|
2009-05-26 19:26:20 +00:00
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
@@db.collection('test').find_one
|
2010-08-04 20:47:13 +00:00
|
|
|
assert_nil @@db.get_last_error['err']
|
2009-05-26 19:26:20 +00:00
|
|
|
assert !@@db.error?
|
|
|
|
assert @@db.previous_error
|
|
|
|
assert_equal 2, @@db.previous_error['nPrev']
|
|
|
|
|
|
|
|
@@db.reset_error_history
|
2010-08-04 20:47:13 +00:00
|
|
|
assert_nil @@db.get_last_error['err']
|
2009-05-26 19:26:20 +00:00
|
|
|
assert !@@db.error?
|
|
|
|
assert_nil @@db.previous_error
|
2009-01-23 18:47:27 +00:00
|
|
|
end
|
|
|
|
|
2009-11-02 18:22:46 +00:00
|
|
|
def test_check_command_response
|
|
|
|
command = {:forceerror => 1}
|
2011-09-06 18:22:50 +00:00
|
|
|
raised = false
|
|
|
|
begin
|
2010-05-18 20:17:17 +00:00
|
|
|
@@db.command(command)
|
2011-09-06 18:22:50 +00:00
|
|
|
rescue => ex
|
|
|
|
raised = true
|
|
|
|
assert ex.message.include?("forced error"),
|
|
|
|
"error message does not contain 'forced error'"
|
|
|
|
assert_equal 10038, ex.error_code
|
|
|
|
assert_equal 10038, ex.result['assertionCode']
|
|
|
|
ensure
|
|
|
|
assert raised, "No assertion raised!"
|
2009-11-02 18:22:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-01 16:01:37 +00:00
|
|
|
def test_last_status
|
2009-10-20 15:31:07 +00:00
|
|
|
@@db['test'].remove
|
2009-10-01 16:01:37 +00:00
|
|
|
@@db['test'].save("i" => 1)
|
|
|
|
|
|
|
|
@@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
|
2010-09-07 12:47:43 +00:00
|
|
|
assert @@db.get_last_error()["updatedExisting"]
|
2009-10-01 16:01:37 +00:00
|
|
|
|
|
|
|
@@db['test'].update({"i" => 1}, {"$set" => {"i" => 500}})
|
2010-09-07 12:47:43 +00:00
|
|
|
assert !@@db.get_last_error()["updatedExisting"]
|
2009-10-01 16:01:37 +00:00
|
|
|
end
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
def test_text_port_number_raises_no_errors
|
2010-10-13 21:09:23 +00:00
|
|
|
conn = standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
db = conn[MONGO_TEST_DB]
|
2010-06-24 17:46:24 +00:00
|
|
|
db.collection('users').remove
|
2009-01-29 21:58:55 +00:00
|
|
|
end
|
|
|
|
|
2010-01-28 21:39:40 +00:00
|
|
|
def test_user_management
|
|
|
|
@@db.add_user("bob", "secret")
|
|
|
|
assert @@db.authenticate("bob", "secret")
|
2010-02-17 21:48:23 +00:00
|
|
|
@@db.logout
|
2010-01-28 21:39:40 +00:00
|
|
|
assert @@db.remove_user("bob")
|
2010-02-24 18:05:45 +00:00
|
|
|
assert_raise Mongo::AuthenticationError do
|
|
|
|
@@db.authenticate("bob", "secret")
|
|
|
|
end
|
2010-01-28 21:39:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_remove_non_existant_user
|
|
|
|
assert !@@db.remove_user("joe")
|
|
|
|
end
|
|
|
|
|
2010-07-03 18:24:24 +00:00
|
|
|
def test_stored_function_management
|
|
|
|
@@db.add_stored_function("sum", "function (x, y) { return x + y; }")
|
|
|
|
assert_equal @@db.eval("return sum(2,3);"), 5
|
|
|
|
assert @@db.remove_stored_function("sum")
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@db.eval("return sum(2,3);")
|
|
|
|
end
|
|
|
|
end
|
2010-04-06 22:29:39 +00:00
|
|
|
|
2010-08-04 20:21:41 +00:00
|
|
|
def test_eval
|
|
|
|
@@db.eval("db.system.save({_id:'hello', value: function() { print('hello'); } })")
|
|
|
|
assert_equal 'hello', @@db['system'].find_one['_id']
|
|
|
|
end
|
|
|
|
|
2010-04-06 22:29:39 +00:00
|
|
|
if @@version >= "1.3.5"
|
|
|
|
def test_db_stats
|
|
|
|
stats = @@db.stats
|
|
|
|
assert stats.has_key?('collections')
|
|
|
|
assert stats.has_key?('dataSize')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-06 18:51:12 +00:00
|
|
|
context "database profiling" do
|
|
|
|
setup do
|
2010-04-05 19:48:35 +00:00
|
|
|
@db = @@conn[MONGO_TEST_DB]
|
2010-01-06 18:51:12 +00:00
|
|
|
@coll = @db['test']
|
|
|
|
@coll.remove
|
|
|
|
@r1 = @coll.insert('a' => 1) # collection not created until it's used
|
|
|
|
end
|
|
|
|
|
|
|
|
should "set default profiling level" do
|
|
|
|
assert_equal :off, @db.profiling_level
|
|
|
|
end
|
|
|
|
|
|
|
|
should "change profiling level" do
|
|
|
|
@db.profiling_level = :slow_only
|
|
|
|
assert_equal :slow_only, @db.profiling_level
|
|
|
|
@db.profiling_level = :off
|
|
|
|
assert_equal :off, @db.profiling_level
|
|
|
|
@db.profiling_level = :all
|
|
|
|
assert_equal :all, @db.profiling_level
|
|
|
|
begin
|
|
|
|
@db.profiling_level = :medium
|
|
|
|
fail "shouldn't be able to do this"
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "return profiling info" do
|
|
|
|
@db.profiling_level = :all
|
|
|
|
@coll.find()
|
|
|
|
@db.profiling_level = :off
|
|
|
|
|
|
|
|
info = @db.profiling_info
|
|
|
|
assert_kind_of Array, info
|
|
|
|
assert info.length >= 1
|
|
|
|
first = info.first
|
|
|
|
assert_kind_of Time, first['ts']
|
|
|
|
assert_kind_of Numeric, first['millis']
|
|
|
|
end
|
|
|
|
|
2010-01-06 20:01:59 +00:00
|
|
|
should "validate collection" do
|
2010-01-06 18:51:12 +00:00
|
|
|
doc = @db.validate_collection(@coll.name)
|
2011-05-10 18:28:04 +00:00
|
|
|
if @@version >= "1.9.1"
|
|
|
|
assert doc['valid']
|
|
|
|
else
|
|
|
|
assert doc['result']
|
|
|
|
end
|
2010-01-06 18:51:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|