Implemented that index fields can also be specified with symbols.

This commit is contained in:
John Nunemaker 2009-05-31 22:16:01 -04:00
parent d679a17478
commit 4d7f06a6be
2 changed files with 31 additions and 3 deletions

View File

@ -460,10 +460,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
@@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]])
list = @@db.index_information(@@coll.name)
@ -309,6 +323,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")