'Tests the a duplicated xblock appears immediately after its source
(if duplicate and source share the same parent), else at the
end of the children of the parent.'

def test_ordering(self):

    def verify_order(source_usage_key, parent_usage_key, source_position=None):
        usage_key = self._duplicate_item(parent_usage_key, source_usage_key)
        parent = self.get_item_from_modulestore(parent_usage_key)
        children = parent.children
        if (source_position is None):
            self.assertNotIn(source_usage_key, children, 'source    item    not    expected    in    children    array')
            self.assertEqual(children[(len(children) - 1)], usage_key, 'duplicated    item    not    at    end')
        else:
            self.assertEqual(children[source_position], source_usage_key, 'source    item    at    wrong    position')
            self.assertEqual(children[(source_position + 1)], usage_key, 'duplicated    item    not    ordered    after    source    item')
    verify_order(self.problem_usage_key, self.vert_usage_key, 0)
    verify_order(self.html_usage_key, self.vert_usage_key, 2)
    verify_order(self.vert_usage_key, self.seq_usage_key, 0)
    verify_order(self.seq_usage_key, self.chapter_usage_key, 0)
    verify_order(self.html_usage_key, self.usage_key)
