2010-08-04 20:47:13 +00:00
|
|
|
require './test/test_helper'
|
2009-12-01 18:49:57 +00:00
|
|
|
|
2009-08-13 19:18:53 +00:00
|
|
|
class TestCollection < Test::Unit::TestCase
|
2010-10-13 21:09:23 +00:00
|
|
|
@@connection ||= standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
@@db = @@connection.db(MONGO_TEST_DB)
|
2009-08-13 19:18:53 +00:00
|
|
|
@@test = @@db.collection("test")
|
2009-10-26 18:54:33 +00:00
|
|
|
@@version = @@connection.server_version
|
2009-08-13 19:18:53 +00:00
|
|
|
|
|
|
|
def setup
|
2010-04-06 21:56:21 +00:00
|
|
|
@@test.remove
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|
|
|
|
|
2009-10-19 21:14:41 +00:00
|
|
|
def test_optional_pk_factory
|
|
|
|
@coll_default_pk = @@db.collection('stuff')
|
2010-08-24 16:49:23 +00:00
|
|
|
assert_equal BSON::ObjectId, @coll_default_pk.pk_factory
|
2009-10-19 21:14:41 +00:00
|
|
|
@coll_default_pk = @@db.create_collection('more-stuff')
|
2010-08-24 16:49:23 +00:00
|
|
|
assert_equal BSON::ObjectId, @coll_default_pk.pk_factory
|
2009-10-19 21:14:41 +00:00
|
|
|
|
|
|
|
# Create a db with a pk_factory.
|
|
|
|
@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
2010-04-05 19:48:35 +00:00
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db(MONGO_TEST_DB, :pk => Object.new)
|
2009-10-19 21:14:41 +00:00
|
|
|
@coll = @db.collection('coll-with-pk')
|
|
|
|
assert @coll.pk_factory.is_a?(Object)
|
|
|
|
|
|
|
|
@coll = @db.create_collection('created_coll_with_pk')
|
|
|
|
assert @coll.pk_factory.is_a?(Object)
|
|
|
|
end
|
|
|
|
|
2010-11-03 22:36:29 +00:00
|
|
|
class TestPK
|
|
|
|
def self.create_pk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pk_factory_on_collection
|
2011-01-05 14:44:46 +00:00
|
|
|
@coll = Collection.new('foo', @@db, TestPK)
|
2010-11-03 22:36:29 +00:00
|
|
|
assert_equal TestPK, @coll.pk_factory
|
|
|
|
|
|
|
|
|
2011-01-05 14:44:46 +00:00
|
|
|
@coll2 = Collection.new('foo', @@db, :pk => TestPK)
|
2010-11-03 22:36:29 +00:00
|
|
|
assert_equal TestPK, @coll2.pk_factory
|
|
|
|
end
|
|
|
|
|
2009-12-04 21:35:12 +00:00
|
|
|
def test_valid_names
|
2010-04-05 22:24:31 +00:00
|
|
|
assert_raise Mongo::InvalidNSName do
|
2009-08-17 15:11:03 +00:00
|
|
|
@@db["te$t"]
|
|
|
|
end
|
|
|
|
|
2010-04-05 22:24:31 +00:00
|
|
|
assert_raise Mongo::InvalidNSName do
|
2009-12-04 21:35:12 +00:00
|
|
|
@@db['$main']
|
|
|
|
end
|
|
|
|
|
|
|
|
assert @@db['$cmd']
|
|
|
|
assert @@db['oplog.$main']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
2009-08-17 15:11:03 +00:00
|
|
|
assert_kind_of Collection, @@db["test"]
|
|
|
|
assert_equal @@db["test"].name(), @@db.collection("test").name()
|
|
|
|
assert_equal @@db["test"].name(), @@db[:test].name()
|
|
|
|
|
|
|
|
assert_kind_of Collection, @@db["test"]["foo"]
|
|
|
|
assert_equal @@db["test"]["foo"].name(), @@db.collection("test.foo").name()
|
|
|
|
assert_equal @@db["test"]["foo"].name(), @@db["test.foo"].name()
|
2009-09-14 14:03:26 +00:00
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
@@db["test"]["foo"].remove
|
2009-09-14 14:03:26 +00:00
|
|
|
@@db["test"]["foo"].insert("x" => 5)
|
|
|
|
assert_equal 5, @@db.collection("test.foo").find_one()["x"]
|
2009-08-17 15:11:03 +00:00
|
|
|
end
|
|
|
|
|
2010-11-09 18:45:33 +00:00
|
|
|
def test_rename_collection
|
|
|
|
@@db.drop_collection('foo1')
|
|
|
|
@@db.drop_collection('bar1')
|
|
|
|
|
|
|
|
@col = @@db.create_collection('foo1')
|
|
|
|
assert_equal 'foo1', @col.name
|
|
|
|
|
|
|
|
@col.rename('bar1')
|
|
|
|
assert_equal 'bar1', @col.name
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:51:32 +00:00
|
|
|
def test_nil_id
|
|
|
|
assert_equal 5, @@test.insert({"_id" => 5, "foo" => "bar"}, {:safe => true})
|
|
|
|
assert_equal 5, @@test.save({"_id" => 5, "foo" => "baz"}, {:safe => true})
|
|
|
|
assert_equal nil, @@test.find_one("foo" => "bar")
|
|
|
|
assert_equal "baz", @@test.find_one(:_id => 5)["foo"]
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.insert({"_id" => 5, "foo" => "bar"}, {:safe => true})
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal nil, @@test.insert({"_id" => nil, "foo" => "bar"}, {:safe => true})
|
|
|
|
assert_equal nil, @@test.save({"_id" => nil, "foo" => "baz"}, {:safe => true})
|
|
|
|
assert_equal nil, @@test.find_one("foo" => "bar")
|
|
|
|
assert_equal "baz", @@test.find_one(:_id => nil)["foo"]
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.insert({"_id" => nil, "foo" => "bar"}, {:safe => true})
|
|
|
|
end
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.insert({:_id => nil, "foo" => "bar"}, {:safe => true})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-27 18:05:45 +00:00
|
|
|
if @@version > "1.1"
|
2010-02-22 20:49:04 +00:00
|
|
|
def setup_for_distinct
|
|
|
|
@@test.remove
|
|
|
|
@@test.insert([{:a => 0, :b => {:c => "a"}},
|
|
|
|
{:a => 1, :b => {:c => "b"}},
|
|
|
|
{:a => 1, :b => {:c => "c"}},
|
|
|
|
{:a => 2, :b => {:c => "a"}},
|
|
|
|
{:a => 3},
|
|
|
|
{:a => 3}])
|
|
|
|
end
|
2009-12-16 16:01:35 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
def test_distinct_queries
|
|
|
|
setup_for_distinct
|
|
|
|
assert_equal [0, 1, 2, 3], @@test.distinct(:a).sort
|
|
|
|
assert_equal ["a", "b", "c"], @@test.distinct("b.c").sort
|
|
|
|
end
|
2009-12-16 16:01:35 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
if @@version >= "1.2"
|
|
|
|
def test_filter_collection_with_query
|
|
|
|
setup_for_distinct
|
|
|
|
assert_equal [2, 3], @@test.distinct(:a, {:a => {"$gt" => 1}}).sort
|
|
|
|
end
|
2009-12-14 18:57:22 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
def test_filter_nested_objects
|
|
|
|
setup_for_distinct
|
|
|
|
assert_equal ["a", "b"], @@test.distinct("b.c", {"b.c" => {"$ne" => "c"}}).sort
|
2009-12-14 18:57:22 +00:00
|
|
|
end
|
2009-10-27 18:05:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-14 18:25:29 +00:00
|
|
|
def test_safe_insert
|
2010-09-13 15:12:26 +00:00
|
|
|
@@test.create_index("hello", :unique => true)
|
2009-08-14 18:25:29 +00:00
|
|
|
a = {"hello" => "world"}
|
|
|
|
@@test.insert(a)
|
|
|
|
@@test.insert(a)
|
2010-09-13 15:12:26 +00:00
|
|
|
assert(@@db.get_last_error['err'].include?("11000"))
|
2009-08-14 18:25:29 +00:00
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.insert(a, :safe => true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-15 14:24:31 +00:00
|
|
|
def test_maximum_insert_size
|
|
|
|
docs = []
|
|
|
|
16.times do
|
|
|
|
docs << {'foo' => 'a' * 1_000_000}
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise InvalidOperation do
|
|
|
|
@@test.insert(docs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-04 20:00:05 +00:00
|
|
|
if @@version >= "1.5.1"
|
|
|
|
def test_safe_mode_with_advanced_safe_with_invalid_options
|
|
|
|
assert_raise_error ArgumentError, "Unknown key(s): wtime" do
|
|
|
|
@@test.insert({:foo => 1}, :safe => {:w => 2, :wtime => 1, :fsync => true})
|
|
|
|
end
|
|
|
|
assert_raise_error ArgumentError, "Unknown key(s): wtime" do
|
|
|
|
@@test.update({:foo => 1}, {:foo => 2}, :safe => {:w => 2, :wtime => 1, :fsync => true})
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise_error ArgumentError, "Unknown key(s): wtime" do
|
|
|
|
@@test.remove({:foo => 2}, :safe => {:w => 2, :wtime => 1, :fsync => true})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-13 19:18:53 +00:00
|
|
|
def test_update
|
|
|
|
id1 = @@test.save("x" => 5)
|
|
|
|
@@test.update({}, {"$inc" => {"x" => 1}})
|
|
|
|
assert_equal 1, @@test.count()
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 6, @@test.find_one(:_id => id1)["x"]
|
2009-08-13 19:18:53 +00:00
|
|
|
|
|
|
|
id2 = @@test.save("x" => 1)
|
|
|
|
@@test.update({"x" => 6}, {"$inc" => {"x" => 1}})
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 7, @@test.find_one(:_id => id1)["x"]
|
|
|
|
assert_equal 1, @@test.find_one(:_id => id2)["x"]
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|
|
|
|
|
2009-11-05 21:08:54 +00:00
|
|
|
if @@version >= "1.1.3"
|
|
|
|
def test_multi_update
|
|
|
|
@@test.save("num" => 10)
|
|
|
|
@@test.save("num" => 10)
|
|
|
|
@@test.save("num" => 10)
|
|
|
|
assert_equal 3, @@test.count
|
|
|
|
|
|
|
|
@@test.update({"num" => 10}, {"$set" => {"num" => 100}}, :multi => true)
|
|
|
|
@@test.find.each do |doc|
|
|
|
|
assert_equal 100, doc["num"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-13 19:18:53 +00:00
|
|
|
def test_upsert
|
|
|
|
@@test.update({"page" => "/"}, {"$inc" => {"count" => 1}}, :upsert => true)
|
|
|
|
@@test.update({"page" => "/"}, {"$inc" => {"count" => 1}}, :upsert => true)
|
|
|
|
|
|
|
|
assert_equal 1, @@test.count()
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal 2, @@test.find_one()["count"]
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|
|
|
|
|
2009-10-26 18:54:33 +00:00
|
|
|
if @@version < "1.1.3"
|
|
|
|
def test_safe_update
|
|
|
|
@@test.create_index("x")
|
|
|
|
@@test.insert("x" => 5)
|
2009-08-13 19:18:53 +00:00
|
|
|
|
2009-10-26 18:54:33 +00:00
|
|
|
@@test.update({}, {"$inc" => {"x" => 1}})
|
|
|
|
assert @@db.error?
|
2009-08-13 19:18:53 +00:00
|
|
|
|
2009-10-26 18:54:33 +00:00
|
|
|
# Can't change an index.
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.update({}, {"$inc" => {"x" => 1}}, :safe => true)
|
|
|
|
end
|
2010-04-06 21:56:21 +00:00
|
|
|
@@test.drop
|
2009-10-26 18:54:33 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
def test_safe_update
|
2010-04-28 19:16:33 +00:00
|
|
|
@@test.create_index("x", :unique => true)
|
2009-10-26 18:54:33 +00:00
|
|
|
@@test.insert("x" => 5)
|
|
|
|
@@test.insert("x" => 10)
|
|
|
|
|
|
|
|
# Can update an indexed collection.
|
|
|
|
@@test.update({}, {"$inc" => {"x" => 1}})
|
|
|
|
assert !@@db.error?
|
|
|
|
|
|
|
|
# Can't duplicate an index.
|
|
|
|
assert_raise OperationFailure do
|
2009-11-23 20:20:05 +00:00
|
|
|
@@test.update({}, {"x" => 10}, :safe => true)
|
2009-10-26 18:54:33 +00:00
|
|
|
end
|
2010-04-06 21:56:21 +00:00
|
|
|
@@test.drop
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|
|
|
|
end
|
2009-08-14 18:25:29 +00:00
|
|
|
|
|
|
|
def test_safe_save
|
2010-04-28 19:16:33 +00:00
|
|
|
@@test.create_index("hello", :unique => true)
|
2009-08-14 18:25:29 +00:00
|
|
|
|
|
|
|
@@test.save("hello" => "world")
|
|
|
|
@@test.save("hello" => "world")
|
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.save({"hello" => "world"}, :safe => true)
|
|
|
|
end
|
2010-04-06 21:56:21 +00:00
|
|
|
@@test.drop
|
2009-08-14 18:25:29 +00:00
|
|
|
end
|
2009-08-14 19:39:49 +00:00
|
|
|
|
2010-02-17 21:48:23 +00:00
|
|
|
def test_mocked_safe_remove
|
2010-10-13 21:09:23 +00:00
|
|
|
@conn = standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
@db = @conn[MONGO_TEST_DB]
|
2010-01-19 16:11:48 +00:00
|
|
|
@test = @db['test-safe-remove']
|
|
|
|
@test.save({:a => 20})
|
|
|
|
@conn.stubs(:receive).returns([[{'ok' => 0, 'err' => 'failed'}], 1, 0])
|
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@test.remove({}, :safe => true)
|
|
|
|
end
|
|
|
|
@test.drop
|
|
|
|
end
|
|
|
|
|
2010-02-17 21:48:23 +00:00
|
|
|
def test_safe_remove
|
2010-10-13 21:09:23 +00:00
|
|
|
@conn = standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
@db = @conn[MONGO_TEST_DB]
|
2010-02-17 21:48:23 +00:00
|
|
|
@test = @db['test-safe-remove']
|
|
|
|
@test.save({:a => 50})
|
2010-12-02 17:47:50 +00:00
|
|
|
assert_equal 1, @test.remove({}, :safe => true)["n"]
|
2010-02-17 21:48:23 +00:00
|
|
|
@test.drop
|
|
|
|
end
|
|
|
|
|
2010-07-28 02:19:25 +00:00
|
|
|
def test_remove_return_value
|
2010-12-02 17:47:50 +00:00
|
|
|
assert_equal true, @@test.remove({})
|
2010-07-28 02:19:25 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 14:20:16 +00:00
|
|
|
def test_count
|
|
|
|
@@test.drop
|
|
|
|
|
|
|
|
assert_equal 0, @@test.count
|
2009-08-18 15:48:16 +00:00
|
|
|
@@test.save("x" => 1)
|
|
|
|
@@test.save("x" => 2)
|
2009-08-18 14:20:16 +00:00
|
|
|
assert_equal 2, @@test.count
|
|
|
|
end
|
|
|
|
|
2009-10-13 20:20:41 +00:00
|
|
|
# Note: #size is just an alias for #count.
|
|
|
|
def test_size
|
|
|
|
@@test.drop
|
|
|
|
|
|
|
|
assert_equal 0, @@test.count
|
|
|
|
assert_equal @@test.size, @@test.count
|
|
|
|
@@test.save("x" => 1)
|
|
|
|
@@test.save("x" => 2)
|
|
|
|
assert_equal @@test.size, @@test.count
|
|
|
|
end
|
|
|
|
|
2009-10-14 18:38:44 +00:00
|
|
|
def test_no_timeout_option
|
|
|
|
@@test.drop
|
|
|
|
|
|
|
|
assert_raise ArgumentError, "Timeout can be set to false only when #find is invoked with a block." do
|
|
|
|
@@test.find({}, :timeout => false)
|
|
|
|
end
|
|
|
|
|
|
|
|
@@test.find({}, :timeout => false) do |cursor|
|
|
|
|
assert_equal 0, cursor.count
|
|
|
|
end
|
|
|
|
|
|
|
|
@@test.save("x" => 1)
|
|
|
|
@@test.save("x" => 2)
|
|
|
|
@@test.find({}, :timeout => false) do |cursor|
|
|
|
|
assert_equal 2, cursor.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-16 18:04:13 +00:00
|
|
|
def test_defualt_timeout
|
|
|
|
cursor = @@test.find
|
|
|
|
assert_equal true, cursor.timeout
|
|
|
|
end
|
|
|
|
|
2010-03-27 15:58:16 +00:00
|
|
|
def test_fields_as_hash
|
|
|
|
@@test.save(:a => 1, :b => 1, :c => 1)
|
|
|
|
|
|
|
|
doc = @@test.find_one({:a => 1}, :fields => {:b => 0})
|
|
|
|
assert_nil doc['b']
|
|
|
|
assert doc['a']
|
|
|
|
assert doc['c']
|
|
|
|
|
|
|
|
doc = @@test.find_one({:a => 1}, :fields => {:a => 1, :b => 1})
|
|
|
|
assert_nil doc['c']
|
|
|
|
assert doc['a']
|
|
|
|
assert doc['b']
|
|
|
|
|
|
|
|
|
|
|
|
assert_raise Mongo::OperationFailure do
|
|
|
|
@@test.find_one({:a => 1}, :fields => {:a => 1, :b => 0})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-04 20:00:05 +00:00
|
|
|
if @@version >= "1.5.1"
|
2010-05-03 21:40:55 +00:00
|
|
|
def test_fields_with_slice
|
|
|
|
@@test.save({:foo => [1, 2, 3, 4, 5, 6], :test => 'slice'})
|
|
|
|
|
|
|
|
doc = @@test.find_one({:test => 'slice'}, :fields => {'foo' => {'$slice' => [0, 3]}})
|
|
|
|
assert_equal [1, 2, 3], doc['foo']
|
|
|
|
@@test.remove
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
def test_find_one
|
2009-08-14 20:43:12 +00:00
|
|
|
id = @@test.save("hello" => "world", "foo" => "bar")
|
2009-08-14 19:39:49 +00:00
|
|
|
|
|
|
|
assert_equal "world", @@test.find_one()["hello"]
|
|
|
|
assert_equal @@test.find_one(id), @@test.find_one()
|
|
|
|
assert_equal @@test.find_one(nil), @@test.find_one()
|
|
|
|
assert_equal @@test.find_one({}), @@test.find_one()
|
|
|
|
assert_equal @@test.find_one("hello" => "world"), @@test.find_one()
|
2010-05-07 01:25:18 +00:00
|
|
|
assert_equal @@test.find_one(BSON::OrderedHash["hello", "world"]), @@test.find_one()
|
2009-08-14 19:39:49 +00:00
|
|
|
|
2009-08-14 20:43:12 +00:00
|
|
|
assert @@test.find_one(nil, :fields => ["hello"]).include?("hello")
|
|
|
|
assert !@@test.find_one(nil, :fields => ["foo"]).include?("hello")
|
2009-08-19 13:57:15 +00:00
|
|
|
assert_equal ["_id"], @@test.find_one(nil, :fields => []).keys()
|
2009-08-14 20:43:12 +00:00
|
|
|
|
2009-08-14 19:39:49 +00:00
|
|
|
assert_equal nil, @@test.find_one("hello" => "foo")
|
2010-05-07 01:25:18 +00:00
|
|
|
assert_equal nil, @@test.find_one(BSON::OrderedHash["hello", "foo"])
|
2010-08-24 16:49:23 +00:00
|
|
|
assert_equal nil, @@test.find_one(ObjectId.new)
|
2009-08-14 19:39:49 +00:00
|
|
|
|
|
|
|
assert_raise TypeError do
|
|
|
|
@@test.find_one(6)
|
|
|
|
end
|
|
|
|
end
|
2009-08-14 21:26:50 +00:00
|
|
|
|
|
|
|
def test_insert_adds_id
|
|
|
|
doc = {"hello" => "world"}
|
|
|
|
@@test.insert(doc)
|
2009-09-04 21:30:28 +00:00
|
|
|
assert(doc.include?(:_id))
|
2009-08-14 21:26:50 +00:00
|
|
|
|
|
|
|
docs = [{"hello" => "world"}, {"hello" => "world"}]
|
|
|
|
@@test.insert(docs)
|
|
|
|
docs.each do |doc|
|
2009-09-04 21:30:28 +00:00
|
|
|
assert(doc.include?(:_id))
|
2009-08-14 21:26:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_save_adds_id
|
|
|
|
doc = {"hello" => "world"}
|
|
|
|
@@test.save(doc)
|
2009-09-04 21:30:28 +00:00
|
|
|
assert(doc.include?(:_id))
|
2009-08-14 21:26:50 +00:00
|
|
|
end
|
2009-08-19 15:51:30 +00:00
|
|
|
|
|
|
|
def test_optional_find_block
|
|
|
|
10.times do |i|
|
|
|
|
@@test.save("i" => i)
|
|
|
|
end
|
|
|
|
|
|
|
|
x = nil
|
|
|
|
@@test.find("i" => 2) { |cursor|
|
|
|
|
x = cursor.count()
|
|
|
|
}
|
|
|
|
assert_equal 1, x
|
|
|
|
|
|
|
|
i = 0
|
2009-09-17 20:45:03 +00:00
|
|
|
@@test.find({}, :skip => 5) do |cursor|
|
2009-08-19 15:51:30 +00:00
|
|
|
cursor.each do |doc|
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 5, i
|
|
|
|
|
|
|
|
c = nil
|
|
|
|
@@test.find() do |cursor|
|
|
|
|
c = cursor
|
|
|
|
end
|
|
|
|
assert c.closed?
|
|
|
|
end
|
2009-08-26 14:57:14 +00:00
|
|
|
|
2010-07-09 19:29:11 +00:00
|
|
|
if @@version > "1.1.1"
|
2009-12-16 16:01:35 +00:00
|
|
|
def test_map_reduce
|
|
|
|
@@test << { "user_id" => 1 }
|
|
|
|
@@test << { "user_id" => 2 }
|
2009-11-23 08:04:48 +00:00
|
|
|
|
2009-12-16 16:01:35 +00:00
|
|
|
m = "function() { emit(this.user_id, 1); }"
|
|
|
|
r = "function(k,vals) { return 1; }"
|
2010-12-30 20:43:35 +00:00
|
|
|
res = @@test.map_reduce(m, r, :out => 'foo');
|
2009-12-16 16:01:35 +00:00
|
|
|
assert res.find_one({"_id" => 1})
|
|
|
|
assert res.find_one({"_id" => 2})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_reduce_with_code_objects
|
|
|
|
@@test << { "user_id" => 1 }
|
|
|
|
@@test << { "user_id" => 2 }
|
2009-11-25 16:25:50 +00:00
|
|
|
|
2009-12-16 16:01:35 +00:00
|
|
|
m = Code.new("function() { emit(this.user_id, 1); }")
|
|
|
|
r = Code.new("function(k,vals) { return 1; }")
|
2010-12-30 20:43:35 +00:00
|
|
|
res = @@test.map_reduce(m, r, :out => 'foo');
|
2009-12-16 16:01:35 +00:00
|
|
|
assert res.find_one({"_id" => 1})
|
|
|
|
assert res.find_one({"_id" => 2})
|
|
|
|
end
|
2009-11-23 08:04:48 +00:00
|
|
|
|
2009-12-16 16:01:35 +00:00
|
|
|
def test_map_reduce_with_options
|
|
|
|
@@test.remove
|
|
|
|
@@test << { "user_id" => 1 }
|
|
|
|
@@test << { "user_id" => 2 }
|
|
|
|
@@test << { "user_id" => 3 }
|
|
|
|
|
|
|
|
m = Code.new("function() { emit(this.user_id, 1); }")
|
|
|
|
r = Code.new("function(k,vals) { return 1; }")
|
2010-12-30 20:43:35 +00:00
|
|
|
res = @@test.map_reduce(m, r, :query => {"user_id" => {"$gt" => 1}}, :out => 'foo');
|
2009-12-16 16:01:35 +00:00
|
|
|
assert_equal 2, res.count
|
|
|
|
assert res.find_one({"_id" => 2})
|
|
|
|
assert res.find_one({"_id" => 3})
|
|
|
|
end
|
2010-07-09 19:29:11 +00:00
|
|
|
|
|
|
|
def test_map_reduce_with_raw_response
|
|
|
|
m = Code.new("function() { emit(this.user_id, 1); }")
|
|
|
|
r = Code.new("function(k,vals) { return 1; }")
|
2010-12-30 20:43:35 +00:00
|
|
|
res = @@test.map_reduce(m, r, :raw => true, :out => 'foo')
|
2010-07-09 19:29:11 +00:00
|
|
|
assert res["result"]
|
|
|
|
assert res["counts"]
|
|
|
|
assert res["timeMillis"]
|
|
|
|
end
|
|
|
|
|
2010-07-13 18:49:59 +00:00
|
|
|
def test_map_reduce_with_output_collection
|
|
|
|
output_collection = "test-map-coll"
|
|
|
|
m = Code.new("function() { emit(this.user_id, 1); }")
|
|
|
|
r = Code.new("function(k,vals) { return 1; }")
|
|
|
|
res = @@test.map_reduce(m, r, :raw => true, :out => output_collection)
|
|
|
|
assert_equal output_collection, res["result"]
|
|
|
|
assert res["counts"]
|
|
|
|
assert res["timeMillis"]
|
|
|
|
end
|
2011-02-22 22:47:47 +00:00
|
|
|
|
|
|
|
def test_map_reduce_with_collection_merge
|
|
|
|
@@test << {:user_id => 1}
|
|
|
|
@@test << {:user_id => 2}
|
|
|
|
output_collection = "test-map-coll"
|
|
|
|
m = Code.new("function() { emit(this.user_id, {count: 1}); }")
|
|
|
|
r = Code.new("function(k,vals) { var sum = 0;" +
|
|
|
|
" vals.forEach(function(v) { sum += v.count;} ); return {count: sum}; }")
|
|
|
|
res = @@test.map_reduce(m, r, :out => output_collection)
|
|
|
|
|
|
|
|
@@test.remove
|
|
|
|
@@test << {:user_id => 3}
|
|
|
|
res = @@test.map_reduce(m, r, :out => {:merge => output_collection})
|
|
|
|
assert res.find.to_a.any? {|doc| doc["_id"] == 3 && doc["value"]["count"] == 1}
|
|
|
|
|
|
|
|
@@test.remove
|
|
|
|
@@test << {:user_id => 3}
|
|
|
|
res = @@test.map_reduce(m, r, :out => {:reduce => output_collection})
|
|
|
|
assert res.find.to_a.any? {|doc| doc["_id"] == 3 && doc["value"]["count"] == 2}
|
|
|
|
|
2011-02-23 19:43:23 +00:00
|
|
|
assert_raise ArgumentError do
|
|
|
|
@@test.map_reduce(m, r, :out => {:inline => 1})
|
|
|
|
end
|
|
|
|
|
|
|
|
@@test.map_reduce(m, r, :raw => true, :out => {:inline => 1})
|
2011-02-22 22:47:47 +00:00
|
|
|
assert res["results"]
|
|
|
|
end
|
2009-11-23 08:04:48 +00:00
|
|
|
end
|
|
|
|
|
2010-04-06 21:56:21 +00:00
|
|
|
if @@version > "1.3.0"
|
|
|
|
def test_find_and_modify
|
|
|
|
@@test << { :a => 1, :processed => false }
|
|
|
|
@@test << { :a => 2, :processed => false }
|
|
|
|
@@test << { :a => 3, :processed => false }
|
|
|
|
|
|
|
|
@@test.find_and_modify(:query => {}, :sort => [['a', -1]], :update => {"$set" => {:processed => true}})
|
|
|
|
|
|
|
|
assert @@test.find_one({:a => 3})['processed']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_and_modify_with_invalid_options
|
|
|
|
@@test << { :a => 1, :processed => false }
|
|
|
|
@@test << { :a => 2, :processed => false }
|
|
|
|
@@test << { :a => 3, :processed => false }
|
|
|
|
|
|
|
|
assert_raise Mongo::OperationFailure do
|
|
|
|
@@test.find_and_modify(:blimey => {})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-06 22:29:39 +00:00
|
|
|
if @@version >= "1.3.5"
|
|
|
|
def test_coll_stats
|
|
|
|
@@test << {:n => 1}
|
|
|
|
@@test.create_index("n")
|
|
|
|
|
|
|
|
assert_equal "#{MONGO_TEST_DB}.test", @@test.stats['ns']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-16 16:53:46 +00:00
|
|
|
def test_saving_dates_pre_epoch
|
2009-09-16 20:28:52 +00:00
|
|
|
begin
|
|
|
|
@@test.save({'date' => Time.utc(1600)})
|
2010-09-30 13:43:17 +00:00
|
|
|
assert_in_delta Time.utc(1600), @@test.find_one()["date"], 2
|
2009-09-16 20:28:52 +00:00
|
|
|
rescue ArgumentError
|
|
|
|
# See note in test_date_before_epoch (BSONTest)
|
|
|
|
end
|
2009-09-16 16:53:46 +00:00
|
|
|
end
|
|
|
|
|
2009-08-26 14:57:14 +00:00
|
|
|
def test_save_symbol_find_string
|
|
|
|
@@test.save(:foo => :mike)
|
|
|
|
|
|
|
|
assert_equal :mike, @@test.find_one(:foo => :mike)["foo"]
|
|
|
|
assert_equal :mike, @@test.find_one("foo" => :mike)["foo"]
|
2009-08-27 20:29:41 +00:00
|
|
|
|
|
|
|
# TODO enable these tests conditionally based on server version (if >1.0)
|
|
|
|
# assert_equal :mike, @@test.find_one(:foo => "mike")["foo"]
|
|
|
|
# assert_equal :mike, @@test.find_one("foo" => "mike")["foo"]
|
2009-08-26 14:57:14 +00:00
|
|
|
end
|
2009-08-26 15:13:40 +00:00
|
|
|
|
2009-09-17 20:45:03 +00:00
|
|
|
def test_limit_and_skip
|
|
|
|
10.times do |i|
|
|
|
|
@@test.save(:foo => i)
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
assert_equal 5, @@test.find({}, :skip => 5).next_document()["foo"]
|
|
|
|
assert_equal nil, @@test.find({}, :skip => 10).next_document()
|
2009-09-17 20:45:03 +00:00
|
|
|
|
|
|
|
assert_equal 5, @@test.find({}, :limit => 5).to_a.length
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
assert_equal 3, @@test.find({}, :skip => 3, :limit => 5).next_document()["foo"]
|
2009-09-17 20:45:03 +00:00
|
|
|
assert_equal 5, @@test.find({}, :skip => 3, :limit => 5).to_a.length
|
|
|
|
end
|
|
|
|
|
2009-09-30 14:49:08 +00:00
|
|
|
def test_large_limit
|
|
|
|
2000.times do |i|
|
|
|
|
@@test.insert("x" => i, "y" => "mongomongo" * 1000)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 2000, @@test.count
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
y = 0
|
|
|
|
@@test.find({}, :limit => 1900).each do |doc|
|
|
|
|
i += 1
|
|
|
|
y += doc["x"]
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1900, i
|
|
|
|
assert_equal 1804050, y
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_small_limit
|
|
|
|
@@test.insert("x" => "hello world")
|
|
|
|
@@test.insert("x" => "goodbye world")
|
|
|
|
|
|
|
|
assert_equal 2, @@test.count
|
|
|
|
|
|
|
|
x = 0
|
|
|
|
@@test.find({}, :limit => 1).each do |doc|
|
|
|
|
x += 1
|
|
|
|
assert_equal "hello world", doc["x"]
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1, x
|
|
|
|
end
|
2010-11-11 22:41:31 +00:00
|
|
|
|
2011-03-18 19:03:37 +00:00
|
|
|
def test_find_with_transformer
|
|
|
|
klass = Struct.new(:id, :a)
|
|
|
|
transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
|
|
|
|
cursor = @@test.find({}, :transformer => transformer)
|
|
|
|
assert_equal(transformer, cursor.transformer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_one_with_transformer
|
|
|
|
klass = Struct.new(:id, :a)
|
|
|
|
transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
|
|
|
|
id = @@test.insert('a' => 1)
|
|
|
|
doc = @@test.find_one(id, :transformer => transformer)
|
|
|
|
assert_instance_of(klass, doc)
|
|
|
|
end
|
2010-11-11 22:41:31 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
def test_ensure_index
|
|
|
|
@@test.drop_indexes
|
|
|
|
@@test.insert("x" => "hello world")
|
|
|
|
assert_equal 1, @@test.index_information.keys.count #default index
|
2010-11-11 22:41:31 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
@@test.ensure_index([["x", Mongo::DESCENDING]], {})
|
|
|
|
assert_equal 2, @@test.index_information.keys.count
|
|
|
|
assert @@test.index_information.keys.include? "x_-1"
|
2010-11-11 22:41:31 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
@@test.ensure_index([["x", Mongo::ASCENDING]])
|
|
|
|
assert @@test.index_information.keys.include? "x_1"
|
2010-11-11 22:41:31 +00:00
|
|
|
|
2010-12-15 20:16:36 +00:00
|
|
|
@@test.ensure_index([["type", 1], ["date", -1]])
|
|
|
|
assert @@test.index_information.keys.include? "type_1_date_-1"
|
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
@@test.drop_index("x_1")
|
2010-12-15 20:16:36 +00:00
|
|
|
assert_equal 3, @@test.index_information.keys.count
|
2010-11-10 02:28:07 +00:00
|
|
|
@@test.drop_index("x_-1")
|
|
|
|
assert_equal 2, @@test.index_information.keys.count
|
2010-12-15 20:16:36 +00:00
|
|
|
|
|
|
|
@@test.ensure_index([["x", Mongo::DESCENDING]], {})
|
|
|
|
assert_equal 3, @@test.index_information.keys.count
|
2010-11-10 02:28:07 +00:00
|
|
|
assert @@test.index_information.keys.include? "x_-1"
|
2010-11-11 22:41:31 +00:00
|
|
|
|
|
|
|
# Make sure that drop_index expires cache properly
|
|
|
|
@@test.ensure_index([['a', 1]])
|
|
|
|
assert @@test.index_information.keys.include?("a_1")
|
|
|
|
@@test.drop_index("a_1")
|
|
|
|
assert !@@test.index_information.keys.include?("a_1")
|
|
|
|
@@test.ensure_index([['a', 1]])
|
|
|
|
assert @@test.index_information.keys.include?("a_1")
|
|
|
|
@@test.drop_index("a_1")
|
2010-11-10 02:28:07 +00:00
|
|
|
end
|
2009-09-30 14:49:08 +00:00
|
|
|
|
2011-01-03 21:16:24 +00:00
|
|
|
def test_ensure_index_timeout
|
|
|
|
@@db.cache_time = 2
|
|
|
|
coll = @@db['ensure_test']
|
|
|
|
coll.expects(:generate_indexes).twice
|
|
|
|
coll.ensure_index([['a', 1]])
|
|
|
|
|
|
|
|
# These will be cached
|
|
|
|
coll.ensure_index([['a', 1]])
|
|
|
|
coll.ensure_index([['a', 1]])
|
|
|
|
coll.ensure_index([['a', 1]])
|
|
|
|
coll.ensure_index([['a', 1]])
|
|
|
|
|
|
|
|
sleep(3)
|
|
|
|
# This won't be, so generate_indexes will be called twice
|
|
|
|
coll.ensure_index([['a', 1]])
|
|
|
|
end
|
|
|
|
|
2009-12-18 19:48:44 +00:00
|
|
|
context "Grouping" do
|
2010-11-09 17:04:47 +00:00
|
|
|
setup do
|
2009-12-18 19:48:44 +00:00
|
|
|
@@test.remove
|
|
|
|
@@test.save("a" => 1)
|
|
|
|
@@test.save("b" => 1)
|
|
|
|
@initial = {"count" => 0}
|
|
|
|
@reduce_function = "function (obj, prev) { prev.count += inc_value; }"
|
|
|
|
end
|
2009-12-06 23:45:42 +00:00
|
|
|
|
2011-01-06 15:05:19 +00:00
|
|
|
should "fail if missing required options" do
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
@@test.group(:initial => {})
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
@@test.group(:reduce => "foo")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-18 19:48:44 +00:00
|
|
|
should "group results using eval form" do
|
2011-01-06 15:05:19 +00:00
|
|
|
assert_equal 1, @@test.group(:initial => @initial, :reduce => Code.new(@reduce_function, {"inc_value" => 0.5}))[0]["count"]
|
|
|
|
assert_equal 2, @@test.group(:initial => @initial, :reduce => Code.new(@reduce_function, {"inc_value" => 1}))[0]["count"]
|
|
|
|
assert_equal 4, @@test.group(:initial => @initial, :reduce => Code.new(@reduce_function, {"inc_value" => 2}))[0]["count"]
|
2009-10-20 15:31:07 +00:00
|
|
|
end
|
|
|
|
|
2009-12-18 19:48:44 +00:00
|
|
|
should "finalize grouped results" do
|
|
|
|
@finalize = "function(doc) {doc.f = doc.count + 200; }"
|
2011-01-06 15:05:19 +00:00
|
|
|
assert_equal 202, @@test.group(:initial => @initial, :reduce => Code.new(@reduce_function, {"inc_value" => 1}), :finalize => @finalize)[0]["f"]
|
2009-12-18 19:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-09 17:04:47 +00:00
|
|
|
context "Grouping with key" do
|
|
|
|
setup do
|
|
|
|
@@test.remove
|
|
|
|
@@test.save("a" => 1, "pop" => 100)
|
|
|
|
@@test.save("a" => 1, "pop" => 100)
|
|
|
|
@@test.save("a" => 2, "pop" => 100)
|
|
|
|
@@test.save("a" => 2, "pop" => 100)
|
|
|
|
@initial = {"count" => 0, "foo" => 1}
|
|
|
|
@reduce_function = "function (obj, prev) { prev.count += obj.pop; }"
|
|
|
|
end
|
|
|
|
|
|
|
|
should "group" do
|
2011-01-06 15:05:19 +00:00
|
|
|
result = @@test.group(:key => :a, :initial => @initial, :reduce => @reduce_function)
|
2010-11-09 17:04:47 +00:00
|
|
|
assert result.all? { |r| r['count'] == 200 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-18 19:48:44 +00:00
|
|
|
context "Grouping with a key function" do
|
|
|
|
setup do
|
|
|
|
@@test.remove
|
|
|
|
@@test.save("a" => 1)
|
|
|
|
@@test.save("a" => 2)
|
|
|
|
@@test.save("a" => 3)
|
|
|
|
@@test.save("a" => 4)
|
|
|
|
@@test.save("a" => 5)
|
|
|
|
@initial = {"count" => 0}
|
|
|
|
@keyf = "function (doc) { if(doc.a % 2 == 0) { return {even: true}; } else {return {odd: true}} };"
|
|
|
|
@reduce = "function (obj, prev) { prev.count += 1; }"
|
|
|
|
end
|
|
|
|
|
|
|
|
should "group results" do
|
2011-01-06 15:05:19 +00:00
|
|
|
results = @@test.group(:keyf => @keyf, :initial => @initial, :reduce => @reduce).sort {|a, b| a['count'] <=> b['count']}
|
2009-12-18 19:48:44 +00:00
|
|
|
assert results[0]['even'] && results[0]['count'] == 2.0
|
|
|
|
assert results[1]['odd'] && results[1]['count'] == 3.0
|
|
|
|
end
|
2011-01-06 15:05:19 +00:00
|
|
|
|
|
|
|
should "group filtered results" do
|
|
|
|
results = @@test.group(:keyf => @keyf, :cond => {:a => {'$ne' => 2}},
|
|
|
|
:initial => @initial, :reduce => @reduce).sort {|a, b| a['count'] <=> b['count']}
|
|
|
|
assert results[0]['even'] && results[0]['count'] == 1.0
|
|
|
|
assert results[1]['odd'] && results[1]['count'] == 3.0
|
|
|
|
end
|
2009-12-18 19:48:44 +00:00
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
context "A collection with two records" do
|
|
|
|
setup do
|
2009-10-20 15:31:07 +00:00
|
|
|
@collection = @@db.collection('test-collection')
|
|
|
|
@collection.insert({:name => "Jones"})
|
|
|
|
@collection.insert({:name => "Smith"})
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
should "have two records" do
|
2009-10-20 15:31:07 +00:00
|
|
|
assert_equal 2, @collection.size
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
should "remove the two records" do
|
2009-10-20 15:31:07 +00:00
|
|
|
@collection.remove()
|
|
|
|
assert_equal 0, @collection.size
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
should "remove all records if an empty document is specified" do
|
2009-10-20 15:31:07 +00:00
|
|
|
@collection.remove({})
|
|
|
|
assert_equal 0, @collection.find.count
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
should "remove only matching records" do
|
2009-10-20 15:31:07 +00:00
|
|
|
@collection.remove({:name => "Jones"})
|
|
|
|
assert_equal 1, @collection.size
|
|
|
|
end
|
2009-08-26 15:13:40 +00:00
|
|
|
end
|
2009-11-17 18:20:57 +00:00
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
context "Creating indexes " do
|
|
|
|
setup do
|
2010-03-16 19:59:26 +00:00
|
|
|
@@db.drop_collection('geo')
|
|
|
|
@@db.drop_collection('test-collection')
|
2009-11-17 18:20:57 +00:00
|
|
|
@collection = @@db.collection('test-collection')
|
2010-03-16 19:59:26 +00:00
|
|
|
@geo = @@db.collection('geo')
|
|
|
|
end
|
|
|
|
|
2011-01-03 21:16:24 +00:00
|
|
|
should "create index using symbols" do
|
|
|
|
@collection.create_index :foo, :name => :bar
|
|
|
|
@geo.create_index :goo, :name => :baz
|
|
|
|
assert @collection.index_information['bar']
|
|
|
|
@collection.drop_index :bar
|
|
|
|
assert_nil @collection.index_information['bar']
|
|
|
|
assert @geo.index_information['baz']
|
|
|
|
@geo.drop_index(:baz)
|
|
|
|
assert_nil @geo.index_information['baz']
|
|
|
|
end
|
|
|
|
|
2010-03-16 19:59:26 +00:00
|
|
|
should "create a geospatial index" do
|
|
|
|
@geo.save({'loc' => [-100, 100]})
|
|
|
|
@geo.create_index([['loc', Mongo::GEO2D]])
|
|
|
|
assert @geo.index_information['loc_2d']
|
|
|
|
end
|
|
|
|
|
|
|
|
should "create a unique index" do
|
2010-04-28 19:16:33 +00:00
|
|
|
@collection.create_index([['a', Mongo::ASCENDING]], :unique => true)
|
2010-03-16 19:59:26 +00:00
|
|
|
assert @collection.index_information['a_1']['unique'] == true
|
|
|
|
end
|
|
|
|
|
2010-11-02 18:50:02 +00:00
|
|
|
should "drop duplicates" do
|
|
|
|
@collection.insert({:a => 1})
|
|
|
|
@collection.insert({:a => 1})
|
|
|
|
assert_equal 2, @collection.find({:a => 1}).count
|
|
|
|
@collection.create_index([['a', Mongo::ASCENDING]], :unique => true, :dropDups => true)
|
|
|
|
assert_equal 1, @collection.find({:a => 1}).count
|
|
|
|
end
|
|
|
|
|
|
|
|
should "drop duplicates with ruby-like drop_dups key" do
|
|
|
|
@collection.insert({:a => 1})
|
|
|
|
@collection.insert({:a => 1})
|
|
|
|
assert_equal 2, @collection.find({:a => 1}).count
|
|
|
|
@collection.create_index([['a', Mongo::ASCENDING]], :unique => true, :drop_dups => true)
|
|
|
|
assert_equal 1, @collection.find({:a => 1}).count
|
|
|
|
end
|
|
|
|
|
2010-03-16 19:59:26 +00:00
|
|
|
should "create an index in the background" do
|
|
|
|
if @@version > '1.3.1'
|
|
|
|
@collection.create_index([['b', Mongo::ASCENDING]], :background => true)
|
|
|
|
assert @collection.index_information['b_1']['background'] == true
|
|
|
|
else
|
|
|
|
assert true
|
|
|
|
end
|
2009-11-17 18:20:57 +00:00
|
|
|
end
|
|
|
|
|
2010-03-19 18:31:10 +00:00
|
|
|
should "require an array of arrays" do
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
@collection.create_index(['c', Mongo::ASCENDING])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-16 19:59:34 +00:00
|
|
|
should "enforce proper index types" do
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
@collection.create_index([['c', 'blah']])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-14 18:20:12 +00:00
|
|
|
should "raise an error if index name is greater than 128" do
|
|
|
|
assert_raise Mongo::OperationFailure do
|
|
|
|
@collection.create_index([['a' * 25, 1], ['b' * 25, 1],
|
|
|
|
['c' * 25, 1], ['d' * 25, 1], ['e' * 25, 1]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "allow for an alternate name to be specified" do
|
|
|
|
@collection.create_index([['a' * 25, 1], ['b' * 25, 1],
|
|
|
|
['c' * 25, 1], ['d' * 25, 1], ['e' * 25, 1]], :name => 'foo_index')
|
|
|
|
assert @collection.index_information['foo_index']
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
should "generate indexes in the proper order" do
|
2009-11-17 18:20:57 +00:00
|
|
|
@collection.expects(:insert_documents) do |sel, coll, safe|
|
|
|
|
assert_equal 'b_1_a_1', sel[:name]
|
|
|
|
end
|
|
|
|
@collection.create_index([['b', 1], ['a', 1]])
|
|
|
|
end
|
|
|
|
|
2010-11-09 18:34:28 +00:00
|
|
|
should "allow multiple calls to create_index" do
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
should "allow creation of multiple indexes" do
|
|
|
|
assert @collection.create_index([['a', 1]])
|
|
|
|
assert @collection.create_index([['a', 1]])
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
context "with an index created" do
|
|
|
|
setup do
|
2009-11-17 18:20:57 +00:00
|
|
|
@collection.create_index([['b', 1], ['a', 1]])
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:52:07 +00:00
|
|
|
should "return properly ordered index information" do
|
2010-03-16 19:59:26 +00:00
|
|
|
assert @collection.index_information['b_1_a_1']
|
2009-11-17 18:20:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-12 17:53:18 +00:00
|
|
|
|
|
|
|
context "Capped collections" do
|
|
|
|
setup do
|
|
|
|
@@db.drop_collection('log')
|
|
|
|
@capped = @@db.create_collection('log', :capped => true, :size => 1024)
|
|
|
|
|
|
|
|
10.times { |n| @capped.insert({:n => n}) }
|
|
|
|
end
|
|
|
|
|
|
|
|
should "find using a standard cursor" do
|
|
|
|
cursor = @capped.find
|
|
|
|
10.times do
|
|
|
|
assert cursor.next_document
|
|
|
|
end
|
|
|
|
assert_nil cursor.next_document
|
|
|
|
@capped.insert({:n => 100})
|
|
|
|
assert_nil cursor.next_document
|
|
|
|
end
|
|
|
|
|
2010-06-25 11:10:17 +00:00
|
|
|
should "fail tailable cursor on a non-capped collection" do
|
2010-04-12 17:53:18 +00:00
|
|
|
col = @@db['regular-collection']
|
|
|
|
col.insert({:a => 1000})
|
|
|
|
tail = Cursor.new(col, :tailable => true, :order => [['$natural', 1]])
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
tail.next_document
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "find using a tailable cursor" do
|
|
|
|
tail = Cursor.new(@capped, :tailable => true, :order => [['$natural', 1]])
|
|
|
|
10.times do
|
|
|
|
assert tail.next_document
|
|
|
|
end
|
|
|
|
assert_nil tail.next_document
|
|
|
|
@capped.insert({:n => 100})
|
|
|
|
assert tail.next_document
|
|
|
|
end
|
|
|
|
end
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|