From 494d4518572add7bb33eb52284b3c1600e60ea1e Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 16 Apr 2010 15:59:34 -0400 Subject: [PATCH] RUBY-123 check index types --- lib/mongo/collection.rb | 9 ++++++++- test/collection_test.rb | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 71d1c50..e3a31ed 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -367,7 +367,14 @@ module Mongo if spec.is_a?(String) || spec.is_a?(Symbol) field_spec[spec.to_s] = 1 elsif spec.is_a?(Array) && spec.all? {|field| field.is_a?(Array) } - spec.each { |f| field_spec[f[0].to_s] = f[1] } + spec.each do |f| + if [Mongo::ASCENDING, Mongo::DESCENDING, Mongo::GEO2D].include?(f[1]) + field_spec[f[0].to_s] = f[1] + else + raise MongoArgumentError, "Invalid index field #{f[1].inspect}; " + + "should be one of Mongo::ASCENDING (1), Mongo::DESCENDING (-1) or Mongo::GEO2D ('2d')." + end + end else raise MongoArgumentError, "Invalid index specification #{spec.inspect}; " + "should be either a string, symbol, or an array of arrays." diff --git a/test/collection_test.rb b/test/collection_test.rb index f3dbfee..b18e0b8 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -577,6 +577,12 @@ class TestCollection < Test::Unit::TestCase end end + should "enforce proper index types" do + assert_raise MongoArgumentError do + @collection.create_index([['c', 'blah']]) + end + 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]