2009-08-13 19:18:53 +00:00
|
|
|
# --
|
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ++
|
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
require 'test/test_helper'
|
2009-08-13 19:18:53 +00:00
|
|
|
class TestCollection < Test::Unit::TestCase
|
2009-10-26 18:54:33 +00:00
|
|
|
@@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
|
|
|
@@db = @@connection.db('ruby-mongo-test')
|
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
|
|
|
|
@@test.drop()
|
|
|
|
end
|
|
|
|
|
2009-10-19 21:14:41 +00:00
|
|
|
def test_optional_pk_factory
|
|
|
|
@coll_default_pk = @@db.collection('stuff')
|
|
|
|
assert_equal Mongo::ObjectID, @coll_default_pk.pk_factory
|
|
|
|
@coll_default_pk = @@db.create_collection('more-stuff')
|
|
|
|
assert_equal Mongo::ObjectID, @coll_default_pk.pk_factory
|
|
|
|
|
|
|
|
# Create a db with a pk_factory.
|
|
|
|
@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test', :pk => Object.new)
|
|
|
|
@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
|
|
|
|
|
2009-08-17 15:11:03 +00:00
|
|
|
def test_collection
|
|
|
|
assert_raise InvalidName do
|
|
|
|
@@db["te$t"]
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-10-27 18:05:45 +00:00
|
|
|
if @@version > "1.1"
|
|
|
|
def test_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}])
|
|
|
|
|
|
|
|
assert_equal [0, 1, 2, 3], @@test.distinct(:a).sort
|
|
|
|
assert_equal ["a", "b", "c"], @@test.distinct("b.c").sort
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-14 18:25:29 +00:00
|
|
|
def test_safe_insert
|
|
|
|
a = {"hello" => "world"}
|
|
|
|
@@test.insert(a)
|
|
|
|
@@test.insert(a)
|
2009-09-04 21:30:28 +00:00
|
|
|
assert(@@db.error.include?("E11000"))
|
2009-08-14 18:25:29 +00:00
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.insert(a, :safe => true)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def test_safe_update
|
|
|
|
@@test.create_index("x", true)
|
|
|
|
@@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
|
|
|
|
@@test.update({}, {"x" => 10}, :safe => true, :upsert => true)
|
|
|
|
end
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|
|
|
|
end
|
2009-08-14 18:25:29 +00:00
|
|
|
|
|
|
|
def test_safe_save
|
|
|
|
@@test.create_index("hello", true)
|
|
|
|
|
|
|
|
@@test.save("hello" => "world")
|
|
|
|
@@test.save("hello" => "world")
|
2009-09-04 21:30:28 +00:00
|
|
|
assert(@@db.error.include?("E11000"))
|
2009-08-14 18:25:29 +00:00
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.save({"hello" => "world"}, :safe => true)
|
|
|
|
end
|
|
|
|
end
|
2009-08-14 19:39:49 +00:00
|
|
|
|
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
|
|
|
|
|
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()
|
|
|
|
assert_equal @@test.find_one(OrderedHash["hello", "world"]), @@test.find_one()
|
|
|
|
|
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")
|
|
|
|
assert_equal nil, @@test.find_one(OrderedHash["hello", "foo"])
|
|
|
|
assert_equal nil, @@test.find_one(ObjectID.new)
|
|
|
|
|
|
|
|
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
|
|
|
|
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)})
|
|
|
|
assert_in_delta Time.utc(1600), @@test.find_one()["date"], 0.001
|
|
|
|
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
|
|
|
|
|
|
|
|
# TODO remove test for deprecated :offset option
|
|
|
|
assert_equal 5, @@test.find({}, :offset => 5).next_object()["foo"]
|
|
|
|
|
|
|
|
assert_equal 5, @@test.find({}, :skip => 5).next_object()["foo"]
|
|
|
|
assert_equal nil, @@test.find({}, :skip => 10).next_object()
|
|
|
|
|
|
|
|
assert_equal 5, @@test.find({}, :limit => 5).to_a.length
|
|
|
|
|
|
|
|
assert_equal 3, @@test.find({}, :skip => 3, :limit => 5).next_object()["foo"]
|
|
|
|
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
|
|
|
|
|
2009-08-26 15:13:40 +00:00
|
|
|
def test_group_with_scope
|
|
|
|
@@test.save("a" => 1)
|
|
|
|
@@test.save("b" => 1)
|
|
|
|
|
|
|
|
reduce_function = "function (obj, prev) { prev.count += inc_value; }"
|
|
|
|
|
|
|
|
assert_equal 2, @@test.group([], {}, {"count" => 0},
|
|
|
|
Code.new(reduce_function,
|
|
|
|
{"inc_value" => 1}))[0]["count"]
|
|
|
|
|
|
|
|
# TODO enable these tests when SERVER-262 is fixed
|
|
|
|
|
|
|
|
# assert_equal 2, @@test.group([], {}, {"count" => 0},
|
|
|
|
# Code.new(reduce_function,
|
|
|
|
# {"inc_value" => 1}), true)[0]["count"]
|
|
|
|
|
|
|
|
assert_equal 4, @@test.group([], {}, {"count" => 0},
|
|
|
|
Code.new(reduce_function,
|
|
|
|
{"inc_value" => 2}))[0]["count"]
|
|
|
|
# assert_equal 4, @@test.group([], {}, {"count" => 0},
|
|
|
|
# Code.new(reduce_function,
|
|
|
|
# {"inc_value" => 2}), true)[0]["count"]
|
|
|
|
|
|
|
|
assert_equal 1, @@test.group([], {}, {"count" => 0},
|
|
|
|
Code.new(reduce_function,
|
|
|
|
{"inc_value" => 0.5}))[0]["count"]
|
|
|
|
# assert_equal 1, @@test.group([], {}, {"count" => 0},
|
|
|
|
# Code.new(reduce_function,
|
|
|
|
# {"inc_value" => 0.5}), true)[0]["count"]
|
2009-10-20 15:31:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "A collection with two records" do
|
|
|
|
setup do
|
|
|
|
@collection = @@db.collection('test-collection')
|
|
|
|
@collection.insert({:name => "Jones"})
|
|
|
|
@collection.insert({:name => "Smith"})
|
|
|
|
end
|
|
|
|
|
|
|
|
should "have two records" do
|
|
|
|
assert_equal 2, @collection.size
|
|
|
|
end
|
|
|
|
|
|
|
|
should "remove the two records" do
|
|
|
|
@collection.remove()
|
|
|
|
assert_equal 0, @collection.size
|
|
|
|
end
|
|
|
|
|
|
|
|
should "remove all records if an empty document is specified" do
|
|
|
|
@collection.remove({})
|
|
|
|
assert_equal 0, @collection.find.count
|
|
|
|
end
|
|
|
|
|
2009-10-22 18:47:40 +00:00
|
|
|
should "remove all records if deprecated clear is used" do
|
|
|
|
@collection.clear
|
|
|
|
assert_equal 0, @collection.find.count
|
|
|
|
end
|
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
should "remove only matching records" do
|
|
|
|
@collection.remove({:name => "Jones"})
|
|
|
|
assert_equal 1, @collection.size
|
|
|
|
end
|
2009-08-26 15:13:40 +00:00
|
|
|
end
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|