Merge branch 'master' of git://github.com/jnunemaker/mongo-ruby-driver into jnunemaker/master
This commit is contained in:
commit
cb3823f48f
|
@ -75,10 +75,11 @@ module XGen
|
|||
|
||||
# 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
|
||||
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -296,6 +296,20 @@ class DBAPITest < Test::Unit::TestCase
|
|||
@@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]])
|
||||
info = @@db.index_information(@@coll.name)
|
||||
|
@ -308,6 +322,20 @@ class DBAPITest < Test::Unit::TestCase
|
|||
@@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")
|
||||
test = @@db.collection("blah")
|
||||
|
@ -601,6 +629,14 @@ class DBAPITest < Test::Unit::TestCase
|
|||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue