Merge branch 'master' of git://github.com/jnunemaker/mongo-ruby-driver into jnunemaker/master

This commit is contained in:
Mike Dirolf 2009-06-02 14:20:19 -04:00
commit cb3823f48f
3 changed files with 44 additions and 7 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

@ -447,10 +447,10 @@ module XGen
# enforce a uniqueness constraint.
def create_index(collection_name, field_or_spec, unique=false)
field_h = OrderedHash.new
if field_or_spec.is_a? String
field_h[field_or_spec] = 1
if field_or_spec.is_a?(String) || field_or_spec.is_a?(Symbol)
field_h[field_or_spec.to_s] = 1
else
field_or_spec.each { |f| field_h[f[0]] = f[1] }
field_or_spec.each { |f| field_h[f[0].to_s] = f[1] }
end
name = gen_index_name(field_h)
sel = {

View File

@ -295,6 +295,20 @@ class DBAPITest < Test::Unit::TestCase
ensure
@@db.drop_index(@@coll.name, name)
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]
assert_equal name, 'a_1'
assert_equal name, info[:name]
assert_equal 1, info[:keys]['a']
ensure
@@db.drop_index(@@coll.name, name)
end
def test_multiple_index_cols
name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
@ -307,6 +321,20 @@ class DBAPITest < Test::Unit::TestCase
ensure
@@db.drop_index(@@coll.name, name)
end
def test_multiple_index_cols_with_symbols
name = @@db.create_index(@@coll.name, [[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]])
list = @@db.index_information(@@coll.name)
assert_equal 2, list.length
info = list[1]
assert_equal name, 'a_-1_b_1_c_-1'
assert_equal name, info[:name]
keys = info[:keys].keys
assert_equal ['a', 'b', 'c'], keys.sort
ensure
@@db.drop_index(@@coll.name, name)
end
def test_unique_index
@@db.drop_collection("blah")
@ -600,6 +628,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
def test_invalid_key_names
@@coll.clear