Permalink
Browse files

Raise OperationFailure when $inc is applied to non-numeric fields

In response to feedback from pull request.
  • Loading branch information...
1 parent 8495e7c commit 9a9df1a470e423d14b40a693090dc1a379574b58 Jennifer Freitag committed Oct 30, 2012
Showing with 19 additions and 1 deletion.
  1. +4 −1 lib/embedded-mongo/backend/collection.rb
  2. +15 −0 test/functional/interface_test.rb
@@ -223,7 +223,10 @@ def apply_update_directive!(directive_key, directive_value, doc)
when '$set'
directive_value.each { |k, v| doc[k] = v }
when '$inc'
- directive_value.each { |k, v| doc[k] += v }
+ directive_value.each do |k, v|
+ raise Mongo::OperationFailure.new("Cannot apply $inc modifier to non-number: #{k}=#{doc[k].inspect}") unless doc[k].kind_of?(Numeric)
+ doc[k] += v
+ end
else
raise NotImplementedError.new("Have yet to implement updating: #{directive_key}")
end
@@ -4,6 +4,7 @@
require File.join(File.dirname(__FILE__), '../../lib/embedded-mongo')
class InterfaceTest < Test::Unit::TestCase
+ include Mongo
def setup
# Tests should pass with either of the following lines
@conn = EmbeddedMongo::Connection.new
@@ -76,6 +77,20 @@ def test_update_increment_record_field
assert_equal 3, (entry['baz'] rescue nil), 'failed to increment value'
end
+ def test_update_increment_record_field_with_incorrect_type
+ selector = {'fubar'=>'rubar'}
+ @foo_collection.insert(
+ selector.merge('baz'=>'not an integer')
+ )
+
+ assert_raise OperationFailure do
+ @foo_collection.update(
+ selector,
+ {'$inc'=>{'baz'=>2}}
+ )
+ end
+ end
+
def test_update_upsert_record_with_id
@foo_collection.update(
{'foo' => 'bart','_id'=>0xdeadbeef},

0 comments on commit 9a9df1a

Please sign in to comment.