From c7314a1a12b11a4f65d4f37436d57b4b01bd68d7 Mon Sep 17 00:00:00 2001 From: Anubhav Sinha Date: Sun, 31 Mar 2019 04:59:30 +0530 Subject: [PATCH] Fixes #6181:Removed the exception which occured during clearing search index (#6518) * First * First * Second * Third * Fourth * Reset * Added test * Fixed * Removed GSE * Removed models * removed a line --- core/controllers/admin.py | 4 +++- core/controllers/admin_test.py | 30 +++++++++++++++++++++++++++++ core/domain/search_services_test.py | 16 +++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/core/controllers/admin.py b/core/controllers/admin.py index 40e8298b732d..865ccae8c00d 100644 --- a/core/controllers/admin.py +++ b/core/controllers/admin.py @@ -31,6 +31,7 @@ from core.domain import recommendations_services from core.domain import rights_manager from core.domain import role_services +from core.domain import search_services from core.domain import stats_services from core.domain import topic_domain from core.domain import topic_services @@ -155,7 +156,8 @@ def post(self): self._generate_dummy_explorations( num_dummy_exps_to_generate, num_dummy_exps_to_publish) elif self.payload.get('action') == 'clear_search_index': - exp_services.clear_search_index() + search_services.clear_collection_search_index() + search_services.clear_exploration_search_index() elif self.payload.get('action') == 'save_config_properties': new_config_property_values = self.payload.get( 'new_config_property_values') diff --git a/core/controllers/admin_test.py b/core/controllers/admin_test.py index b3a26f860dd2..545e559aefb3 100644 --- a/core/controllers/admin_test.py +++ b/core/controllers/admin_test.py @@ -15,8 +15,10 @@ """Tests for the admin page.""" from core.controllers import base +from core.domain import collection_services from core.domain import exp_domain from core.domain import exp_services +from core.domain import search_services from core.domain import stats_domain from core.domain import stats_services from core.tests import test_utils @@ -295,3 +297,31 @@ def test_that_handler_raises_exception(self): self.assertEqual( response['error'], 'Exploration \'exp\' does not have \'state name\' state.') + + +class ClearSearchIndexTest(test_utils.GenericTestBase): + """Tests that search index gets cleared.""" + + def test_clear_search_index(self): + exp_services.load_demo('0') + result_explorations = search_services.search_explorations( + 'Welcome', 2)[0] + self.assertEqual(result_explorations, ['0']) + collection_services.load_demo('0') + result_collections = search_services.search_collections('Welcome', 2)[0] + self.assertEqual(result_collections, ['0']) + self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME) + self.login(self.ADMIN_EMAIL, is_super_admin=True) + response = self.get_html_response('/admin') + csrf_token = self.get_csrf_token_from_response(response) + generated_exps_response = self.post_json( + '/adminhandler', { + 'action': 'clear_search_index' + }, + csrf_token=csrf_token) + self.assertEqual(generated_exps_response, {}) + result_explorations = search_services.search_explorations( + 'Welcome', 2)[0] + self.assertEqual(result_explorations, []) + result_collections = search_services.search_collections('Welcome', 2)[0] + self.assertEqual(result_collections, []) diff --git a/core/domain/search_services_test.py b/core/domain/search_services_test.py index bf82cedc99ec..17c1b64b1fac 100644 --- a/core/domain/search_services_test.py +++ b/core/domain/search_services_test.py @@ -298,3 +298,19 @@ def mock_get_rights(unused_exp_id): search_services.update_exploration_status_in_search(self.EXP_ID) self.assertEqual(delete_docs_counter.times_called, 1) + + def test_clear_exploration_search_index(self): + exp_services.load_demo('0') + result = search_services.search_explorations('Welcome', 2)[0] + self.assertEqual(result, ['0']) + search_services.clear_exploration_search_index() + result = search_services.search_explorations('Welcome', 2)[0] + self.assertEqual(result, []) + + def test_clear_collection_search_index(self): + collection_services.load_demo('0') + result = search_services.search_collections('Welcome', 2)[0] + self.assertEqual(result, ['0']) + search_services.clear_collection_search_index() + result = search_services.search_collections('Welcome', 2)[0] + self.assertEqual(result, [])