'Ensure that EmbeddedDocumentFields can contain their own documents.'

def test_recursive_embedding(self):

    class TreeNode(EmbeddedDocument, ):
        name = StringField()
        children = ListField(EmbeddedDocumentField('self'))
    class Tree(Document, ):
        name = StringField()
        children = ListField(EmbeddedDocumentField('TreeNode'))
    Tree.drop_collection()
    tree = Tree(name='Tree')
    first_child = TreeNode(name='Child    1')
    tree.children.append(first_child)
    second_child = TreeNode(name='Child    2')
    first_child.children.append(second_child)
    tree.save()
    tree = Tree.objects.first()
    self.assertEqual(len(tree.children), 1)
    self.assertEqual(len(tree.children[0].children), 1)
    third_child = TreeNode(name='Child    3')
    tree.children[0].children.append(third_child)
    tree.save()
    self.assertEqual(len(tree.children), 1)
    self.assertEqual(tree.children[0].name, first_child.name)
    self.assertEqual(tree.children[0].children[0].name, second_child.name)
    self.assertEqual(tree.children[0].children[1].name, third_child.name)
    tree.children[0].name = 'I    am    Child    1'
    tree.children[0].children[0].name = 'I    am    Child    2'
    tree.children[0].children[1].name = 'I    am    Child    3'
    tree.save()
    self.assertEqual(tree.children[0].name, 'I    am    Child    1')
    self.assertEqual(tree.children[0].children[0].name, 'I    am    Child    2')
    self.assertEqual(tree.children[0].children[1].name, 'I    am    Child    3')
    self.assertEqual(len(tree.children[0].children), 2)
    del tree.children[0].children[1]
    tree.save()
    self.assertEqual(len(tree.children[0].children), 1)
    tree.children[0].children.pop(0)
    tree.save()
    self.assertEqual(len(tree.children[0].children), 0)
    self.assertEqual(tree.children[0].children, [])
    tree.children[0].children.insert(0, third_child)
    tree.children[0].children.insert(0, second_child)
    tree.save()
    self.assertEqual(len(tree.children[0].children), 2)
    self.assertEqual(tree.children[0].children[0].name, second_child.name)
    self.assertEqual(tree.children[0].children[1].name, third_child.name)
