2008-12-02 00:39:39 +00:00
|
|
|
$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
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@db = XGen::Mongo::Driver::Mongo.new.db('ruby-mongo-test')
|
|
|
|
@coll = @db.collection('test')
|
|
|
|
@coll.clear
|
2008-12-02 12:20:29 +00:00
|
|
|
@coll.insert('a' => 1) # collection not created until it's used
|
|
|
|
@coll_full_name = 'ruby-mongo-test.test'
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2008-12-08 16:38:42 +00:00
|
|
|
@coll.clear unless @coll == nil || @db.socket.closed?
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear
|
|
|
|
assert_equal 1, @coll.count
|
|
|
|
@coll.clear
|
|
|
|
assert_equal 0, @coll.count
|
|
|
|
end
|
2008-12-02 01:20:00 +00:00
|
|
|
|
|
|
|
def test_insert
|
|
|
|
@coll.insert('a' => 2)
|
|
|
|
@coll.insert('b' => 3)
|
|
|
|
|
|
|
|
assert_equal 3, @coll.count
|
|
|
|
docs = @coll.find().collect
|
|
|
|
assert_equal 3, docs.length
|
2008-12-08 16:38:42 +00:00
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
|
|
|
assert docs.detect { |row| row['b'] == 3 }
|
2008-12-02 01:20:00 +00:00
|
|
|
end
|
2008-12-02 12:20:29 +00:00
|
|
|
|
|
|
|
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"
|
2008-12-02 12:38:22 +00:00
|
|
|
assert !@db.collection_names.include?(@coll_full_name)
|
2008-12-08 16:38:42 +00:00
|
|
|
@coll = nil
|
2008-12-02 12:20:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection_names
|
|
|
|
names = @db.collection_names
|
2008-12-02 12:38:22 +00:00
|
|
|
assert names.length >= 1
|
|
|
|
assert names.include?(@coll_full_name)
|
2008-12-02 12:20:29 +00:00
|
|
|
|
|
|
|
coll2 = @db.collection('test2')
|
|
|
|
coll2.insert('a' => 1) # collection not created until it's used
|
|
|
|
names = @db.collection_names
|
2008-12-02 12:38:22 +00:00
|
|
|
assert names.length >= 2
|
|
|
|
assert names.include?(@coll_full_name)
|
2008-12-02 12:20:29 +00:00
|
|
|
assert names.include?('ruby-mongo-test.test2')
|
|
|
|
ensure
|
|
|
|
@db.drop_collection('test2')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collections_info
|
|
|
|
cursor = @db.collections_info
|
|
|
|
rows = cursor.collect
|
2008-12-02 12:38:22 +00:00
|
|
|
assert rows.length >= 1
|
|
|
|
row = rows.detect { |r| r['name'] == @coll_full_name }
|
|
|
|
assert_not_nil row
|
2008-12-02 15:25:36 +00:00
|
|
|
assert_equal @coll.name, row['options']['create']
|
2008-12-02 12:20:29 +00:00
|
|
|
end
|
2008-12-02 12:26:45 +00:00
|
|
|
|
|
|
|
def test_full_coll_name
|
|
|
|
assert_equal @coll_full_name, @db.full_coll_name(@coll.name)
|
|
|
|
end
|
2008-12-02 12:38:22 +00:00
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def test_index_information
|
|
|
|
@db.create_index(@coll.name, 'index_name', ['a'])
|
|
|
|
list = @db.index_information(@coll.name)
|
|
|
|
assert_equal 1, list.length
|
2008-12-02 12:38:22 +00:00
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
info = list[0]
|
|
|
|
assert_equal 'index_name', info[:name]
|
|
|
|
assert_equal 1, info[:keys]['a']
|
|
|
|
end
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|