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
|
|
|
|
|
2008-12-19 02:57:20 +00:00
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
2008-12-02 00:39:39 +00:00
|
|
|
def setup
|
2008-12-08 21:41:52 +00:00
|
|
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2008-12-19 02:57:20 +00:00
|
|
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
|
|
|
@db = Mongo.new(host, port).db('ruby-mongo-test')
|
2008-12-02 00:39:39 +00:00
|
|
|
@coll = @db.collection('test')
|
|
|
|
@coll.clear
|
2008-12-18 21:03:33 +00:00
|
|
|
@r1 = @coll.insert('a' => 1) # collection not created until it's used
|
2008-12-02 12:20:29 +00:00
|
|
|
@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
|
2008-12-18 21:03:33 +00:00
|
|
|
@coll.insert('a' => 2)
|
|
|
|
@coll.insert('b' => 3)
|
2008-12-02 01:20:00 +00:00
|
|
|
|
|
|
|
assert_equal 3, @coll.count
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find().to_a
|
2008-12-02 01:20:00 +00:00
|
|
|
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-08 20:04:07 +00:00
|
|
|
|
|
|
|
@coll << {'b' => 4}
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find().to_a
|
2008-12-08 20:04:07 +00:00
|
|
|
assert_equal 4, docs.length
|
|
|
|
assert docs.detect { |row| row['b'] == 4 }
|
2008-12-02 01:20:00 +00:00
|
|
|
end
|
2009-01-07 15:11:30 +00:00
|
|
|
|
2008-12-08 21:43:30 +00:00
|
|
|
def test_find_simple
|
2008-12-18 21:03:33 +00:00
|
|
|
@r2 = @coll.insert('a' => 2)
|
|
|
|
@r3 = @coll.insert('b' => 3)
|
2008-12-08 20:32:38 +00:00
|
|
|
# Check sizes
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find().to_a
|
2008-12-06 21:31:27 +00:00
|
|
|
assert_equal 3, docs.size
|
|
|
|
assert_equal 3, @coll.count
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by other value
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => @r1['a']).to_a
|
2008-12-08 20:32:38 +00:00
|
|
|
assert_equal 1, docs.size
|
|
|
|
doc = docs.first
|
|
|
|
assert_equal doc['_id'], @r1['_id']
|
2008-12-08 21:43:30 +00:00
|
|
|
assert_equal doc['a'], @r1['a']
|
|
|
|
end
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 21:43:30 +00:00
|
|
|
def test_find_advanced
|
2008-12-18 21:03:33 +00:00
|
|
|
@coll.insert('a' => 2)
|
|
|
|
@coll.insert('b' => 3)
|
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (less than)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => { '$lt' => 10 }).to_a
|
2008-12-08 20:32:38 +00:00
|
|
|
assert_equal 2, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (greater than)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => { '$gt' => 1 }).to_a
|
2008-12-08 20:32:38 +00:00
|
|
|
assert_equal 1, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (less than or equal to)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => { '$lte' => 1 }).to_a
|
2008-12-08 20:32:38 +00:00
|
|
|
assert_equal 1, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (greater than or equal to)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => { '$gte' => 1 }).to_a
|
2008-12-08 20:32:38 +00:00
|
|
|
assert_equal 2, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (between)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
|
2008-12-08 20:32:38 +00:00
|
|
|
assert_equal 1, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
2008-12-08 22:20:31 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (in clause)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => {'$in' => [1,2]}).to_a
|
2008-12-08 20:35:49 +00:00
|
|
|
assert_equal 2, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
2008-12-08 22:20:31 +00:00
|
|
|
|
2008-12-08 21:41:00 +00:00
|
|
|
# Find by advanced query (regexp)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find('a' => /[1|2]/).to_a
|
2008-12-08 21:41:00 +00:00
|
|
|
assert_equal 2, docs.size
|
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
2008-12-08 21:43:30 +00:00
|
|
|
end
|
2008-12-08 22:20:31 +00:00
|
|
|
|
2008-12-08 21:43:30 +00:00
|
|
|
def test_find_sorting
|
2009-01-07 01:38:04 +00:00
|
|
|
@coll.clear
|
|
|
|
@coll.insert('a' => 1, 'b' => 2)
|
|
|
|
@coll.insert('a' => 2, 'b' => 1)
|
|
|
|
@coll.insert('a' => 3, 'b' => 2)
|
|
|
|
@coll.insert('a' => 4, 'b' => 1)
|
2008-12-09 17:35:03 +00:00
|
|
|
|
2008-12-08 21:41:00 +00:00
|
|
|
# Sorting (ascending)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => 1}).to_a
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 4, docs.size
|
2009-01-06 23:49:48 +00:00
|
|
|
assert_equal 1, docs[0]['a']
|
|
|
|
assert_equal 2, docs[1]['a']
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 3, docs[2]['a']
|
|
|
|
assert_equal 4, docs[3]['a']
|
2008-12-09 17:35:03 +00:00
|
|
|
|
2008-12-08 21:41:00 +00:00
|
|
|
# Sorting (descending)
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => -1}).to_a
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 4, docs.size
|
|
|
|
assert_equal 4, docs[0]['a']
|
|
|
|
assert_equal 3, docs[1]['a']
|
|
|
|
assert_equal 2, docs[2]['a']
|
|
|
|
assert_equal 1, docs[3]['a']
|
2008-12-09 20:06:35 +00:00
|
|
|
|
2008-12-09 20:16:25 +00:00
|
|
|
# Sorting using array of names; assumes ascending order.
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).to_a
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 4, docs.size
|
|
|
|
assert_equal 1, docs[0]['a']
|
|
|
|
assert_equal 2, docs[1]['a']
|
|
|
|
assert_equal 3, docs[2]['a']
|
|
|
|
assert_equal 4, docs[3]['a']
|
2009-01-07 15:11:30 +00:00
|
|
|
|
|
|
|
# Sorting using single name; assumes ascending order.
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
2009-01-07 15:11:30 +00:00
|
|
|
assert_equal 4, docs.size
|
|
|
|
assert_equal 1, docs[0]['a']
|
|
|
|
assert_equal 2, docs[1]['a']
|
|
|
|
assert_equal 3, docs[2]['a']
|
|
|
|
assert_equal 4, docs[3]['a']
|
|
|
|
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).to_a
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 4, docs.size
|
|
|
|
assert_equal 2, docs[0]['a']
|
|
|
|
assert_equal 4, docs[1]['a']
|
|
|
|
assert_equal 1, docs[2]['a']
|
|
|
|
assert_equal 3, docs[3]['a']
|
2008-12-09 20:06:35 +00:00
|
|
|
|
|
|
|
# Sorting using empty array; no order guarantee but should not blow up.
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 4, docs.size
|
2008-12-09 20:06:35 +00:00
|
|
|
|
|
|
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
|
|
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh['a'] = -1
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
2009-01-07 01:38:04 +00:00
|
|
|
assert_equal 4, docs.size
|
|
|
|
assert_equal 4, docs[0]['a']
|
|
|
|
assert_equal 3, docs[1]['a']
|
|
|
|
assert_equal 2, docs[2]['a']
|
|
|
|
assert_equal 1, docs[3]['a']
|
|
|
|
|
2009-01-07 15:11:30 +00:00
|
|
|
# TODO this will not pass due to known Mongo bug #898
|
|
|
|
# oh = OrderedHash.new
|
|
|
|
# oh['b'] = -1
|
|
|
|
# oh['a'] = 1
|
2009-01-07 19:07:17 +00:00
|
|
|
# docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
2009-01-07 15:11:30 +00:00
|
|
|
# assert_equal 4, docs.size
|
|
|
|
# assert_equal 1, docs[0]['a']
|
|
|
|
# assert_equal 3, docs[1]['a']
|
|
|
|
# assert_equal 2, docs[2]['a']
|
|
|
|
# assert_equal 4, docs[3]['a']
|
2008-12-06 21:31:27 +00:00
|
|
|
end
|
2008-12-02 12:20:29 +00:00
|
|
|
|
2009-01-06 22:55:00 +00:00
|
|
|
def test_find_limits
|
2009-01-06 23:49:48 +00:00
|
|
|
@coll.insert('b' => 2)
|
|
|
|
@coll.insert('c' => 3)
|
|
|
|
@coll.insert('d' => 4)
|
2009-01-07 15:11:30 +00:00
|
|
|
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({}, :limit => 1).to_a
|
2009-01-06 22:55:00 +00:00
|
|
|
assert_equal 1, docs.size
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({}, :limit => 2).to_a
|
2009-01-06 22:55:00 +00:00
|
|
|
assert_equal 2, docs.size
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({}, :limit => 3).to_a
|
2009-01-06 22:55:00 +00:00
|
|
|
assert_equal 3, docs.size
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({}, :limit => 4).to_a
|
2009-01-06 23:49:48 +00:00
|
|
|
assert_equal 4, docs.size
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({}).to_a
|
2009-01-06 23:49:48 +00:00
|
|
|
assert_equal 4, docs.size
|
2009-01-07 19:07:17 +00:00
|
|
|
docs = @coll.find({}, :limit => 99).to_a
|
2009-01-07 14:46:30 +00:00
|
|
|
assert_equal 4, docs.size
|
2009-01-06 22:55:00 +00:00
|
|
|
end
|
|
|
|
|
2008-12-02 12:20:29 +00:00
|
|
|
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
|
2009-01-07 19:07:17 +00:00
|
|
|
rows = cursor.to_a
|
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
|
|
|
|
2008-12-17 18:52:10 +00:00
|
|
|
def test_collection_options
|
|
|
|
@db.drop_collection('foobar')
|
|
|
|
@db.strict = true
|
|
|
|
|
|
|
|
begin
|
|
|
|
coll = @db.create_collection('foobar', :capped => true, :size => 1024)
|
|
|
|
options = coll.options()
|
|
|
|
assert_equal 'foobar', options['create']
|
|
|
|
assert_equal true, options['capped']
|
|
|
|
assert_equal 1024, options['size']
|
|
|
|
rescue => ex
|
|
|
|
@db.drop_collection('foobar')
|
|
|
|
fail "did not expect exception \"#{ex}\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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-18 21:03:33 +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-08 20:27:20 +00:00
|
|
|
|
|
|
|
def test_array
|
|
|
|
@coll << {'b' => [1, 2, 3]}
|
2009-01-07 19:07:17 +00:00
|
|
|
rows = @coll.find({}, {:fields => ['b']}).to_a
|
2008-12-08 20:27:20 +00:00
|
|
|
assert_equal 1, rows.length
|
|
|
|
assert_equal [1, 2, 3], rows[0]['b']
|
|
|
|
end
|
2008-12-08 20:33:30 +00:00
|
|
|
|
2008-12-08 20:57:20 +00:00
|
|
|
def test_regex
|
|
|
|
regex = /foobar/i
|
|
|
|
@coll << {'b' => regex}
|
2009-01-07 19:07:17 +00:00
|
|
|
rows = @coll.find({}, {:fields => ['b']}).to_a
|
2008-12-08 20:57:20 +00:00
|
|
|
assert_equal 1, rows.length
|
|
|
|
assert_equal regex, rows[0]['b']
|
|
|
|
end
|
2008-12-08 21:41:52 +00:00
|
|
|
|
2008-12-16 22:08:15 +00:00
|
|
|
def test_strict
|
|
|
|
assert !@db.strict?
|
|
|
|
@db.strict = true
|
|
|
|
assert @db.strict?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strict_access_collection
|
|
|
|
@db.strict = true
|
|
|
|
begin
|
|
|
|
@db.collection('does-not-exist')
|
|
|
|
fail "expected exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
|
|
|
ensure
|
|
|
|
@db.strict = false
|
|
|
|
@db.drop_collection('does-not-exist')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strict_create_collection
|
|
|
|
@db.drop_collection('foobar')
|
|
|
|
@db.strict = true
|
|
|
|
|
|
|
|
begin
|
|
|
|
@db.create_collection('foobar')
|
|
|
|
assert true
|
|
|
|
rescue => ex
|
|
|
|
fail "did not expect exception \"#{ex}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
# Now the collection exists. This time we should see an exception.
|
|
|
|
begin
|
|
|
|
@db.create_collection('foobar')
|
|
|
|
fail "expected exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
|
|
|
ensure
|
|
|
|
@db.strict = false
|
|
|
|
@db.drop_collection('foobar')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-07 19:07:17 +00:00
|
|
|
def test_to_a
|
|
|
|
cursor = @coll.find()
|
|
|
|
rows = cursor.to_a
|
|
|
|
|
|
|
|
# Make sure we get back exactly the same array the next time we ask
|
|
|
|
rows2 = cursor.to_a
|
|
|
|
assert_same rows, rows2
|
|
|
|
|
|
|
|
# Make sure we can still iterate after calling to_a
|
|
|
|
rows_with_each = cursor.collect{|row| row}
|
|
|
|
assert_equal rows, rows_with_each
|
|
|
|
|
|
|
|
# Make sure we can iterate more than once after calling to_a
|
|
|
|
end
|
|
|
|
|
2009-01-07 19:22:53 +00:00
|
|
|
def test_to_a_after_each
|
|
|
|
cursor = @coll.find
|
|
|
|
cursor.each { |row| row }
|
|
|
|
begin
|
|
|
|
cursor.to_a
|
|
|
|
fail "expected \"can't call\" error"
|
|
|
|
rescue => ex
|
|
|
|
assert_equal "can't call Cursor#to_a after calling Cursor#each", ex.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-16 22:35:31 +00:00
|
|
|
def test_ismaster
|
|
|
|
assert @db.master?
|
|
|
|
end
|
|
|
|
|
2009-01-13 18:08:04 +00:00
|
|
|
def test_master
|
|
|
|
assert_equal "#{@db.host}:#{@db.port}", @db.master
|
|
|
|
end
|
|
|
|
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|