2011-02-20 15:03:05 +00:00
|
|
|
require './test/test_helper'
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
class CollectionTest < Test::Unit::TestCase
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
context "Basic operations: " do
|
|
|
|
setup do
|
|
|
|
@logger = mock()
|
2011-11-15 20:59:42 +00:00
|
|
|
@logger.expects(:warn)
|
2009-11-02 20:50:16 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "send update message" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@conn.expects(:send_message).with do |op, msg, log|
|
2010-09-08 18:27:27 +00:00
|
|
|
op == 2001
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2011-09-07 21:14:53 +00:00
|
|
|
@coll.stubs(:log_operation)
|
2010-02-22 20:49:04 +00:00
|
|
|
@coll.update({}, {:title => 'Moby Dick'})
|
2009-11-02 20:50:16 +00:00
|
|
|
end
|
2009-11-05 20:14:48 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "send insert message" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@conn.expects(:send_message).with do |op, msg, log|
|
2010-09-08 18:27:27 +00:00
|
|
|
op == 2002
|
|
|
|
end
|
2011-09-07 21:14:53 +00:00
|
|
|
@coll.expects(:log_operation).with do |name, payload|
|
2011-01-29 06:20:41 +00:00
|
|
|
(name == :insert) && payload[:documents][0][:title].include?('Moby')
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
|
|
|
@coll.insert({:title => 'Moby Dick'})
|
2010-02-08 18:48:18 +00:00
|
|
|
end
|
|
|
|
|
2010-03-15 15:51:22 +00:00
|
|
|
should "send sort data" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
2011-10-17 18:41:09 +00:00
|
|
|
@conn.expects(:checkout_writer).returns(mock())
|
2010-03-15 15:51:22 +00:00
|
|
|
@conn.expects(:receive_message).with do |op, msg, log, sock|
|
2010-09-08 18:27:27 +00:00
|
|
|
op == 2004
|
2010-03-15 15:51:22 +00:00
|
|
|
end.returns([[], 0, 0])
|
2011-09-07 21:14:53 +00:00
|
|
|
@logger.expects(:debug)
|
2010-03-15 15:51:22 +00:00
|
|
|
@coll.find({:title => 'Moby Dick'}).sort([['title', 1], ['author', 1]]).next_document
|
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "not log binary data" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
2010-04-05 14:39:55 +00:00
|
|
|
data = BSON::Binary.new(("BINARY " * 1000).unpack("c*"))
|
2010-02-22 20:49:04 +00:00
|
|
|
@conn.expects(:send_message).with do |op, msg, log|
|
2010-09-08 18:27:27 +00:00
|
|
|
op == 2002
|
|
|
|
end
|
2011-09-07 21:14:53 +00:00
|
|
|
@coll.expects(:log_operation).with do |name, payload|
|
2011-01-29 06:20:41 +00:00
|
|
|
(name == :insert) && payload[:documents][0][:data].inspect.include?('Binary')
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
|
|
|
@coll.insert({:data => data})
|
|
|
|
end
|
|
|
|
|
|
|
|
should "send safe update message" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
|
2010-09-08 18:27:27 +00:00
|
|
|
op == 2001
|
|
|
|
end
|
2011-09-07 21:14:53 +00:00
|
|
|
@coll.expects(:log_operation).with do |name, payload|
|
2011-01-29 06:20:41 +00:00
|
|
|
(name == :update) && payload[:document][:title].include?('Moby')
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
|
|
|
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
|
2009-11-05 20:14:48 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "send safe insert message" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
|
2010-09-08 18:27:27 +00:00
|
|
|
op == 2001
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2011-09-07 21:14:53 +00:00
|
|
|
@coll.stubs(:log_operation)
|
2010-02-22 20:49:04 +00:00
|
|
|
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
|
2009-11-05 20:14:48 +00:00
|
|
|
end
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
should "not call insert for each ensure_index call" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@coll.expects(:generate_indexes).once
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
|
|
|
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
|
|
|
end
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
should "call generate_indexes for a new direction on the same field for ensure_index" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@coll.expects(:generate_indexes).twice
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
|
|
|
@coll.ensure_index [["x", Mongo::ASCENDING]]
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
end
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
should "call generate_indexes twice because the cache time is 0 seconds" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@db.cache_time = 0
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@coll.expects(:generate_indexes).twice
|
|
|
|
|
|
|
|
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
|
|
|
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
|
|
|
end
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
should "call generate_indexes for each key when calling ensure_indexes" do
|
|
|
|
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
|
|
|
|
@db = @conn['testing']
|
|
|
|
@db.cache_time = 300
|
|
|
|
@coll = @db.collection('books')
|
|
|
|
@coll.expects(:generate_indexes).once.with do |a, b, c|
|
|
|
|
a == {"x"=>-1, "y"=>-1}
|
|
|
|
end
|
2011-01-31 20:53:38 +00:00
|
|
|
|
2010-11-10 02:28:07 +00:00
|
|
|
@coll.ensure_index [["x", Mongo::DESCENDING], ["y", Mongo::DESCENDING]]
|
|
|
|
end
|
2009-11-02 20:50:16 +00:00
|
|
|
end
|
|
|
|
end
|