RUBY-137 specify index name

This commit is contained in:
Kyle Banker 2010-06-14 14:20:12 -04:00
parent 67a56b64eb
commit 31d2483294
2 changed files with 15 additions and 2 deletions

View File

@ -374,7 +374,7 @@ module Mongo
#
# @core indexes create_index-instance_method
def create_index(spec, opts={})
opts.assert_valid_keys(:min, :max, :background, :unique, :dropDups)
opts.assert_valid_keys(:min, :max, :name, :background, :unique, :dropDups)
field_spec = BSON::OrderedHash.new
if spec.is_a?(String) || spec.is_a?(Symbol)
field_spec[spec.to_s] = 1
@ -392,7 +392,7 @@ module Mongo
"should be either a string, symbol, or an array of arrays."
end
name = generate_index_name(field_spec)
name = opts.delete(:name) || generate_index_name(field_spec)
selector = {
:name => name,

View File

@ -626,6 +626,19 @@ class TestCollection < Test::Unit::TestCase
end
end
should "raise an error if index name is greater than 128" do
assert_raise Mongo::OperationFailure do
@collection.create_index([['a' * 25, 1], ['b' * 25, 1],
['c' * 25, 1], ['d' * 25, 1], ['e' * 25, 1]])
end
end
should "allow for an alternate name to be specified" do
@collection.create_index([['a' * 25, 1], ['b' * 25, 1],
['c' * 25, 1], ['d' * 25, 1], ['e' * 25, 1]], :name => 'foo_index')
assert @collection.index_information['foo_index']
end
should "generate indexes in the proper order" do
@collection.expects(:insert_documents) do |sel, coll, safe|
assert_equal 'b_1_a_1', sel[:name]