2009-01-14 20:49:49 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
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-14 20:49:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-01-14 23:37:28 +00:00
|
|
|
if @db.connected?
|
|
|
|
@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
|
|
|
|
@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
|
|
|
|
|
|
|
|
coll.insert('name' => 'Fred')
|
|
|
|
row = coll.find({'name' => 'Fred'}, :limit => 1).next_object
|
|
|
|
assert_not_nil row
|
|
|
|
assert_equal 'Fred', row['name']
|
|
|
|
assert_kind_of XGen::Mongo::Driver::ObjectID, row['_id']
|
|
|
|
|
|
|
|
oid = XGen::Mongo::Driver::ObjectID.new
|
|
|
|
coll.insert('_id' => oid, 'name' => 'Barney')
|
|
|
|
row = coll.find({'name' => 'Barney'}, :limit => 1).next_object
|
|
|
|
assert_not_nil row
|
|
|
|
assert_equal 'Barney', row['name']
|
|
|
|
assert_equal oid, row['_id']
|
|
|
|
|
|
|
|
coll.clear
|
|
|
|
end
|
|
|
|
|
2009-01-14 20:49:49 +00:00
|
|
|
end
|