def test_basic_computation(self): recommendations_services.update_topic_similarities( 'Art,Biology,Chemistry\n' '1.0,0.2,0.1\n' '0.2,1.0,0.8\n' '0.1,0.8,1.0') with self.swap( jobs_registry, 'ALL_CONTINUOUS_COMPUTATION_MANAGERS', self.ALL_CONTINUOUS_COMPUTATION_MANAGERS_FOR_TESTS): ModifiedExplorationRecommendationsAggregator.start_computation() self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_4')) self.assertEqual( recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
def test_recommendations_after_changes_in_rights(self): with self.swap( jobs_registry, 'ALL_CONTINUOUS_COMPUTATION_MANAGERS', self.ALL_CONTINUOUS_COMPUTATION_MANAGERS_FOR_TESTS): ModifiedExplorationRecommendationsAggregator.start_computation() self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) rights_manager.unpublish_exploration(self.ADMIN_ID, 'exp_id_4') ModifiedExplorationRecommendationsAggregator.stop_computation( self.ADMIN_ID) ModifiedExplorationRecommendationsAggregator.start_computation() self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommendations, ['exp_id_2', 'exp_id_3'])
def test_basic_computation(self): recommendations_services.update_topic_similarities( 'Art,Biology,Chemistry\n' '1.0,0.2,0.1\n' '0.2,1.0,0.8\n' '0.1,0.8,1.0') with self.swap( jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS ): self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_4')) self.assertEqual( recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
def test_recommendations_after_changes_in_rights(self): with self.swap( jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS ): self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) rights_manager.unpublish_exploration(self.admin_id, 'exp_id_4') self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommendations, ['exp_id_2', 'exp_id_3'])
def test_recommendations_after_changes_in_rights(self): with self.swap( jobs_registry, 'ALL_CONTINUOUS_COMPUTATION_MANAGERS', self.ALL_CC_MANAGERS_FOR_TESTS ): ModifiedExplorationRecommendationsAggregator.start_computation() self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) rights_manager.unpublish_exploration(self.admin_id, 'exp_id_4') ModifiedExplorationRecommendationsAggregator.stop_computation( self.admin_id) ModifiedExplorationRecommendationsAggregator.start_computation() self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommendations, ['exp_id_2', 'exp_id_3'])
def test_basic_computation(self): recommendations_services.update_topic_similarities( 'Art,Biology,Chemistry\n' '1.0,0.2,0.1\n' '0.2,1.0,0.8\n' '0.1,0.8,1.0') with self.swap( jobs_registry, 'ALL_CONTINUOUS_COMPUTATION_MANAGERS', self.ALL_CC_MANAGERS_FOR_TESTS ): ModifiedExplorationRecommendationsAggregator.start_computation() self.assertEqual( self.count_jobs_in_taskqueue( queue_name=taskqueue_services.QUEUE_NAME_DEFAULT), 1) self.process_and_flush_pending_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_4')) self.assertEqual( recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
def test_recommendations_after_changes_in_rights(self): with self.swap( jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS ): self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_mapreduce_taskqueue( taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 1) self.process_and_flush_pending_mapreduce_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) rights_manager.unpublish_exploration(self.admin, 'exp_id_4') self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_mapreduce_taskqueue( taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 1) self.process_and_flush_pending_mapreduce_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommendations, ['exp_id_2', 'exp_id_3'])
def test_basic_computation(self): recommendations_services.update_topic_similarities( 'Art,Biology,Chemistry\n' '1.0,0.2,0.1\n' '0.2,1.0,0.8\n' '0.1,0.8,1.0') with self.swap( jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS ): self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_mapreduce_taskqueue( taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 1) self.process_and_flush_pending_mapreduce_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, ['exp_id_4', 'exp_id_2', 'exp_id_3']) recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_4')) self.assertEqual( recommendations, ['exp_id_1', 'exp_id_2', 'exp_id_3'])
def test_get_and_set_exploration_recommendations(self): recommended_exp_ids = ["exp_id_2", "exp_id_3"] recommendations_services.set_recommendations("exp_id_1", recommended_exp_ids) saved_recommendation_ids = recommendations_services.get_exploration_recommendations("exp_id_1") self.assertEqual(recommended_exp_ids, saved_recommendation_ids) recommended_exp_ids = ["exp_id_3"] recommendations_services.set_recommendations("exp_id_1", recommended_exp_ids) saved_recommendation_ids = recommendations_services.get_exploration_recommendations("exp_id_1") self.assertEqual(recommended_exp_ids, saved_recommendation_ids)
def test_get_and_set_exploration_recommendations(self): recommended_exp_ids = ['exp_id_2', 'exp_id_3'] recommendations_services.set_recommendations('exp_id_1', recommended_exp_ids) saved_recommendation_ids = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommended_exp_ids, saved_recommendation_ids) recommended_exp_ids = ['exp_id_3'] recommendations_services.set_recommendations('exp_id_1', recommended_exp_ids) saved_recommendation_ids = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommended_exp_ids, saved_recommendation_ids)
def test_recommendations_with_invalid_exploration_summaries_would_be_empty( self): def _mock_get_non_private_exploration_summaries(): """Return an invalid exploration summary dict.""" return {'new_exp_id': 'new_exploration_summary'} with self.swap( jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS ): # We need to swap here to make the recommendations an empty list # (since 'get_exploration_recommendations()' returns a list of ids # of at most 10 recommended explorations to play after completing # the exploration). with self.swap( exp_services, 'get_non_private_exploration_summaries', _mock_get_non_private_exploration_summaries): self.job_class.enqueue(self.job_class.create_new()) self.assertEqual( self.count_jobs_in_mapreduce_taskqueue( taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 1) self.process_and_flush_pending_mapreduce_tasks() recommendations = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual( recommendations, [])
def test_get_and_set_exploration_recommendations(self): recommended_exp_ids = ['exp_id_2', 'exp_id_3'] recommendations_services.set_recommendations( 'exp_id_1', recommended_exp_ids) saved_recommendation_ids = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommended_exp_ids, saved_recommendation_ids) recommended_exp_ids = ['exp_id_3'] recommendations_services.set_recommendations( 'exp_id_1', recommended_exp_ids) saved_recommendation_ids = ( recommendations_services.get_exploration_recommendations( 'exp_id_1')) self.assertEqual(recommended_exp_ids, saved_recommendation_ids)
def get(self, exploration_id): """Handles GET requests.""" self.values.update({ 'recommended_exp_ids': ( recommendations_services.get_exploration_recommendations( exploration_id)) }) self.render_json(self.values)
def get(self, exploration_id): """Handles GET requests.""" self.values.update({ 'recommended_exp_ids': ( recommendations_services.get_exploration_recommendations( exploration_id)) }) self.render_json(self.values)
def get(self, exploration_id): """Handles GET requests.""" collection_id = self.request.get('collection_id') include_system_recommendations = self.request.get( 'include_system_recommendations') try: author_recommended_exp_ids = json.loads( self.request.get('stringified_author_recommended_ids')) except Exception: raise self.PageNotFoundException auto_recommended_exp_ids = [] if self.user_id and collection_id: next_exp_ids_in_collection = ( collection_services. get_next_exploration_ids_to_complete_by_user( # pylint: disable=line-too-long self.user_id, collection_id)) auto_recommended_exp_ids = list( set(next_exp_ids_in_collection) - set(author_recommended_exp_ids)) else: next_exp_ids_in_collection = [] if collection_id: collection = collection_services.get_collection_by_id( collection_id) next_exp_ids_in_collection = ( collection.get_next_exploration_ids_in_sequence( exploration_id)) if next_exp_ids_in_collection: auto_recommended_exp_ids = list( set(next_exp_ids_in_collection) - set(author_recommended_exp_ids)) elif include_system_recommendations: system_chosen_exp_ids = ( recommendations_services.get_exploration_recommendations( exploration_id)) filtered_exp_ids = list( set(system_chosen_exp_ids) - set(author_recommended_exp_ids)) auto_recommended_exp_ids = random.sample( filtered_exp_ids, min(MAX_SYSTEM_RECOMMENDATIONS, len(filtered_exp_ids))) self.values.update({ 'summaries': (summary_services.get_displayable_exp_summary_dicts_matching_ids( author_recommended_exp_ids + auto_recommended_exp_ids)), }) self.render_json(self.values)
def get(self, exploration_id): """Handles GET requests.""" collection_id = self.request.get('collection_id') include_system_recommendations = self.request.get( 'include_system_recommendations') try: author_recommended_exp_ids = json.loads(self.request.get( 'stringified_author_recommended_ids')) except Exception: raise self.PageNotFoundException auto_recommended_exp_ids = [] if self.user_id and collection_id: next_exp_ids_in_collection = ( collection_services.get_next_exploration_ids_to_complete_by_user( # pylint: disable=line-too-long self.user_id, collection_id)) auto_recommended_exp_ids = list( set(next_exp_ids_in_collection) - set(author_recommended_exp_ids)) else: next_exp_ids_in_collection = [] if collection_id: collection = collection_services.get_collection_by_id( collection_id) next_exp_ids_in_collection = ( collection.get_next_exploration_ids_in_sequence( exploration_id)) if next_exp_ids_in_collection: auto_recommended_exp_ids = list( set(next_exp_ids_in_collection) - set(author_recommended_exp_ids)) elif include_system_recommendations: system_chosen_exp_ids = ( recommendations_services.get_exploration_recommendations( exploration_id)) filtered_exp_ids = list( set(system_chosen_exp_ids) - set(author_recommended_exp_ids)) auto_recommended_exp_ids = random.sample( filtered_exp_ids, min(MAX_SYSTEM_RECOMMENDATIONS, len(filtered_exp_ids))) self.values.update({ 'summaries': ( summary_services.get_displayable_exp_summary_dicts_matching_ids( author_recommended_exp_ids + auto_recommended_exp_ids)), }) self.render_json(self.values)
def get(self, exploration_id): """Handles GET requests.""" collection_id = self.request.get('collection_id') recommended_exp_ids = [] if self.user_id and collection_id: recommended_exp_ids = ( collection_services. get_next_exploration_ids_to_complete_by_user( self.user_id, collection_id)) else: recommended_exp_ids = ( recommendations_services.get_exploration_recommendations( exploration_id)) self.values.update({'recommended_exp_ids': recommended_exp_ids}) self.render_json(self.values)
def get(self, exploration_id): """Handles GET requests.""" collection_id = self.request.get('collection_id') recommended_exp_ids = [] if self.user_id and collection_id: recommended_exp_ids = ( collection_services.get_next_exploration_ids_to_complete_by_user( self.user_id, collection_id)) else: recommended_exp_ids = ( recommendations_services.get_exploration_recommendations( exploration_id)) self.values.update({ 'recommended_exp_ids': recommended_exp_ids }) self.render_json(self.values)