API CHANGE _id is aded to hash instances that get inserted / saved

This commit is contained in:
Mike Dirolf 2009-08-14 17:26:50 -04:00
parent 402b895385
commit 723f823ea1
3 changed files with 24 additions and 7 deletions

View File

@ -501,7 +501,7 @@ module XGen
} }
else else
objects = objects.collect do |o| 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
end end
send_to_db(InsertMessage.new(@name, collection_name, true, *objects)) send_to_db(InsertMessage.new(@name, collection_name, true, *objects))

View File

@ -34,7 +34,6 @@ class TestCollection < Test::Unit::TestCase
def test_safe_insert def test_safe_insert
a = {"hello" => "world"} a = {"hello" => "world"}
@@test.insert(a) @@test.insert(a)
a = @@test.find_one() # TODO we need this because insert doesn't add _id
@@test.insert(a) @@test.insert(a)
assert @@db.error.include? "E11000" assert @@db.error.include? "E11000"
@ -108,5 +107,24 @@ class TestCollection < Test::Unit::TestCase
@@test.find_one(6) @@test.find_one(6)
end end
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 end

View File

@ -636,19 +636,18 @@ class DBAPITest < Test::Unit::TestCase
assert_kind_of ObjectID, id assert_kind_of ObjectID, id
assert_equal 1, @@coll.count 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 1, @@coll.count
assert_equal "world", @@coll.find_one()["hello"] assert_equal "world", @@coll.find_one()["hello"]
doc = @@coll.find_one a["hello"] = "mike"
doc["hello"] = "mike" @@coll.save(a)
@@coll.save(doc)
assert_equal 1, @@coll.count assert_equal 1, @@coll.count
assert_equal "mike", @@coll.find_one()["hello"] assert_equal "mike", @@coll.find_one()["hello"]
@@coll.save(a) @@coll.save({"hello" => "world"})
assert_equal 2, @@coll.count assert_equal 2, @@coll.count
end end