Updated save to actually check if id exists in the collection.

This commit is contained in:
John Nunemaker 2009-05-31 22:45:05 -04:00
parent 4cfa9cbcf3
commit f95102c2bb
2 changed files with 13 additions and 4 deletions

View File

@ -72,13 +72,14 @@ module XGen
cursor = find(selector, h)
cursor.next_object # don't need to explicitly close b/c of limit
end
# Save an updated +object+ to the collection, or insert it if it doesn't exist already.
def save(object)
if id = object[:_id] || object['_id']
modify({:_id => id}, object)
else
id = object[:_id] || object['_id']
if id.nil? || find_first({'_id' => id}).nil?
insert(object)
else
modify({:_id => id}, object)
end
end

View File

@ -612,6 +612,14 @@ class DBAPITest < Test::Unit::TestCase
@@coll.save(a)
assert_equal 2, @@coll.count
end
def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
@@coll.clear
a = {'_id' => '1', 'hello' => 'world'}
@@coll.save(a)
assert_equal(1, @@coll.count)
end
# TODO this test fails with error message "Undefed Before end of object"
# That is a database error. The undefined type may go away.