Fix QuerySet.ensure_index for new index specs

This commit is contained in:
Colin Howe 2011-05-29 13:33:00 +01:00
parent 1fa47206aa
commit 40df08c74c
2 changed files with 23 additions and 3 deletions

View File

@ -376,9 +376,13 @@ class QuerySet(object):
construct a multi-field index); keys may be prefixed with a **+**
or a **-** to determine the index ordering
"""
index_list = QuerySet._build_index_spec(self._document, key_or_list)
self._collection.ensure_index(index_list, drop_dups=drop_dups,
background=background)
index_spec = QuerySet._build_index_spec(self._document, key_or_list)
self._collection.ensure_index(
index_spec['fields'],
drop_dups=drop_dups,
background=background,
sparse=index_spec.get('sparse', False),
unique=index_spec.get('unique', False))
return self
@classmethod

View File

@ -2099,6 +2099,22 @@ class QuerySetTest(unittest.TestCase):
Number.drop_collection()
def test_ensure_index(self):
"""Ensure that manual creation of indexes works.
"""
class Comment(Document):
message = StringField()
Comment.objects.ensure_index('message')
info = Comment.objects._collection.index_information()
info = [(value['key'],
value.get('unique', False),
value.get('sparse', False))
for key, value in info.iteritems()]
self.assertTrue(([('_types', 1), ('message', 1)], False, False) in info)
class QTest(unittest.TestCase):
def setUp(self):