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
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2008-12-19 02:57:20 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
@@conn = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
2009-10-26 18:54:33 +00:00
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db = @@conn.db("ruby-mongo-test")
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll = @@db.collection('test')
|
2009-11-23 20:20:05 +00:00
|
|
|
@@version = @@conn.server_version
|
2009-02-05 15:10:41 +00:00
|
|
|
|
2008-12-02 00:39:39 +00:00
|
|
|
def setup
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-07-27 23:43:20 +00:00
|
|
|
@r1 = {'a' => 1}
|
|
|
|
@@coll.insert(@r1) # collection not created until it's used
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll_full_name = 'ruby-mongo-test.test'
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-03-13 21:09:19 +00:00
|
|
|
@@db.error
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal 1, @@coll.count
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal 0, @@coll.count
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|
2008-12-02 01:20:00 +00:00
|
|
|
|
|
|
|
def test_insert
|
2009-07-27 23:43:20 +00:00
|
|
|
assert_kind_of ObjectID, @@coll.insert('a' => 2)
|
|
|
|
assert_kind_of ObjectID, @@coll.insert('b' => 3)
|
2008-12-02 01:20:00 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal 3, @@coll.count
|
|
|
|
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
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll << {'b' => 4}
|
|
|
|
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
|
|
|
|
2009-08-13 14:29:08 +00:00
|
|
|
def test_save_ordered_hash
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh['a'] = -1
|
|
|
|
oh['b'] = 'foo'
|
|
|
|
|
|
|
|
oid = @@coll.save(oh)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 'foo', @@coll.find_one(oid)['b']
|
2009-08-14 15:38:25 +00:00
|
|
|
|
|
|
|
oh = OrderedHash['a' => 1, 'b' => 'foo']
|
|
|
|
oid = @@coll.save(oh)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 'foo', @@coll.find_one(oid)['b']
|
2009-08-13 14:29:08 +00:00
|
|
|
end
|
|
|
|
|
2009-01-23 13:33:26 +00:00
|
|
|
def test_insert_multiple
|
2009-08-14 18:25:29 +00:00
|
|
|
ids = @@coll.insert([{'a' => 2}, {'b' => 3}])
|
2009-07-28 16:08:29 +00:00
|
|
|
|
2009-07-27 23:43:20 +00:00
|
|
|
ids.each do |i|
|
|
|
|
assert_kind_of ObjectID, i
|
|
|
|
end
|
2009-01-23 13:33:26 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal 3, @@coll.count
|
|
|
|
docs = @@coll.find().to_a
|
2009-01-23 13:33:26 +00:00
|
|
|
assert_equal 3, docs.length
|
|
|
|
assert docs.detect { |row| row['a'] == 1 }
|
|
|
|
assert docs.detect { |row| row['a'] == 2 }
|
|
|
|
assert docs.detect { |row| row['b'] == 3 }
|
|
|
|
end
|
|
|
|
|
2009-06-18 13:38:55 +00:00
|
|
|
def test_count_on_nonexisting
|
|
|
|
@@db.drop_collection('foo')
|
|
|
|
assert_equal 0, @@db.collection('foo').count()
|
|
|
|
end
|
|
|
|
|
2008-12-08 21:43:30 +00:00
|
|
|
def test_find_simple
|
2009-02-05 15:10:41 +00:00
|
|
|
@r2 = @@coll.insert('a' => 2)
|
|
|
|
@r3 = @@coll.insert('b' => 3)
|
2008-12-08 20:32:38 +00:00
|
|
|
# Check sizes
|
2009-02-05 15:10:41 +00:00
|
|
|
docs = @@coll.find().to_a
|
2008-12-06 21:31:27 +00:00
|
|
|
assert_equal 3, docs.size
|
2009-02-05 15:10:41 +00:00
|
|
|
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-02-05 15:10:41 +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
|
2009-02-01 14:03:55 +00:00
|
|
|
# Can't compare _id values because at insert, an _id was added to @r1 by
|
|
|
|
# the database but we don't know what it is without re-reading the record
|
|
|
|
# (which is what we are doing right now).
|
2009-11-23 20:20:05 +00:00
|
|
|
# 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
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll.insert('a' => 2)
|
|
|
|
@@coll.insert('b' => 3)
|
2008-12-18 21:03:33 +00:00
|
|
|
|
2008-12-08 20:32:38 +00:00
|
|
|
# Find by advanced query (less than)
|
2009-02-05 15:10:41 +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-02-05 15:10:41 +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-02-05 15:10:41 +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-02-05 15:10:41 +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-02-05 15:10:41 +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-02-05 15:10:41 +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-02-05 15:10:41 +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-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-02-05 15:10:41 +00:00
|
|
|
@@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-10-08 14:02:54 +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-10-08 14:02:54 +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-11-23 23:09:13 +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-02-05 15:10:41 +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-02-05 15:10:41 +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
|
|
|
|
2009-10-08 14:02:54 +00:00
|
|
|
# Sorting using empty array; no order guarantee should not blow up.
|
2009-02-05 15:10:41 +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-11-23 23:09:13 +00:00
|
|
|
assert_raise InvalidSortValueError do
|
|
|
|
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
|
|
|
end
|
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-02-05 15:10:41 +00:00
|
|
|
@@coll.insert('b' => 2)
|
|
|
|
@@coll.insert('c' => 3)
|
|
|
|
@@coll.insert('d' => 4)
|
2009-01-07 15:11:30 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
docs = @@coll.find({}, :limit => 1).to_a
|
2009-01-06 22:55:00 +00:00
|
|
|
assert_equal 1, docs.size
|
2009-02-05 15:10:41 +00:00
|
|
|
docs = @@coll.find({}, :limit => 2).to_a
|
2009-01-06 22:55:00 +00:00
|
|
|
assert_equal 2, docs.size
|
2009-02-05 15:10:41 +00:00
|
|
|
docs = @@coll.find({}, :limit => 3).to_a
|
2009-01-06 22:55:00 +00:00
|
|
|
assert_equal 3, docs.size
|
2009-02-05 15:10:41 +00:00
|
|
|
docs = @@coll.find({}, :limit => 4).to_a
|
2009-01-06 23:49:48 +00:00
|
|
|
assert_equal 4, docs.size
|
2009-02-05 15:10:41 +00:00
|
|
|
docs = @@coll.find({}).to_a
|
2009-01-06 23:49:48 +00:00
|
|
|
assert_equal 4, docs.size
|
2009-02-05 15:10:41 +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
|
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
def test_find_one_no_records
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-08-14 19:39:49 +00:00
|
|
|
x = @@coll.find_one('a' => 1)
|
2009-02-09 14:46:30 +00:00
|
|
|
assert_nil x
|
|
|
|
end
|
|
|
|
|
2008-12-02 12:20:29 +00:00
|
|
|
def test_drop_collection
|
2009-02-05 15:10:41 +00:00
|
|
|
assert @@db.drop_collection(@@coll.name), "drop of collection #{@@coll.name} failed"
|
2009-08-06 19:52:07 +00:00
|
|
|
assert !@@db.collection_names.include?(@@coll.name)
|
2008-12-02 12:20:29 +00:00
|
|
|
end
|
|
|
|
|
2009-03-02 15:49:27 +00:00
|
|
|
def test_other_drop
|
2009-08-06 19:52:07 +00:00
|
|
|
assert @@db.collection_names.include?(@@coll.name)
|
2009-03-02 15:49:27 +00:00
|
|
|
@@coll.drop
|
2009-08-06 19:52:07 +00:00
|
|
|
assert !@@db.collection_names.include?(@@coll.name)
|
2009-03-02 15:49:27 +00:00
|
|
|
end
|
|
|
|
|
2008-12-02 12:20:29 +00:00
|
|
|
def test_collection_names
|
2009-02-05 15:10:41 +00:00
|
|
|
names = @@db.collection_names
|
2008-12-02 12:38:22 +00:00
|
|
|
assert names.length >= 1
|
2009-08-06 19:52:07 +00:00
|
|
|
assert names.include?(@@coll.name)
|
2008-12-02 12:20:29 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
coll2 = @@db.collection('test2')
|
2008-12-02 12:20:29 +00:00
|
|
|
coll2.insert('a' => 1) # collection not created until it's used
|
2009-02-05 15:10:41 +00:00
|
|
|
names = @@db.collection_names
|
2008-12-02 12:38:22 +00:00
|
|
|
assert names.length >= 2
|
2009-08-06 19:52:07 +00:00
|
|
|
assert names.include?(@@coll.name)
|
2008-12-02 12:20:29 +00:00
|
|
|
assert names.include?('ruby-mongo-test.test2')
|
|
|
|
ensure
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.drop_collection('test2')
|
2008-12-02 12:20:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collections_info
|
2009-02-05 15:10:41 +00:00
|
|
|
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
|
2009-02-05 15:10:41 +00:00
|
|
|
row = rows.detect { |r| r['name'] == @@coll_full_name }
|
2008-12-02 12:38:22 +00:00
|
|
|
assert_not_nil row
|
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
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.drop_collection('foobar')
|
|
|
|
@@db.strict = true
|
2008-12-17 18:52:10 +00:00
|
|
|
|
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
|
2008-12-17 18:52:10 +00:00
|
|
|
options = coll.options()
|
|
|
|
assert_equal 'foobar', options['create']
|
|
|
|
assert_equal true, options['capped']
|
|
|
|
assert_equal 1024, options['size']
|
|
|
|
rescue => ex
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.drop_collection('foobar')
|
2008-12-17 18:52:10 +00:00
|
|
|
fail "did not expect exception \"#{ex}\""
|
2009-02-05 15:10:41 +00:00
|
|
|
ensure
|
|
|
|
@@db.strict = false
|
2008-12-17 18:52:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def test_index_information
|
2009-06-02 15:24:52 +00:00
|
|
|
assert_equal @@coll.index_information.length, 1
|
|
|
|
|
2009-02-26 17:06:03 +00:00
|
|
|
name = @@db.create_index(@@coll.name, 'a')
|
2009-06-02 15:24:52 +00:00
|
|
|
info = @@db.index_information(@@coll.name)
|
|
|
|
assert_equal name, "a_1"
|
|
|
|
assert_equal @@coll.index_information, info
|
|
|
|
assert_equal 2, info.length
|
|
|
|
|
2009-06-04 19:32:26 +00:00
|
|
|
assert info.has_key?(name)
|
2009-06-02 15:24:52 +00:00
|
|
|
assert_equal info[name], [["a", ASCENDING]]
|
2009-02-05 18:53:46 +00:00
|
|
|
ensure
|
2009-02-26 17:06:03 +00:00
|
|
|
@@db.drop_index(@@coll.name, name)
|
2009-02-05 18:53:46 +00:00
|
|
|
end
|
2009-06-02 18:37:58 +00:00
|
|
|
|
2009-06-01 02:16:01 +00:00
|
|
|
def test_index_create_with_symbol
|
2009-06-02 18:37:58 +00:00
|
|
|
assert_equal @@coll.index_information.length, 1
|
|
|
|
|
2009-06-01 02:16:01 +00:00
|
|
|
name = @@db.create_index(@@coll.name, :a)
|
2009-06-02 18:37:58 +00:00
|
|
|
info = @@db.index_information(@@coll.name)
|
|
|
|
assert_equal name, "a_1"
|
|
|
|
assert_equal @@coll.index_information, info
|
|
|
|
assert_equal 2, info.length
|
|
|
|
|
2009-06-04 19:32:26 +00:00
|
|
|
assert info.has_key?(name)
|
2009-06-02 18:37:58 +00:00
|
|
|
assert_equal info[name], [["a", ASCENDING]]
|
2009-06-01 02:16:01 +00:00
|
|
|
ensure
|
|
|
|
@@db.drop_index(@@coll.name, name)
|
|
|
|
end
|
2009-02-05 18:53:46 +00:00
|
|
|
|
|
|
|
def test_multiple_index_cols
|
2009-02-26 17:06:03 +00:00
|
|
|
name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
|
2009-06-02 15:24:52 +00:00
|
|
|
info = @@db.index_information(@@coll.name)
|
|
|
|
assert_equal 2, info.length
|
2009-02-05 18:53:46 +00:00
|
|
|
|
2009-02-26 17:06:03 +00:00
|
|
|
assert_equal name, 'a_-1_b_1_c_-1'
|
2009-06-04 19:32:26 +00:00
|
|
|
assert info.has_key?(name)
|
2009-06-02 15:24:52 +00:00
|
|
|
assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name]
|
2009-02-05 18:53:46 +00:00
|
|
|
ensure
|
2009-02-26 17:06:03 +00:00
|
|
|
@@db.drop_index(@@coll.name, name)
|
2008-12-08 16:38:42 +00:00
|
|
|
end
|
2009-06-02 18:37:58 +00:00
|
|
|
|
2009-06-01 02:16:01 +00:00
|
|
|
def test_multiple_index_cols_with_symbols
|
|
|
|
name = @@db.create_index(@@coll.name, [[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]])
|
2009-06-02 18:37:58 +00:00
|
|
|
info = @@db.index_information(@@coll.name)
|
|
|
|
assert_equal 2, info.length
|
2009-06-01 02:16:01 +00:00
|
|
|
|
|
|
|
assert_equal name, 'a_-1_b_1_c_-1'
|
2009-06-04 19:32:26 +00:00
|
|
|
assert info.has_key?(name)
|
2009-06-02 18:37:58 +00:00
|
|
|
assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name]
|
2009-06-01 02:16:01 +00:00
|
|
|
ensure
|
|
|
|
@@db.drop_index(@@coll.name, name)
|
|
|
|
end
|
2008-12-08 20:27:20 +00:00
|
|
|
|
2009-04-21 18:44:57 +00:00
|
|
|
def test_unique_index
|
|
|
|
@@db.drop_collection("blah")
|
|
|
|
test = @@db.collection("blah")
|
|
|
|
test.create_index("hello")
|
|
|
|
|
|
|
|
test.insert("hello" => "world")
|
|
|
|
test.insert("hello" => "mike")
|
|
|
|
test.insert("hello" => "world")
|
|
|
|
assert !@@db.error?
|
|
|
|
|
|
|
|
@@db.drop_collection("blah")
|
|
|
|
test = @@db.collection("blah")
|
|
|
|
test.create_index("hello", unique=true)
|
|
|
|
|
|
|
|
test.insert("hello" => "world")
|
|
|
|
test.insert("hello" => "mike")
|
|
|
|
test.insert("hello" => "world")
|
|
|
|
assert @@db.error?
|
2009-06-02 13:27:50 +00:00
|
|
|
end
|
2009-04-21 18:44:57 +00:00
|
|
|
|
2009-06-02 13:27:50 +00:00
|
|
|
def test_index_on_subfield
|
2009-04-21 18:44:57 +00:00
|
|
|
@@db.drop_collection("blah")
|
2009-06-02 13:27:50 +00:00
|
|
|
test = @@db.collection("blah")
|
|
|
|
|
|
|
|
test.insert("hello" => {"a" => 4, "b" => 5})
|
|
|
|
test.insert("hello" => {"a" => 7, "b" => 2})
|
|
|
|
test.insert("hello" => {"a" => 4, "b" => 10})
|
|
|
|
assert !@@db.error?
|
|
|
|
|
|
|
|
@@db.drop_collection("blah")
|
|
|
|
test = @@db.collection("blah")
|
|
|
|
test.create_index("hello.a", unique=true)
|
|
|
|
|
|
|
|
test.insert("hello" => {"a" => 4, "b" => 5})
|
|
|
|
test.insert("hello" => {"a" => 7, "b" => 2})
|
|
|
|
test.insert("hello" => {"a" => 4, "b" => 10})
|
|
|
|
assert @@db.error?
|
2009-04-21 18:44:57 +00:00
|
|
|
end
|
|
|
|
|
2008-12-08 20:27:20 +00:00
|
|
|
def test_array
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll << {'b' => [1, 2, 3]}
|
|
|
|
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
2009-10-26 18:54:33 +00:00
|
|
|
if @@version < "1.1.3"
|
|
|
|
assert_equal 1, rows.length
|
|
|
|
assert_equal [1, 2, 3], rows[0]['b']
|
|
|
|
else
|
|
|
|
assert_equal 2, rows.length
|
|
|
|
assert_equal [1, 2, 3], rows[1]['b']
|
|
|
|
end
|
2008-12-08 20:27:20 +00:00
|
|
|
end
|
2008-12-08 20:33:30 +00:00
|
|
|
|
2008-12-08 20:57:20 +00:00
|
|
|
def test_regex
|
|
|
|
regex = /foobar/i
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll << {'b' => regex}
|
|
|
|
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
2009-10-26 18:54:33 +00:00
|
|
|
if @@version < "1.1.3"
|
|
|
|
assert_equal 1, rows.length
|
|
|
|
assert_equal regex, rows[0]['b']
|
|
|
|
else
|
|
|
|
assert_equal 2, rows.length
|
|
|
|
assert_equal regex, rows[1]['b']
|
|
|
|
end
|
2008-12-08 20:57:20 +00:00
|
|
|
end
|
2008-12-08 21:41:52 +00:00
|
|
|
|
2009-01-26 13:51:27 +00:00
|
|
|
def test_non_oid_id
|
|
|
|
# Note: can't use Time.new because that will include fractional seconds,
|
|
|
|
# which Mongo does not store.
|
|
|
|
t = Time.at(1234567890)
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll << {'_id' => t}
|
|
|
|
rows = @@coll.find({'_id' => t}).to_a
|
2009-01-26 13:51:27 +00:00
|
|
|
assert_equal 1, rows.length
|
|
|
|
assert_equal t, rows[0]['_id']
|
|
|
|
end
|
|
|
|
|
2008-12-16 22:08:15 +00:00
|
|
|
def test_strict
|
2009-02-05 15:10:41 +00:00
|
|
|
assert !@@db.strict?
|
|
|
|
@@db.strict = true
|
|
|
|
assert @@db.strict?
|
|
|
|
ensure
|
|
|
|
@@db.strict = false
|
2008-12-16 22:08:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_strict_access_collection
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.strict = true
|
2008-12-16 22:08:15 +00:00
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.collection('does-not-exist')
|
2008-12-16 22:08:15 +00:00
|
|
|
fail "expected exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
|
|
|
ensure
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.strict = false
|
|
|
|
@@db.drop_collection('does-not-exist')
|
2008-12-16 22:08:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strict_create_collection
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.drop_collection('foobar')
|
|
|
|
@@db.strict = true
|
2008-12-16 22:08:15 +00:00
|
|
|
|
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.create_collection('foobar')
|
2008-12-16 22:08:15 +00:00
|
|
|
assert true
|
|
|
|
rescue => ex
|
|
|
|
fail "did not expect exception \"#{ex}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
# Now the collection exists. This time we should see an exception.
|
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.create_collection('foobar')
|
2008-12-16 22:08:15 +00:00
|
|
|
fail "expected exception"
|
|
|
|
rescue => ex
|
|
|
|
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
|
|
|
ensure
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db.strict = false
|
|
|
|
@@db.drop_collection('foobar')
|
2008-12-16 22:08:15 +00:00
|
|
|
end
|
2009-03-02 15:49:27 +00:00
|
|
|
|
|
|
|
# Now we're not in strict mode - should succeed
|
|
|
|
@@db.create_collection('foobar')
|
|
|
|
@@db.create_collection('foobar')
|
|
|
|
@@db.drop_collection('foobar')
|
|
|
|
end
|
|
|
|
|
2009-01-07 19:07:17 +00:00
|
|
|
def test_to_a
|
2009-02-05 15:10:41 +00:00
|
|
|
cursor = @@coll.find()
|
2009-01-07 19:07:17 +00:00
|
|
|
rows = cursor.to_a
|
|
|
|
|
2009-08-21 18:11:59 +00:00
|
|
|
assert_raise InvalidOperation do
|
|
|
|
cursor.to_a
|
|
|
|
end
|
2009-01-07 19:07:17 +00:00
|
|
|
|
2009-08-21 18:11:59 +00:00
|
|
|
cursor.each { |doc| fail "should be no docs in each now" }
|
2009-01-07 19:07:17 +00:00
|
|
|
end
|
|
|
|
|
2009-01-07 19:22:53 +00:00
|
|
|
def test_to_a_after_each
|
2009-02-05 15:10:41 +00:00
|
|
|
cursor = @@coll.find
|
2009-01-07 19:22:53 +00:00
|
|
|
cursor.each { |row| row }
|
2009-08-21 18:11:59 +00:00
|
|
|
assert_raise InvalidOperation do
|
2009-01-07 19:22:53 +00:00
|
|
|
cursor.to_a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-16 22:35:31 +00:00
|
|
|
def test_ismaster
|
2009-11-23 20:20:05 +00:00
|
|
|
assert @@conn.master?
|
2008-12-16 22:35:31 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 18:08:04 +00:00
|
|
|
def test_master
|
2009-11-23 20:20:05 +00:00
|
|
|
assert_equal "#{@@conn.host}:#{@@conn.port}", @@conn.master
|
2009-01-13 18:08:04 +00:00
|
|
|
end
|
|
|
|
|
2009-03-12 21:25:23 +00:00
|
|
|
def test_where
|
|
|
|
@@coll.insert('a' => 2)
|
|
|
|
@@coll.insert('a' => 3)
|
|
|
|
|
|
|
|
assert_equal 3, @@coll.count
|
2009-08-18 15:48:16 +00:00
|
|
|
assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count()
|
|
|
|
assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count()
|
2009-03-12 21:25:23 +00:00
|
|
|
end
|
|
|
|
|
2009-03-13 15:03:52 +00:00
|
|
|
def test_eval
|
|
|
|
assert_equal 3, @@db.eval('function (x) {return x;}', 3)
|
|
|
|
|
|
|
|
assert_equal nil, @@db.eval("function (x) {db.test_eval.save({y:x});}", 5)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 5, @@db.collection('test_eval').find_one['y']
|
2009-03-13 15:03:52 +00:00
|
|
|
|
|
|
|
assert_equal 5, @@db.eval("function (x, y) {return x + y;}", 2, 3)
|
|
|
|
assert_equal 5, @@db.eval("function () {return 5;}")
|
|
|
|
assert_equal 5, @@db.eval("2 + 3;")
|
|
|
|
|
|
|
|
assert_equal 5, @@db.eval(Code.new("2 + 3;"))
|
|
|
|
assert_equal 2, @@db.eval(Code.new("return i;", {"i" => 2}))
|
|
|
|
assert_equal 5, @@db.eval(Code.new("i + 3;", {"i" => 2}))
|
|
|
|
|
2009-08-19 19:37:29 +00:00
|
|
|
assert_raise OperationFailure do
|
2009-03-13 15:03:52 +00:00
|
|
|
@@db.eval("5 ++ 5;")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-29 12:15:59 +00:00
|
|
|
def test_hint
|
2009-02-26 17:06:03 +00:00
|
|
|
name = @@coll.create_index('a')
|
2009-01-29 12:15:59 +00:00
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_nil @@coll.hint
|
|
|
|
assert_equal 1, @@coll.find({'a' => 1}, :hint => 'a').to_a.size
|
|
|
|
assert_equal 1, @@coll.find({'a' => 1}, :hint => ['a']).to_a.size
|
|
|
|
assert_equal 1, @@coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
|
|
|
|
|
|
|
|
@@coll.hint = 'a'
|
|
|
|
assert_equal({'a' => 1}, @@coll.hint)
|
|
|
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
|
|
|
|
|
|
|
@@coll.hint = ['a']
|
|
|
|
assert_equal({'a' => 1}, @@coll.hint)
|
|
|
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
|
|
|
|
|
|
|
@@coll.hint = {'a' => 1}
|
|
|
|
assert_equal({'a' => 1}, @@coll.hint)
|
|
|
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
|
|
|
|
|
|
|
@@coll.hint = nil
|
|
|
|
assert_nil @@coll.hint
|
|
|
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
2009-01-30 19:35:22 +00:00
|
|
|
ensure
|
2009-02-26 17:06:03 +00:00
|
|
|
@@coll.drop_index(name)
|
2009-01-29 12:15:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-04-24 14:03:40 +00:00
|
|
|
def test_hash_default_value_id
|
|
|
|
val = Hash.new(0)
|
|
|
|
val["x"] = 5
|
|
|
|
@@coll.insert val
|
2009-08-14 19:39:49 +00:00
|
|
|
id = @@coll.find_one("x" => 5)["_id"]
|
2009-04-24 14:03:40 +00:00
|
|
|
assert id != 0
|
|
|
|
end
|
|
|
|
|
2009-04-27 18:19:38 +00:00
|
|
|
def test_group
|
|
|
|
@@db.drop_collection("test")
|
|
|
|
test = @@db.collection("test")
|
|
|
|
|
|
|
|
assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
|
2009-08-19 19:54:53 +00:00
|
|
|
assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)
|
2009-04-27 18:19:38 +00:00
|
|
|
|
|
|
|
test.insert("a" => 2)
|
|
|
|
test.insert("b" => 5)
|
|
|
|
test.insert("a" => 1)
|
|
|
|
|
|
|
|
assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
|
2009-08-19 19:54:53 +00:00
|
|
|
assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)[0]["count"]
|
2009-04-27 18:19:38 +00:00
|
|
|
assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
|
2009-08-19 19:54:53 +00:00
|
|
|
assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)[0]["count"]
|
2009-08-19 19:18:42 +00:00
|
|
|
|
|
|
|
test.insert("a" => 2, "b" => 3)
|
|
|
|
expected = [{"a" => 2, "count" => 2},
|
|
|
|
{"a" => nil, "count" => 1},
|
|
|
|
{"a" => 1, "count" => 1}]
|
|
|
|
assert_equal expected, test.group(["a"], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
|
2009-08-19 19:54:53 +00:00
|
|
|
assert_equal expected, test.group(["a"], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)
|
2009-08-19 19:37:29 +00:00
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
test.group([], {}, {}, "5 ++ 5")
|
|
|
|
end
|
2009-08-19 19:54:53 +00:00
|
|
|
assert_raise OperationFailure do
|
|
|
|
test.group([], {}, {}, "5 ++ 5", true)
|
|
|
|
end
|
2009-04-27 18:19:38 +00:00
|
|
|
end
|
|
|
|
|
2009-04-28 18:55:36 +00:00
|
|
|
def test_deref
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-04-28 18:55:36 +00:00
|
|
|
|
|
|
|
assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
|
2009-05-12 18:21:17 +00:00
|
|
|
@@coll.insert({"x" => "hello"})
|
2009-08-14 19:39:49 +00:00
|
|
|
key = @@coll.find_one()["_id"]
|
2009-05-12 18:21:17 +00:00
|
|
|
assert_equal "hello", @@db.dereference(DBRef.new("test", key))["x"]
|
2009-04-28 18:55:36 +00:00
|
|
|
|
|
|
|
assert_equal nil, @@db.dereference(DBRef.new("test", 4))
|
|
|
|
obj = {"_id" => 4}
|
|
|
|
@@coll.insert(obj)
|
|
|
|
assert_equal obj, @@db.dereference(DBRef.new("test", 4))
|
2009-05-12 18:21:17 +00:00
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-05-12 18:21:17 +00:00
|
|
|
@@coll.insert({"x" => "hello"})
|
|
|
|
assert_equal nil, @@db.dereference(DBRef.new("test", nil))
|
2009-04-28 18:55:36 +00:00
|
|
|
end
|
|
|
|
|
2009-05-18 13:59:10 +00:00
|
|
|
def test_save
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-05-18 13:59:10 +00:00
|
|
|
|
|
|
|
a = {"hello" => "world"}
|
|
|
|
|
2009-07-28 16:08:29 +00:00
|
|
|
id = @@coll.save(a)
|
|
|
|
assert_kind_of ObjectID, id
|
2009-05-18 13:59:10 +00:00
|
|
|
assert_equal 1, @@coll.count
|
|
|
|
|
2009-08-14 21:26:50 +00:00
|
|
|
assert_equal id, @@coll.save(a)
|
2009-05-18 13:59:10 +00:00
|
|
|
assert_equal 1, @@coll.count
|
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal "world", @@coll.find_one()["hello"]
|
2009-05-18 13:59:10 +00:00
|
|
|
|
2009-08-14 21:26:50 +00:00
|
|
|
a["hello"] = "mike"
|
|
|
|
@@coll.save(a)
|
2009-05-18 13:59:10 +00:00
|
|
|
assert_equal 1, @@coll.count
|
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal "mike", @@coll.find_one()["hello"]
|
2009-05-18 13:59:10 +00:00
|
|
|
|
2009-08-14 21:26:50 +00:00
|
|
|
@@coll.save({"hello" => "world"})
|
2009-05-18 13:59:10 +00:00
|
|
|
assert_equal 2, @@coll.count
|
|
|
|
end
|
2009-06-02 18:37:58 +00:00
|
|
|
|
2009-08-03 21:57:44 +00:00
|
|
|
def test_save_long
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-08-03 21:57:44 +00:00
|
|
|
@@coll.insert("x" => 9223372036854775807)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 9223372036854775807, @@coll.find_one()["x"]
|
2009-08-03 21:57:44 +00:00
|
|
|
end
|
|
|
|
|
2009-07-28 18:44:35 +00:00
|
|
|
def test_find_by_oid
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-07-28 18:44:35 +00:00
|
|
|
|
|
|
|
@@coll.save("hello" => "mike")
|
|
|
|
id = @@coll.save("hello" => "world")
|
|
|
|
assert_kind_of ObjectID, id
|
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal "world", @@coll.find_one(:_id => id)["hello"]
|
2009-07-28 18:44:35 +00:00
|
|
|
@@coll.find(:_id => id).to_a.each do |doc|
|
|
|
|
assert_equal "world", doc["hello"]
|
|
|
|
end
|
2009-07-28 19:00:54 +00:00
|
|
|
|
|
|
|
id = ObjectID.from_string(id.to_s)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal "world", @@coll.find_one(:_id => id)["hello"]
|
2009-07-28 18:44:35 +00:00
|
|
|
end
|
|
|
|
|
2009-06-01 02:45:05 +00:00
|
|
|
def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-06-02 18:37:58 +00:00
|
|
|
|
2009-06-01 02:45:05 +00:00
|
|
|
a = {'_id' => '1', 'hello' => 'world'}
|
2009-06-02 18:37:58 +00:00
|
|
|
@@coll.save(a)
|
|
|
|
assert_equal(1, @@coll.count)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal("world", @@coll.find_one()["hello"])
|
2009-06-02 18:37:58 +00:00
|
|
|
|
|
|
|
a["hello"] = "mike"
|
|
|
|
@@coll.save(a)
|
2009-06-01 02:45:05 +00:00
|
|
|
assert_equal(1, @@coll.count)
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal("mike", @@coll.find_one()["hello"])
|
2009-06-01 02:45:05 +00:00
|
|
|
end
|
2009-05-18 13:59:10 +00:00
|
|
|
|
2009-06-01 21:38:11 +00:00
|
|
|
def test_invalid_key_names
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-06-01 21:38:11 +00:00
|
|
|
|
|
|
|
@@coll.insert({"hello" => "world"})
|
|
|
|
@@coll.insert({"hello" => {"hello" => "world"}})
|
|
|
|
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"$hello" => "world"})
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"hello" => {"$hello" => "world"}})
|
|
|
|
end
|
|
|
|
|
|
|
|
@@coll.insert({"he$llo" => "world"})
|
|
|
|
@@coll.insert({"hello" => {"hell$o" => "world"}})
|
|
|
|
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({".hello" => "world"})
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"hello" => {".hello" => "world"}})
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"hello." => "world"})
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"hello" => {"hello." => "world"}})
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"hel.lo" => "world"})
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-06-01 21:38:11 +00:00
|
|
|
@@coll.insert({"hello" => {"hel.lo" => "world"}})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-04 18:24:18 +00:00
|
|
|
def test_collection_names
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise TypeError do
|
2009-08-04 18:24:18 +00:00
|
|
|
@@db.collection(5)
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:24:18 +00:00
|
|
|
@@db.collection("")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:24:18 +00:00
|
|
|
@@db.collection("te$t")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:24:18 +00:00
|
|
|
@@db.collection(".test")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:24:18 +00:00
|
|
|
@@db.collection("test.")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:24:18 +00:00
|
|
|
@@db.collection("tes..t")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-04 18:16:02 +00:00
|
|
|
def test_rename_collection
|
|
|
|
@@db.drop_collection("foo")
|
|
|
|
@@db.drop_collection("bar")
|
|
|
|
a = @@db.collection("foo")
|
|
|
|
b = @@db.collection("bar")
|
|
|
|
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise TypeError do
|
2009-08-04 18:16:02 +00:00
|
|
|
a.rename(5)
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:16:02 +00:00
|
|
|
a.rename("")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:16:02 +00:00
|
|
|
a.rename("te$t")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:16:02 +00:00
|
|
|
a.rename(".test")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:16:02 +00:00
|
|
|
a.rename("test.")
|
|
|
|
end
|
2009-08-13 19:52:11 +00:00
|
|
|
assert_raise InvalidName do
|
2009-08-04 18:16:02 +00:00
|
|
|
a.rename("tes..t")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 0, a.count()
|
|
|
|
assert_equal 0, b.count()
|
|
|
|
|
|
|
|
a.insert("x" => 1)
|
|
|
|
a.insert("x" => 2)
|
|
|
|
|
|
|
|
assert_equal 2, a.count()
|
|
|
|
|
|
|
|
a.rename("bar")
|
|
|
|
|
|
|
|
assert_equal 0, a.count()
|
|
|
|
assert_equal 2, b.count()
|
|
|
|
|
|
|
|
assert_equal 1, b.find().to_a()[0]["x"]
|
|
|
|
assert_equal 2, b.find().to_a()[1]["x"]
|
|
|
|
|
|
|
|
b.rename(:foo)
|
|
|
|
|
|
|
|
assert_equal 2, a.count()
|
|
|
|
assert_equal 0, b.count()
|
|
|
|
end
|
|
|
|
|
2009-08-11 15:42:34 +00:00
|
|
|
# doesn't really test functionality, just that the option is set correctly
|
|
|
|
def test_snapshot
|
|
|
|
@@db.collection("test").find({}, :snapshot => true).to_a
|
2009-11-23 18:13:14 +00:00
|
|
|
assert_raise OperationFailure do
|
2009-08-11 15:42:34 +00:00
|
|
|
@@db.collection("test").find({}, :snapshot => true, :sort => 'a').to_a
|
|
|
|
end
|
|
|
|
end
|
2009-09-04 15:02:04 +00:00
|
|
|
|
|
|
|
def test_encodings
|
|
|
|
if RUBY_VERSION >= '1.9'
|
|
|
|
ascii = "hello world"
|
|
|
|
utf8 = "hello world".encode("UTF-8")
|
|
|
|
iso8859 = "hello world".encode("ISO-8859-1")
|
|
|
|
|
|
|
|
assert_equal "US-ASCII", ascii.encoding.name
|
|
|
|
assert_equal "UTF-8", utf8.encoding.name
|
|
|
|
assert_equal "ISO-8859-1", iso8859.encoding.name
|
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-09-04 15:02:04 +00:00
|
|
|
@@coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859)
|
|
|
|
doc = @@coll.find_one()
|
|
|
|
|
|
|
|
assert_equal "UTF-8", doc["ascii"].encoding.name
|
|
|
|
assert_equal "UTF-8", doc["utf8"].encoding.name
|
|
|
|
assert_equal "UTF-8", doc["iso8859"].encoding.name
|
|
|
|
end
|
|
|
|
end
|
2008-12-02 00:39:39 +00:00
|
|
|
end
|