use repsert instead of checking for document existence, update tests to match new index_information API

This commit is contained in:
Mike Dirolf 2009-06-02 14:37:58 -04:00
parent cb3823f48f
commit e5ce547e87
2 changed files with 29 additions and 25 deletions

View File

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

View File

@ -295,17 +295,18 @@ class DBAPITest < Test::Unit::TestCase
ensure ensure
@@db.drop_index(@@coll.name, name) @@db.drop_index(@@coll.name, name)
end end
def test_index_create_with_symbol
name = @@db.create_index(@@coll.name, :a)
list = @@db.index_information(@@coll.name)
assert_equal @@coll.index_information, list
assert_equal 2, list.length
info = list[1] def test_index_create_with_symbol
assert_equal name, 'a_1' assert_equal @@coll.index_information.length, 1
assert_equal name, info[:name]
assert_equal 1, info[:keys]['a'] name = @@db.create_index(@@coll.name, :a)
info = @@db.index_information(@@coll.name)
assert_equal name, "a_1"
assert_equal @@coll.index_information, info
assert_equal 2, info.length
assert info.has_key? name
assert_equal info[name], [["a", ASCENDING]]
ensure ensure
@@db.drop_index(@@coll.name, name) @@db.drop_index(@@coll.name, name)
end end
@ -321,17 +322,15 @@ class DBAPITest < Test::Unit::TestCase
ensure ensure
@@db.drop_index(@@coll.name, name) @@db.drop_index(@@coll.name, name)
end end
def test_multiple_index_cols_with_symbols def test_multiple_index_cols_with_symbols
name = @@db.create_index(@@coll.name, [[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]]) name = @@db.create_index(@@coll.name, [[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]])
list = @@db.index_information(@@coll.name) info = @@db.index_information(@@coll.name)
assert_equal 2, list.length assert_equal 2, info.length
info = list[1]
assert_equal name, 'a_-1_b_1_c_-1' assert_equal name, 'a_-1_b_1_c_-1'
assert_equal name, info[:name] assert info.has_key? name
keys = info[:keys].keys assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name]
assert_equal ['a', 'b', 'c'], keys.sort
ensure ensure
@@db.drop_index(@@coll.name, name) @@db.drop_index(@@coll.name, name)
end end
@ -628,13 +627,19 @@ class DBAPITest < Test::Unit::TestCase
@@coll.save(a) @@coll.save(a)
assert_equal 2, @@coll.count assert_equal 2, @@coll.count
end end
def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
@@coll.clear @@coll.clear
a = {'_id' => '1', 'hello' => 'world'} a = {'_id' => '1', 'hello' => 'world'}
@@coll.save(a) @@coll.save(a)
assert_equal(1, @@coll.count) assert_equal(1, @@coll.count)
assert_equal("world", @@coll.find_first()["hello"])
a["hello"] = "mike"
@@coll.save(a)
assert_equal(1, @@coll.count)
assert_equal("mike", @@coll.find_first()["hello"])
end end
def test_invalid_key_names def test_invalid_key_names