"""Check for consistency errors in parent-child relationships.

"""
import logging

from website.app import init_app
from website import models
from framework import Q


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARN)
file_logger = logging.FileHandler('consistency/children.log')
logger.addHandler(file_logger)

app = init_app()


def find_orphaned_children(filters=None):
    """Find parents that don't point to their children.

    """
    errors = []
    query = Q('__backrefs.parent.node.nodes.0', 'exists', True)
    if filters:
        query = query & filters
    with_parent = models.Node.find(query)
    for child in with_parent:
        if len(child.node__parent) > 1:
            msg = u'Inconsistency: Child {} ({}) has {} parents.'.format(
                child.title,
                child._primary_key,
                len(child.node__parent),
            )
            logger.error(msg)
            errors.append(msg)
            continue
        parent = child.node__parent[0]
        if child not in parent.nodes:
            msg = u'Inconsistency: Parent {} ({}) does not point to child {} ({})'.format(
                parent.title,
                parent._primary_key,
                child.title,
                child._primary_key,
            )
            logger.error(msg)
            parent.nodes.append(child)
            parent.save()
            errors.append(msg)
    return errors


def find_missing_children(filters=None):
    """Find children that don't point to their parents.

    """
    errors = []
    query = Q('nodes.0', 'exists', True)
    if filters:
        query = query & filters
    with_children = models.Node.find(query)
    for parent in with_children:
        for child in parent.nodes:
            if not child.node__parent or child.node__parent[0] != parent:
                msg = u'Inconsistency: Child {} ({}) does not point to parent {} ({})'.format(
                    child.title,
                    child._primary_key,
                    parent.title,
                    parent._primary_key,
                )
                logger.error(msg)
                child.node__parent.append(parent)
                child.save()
                errors.append(msg)
    return errors


if __name__ == '__main__':
    errors = find_missing_children()
    errors = find_orphaned_children()

@@ -65,56 +65,58 @@
 %22%22%22%0A
-import logging%0A%0Afrom website.app import init_app
+from nose.tools import *    #noqa (PEP 8 asserts)%0A
 %0Afro
@@ -148,17 +148,18 @@
 rom 
-framework
+modularodm
  imp
@@ -169,196 +169,98 @@
  Q%0A%0A
-%0Alogger = logging.getLogger(__name__)%0Alogging.basicConfig(level=logging.WARN)%0Afile_logger = logging.FileHandler('consistency/children.log')%0Alogger.addHandler(file_logger)%0A%0Aapp = init_app()
+from tests.base import OsfTestCase%0Afrom tests.factories import UserFactory, ProjectFactory
 %0A%0A%0Ad
@@ -803,38 +803,8 @@
   )%0A
-            logger.error(msg)%0A
     
@@ -1169,38 +1169,8 @@
   )%0A
-            logger.error(msg)%0A
     
@@ -1943,42 +1943,8 @@
   )%0A
-                logger.error(msg)%0A
     
@@ -2073,16 +2073,1067 @@
 rrors%0A%0A%0A
+class TestParentChildMigration(OsfTestCase):%0A%0A    def setUp(self):%0A        super(TestParentChildMigration, self).setUp()%0A        self.user = UserFactory()%0A        self.parent_project = ProjectFactory(creator=self.user)%0A        self.first_child = ProjectFactory(creator=self.user, project=self.parent_project)%0A        self.second_child = ProjectFactory(creator=self.user, project=self.parent_project)%0A%0A    def test_orphaned_children(self):%0A        assert_equal(len(self.parent_project.nodes), 2)%0A%0A        self.parent_project.nodes.remove(self.second_child)%0A        assert_equal(len(self.parent_project.nodes), 1)%0A%0A        find_orphaned_children(filters=None)%0A        assert_equal(len(self.parent_project.nodes), 2)%0A%0A    def test_missing_children(self):%0A        assert self.parent_project in self.first_child.node__parent%0A%0A        self.first_child.node__parent = %5B%5D%0A        assert self.parent_project not in self.first_child.node__parent%0A%0A        find_missing_children(filters=None)%0A        assert self.parent_project in self.first_child.node__parent%0A%0A%0A
 if __nam

Add tests for find_bad_children.py