More the deletion code over to the QuerySet object.
The Document object doens't have any delete_rule specific code anymore, and leverages the QuerySet's ability to deny/cascade/nullify its relations.
This commit is contained in:
@@ -661,7 +661,35 @@ class DocumentTest(unittest.TestCase):
|
||||
"""Ensure that a chain of documents is also deleted upon cascaded
|
||||
deletion.
|
||||
"""
|
||||
self.fail()
|
||||
|
||||
class BlogPost(Document):
|
||||
content = StringField()
|
||||
author = ReferenceField(self.Person, delete_rule=CASCADE)
|
||||
|
||||
class Comment(Document):
|
||||
text = StringField()
|
||||
post = ReferenceField(BlogPost, delete_rule=CASCADE)
|
||||
|
||||
|
||||
author = self.Person(name='Test User')
|
||||
author.save()
|
||||
|
||||
post = BlogPost(content = 'Watched some TV')
|
||||
post.author = author
|
||||
post.save()
|
||||
|
||||
comment = Comment(text = 'Kudos.')
|
||||
comment.post = post
|
||||
comment.save()
|
||||
|
||||
# Delete the Person, which should lead to deletion of the BlogPost, and,
|
||||
# recursively to the Comment, too
|
||||
author.delete()
|
||||
self.assertEqual(len(Comment.objects), 0)
|
||||
|
||||
self.Person.drop_collection()
|
||||
BlogPost.drop_collection()
|
||||
Comment.drop_collection()
|
||||
|
||||
def test_delete_rule_deny(self):
|
||||
"""Ensure that a document cannot be referenced if there are still
|
||||
|
||||
@@ -737,7 +737,20 @@ class QuerySetTest(unittest.TestCase):
|
||||
def test_delete_rule_cascade(self):
|
||||
"""Ensure cascading deletion of referring documents from the database.
|
||||
"""
|
||||
self.fail()
|
||||
class BlogPost(Document):
|
||||
content = StringField()
|
||||
author = ReferenceField(self.Person, delete_rule=CASCADE)
|
||||
BlogPost.drop_collection()
|
||||
|
||||
me = self.Person(name='Test User')
|
||||
me.save()
|
||||
|
||||
post = BlogPost(content='Watching TV', author=me)
|
||||
post.save()
|
||||
|
||||
self.assertEqual(1, BlogPost.objects.count())
|
||||
self.Person.objects.delete()
|
||||
self.assertEqual(0, BlogPost.objects.count())
|
||||
|
||||
def test_delete_rule_nullify(self):
|
||||
"""Ensure nullification of references to deleted documents.
|
||||
|
||||
Reference in New Issue
Block a user