Permalink
Browse files

fix for collection#save plus tests

  • Loading branch information...
1 parent 3c39ac0 commit c30826eff27db0d3d13c26737678ad9fe9947a00 @brandonblack brandonblack committed Nov 20, 2012
Showing with 74 additions and 21 deletions.
  1. +2 −3 lib/mongo/collection.rb
  2. +16 −0 test/functional/safe_test.rb
  3. +37 −18 test/unit/safe_test.rb
  4. +19 −0 test/unit/write_concern_test.rb
@@ -330,13 +330,12 @@ def find_one(spec_or_object_id=nil, opts={})
#
# @raise [Mongo::OperationFailure] will be raised iff :w > 0 and the operation fails.
def save(doc, opts={})
- write_concern = get_write_concern(opts, self)
if doc.has_key?(:_id) || doc.has_key?('_id')
id = doc[:_id] || doc['_id']
- update({:_id => id}, doc, :upsert => true, :write_concern => write_concern)
+ update({:_id => id}, doc, opts.merge!({:upsert => true}))
id
else
- insert(doc, write_concern)
+ insert(doc, opts)
end
end
@@ -24,6 +24,22 @@ class SafeTest < Test::Unit::TestCase
@collection.insert({:a => 1}, :safe => false)
end
+ should "allow safe override on save" do
+ @collection.insert({:a => 1})
+ id = @collection.insert({:a => 2})
+ assert_nothing_raised do
+ @collection.save({:_id => id.to_s, :a => 1}, :safe => false)
+ end
+ end
+
+ should "propogate safe option on save" do
+ @collection.insert({:a => 1})
+ id = @collection.insert({:a => 2})
+ assert_raise(OperationFailure) do
+ @collection.save({:_id => id.to_s, :a => 1})
+ end
+ end
+
should "propogate safe option on update" do
@collection.insert({:a => 1})
@collection.insert({:a => 2})
@@ -1,36 +1,36 @@
require 'test_helper'
-class WriteConcernTest < Test::Unit::TestCase
+class SafeTest < Test::Unit::TestCase
context "Write-Concern modes on Mongo::Connection " do
setup do
@safe_value = {:w => 7, :j => false, :fsync => false, :wtimeout => false}
- @client = Mongo::Connection.new('localhost', 27017, :safe => @safe_value, :connect => false)
+ @connection = Mongo::Connection.new('localhost', 27017, :safe => @safe_value, :connect => false)
end
should "propogate to DB" do
- db = @client['foo']
+ db = @connection['foo']
assert_equal @safe_value[:w], db.write_concern[:w]
- db = @client.db('foo')
+ db = @connection.db('foo')
assert_equal @safe_value[:w], db.write_concern[:w]
- db = DB.new('foo', @client)
+ db = DB.new('foo', @connection)
assert_equal @safe_value[:w], db.write_concern[:w]
end
should "allow db override" do
- db = DB.new('foo', @client, :safe => false)
+ db = DB.new('foo', @connection, :safe => false)
assert_equal 0, db.write_concern[:w]
- db = @client.db('foo', :safe => false)
+ db = @connection.db('foo', :safe => false)
assert_equal 0, db.write_concern[:w]
end
context "on DB: " do
setup do
- @db = @client['foo']
+ @db = @connection['foo']
end
should "propogate to collection" do
@@ -55,69 +55,88 @@ class WriteConcernTest < Test::Unit::TestCase
context "on operations supporting safe mode" do
setup do
- @col = @client['foo']['bar']
+ @col = @connection['foo']['bar']
end
should "use default value on insert" do
- @client.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
safe == @safe_value
end
@col.insert({:a => 1})
end
should "allow override alternate value on insert" do
- @client.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
safe == {:w => 100, :j => false, :fsync => false, :wtimeout => false}
end
@col.insert({:a => 1}, :safe => {:w => 100})
end
should "allow override to disable on insert" do
- @client.expects(:send_message)
+ @connection.expects(:send_message)
@col.insert({:a => 1}, :safe => false)
end
should "use default value on update" do
- @client.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
safe == @safe_value
end
@col.update({:a => 1}, {:a => 2})
end
should "allow override alternate value on update" do
- @client.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
safe == {:w => 100, :j => false, :fsync => false, :wtimeout => false}
end
@col.update({:a => 1}, {:a => 2}, :safe => {:w => 100})
end
should "allow override to disable on update" do
- @client.expects(:send_message)
+ @connection.expects(:send_message)
@col.update({:a => 1}, {:a => 2}, :safe => false)
end
+ should "use default value on save" do
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ safe == @safe_value
+ end
+ @col.save({:a => 1})
+ end
+
+ should "allow override alternate value on save" do
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ safe == @safe_value.merge(:w => 1)
+ end
+ @col.save({:a => 1}, :safe => true)
+ end
+
+ should "allow override to disable on save" do
+ @connection.expects(:send_message)
+ @col.save({:a => 1}, :safe => false)
+ end
+
should "use default value on remove" do
- @client.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
safe == @safe_value
end
@col.remove
end
should "allow override alternate value on remove" do
- @client.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
+ @connection.expects(:send_message_with_gle).with do |op, msg, log, n, safe|
safe == {:w => 100, :j => false, :fsync => false, :wtimeout => false}
end
@col.remove({}, :safe => {:w => 100})
end
should "allow override to disable on remove" do
- @client.expects(:send_message)
+ @connection.expects(:send_message)
@col.remove({}, :safe => false)
end
end
@@ -117,6 +117,25 @@ class Mongo::Client
@collection.update({:a => 1}, {:a => 2}, :w => 0)
end
+ should "use default value on save" do
+ @client.expects(:send_message_with_gle).with do |op, msg, log, n, wc|
+ wc == @write_concern
+ end
+ @collection.save({:a => 1})
+ end
+
+ should "allow override alternate value on save" do
+ @client.expects(:send_message_with_gle).with do |op, msg, log, n, wc|
+ wc == @write_concern.merge(:w => 1)
+ end
+ @collection.save({:a => 1}, :w => 1)
+ end
+
+ should "allow override to disable on save" do
+ @client.expects(:send_message)
+ @collection.save({:a => 1}, :w => 0)
+ end
+
should "use default value on remove" do
@client.expects(:send_message_with_gle).with do |op, msg, log, n, wc|
wc == @write_concern

0 comments on commit c30826e

Please sign in to comment.