Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Autogenerate _id on upsert #5

Merged
merged 1 commit into from over 2 years ago

2 participants

Ryan Biesemeyer Greg Brockman
Ryan Biesemeyer

One line functionality patch (and some test cases).

Greg Brockman gdb merged commit 7f49cb1 into from October 18, 2011
Greg Brockman gdb closed this October 18, 2011
Greg Brockman
Owner
gdb commented October 18, 2011

Good catch, thanks :).

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

Showing 1 unique commit by 1 author.

Oct 18, 2011
Ryan Biesemeyer added tests for upsert, '_id' generation for upserts that don't provi…
…de one
1326333
This page is out of date. Refresh to see the latest.
1  lib/embedded-mongo/backend/collection.rb
@@ -65,6 +65,7 @@ def update(selector, update, opts)
65 65
 
66 66
       if n == 0 and upsert
67 67
         selector = EmbeddedMongo::Util.deep_clone(selector)
  68
+        selector['_id'] ||= BSON::ObjectId.new
68 69
         apply_update!(update, selector)
69 70
         insert(selector)
70 71
         @db.set_last_error({ 'updatedExisting' => false, 'upserted' => update['_id'], 'n' => 1 })
42  test/functional/interface_test.rb
@@ -31,6 +31,48 @@ def test_insert_update_and_find
31 31
     assert_equal({ '_id' => id, 'test' => 'tar'}, cursor.first)
32 32
   end
33 33
 
  34
+  def test_update_upsert_record_not_present
  35
+    selector = {'schubar'=>'mubar'}
  36
+    @foo_collection.update(
  37
+      selector,
  38
+      {'$set'=>{'baz'=>'bingo'}},
  39
+      :upsert => true
  40
+    )
  41
+    cursor = @foo_collection.find(selector)
  42
+    assert_equal 1, cursor.count
  43
+    entry = cursor.first
  44
+    assert_equal 'bingo', (entry['baz'] rescue nil)
  45
+  end
  46
+
  47
+  def test_update_upsert_record_present
  48
+    selector = {'fubar'=>'rubar'}
  49
+    @foo_collection.insert(
  50
+      selector.merge('tweedle'=>'dee')
  51
+    )
  52
+    @foo_collection.update(
  53
+      selector,
  54
+      {'$set'=>{'baz'=>'bingo'}},
  55
+      :upsert => true
  56
+    )
  57
+    cursor = @foo_collection.find(selector)
  58
+    assert_equal 1, cursor.count
  59
+    entry = cursor.first
  60
+    assert_equal 'bingo', (entry['baz'] rescue nil), 'failed to set new value'
  61
+    assert_equal 'dee', (entry['tweedle'] rescue nil), 'overwrote unrelated value in record'
  62
+  end
  63
+
  64
+  def test_update_upsert_record_with_id
  65
+    @foo_collection.update(
  66
+      {'foo' => 'bart','_id'=>0xdeadbeef},
  67
+      {'$set'=>{'baz'=>'bingo'}},
  68
+      :upsert => true
  69
+    )
  70
+    cursor = @foo_collection.find({ 'foo' => 'bart' })
  71
+    assert_equal 1, cursor.count
  72
+    entry = cursor.first
  73
+    assert_equal 0xdeadbeef, (entry['_id'] rescue nil), 'overwrote id'
  74
+  end
  75
+
34 76
   def test_changing_ids
35 77
     id = @foo_collection.insert({ 'zing' => 'zong' })
36 78
     @foo_collection.update({ '_id' => id }, { '_id' => 'other_id' })
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.