'Test that references types get deserialized correctly'

@XBlock.register_temp_plugin(ReferenceTestXBlock, 'ref_test')
def test_reference_converters(self):

    course_key = CourseKey.from_string('edX/toy/2012_Fall')
    def setup_test():
        course = self.draft_store.get_course(course_key)
        p1ele = self.draft_store.create_item(99, course_key, 'problem', block_id='p1', runtime=course.runtime)
        p2ele = self.draft_store.create_item(99, course_key, 'problem', block_id='p2', runtime=course.runtime)
        self.refloc = course.id.make_usage_key('ref_test', 'ref_test')
        self.draft_store.create_item(99, self.refloc.course_key, self.refloc.block_type, block_id=self.refloc.block_id, runtime=course.runtime, fields={'reference_link': p1ele.location, 'reference_list': [p1ele.location, p2ele.location], 'reference_dict': {'p1': p1ele.location, 'p2': p2ele.location}, 'children': [p1ele.location, p2ele.location]})
    def check_xblock_fields():
        def check_children(xblock):
            for child in xblock.children:
                assert_is_instance(child, UsageKey)
        course = self.draft_store.get_course(course_key)
        check_children(course)
        refele = self.draft_store.get_item(self.refloc)
        check_children(refele)
        assert_is_instance(refele.reference_link, UsageKey)
        assert_greater(len(refele.reference_list), 0)
        for ref in refele.reference_list:
            assert_is_instance(ref, UsageKey)
        assert_greater(len(refele.reference_dict), 0)
        for ref in refele.reference_dict.itervalues():
            assert_is_instance(ref, UsageKey)
    def check_mongo_fields():
        def get_item(location):
            return self.draft_store._find_one(as_draft(location))
        def check_children(payload):
            for child in payload['definition']['children']:
                assert_is_instance(child, basestring)
        refele = get_item(self.refloc)
        check_children(refele)
        assert_is_instance(refele['definition']['data']['reference_link'], basestring)
        assert_greater(len(refele['definition']['data']['reference_list']), 0)
        for ref in refele['definition']['data']['reference_list']:
            assert_is_instance(ref, basestring)
        assert_greater(len(refele['metadata']['reference_dict']), 0)
        for ref in refele['metadata']['reference_dict'].itervalues():
            assert_is_instance(ref, basestring)
    setup_test()
    check_xblock_fields()
    check_mongo_fields()
