'Tests that children are automatically refreshed if capa type field changes'

@patch('xmodule.library_tools.SearchEngine.get_search_engine', Mock(return_value=None, autospec=True))
def test_refreshes_children_if_capa_type_change(self):

    (name1, name2) = ('Option    Problem', 'Multiple    Choice    Problem')
    ItemFactory.create(category='problem', parent_location=self.library.location, user_id=self.user.id, publish_item=False, display_name=name1, data='<problem><optionresponse></optionresponse></problem>')
    ItemFactory.create(category='problem', parent_location=self.library.location, user_id=self.user.id, publish_item=False, display_name=name2, data='<problem><multiplechoiceresponse></multiplechoiceresponse></problem>')
    with modulestore().default_store(ModuleStoreEnum.Type.split):
        course = CourseFactory.create()
    lc_block = self._add_library_content_block(course, self.lib_key)
    lc_block = self._refresh_children(lc_block)
    self.assertEqual(len(lc_block.children), 2)
    resp = self._update_item(lc_block.location, {'capa_type': 'optionresponse'})
    self.assertEqual(resp.status_code, 200)
    lc_block = modulestore().get_item(lc_block.location)
    self.assertEqual(len(lc_block.children), 1)
    html_block = modulestore().get_item(lc_block.children[0])
    self.assertEqual(html_block.display_name, name1)
    resp = self._update_item(lc_block.location, {'capa_type': 'multiplechoiceresponse'})
    self.assertEqual(resp.status_code, 200)
    lc_block = modulestore().get_item(lc_block.location)
    self.assertEqual(len(lc_block.children), 1)
    html_block = modulestore().get_item(lc_block.children[0])
    self.assertEqual(html_block.display_name, name2)
