Skip to content

Commit

Permalink
Merge pull request #2021 from pauloAmaral/fix_indices_sortedlist_embe…
Browse files Browse the repository at this point in the history
…dded_document_list

Generate Unique Indices for SortedListField and EmbeddedDocumentListField
  • Loading branch information
bagerard committed Mar 18, 2019
2 parents 3d762fe + ba6a37f commit 73b12cc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,5 @@ that much better:
* Andy Yankovsky (https://github.com/werat)
* Bastien Gérard (https://github.com/bagerard)
* Trevor Hall (https://github.com/tjhall13)
* Gleb Voropaev (https://github.com/buggyspace)
* Gleb Voropaev (https://github.com/buggyspace)
* Paulo Amaral (https://github.com/pauloAmaral)
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog

Development
===========
- Generate Unique Indices for SortedListField and EmbeddedDocumentListFields #2020
- (Fill this out as you fix issues and develop your features).

Changes in 0.17.0
Expand Down
3 changes: 2 additions & 1 deletion mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,8 @@ def _unique_with_indexes(cls, namespace=''):
index = {'fields': fields, 'unique': True, 'sparse': sparse}
unique_indexes.append(index)

if field.__class__.__name__ == 'ListField':
if field.__class__.__name__ in {'EmbeddedDocumentListField',
'ListField', 'SortedListField'}:
field = field.field

# Grab any embedded document field unique indexes
Expand Down
71 changes: 71 additions & 0 deletions tests/document/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,77 @@ class BlogPost(Document):

self.assertRaises(NotUniqueError, post2.save)

def test_unique_embedded_document_in_sorted_list(self):
"""
Ensure that the uniqueness constraints are applied to fields in
embedded documents, even when the embedded documents in a sorted list
field.
"""
class SubDocument(EmbeddedDocument):
year = IntField()
slug = StringField(unique=True)

class BlogPost(Document):
title = StringField()
subs = SortedListField(EmbeddedDocumentField(SubDocument),
ordering='year')

BlogPost.drop_collection()

post1 = BlogPost(
title='test1', subs=[
SubDocument(year=2009, slug='conflict'),
SubDocument(year=2009, slug='conflict')
]
)
post1.save()

# confirm that the unique index is created
indexes = BlogPost._get_collection().index_information()
self.assertIn('subs.slug_1', indexes)
self.assertTrue(indexes['subs.slug_1']['unique'])

post2 = BlogPost(
title='test2', subs=[SubDocument(year=2014, slug='conflict')]
)

self.assertRaises(NotUniqueError, post2.save)

def test_unique_embedded_document_in_embedded_document_list(self):
"""
Ensure that the uniqueness constraints are applied to fields in
embedded documents, even when the embedded documents in an embedded
list field.
"""
class SubDocument(EmbeddedDocument):
year = IntField()
slug = StringField(unique=True)

class BlogPost(Document):
title = StringField()
subs = EmbeddedDocumentListField(SubDocument)

BlogPost.drop_collection()

post1 = BlogPost(
title='test1', subs=[
SubDocument(year=2009, slug='conflict'),
SubDocument(year=2009, slug='conflict')
]
)
post1.save()

# confirm that the unique index is created
indexes = BlogPost._get_collection().index_information()
self.assertIn('subs.slug_1', indexes)
self.assertTrue(indexes['subs.slug_1']['unique'])

post2 = BlogPost(
title='test2', subs=[SubDocument(year=2014, slug='conflict')]
)

self.assertRaises(NotUniqueError, post2.save)

def test_unique_with_embedded_document_and_embedded_unique(self):
"""Ensure that uniqueness constraints are applied to fields on
embedded documents. And work with unique_with as well.
Expand Down

0 comments on commit 73b12cc

Please sign in to comment.