Skip to content

Commit

Permalink
Fixed index inheritance issues
Browse files Browse the repository at this point in the history
firmed up testcases (MongoEngine#123) (MongoEngine#125)
  • Loading branch information
rozza committed Sep 11, 2012
1 parent 5c6035d commit 5949970
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Expand Up @@ -2,6 +2,11 @@
Changelog
=========


Changes in 0.7.4
================
- Fixed index inheritance issues - firmed up testcases (MongoEngine/mongoengine#123) (MongoEngine/mongoengine#125)

Changes in 0.7.3
================
- Reverted EmbeddedDocuments meta handling - now can turn off inheritance (MongoEngine/mongoengine#119)
Expand Down
6 changes: 4 additions & 2 deletions mongoengine/queryset.py
Expand Up @@ -501,8 +501,10 @@ def _build_index_spec(cls, doc_cls, spec):
"""
if isinstance(spec, basestring):
spec = {'fields': [spec]}
if isinstance(spec, (list, tuple)):
spec = {'fields': spec}
elif isinstance(spec, (list, tuple)):
spec = {'fields': list(spec)}
elif isinstance(spec, dict):
spec = dict(spec)

index_list = []
direction = None
Expand Down
32 changes: 32 additions & 0 deletions tests/test_document.py
Expand Up @@ -420,6 +420,9 @@ class Animal(Document):
'indexes': ['name']
}

self.assertEqual(Animal._meta['index_specs'],
[{'fields': [('_types', 1), ('name', 1)]}])

Animal.drop_collection()

dog = Animal(name='dog')
Expand All @@ -441,6 +444,9 @@ class Animal(Document):
'allow_inheritance': False,
'indexes': ['name']
}

self.assertEqual(Animal._meta['index_specs'],
[{'fields': [('name', 1)]}])
collection.update({}, {"$unset": {"_types": 1, "_cls": 1}}, multi=True)

# Confirm extra data is removed
Expand Down Expand Up @@ -658,6 +664,12 @@ class BlogPost(Document):
'allow_inheritance': True
}

self.assertEqual(BlogPost._meta['index_specs'],
[{'fields': [('_types', 1), ('addDate', -1)]},
{'fields': [('tags', 1)]},
{'fields': [('_types', 1), ('category', 1),
('addDate', -1)]}])

BlogPost.drop_collection()

info = BlogPost.objects._collection.index_information()
Expand All @@ -681,6 +693,13 @@ class ExtendedBlogPost(BlogPost):
title = StringField()
meta = {'indexes': ['title']}

self.assertEqual(ExtendedBlogPost._meta['index_specs'],
[{'fields': [('_types', 1), ('addDate', -1)]},
{'fields': [('tags', 1)]},
{'fields': [('_types', 1), ('category', 1),
('addDate', -1)]},
{'fields': [('_types', 1), ('title', 1)]}])

BlogPost.drop_collection()

list(ExtendedBlogPost.objects)
Expand Down Expand Up @@ -711,6 +730,8 @@ class B(A):
description = StringField()

self.assertEqual(A._meta['index_specs'], B._meta['index_specs'])
self.assertEqual([{'fields': [('_types', 1), ('title', 1)]}],
A._meta['index_specs'])

def test_build_index_spec_is_not_destructive(self):

Expand Down Expand Up @@ -791,6 +812,9 @@ class Person(Document):
'allow_inheritance': False
}

self.assertEqual([{'fields': [('rank.title', 1)]}],
Person._meta['index_specs'])

Person.drop_collection()

# Indexes are lazy so use list() to perform query
Expand All @@ -809,6 +833,10 @@ class Place(Document):
'*location.point',
],
}

self.assertEqual([{'fields': [('location.point', '2d')]}],
Place._meta['index_specs'])

Place.drop_collection()

info = Place.objects._collection.index_information()
Expand All @@ -834,6 +862,10 @@ class BlogPost(Document):
],
}

self.assertEqual([{'fields': [('addDate', -1)], 'unique': True,
'sparse': True, 'types': False}],
BlogPost._meta['index_specs'])

BlogPost.drop_collection()

info = BlogPost.objects._collection.index_information()
Expand Down

0 comments on commit 5949970

Please sign in to comment.