Fixed "$pull" semantics for nested ListFields (#447)
This commit is contained in:
@@ -1497,9 +1497,6 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
def test_pull_nested(self):
|
||||
|
||||
class User(Document):
|
||||
name = StringField()
|
||||
|
||||
class Collaborator(EmbeddedDocument):
|
||||
user = StringField()
|
||||
|
||||
@@ -1514,8 +1511,7 @@ class QuerySetTest(unittest.TestCase):
|
||||
Site.drop_collection()
|
||||
|
||||
c = Collaborator(user='Esteban')
|
||||
s = Site(name="test", collaborators=[c])
|
||||
s.save()
|
||||
s = Site(name="test", collaborators=[c]).save()
|
||||
|
||||
Site.objects(id=s.id).update_one(pull__collaborators__user='Esteban')
|
||||
self.assertEqual(Site.objects.first().collaborators, [])
|
||||
@@ -1525,6 +1521,71 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
self.assertRaises(InvalidQueryError, pull_all)
|
||||
|
||||
def test_pull_from_nested_embedded(self):
|
||||
|
||||
class User(EmbeddedDocument):
|
||||
name = StringField()
|
||||
|
||||
def __unicode__(self):
|
||||
return '%s' % self.name
|
||||
|
||||
class Collaborator(EmbeddedDocument):
|
||||
helpful = ListField(EmbeddedDocumentField(User))
|
||||
unhelpful = ListField(EmbeddedDocumentField(User))
|
||||
|
||||
class Site(Document):
|
||||
name = StringField(max_length=75, unique=True, required=True)
|
||||
collaborators = EmbeddedDocumentField(Collaborator)
|
||||
|
||||
|
||||
Site.drop_collection()
|
||||
|
||||
c = User(name='Esteban')
|
||||
f = User(name='Frank')
|
||||
s = Site(name="test", collaborators=Collaborator(helpful=[c], unhelpful=[f])).save()
|
||||
|
||||
Site.objects(id=s.id).update_one(pull__collaborators__helpful=c)
|
||||
self.assertEqual(Site.objects.first().collaborators['helpful'], [])
|
||||
|
||||
Site.objects(id=s.id).update_one(pull__collaborators__unhelpful={'name': 'Frank'})
|
||||
self.assertEqual(Site.objects.first().collaborators['unhelpful'], [])
|
||||
|
||||
def pull_all():
|
||||
Site.objects(id=s.id).update_one(pull_all__collaborators__helpful__name=['Ross'])
|
||||
|
||||
self.assertRaises(InvalidQueryError, pull_all)
|
||||
|
||||
def test_pull_from_nested_mapfield(self):
|
||||
|
||||
class Collaborator(EmbeddedDocument):
|
||||
user = StringField()
|
||||
|
||||
def __unicode__(self):
|
||||
return '%s' % self.user
|
||||
|
||||
class Site(Document):
|
||||
name = StringField(max_length=75, unique=True, required=True)
|
||||
collaborators = MapField(ListField(EmbeddedDocumentField(Collaborator)))
|
||||
|
||||
|
||||
Site.drop_collection()
|
||||
|
||||
c = Collaborator(user='Esteban')
|
||||
f = Collaborator(user='Frank')
|
||||
s = Site(name="test", collaborators={'helpful':[c],'unhelpful':[f]})
|
||||
s.save()
|
||||
|
||||
Site.objects(id=s.id).update_one(pull__collaborators__helpful__user='Esteban')
|
||||
self.assertEqual(Site.objects.first().collaborators['helpful'], [])
|
||||
|
||||
Site.objects(id=s.id).update_one(pull__collaborators__unhelpful={'user':'Frank'})
|
||||
self.assertEqual(Site.objects.first().collaborators['unhelpful'], [])
|
||||
|
||||
def pull_all():
|
||||
Site.objects(id=s.id).update_one(pull_all__collaborators__helpful__user=['Ross'])
|
||||
|
||||
self.assertRaises(InvalidQueryError, pull_all)
|
||||
|
||||
def test_update_one_pop_generic_reference(self):
|
||||
|
||||
class BlogTag(Document):
|
||||
|
||||
Reference in New Issue
Block a user