From be78209f94637319a4e293e77816f8dc3ef24103 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Mon, 5 Dec 2011 04:44:40 -0800 Subject: [PATCH] Added test showing you can add index for dynamic docs --- tests/dynamic_document.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/dynamic_document.py b/tests/dynamic_document.py index b40ba062..6363672b 100644 --- a/tests/dynamic_document.py +++ b/tests/dynamic_document.py @@ -448,3 +448,31 @@ class DynamicDocTest(unittest.TestCase): doc.dict_field['embedded'].string_field = 'Hello World' self.assertEquals(doc._get_changed_fields(), ['dict_field.embedded.string_field']) self.assertEquals(doc._delta(), ({'dict_field.embedded.string_field': 'Hello World'}, {})) + + def test_indexes(self): + """Ensure that indexes are used when meta[indexes] is specified. + """ + class BlogPost(DynamicDocument): + meta = { + 'indexes': [ + '-date', + ('category', '-date') + ], + } + + BlogPost.drop_collection() + + info = BlogPost.objects._collection.index_information() + # _id, '-date', ('cat', 'date') + # NB: there is no index on _types by itself, since + # the indices on -date and tags will both contain + # _types as first element in the key + self.assertEqual(len(info), 3) + + # Indexes are lazy so use list() to perform query + list(BlogPost.objects) + info = BlogPost.objects._collection.index_information() + info = [value['key'] for key, value in info.iteritems()] + self.assertTrue([('_types', 1), ('category', 1), ('date', -1)] + in info) + self.assertTrue([('_types', 1), ('date', -1)] in info)