New switch_to_master db method. Reorganized db tests a bit.

This commit is contained in:
Jim Menard 2009-01-14 15:49:49 -05:00
parent abf5b7f8f0
commit fdb4ed7dc5
3 changed files with 60 additions and 15 deletions

View File

@ -167,6 +167,20 @@ module XGen
end
end
# Switches our socket to the master database. If we are already the
# master, no change is made.
def switch_to_master
master_str = master()
unless master_str == "#@host:#@port"
@semaphore.synchronize {
master_str =~ /(.+):(\d+)/
@host, @port = $1, $2
close()
@socket = TCPSocket.new(@host, @port)
}
end
end
# Close the connection to the database.
def close
@socket.close

46
tests/test_db.rb Normal file
View File

@ -0,0 +1,46 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
require 'test/unit'
# NOTE: assumes Mongo is running
class DBAPITest < Test::Unit::TestCase
include XGen::Mongo::Driver
def setup
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')
@coll = @db.collection('test')
@coll.clear
@r1 = @coll.insert('a' => 1) # collection not created until it's used
@coll_full_name = 'ruby-mongo-test.test'
end
def teardown
@coll.clear unless @coll == nil || @db.socket.closed?
end
def test_close
@db.close
assert @db.socket.closed?
begin
@coll.insert('a' => 1)
fail "expected IOError exception"
rescue IOError => ex
assert_match /closed stream/, ex.to_s
end
end
def test_full_coll_name
assert_equal @coll_full_name, @db.full_coll_name(@coll.name)
end
def test_master
# Doesn't really test anything since we probably only have one database
# during this test.
@db.switch_to_master
assert_not_nil @db.socket
end
end

View File

@ -196,17 +196,6 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 4, docs.size
end
def test_close
@db.close
assert @db.socket.closed?
begin
@coll.insert('a' => 1)
fail "expected IOError exception"
rescue IOError => ex
assert_match /closed stream/, ex.to_s
end
end
def test_drop_collection
assert @db.drop_collection(@coll.name), "drop of collection #{@coll.name} failed"
assert !@db.collection_names.include?(@coll_full_name)
@ -253,10 +242,6 @@ class DBAPITest < Test::Unit::TestCase
end
end
def test_full_coll_name
assert_equal @coll_full_name, @db.full_coll_name(@coll.name)
end
def test_index_information
@db.create_index(@coll.name, 'index_name', ['a'])
list = @db.index_information(@coll.name)