'Ensure self-referencing CASCADE deletes do not result in infinite
loop'

def test_reverse_delete_rule_cascade_self_referencing(self):

    class Category(Document, ):
        name = StringField()
        parent = ReferenceField('self', reverse_delete_rule=CASCADE)
    Category.drop_collection()
    num_children = 3
    base = Category(name='Root')
    base.save()
    for i in range(num_children):
        child_name = ('Child-%i' % i)
        child = Category(name=child_name, parent=base)
        child.save()
        for i in range(num_children):
            child_child_name = ('Child-Child-%i' % i)
            child_child = Category(name=child_child_name, parent=child)
            child_child.save()
    tree_size = ((1 + num_children) + (num_children * num_children))
    self.assertEqual(tree_size, Category.objects.count())
    self.assertEqual(num_children, Category.objects(parent=base).count())
    base.delete()
    self.assertEqual(0, Category.objects.count())
