2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../test_helper", __FILE__)
|
2010-11-03 21:36:08 +00:00
|
|
|
include Mongo
|
|
|
|
|
|
|
|
class SafeTest < Test::Unit::TestCase
|
2010-11-09 18:07:01 +00:00
|
|
|
context "Safe mode propogation: " do
|
2010-11-03 21:36:08 +00:00
|
|
|
setup do
|
|
|
|
@con = standard_connection(:safe => {:w => 1})
|
|
|
|
@db = @con[MONGO_TEST_DB]
|
|
|
|
@col = @db['test-safe']
|
|
|
|
@col.create_index([[:a, 1]], :unique => true)
|
|
|
|
@col.remove
|
|
|
|
end
|
|
|
|
|
|
|
|
should "propogate safe option on insert" do
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
|
|
|
|
assert_raise_error(OperationFailure, "duplicate key") do
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "allow safe override on insert" do
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
@col.insert({:a => 1}, :safe => false)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "propogate safe option on update" do
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
@col.insert({:a => 2})
|
|
|
|
|
|
|
|
assert_raise_error(OperationFailure, "duplicate key") do
|
|
|
|
@col.update({:a => 2}, {:a => 1})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "allow safe override on update" do
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
@col.insert({:a => 2})
|
|
|
|
@col.update({:a => 2}, {:a => 1}, :safe => false)
|
|
|
|
end
|
|
|
|
end
|
2010-11-09 18:07:01 +00:00
|
|
|
|
|
|
|
context "Safe error objects" do
|
|
|
|
setup do
|
|
|
|
@con = standard_connection
|
|
|
|
@db = @con[MONGO_TEST_DB]
|
|
|
|
@col = @db['test']
|
|
|
|
@col.remove
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
@col.insert({:a => 1})
|
|
|
|
end
|
|
|
|
|
|
|
|
should "return object on update" do
|
|
|
|
response = @col.update({:a => 1}, {"$set" => {:a => 2}},
|
|
|
|
:multi => true, :safe => true)
|
|
|
|
|
|
|
|
assert response['updatedExisting']
|
|
|
|
assert_equal 3, response['n']
|
|
|
|
end
|
|
|
|
|
|
|
|
should "return object on remove" do
|
|
|
|
response = @col.remove({}, :safe => true)
|
|
|
|
assert_equal 3, response['n']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-03 21:36:08 +00:00
|
|
|
end
|