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.
|
|
|
|
# ++
|
|
|
|
|
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'mongo'
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
# NOTE: assumes Mongo is running
|
|
|
|
class TestCollection < Test::Unit::TestCase
|
|
|
|
include XGen::Mongo
|
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
|
|
|
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
|
|
|
|
@@test = @@db.collection("test")
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@@test.drop()
|
|
|
|
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()
|
|
|
|
end
|
|
|
|
|
2009-08-14 18:25:29 +00:00
|
|
|
def test_safe_insert
|
|
|
|
a = {"hello" => "world"}
|
|
|
|
@@test.insert(a)
|
|
|
|
@@test.insert(a)
|
|
|
|
assert @@db.error.include? "E11000"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_safe_update
|
|
|
|
@@test.create_index("x")
|
|
|
|
@@test.insert("x" => 5)
|
|
|
|
|
|
|
|
@@test.update({}, {"$inc" => {"x" => 1}})
|
|
|
|
assert @@db.error?
|
|
|
|
|
|
|
|
assert_raise OperationFailure do
|
|
|
|
@@test.update({}, {"$inc" => {"x" => 1}}, :safe => true)
|
|
|
|
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")
|
|
|
|
assert @@db.error.include? "E11000"
|
|
|
|
|
|
|
|
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
|
2009-08-18 15:48:16 +00:00
|
|
|
|
|
|
|
# TODO remove this test - it's deprecated
|
|
|
|
assert_equal 1, @@test.count("x" => 1)
|
2009-08-18 14:20:16 +00:00
|
|
|
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)
|
|
|
|
assert doc.include? :_id
|
|
|
|
|
|
|
|
docs = [{"hello" => "world"}, {"hello" => "world"}]
|
|
|
|
@@test.insert(docs)
|
|
|
|
docs.each do |doc|
|
|
|
|
assert doc.include? :_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_save_adds_id
|
|
|
|
doc = {"hello" => "world"}
|
|
|
|
@@test.save(doc)
|
|
|
|
assert doc.include? :_id
|
|
|
|
end
|
2009-08-13 19:18:53 +00:00
|
|
|
end
|
|
|
|
|