def handle(self, *args, **options): """ By convention set by Django developers, this method actually executes command's actions. So, there could be no better docstring than emphasize this once again. """ course_ids = options['course_ids'] all_option = options['all'] setup_option = options['setup'] index_all_courses_option = all_option or setup_option if (not len(course_ids) and not index_all_courses_option) or ( len(course_ids) and index_all_courses_option): # lint-amnesty, pylint: disable=len-as-condition raise CommandError( "reindex_course requires one or more <course_id>s OR the --all or --setup flags." ) store = modulestore() if index_all_courses_option: index_names = (CoursewareSearchIndexer.INDEX_NAME, CourseAboutSearchIndexer.INDEX_NAME) if setup_option: for index_name in index_names: try: searcher = SearchEngine.get_search_engine(index_name) except exceptions.ElasticsearchException as exc: logging.exception(u'Search Engine error - %s', exc) return index_exists = searcher._es.indices.exists( index=index_name) # pylint: disable=protected-access index_mapping = searcher._es.indices.get_mapping( # pylint: disable=protected-access index=index_name, ) if index_exists else {} if index_exists and index_mapping: return # if reindexing is done during devstack setup step, don't prompt the user if setup_option or query_yes_no(self.CONFIRMATION_PROMPT, default="no"): # in case of --setup or --all, get the list of course keys from all courses # that are stored in the modulestore course_keys = [ course.id for course in modulestore().get_courses() ] else: return else: # in case course keys are provided as arguments course_keys = list(map(self._parse_course_key, course_ids)) for course_key in course_keys: try: CoursewareSearchIndexer.do_course_reindex(store, course_key) except Exception as exc: # lint-amnesty, pylint: disable=broad-except logging.exception( 'Error indexing course %s due to the error: %s', course_key, exc)
def test_indexing_no_item(self, mock_get_course): """ Test system logs an error if no item found. """ # set mocked exception response err = ItemNotFoundError mock_get_course.return_value = err # Start manual reindex and check error in response with self.assertRaises(SearchIndexingError): CoursewareSearchIndexer.do_course_reindex(modulestore(), self.course.id)
def test_indexing_seq_error_responses(self, mock_index_dictionary): """ Test do_course_reindex response with mocked error data for sequence """ # results are indexed because they are published from ItemFactory response = perform_search("unique", user=self.user, size=10, from_=0, course_id=str(self.course.id)) self.assertEqual(response['total'], 1) # set mocked exception response err = Exception mock_index_dictionary.return_value = err # Start manual reindex and check error in response with self.assertRaises(SearchIndexingError): CoursewareSearchIndexer.do_course_reindex(modulestore(), self.course.id)
def test_indexing_responses(self): """ Test do_course_reindex response with real data """ # results are indexed because they are published from ItemFactory response = perform_search("unique", user=self.user, size=10, from_=0, course_id=str(self.course.id)) self.assertEqual(response['total'], 1) # Start manual reindex CoursewareSearchIndexer.do_course_reindex(modulestore(), self.course.id) # Check results are the same following reindex response = perform_search("unique", user=self.user, size=10, from_=0, course_id=str(self.course.id)) self.assertEqual(response['total'], 1)