From 723f823ea1640ceee1b5644d6ad3e612c9e09cfe Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Fri, 14 Aug 2009 17:26:50 -0400 Subject: [PATCH] API CHANGE _id is aded to hash instances that get inserted / saved --- lib/mongo/db.rb | 2 +- tests/test_collection.rb | 20 +++++++++++++++++++- tests/test_db_api.rb | 9 ++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 5b18c4b..1053155 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -501,7 +501,7 @@ module XGen } else objects = objects.collect do |o| - o[:_id] || o['_id'] ? o : o.merge(:_id => ObjectID.new) + o[:_id] || o['_id'] ? o : o.merge!(:_id => ObjectID.new) end end send_to_db(InsertMessage.new(@name, collection_name, true, *objects)) diff --git a/tests/test_collection.rb b/tests/test_collection.rb index 6745df0..e7f339c 100644 --- a/tests/test_collection.rb +++ b/tests/test_collection.rb @@ -34,7 +34,6 @@ class TestCollection < Test::Unit::TestCase def test_safe_insert a = {"hello" => "world"} @@test.insert(a) - a = @@test.find_one() # TODO we need this because insert doesn't add _id @@test.insert(a) assert @@db.error.include? "E11000" @@ -108,5 +107,24 @@ class TestCollection < Test::Unit::TestCase @@test.find_one(6) end end + + def test_insert_adds_id + doc = {"hello" => "world"} + @@test.insert(doc) + assert doc.include? :_id + + docs = [{"hello" => "world"}, {"hello" => "world"}] + @@test.insert(docs) + docs.each do |doc| + assert doc.include? :_id + end + end + + def test_save_adds_id + doc = {"hello" => "world"} + @@test.save(doc) + assert doc.include? :_id + + end end diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 628ea4b..a35d6dc 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -636,19 +636,18 @@ class DBAPITest < Test::Unit::TestCase assert_kind_of ObjectID, id assert_equal 1, @@coll.count - assert_equal id, @@coll.save(@@coll.find_one) + assert_equal id, @@coll.save(a) assert_equal 1, @@coll.count assert_equal "world", @@coll.find_one()["hello"] - doc = @@coll.find_one - doc["hello"] = "mike" - @@coll.save(doc) + a["hello"] = "mike" + @@coll.save(a) assert_equal 1, @@coll.count assert_equal "mike", @@coll.find_one()["hello"] - @@coll.save(a) + @@coll.save({"hello" => "world"}) assert_equal 2, @@coll.count end