Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for $inc modifier #8

Merged
merged 2 commits into from

2 participants

@jfreitag

No description provided.

lib/embedded-mongo/backend/collection.rb
@@ -222,6 +222,8 @@ def apply_update_directive!(directive_key, directive_value, doc)
case directive_key
when '$set'
directive_value.each { |k, v| doc[k] = v }
+ when '$inc'
+ directive_value.each { |k, v| doc[k] += v }
@gdb Owner
gdb added a note

If the type of doc[k] is off, should raise:

"Mongo::OperationFailure: 10140: Cannot apply $inc modifier to non-number"

@gdb Owner
gdb added a note

(To be clear, this will currently raise a Type error -- I would instead raise the Mongo::OperationFailure.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jennifer Freitag Raise OperationFailure when $inc is applied to non-numeric fields
In response to feedback from pull request.
9a9df1a
@jfreitag

I have added a new commit to raise an OperationFailure and a test that indicates the new behavior. Please let me know if I missed anything else. Thanks!

@gdb gdb commented on the diff
test/functional/interface_test.rb
@@ -4,6 +4,7 @@
require File.join(File.dirname(__FILE__), '../../lib/embedded-mongo')
class InterfaceTest < Test::Unit::TestCase
+ include Mongo
@gdb Owner
gdb added a note

I prefer not to include namespaces when not needed -- it makes the resulting code a lot harder to trace. No worries though; I'll fix it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gdb
Owner

LGTM, thanks!

@gdb gdb merged commit 844456d into gdb:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 24, 2012
  1. Add support for $inc modifier

    Jennifer Freitag authored
Commits on Oct 30, 2012
  1. Raise OperationFailure when $inc is applied to non-numeric fields

    Jennifer Freitag authored
    In response to feedback from pull request.
This page is out of date. Refresh to see the latest.
View
5 lib/embedded-mongo/backend/collection.rb
@@ -222,6 +222,11 @@ def apply_update_directive!(directive_key, directive_value, doc)
case directive_key
when '$set'
directive_value.each { |k, v| doc[k] = v }
+ when '$inc'
+ 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
View
30 test/functional/interface_test.rb
@@ -4,6 +4,7 @@
require File.join(File.dirname(__FILE__), '../../lib/embedded-mongo')
class InterfaceTest < Test::Unit::TestCase
+ include Mongo
@gdb Owner
gdb added a note

I prefer not to include namespaces when not needed -- it makes the resulting code a lot harder to trace. No worries though; I'll fix it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
def setup
# Tests should pass with either of the following lines
@conn = EmbeddedMongo::Connection.new
@@ -61,6 +62,35 @@ def test_update_upsert_record_present
assert_equal 'dee', (entry['tweedle'] rescue nil), 'overwrote unrelated value in record'
end
+ def test_update_increment_record_field
+ selector = {'fubar'=>'rubar'}
+ @foo_collection.insert(
+ selector.merge('baz'=>1)
+ )
+ @foo_collection.update(
+ selector,
+ {'$inc'=>{'baz'=>2}}
+ )
+ cursor = @foo_collection.find(selector)
+ assert_equal 1, cursor.count
+ entry = cursor.first
+ 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},
Something went wrong with that request. Please try again.