'Test finding non orphan parents from many possible parents.'

@ddt.data(ModuleStoreEnum.Type.mongo)
def test_get_non_orphan_parents(self, default_ms):

    self.initdb(default_ms)
    course_id = self.course_locations[self.MONGO_COURSEID].course_key
    self._create_block_hierarchy()
    self.store.publish(self.course.location, self.user_id)
    mongo_store = self.store._get_modulestore_for_courselike(course_id)
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_id):
        parent = mongo_store.get_parent_location(self.problem_x1a_1)
        self.assertEqual(parent, self.vertical_x1a)
    orphan_sequential = course_id.make_usage_key('sequential', 'OrphanSequential')
    orphan_vertical = course_id.make_usage_key('vertical', 'OrphanVertical')
    orphan_locations = [orphan_sequential, orphan_vertical]
    for location in orphan_locations:
        self.store.create_item(self.user_id, location.course_key, location.block_type, block_id=location.block_id)
        self.store.publish(location, self.user_id)
    found_orphans = mongo_store.get_orphans(course_id)
    self.assertEqual(set(found_orphans), set(orphan_locations))
    self.assertEqual(len(set(found_orphans)), 2)
    mongo_store.collection.update(orphan_sequential.to_deprecated_son('_id.'), {'$push': {'definition.children': unicode(self.problem_x1a_1)}})
    mongo_store.collection.update(orphan_vertical.to_deprecated_son('_id.'), {'$push': {'definition.children': unicode(self.problem_x1a_1)}})
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_id):
        parent = mongo_store.get_parent_location(self.problem_x1a_1)
        self.assertEqual(parent, self.vertical_x1a)
    mongo_store.collection.update(self.sequential_x1.to_deprecated_son('_id.'), {'$push': {'definition.children': unicode(self.problem_x1a_1)}})
    with self.store.branch_setting(ModuleStoreEnum.Branch.published_only, course_id):
        self.assertTrue(self.store.has_item(self.problem_x1a_1))
        with self.assertRaises(ReferentialIntegrityError):
            self.store.get_parent_location(self.problem_x1a_1)
