Implemented that index fields can also be specified with symbols.
This commit is contained in:
parent
d679a17478
commit
4d7f06a6be
|
@ -460,10 +460,10 @@ module XGen
|
||||||
# enforce a uniqueness constraint.
|
# enforce a uniqueness constraint.
|
||||||
def create_index(collection_name, field_or_spec, unique=false)
|
def create_index(collection_name, field_or_spec, unique=false)
|
||||||
field_h = OrderedHash.new
|
field_h = OrderedHash.new
|
||||||
if field_or_spec.is_a? String
|
if field_or_spec.is_a?(String) || field_or_spec.is_a?(Symbol)
|
||||||
field_h[field_or_spec] = 1
|
field_h[field_or_spec.to_s] = 1
|
||||||
else
|
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
|
end
|
||||||
name = gen_index_name(field_h)
|
name = gen_index_name(field_h)
|
||||||
sel = {
|
sel = {
|
||||||
|
|
|
@ -294,6 +294,20 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
ensure
|
ensure
|
||||||
@@db.drop_index(@@coll.name, name)
|
@@db.drop_index(@@coll.name, name)
|
||||||
end
|
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
|
def test_multiple_index_cols
|
||||||
name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
|
name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
|
||||||
|
@ -308,6 +322,20 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
ensure
|
ensure
|
||||||
@@db.drop_index(@@coll.name, name)
|
@@db.drop_index(@@coll.name, name)
|
||||||
end
|
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
|
def test_unique_index
|
||||||
@@db.drop_collection("blah")
|
@@db.drop_collection("blah")
|
||||||
|
|
Loading…
Reference in New Issue