Skip to content
Browse files

Merge branch 'master' of git://github.com/jnunemaker/mongo-ruby-drive…

…r into jnunemaker/master
  • Loading branch information...
2 parents 25e9604 + 13a9cfe commit cb3823f48f968626ba1dc15097449d2b8b53188a Mike Dirolf committed
Showing with 44 additions and 7 deletions.
  1. +5 −4 lib/mongo/collection.rb
  2. +3 −3 lib/mongo/db.rb
  3. +36 −0 tests/test_db_api.rb
View
9 lib/mongo/collection.rb
@@ -72,13 +72,14 @@ def find_first(selector={}, options={})
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
6 lib/mongo/db.rb
@@ -447,10 +447,10 @@ def index_information(collection_name)
# 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
36 tests/test_db_api.rb
@@ -295,6 +295,20 @@ def test_index_information
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 @@ def test_multiple_index_cols
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 @@ def test_save
@@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

0 comments on commit cb3823f

Please sign in to comment.
Something went wrong with that request. Please try again.