Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix for collection#save plus tests

  • Loading branch information...
commit c30826eff27db0d3d13c26737678ad9fe9947a00 1 parent 3c39ac0
@brandonblack brandonblack authored
View
5 lib/mongo/collection.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
View
16 test/functional/safe_test.rb
@@ -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})
View
55 test/unit/safe_test.rb
@@ -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,11 +55,11 @@ 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
@@ -67,7 +67,7 @@ class WriteConcernTest < Test::Unit::TestCase
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
@@ -75,12 +75,12 @@ class WriteConcernTest < Test::Unit::TestCase
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
@@ -88,7 +88,7 @@ class WriteConcernTest < Test::Unit::TestCase
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
@@ -96,12 +96,31 @@ class WriteConcernTest < Test::Unit::TestCase
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
@@ -109,7 +128,7 @@ class WriteConcernTest < Test::Unit::TestCase
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
@@ -117,7 +136,7 @@ class WriteConcernTest < Test::Unit::TestCase
end
should "allow override to disable on remove" do
- @client.expects(:send_message)
+ @connection.expects(:send_message)
@col.remove({}, :safe => false)
end
end
View
19 test/unit/write_concern_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.