def test_rating_assignation(self): """Check ratings are correctly assigned to an exploration""" exp_services.save_new_exploration( self.EXP_ID, exp_domain.Exploration.create_default_exploration( self.EXP_ID, 'A title', 'A category')) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), { '1': 0, '2': 0, '3': 0, '4': 0, '5': 0 }) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), None) rating_services.assign_rating_to_exploration(self.USER_ID_1, self.EXP_ID, 2) rating_services.assign_rating_to_exploration(self.USER_ID_2, self.EXP_ID, 4) rating_services.assign_rating_to_exploration(self.USER_ID_1, self.EXP_ID, 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, self.EXP_ID), 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), { '1': 0, '2': 0, '3': 1, '4': 1, '5': 0 }) rating_services.assign_rating_to_exploration(self.USER_ID_1, self.EXP_ID, 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), { '1': 0, '2': 0, '3': 0, '4': 2, '5': 0 })
def test_rating_assignations_do_not_conflict(self) -> None: """Check that ratings of different explorations are independent.""" exp_id_a: Final = 'exp_id_A' exp_id_b: Final = 'exp_id_B' exp_services.save_new_exploration( # type: ignore[no-untyped-call] exp_id_a, exp_domain.Exploration.create_default_exploration( exp_id_a)) # type: ignore[no-untyped-call] exp_services.save_new_exploration( # type: ignore[no-untyped-call] exp_id_b, exp_domain.Exploration.create_default_exploration( exp_id_b)) # type: ignore[no-untyped-call] rating_services.assign_rating_to_exploration(self.USER_ID_1, exp_id_a, 1) rating_services.assign_rating_to_exploration(self.USER_ID_1, exp_id_b, 3) rating_services.assign_rating_to_exploration(self.USER_ID_2, exp_id_a, 2) rating_services.assign_rating_to_exploration(self.USER_ID_2, exp_id_b, 5) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, exp_id_a), 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, exp_id_b), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, exp_id_a), 2) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, exp_id_b), 5) self.assertEqual( rating_services.get_overall_ratings_for_exploration(exp_id_a), { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0 }) self.assertEqual( rating_services.get_overall_ratings_for_exploration(exp_id_b), { '1': 0, '2': 0, '3': 1, '4': 0, '5': 1 })
def test_rating_assignations_do_not_conflict(self): """Check that ratings of different explorations are independant.""" exp_id_a = 'exp_id_A' exp_id_b = 'exp_id_B' exp_services.save_new_exploration( exp_id_a, exp_domain.Exploration.create_default_exploration( exp_id_a, 'A title', 'A category')) exp_services.save_new_exploration( exp_id_b, exp_domain.Exploration.create_default_exploration( exp_id_b, 'A title', 'A category')) rating_services.assign_rating_to_exploration(self.USER_ID_1, exp_id_a, 1) rating_services.assign_rating_to_exploration(self.USER_ID_1, exp_id_b, 3) rating_services.assign_rating_to_exploration(self.USER_ID_2, exp_id_a, 2) rating_services.assign_rating_to_exploration(self.USER_ID_2, exp_id_b, 5) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, exp_id_a), 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, exp_id_b), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, exp_id_a), 2) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, exp_id_b), 5) self.assertEqual( rating_services.get_overall_ratings_for_exploration(exp_id_a), { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0 }) self.assertEqual( rating_services.get_overall_ratings_for_exploration(exp_id_b), { '1': 0, '2': 0, '3': 1, '4': 0, '5': 1 })
def test_rating_assignations_do_not_conflict(self): """Check that ratings of different explorations are independant.""" EXP_ID_A = 'exp_id_A' EXP_ID_B = 'exp_id_B' self.exploration = exp_domain.Exploration.create_default_exploration( EXP_ID_A, 'A title', 'A category') exp_services.save_new_exploration(EXP_ID_A, self.exploration) self.exploration = exp_domain.Exploration.create_default_exploration( EXP_ID_B, 'A title', 'A category') exp_services.save_new_exploration(EXP_ID_B, self.exploration) rating_services.assign_rating_to_exploration(self.USER_ID_1, EXP_ID_A, 1) rating_services.assign_rating_to_exploration(self.USER_ID_1, EXP_ID_B, 3) rating_services.assign_rating_to_exploration(self.USER_ID_2, EXP_ID_A, 2) rating_services.assign_rating_to_exploration(self.USER_ID_2, EXP_ID_B, 5) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, EXP_ID_A), 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, EXP_ID_B), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, EXP_ID_A), 2) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, EXP_ID_B), 5) self.assertEqual( rating_services.get_overall_ratings_for_exploration(EXP_ID_A), { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0 }) self.assertEqual( rating_services.get_overall_ratings_for_exploration(EXP_ID_B), { '1': 0, '2': 0, '3': 1, '4': 0, '5': 1 })
def test_rating_assignation_with_no_exploration_summary_ratings( self) -> None: def _mock_get_exploration_summary_by_id( exp_id: str, strict: bool = True) -> exp_models.ExpSummaryModel: """Assign None to exploration summary ratings.""" exp_summary_model = exp_models.ExpSummaryModel.get(exp_id, strict=strict) # Ruling out the possibility of None for mypy type checking. assert exp_summary_model is not None exp_summary_model.ratings = None return exp_summary_model with self.swap(exp_fetchers, 'get_exploration_summary_by_id', _mock_get_exploration_summary_by_id): exp_services.save_new_exploration( # type: ignore[no-untyped-call] 'exp_id_a', exp_domain.Exploration.create_default_exploration( 'exp_id_a')) # type: ignore[no-untyped-call] rating_services.assign_rating_to_exploration( self.USER_ID_1, 'exp_id_a', 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, 'exp_id_a'), 1)
def test_rating_assignation(self): """Check ratings are correctly assigned to an exploration""" exp_services.save_new_exploration( self.EXP_ID, exp_domain.Exploration.create_default_exploration(self.EXP_ID)) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0}) exp_summary = exp_services.get_exploration_summary_by_id(self.EXP_ID) self.assertEqual( exp_summary.scaled_average_rating, 0) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), None) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 2) rating_services.assign_rating_to_exploration( self.USER_ID_2, self.EXP_ID, 4) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 3) exp_summary = exp_services.get_exploration_summary_by_id(self.EXP_ID) self.assertAlmostEqual( exp_summary.scaled_average_rating, 1.5667471839848, places=4) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, self.EXP_ID), 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 1, '4': 1, '5': 0}) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 0, '4': 2, '5': 0})
def test_rating_assignation(self): """Check ratings are correctly assigned to an exploration.""" exp_services.save_new_exploration( self.EXP_ID, exp_domain.Exploration.create_default_exploration(self.EXP_ID)) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0}) exp_summary = exp_services.get_exploration_summary_by_id(self.EXP_ID) self.assertEqual( exp_summary.scaled_average_rating, 0) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), None) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 2) rating_services.assign_rating_to_exploration( self.USER_ID_2, self.EXP_ID, 4) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 3) exp_summary = exp_services.get_exploration_summary_by_id(self.EXP_ID) self.assertAlmostEqual( exp_summary.scaled_average_rating, 1.5667471839848, places=4) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, self.EXP_ID), 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 1, '4': 1, '5': 0}) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 0, '4': 2, '5': 0})
def test_rating_assignations_do_not_conflict(self): """Check that ratings of different explorations are independant.""" exp_id_a = 'exp_id_A' exp_id_b = 'exp_id_B' exp_services.save_new_exploration( exp_id_a, exp_domain.Exploration.create_default_exploration( exp_id_a, 'A title', 'A category')) exp_services.save_new_exploration( exp_id_b, exp_domain.Exploration.create_default_exploration( exp_id_b, 'A title', 'A category')) rating_services.assign_rating_to_exploration( self.USER_ID_1, exp_id_a, 1) rating_services.assign_rating_to_exploration( self.USER_ID_1, exp_id_b, 3) rating_services.assign_rating_to_exploration( self.USER_ID_2, exp_id_a, 2) rating_services.assign_rating_to_exploration( self.USER_ID_2, exp_id_b, 5) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, exp_id_a), 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, exp_id_b), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, exp_id_a), 2) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, exp_id_b), 5) self.assertEqual( rating_services.get_overall_ratings_for_exploration(exp_id_a), {'1': 1, '2': 1, '3': 0, '4': 0, '5': 0}) self.assertEqual( rating_services.get_overall_ratings_for_exploration(exp_id_b), {'1': 0, '2': 0, '3': 1, '4': 0, '5': 1})
def get(self, exploration_id): """Handles GET requests.""" self.values.update({ 'overall_ratings': rating_services.get_overall_ratings_for_exploration( exploration_id), 'user_rating': ( rating_services.get_user_specific_rating_for_exploration( self.user_id, exploration_id) if self.user_id else None) }) self.render_json(self.values)
def test_rating_assignations_do_not_conflict(self): """Check that ratings of different explorations are independant.""" EXP_ID_A = 'exp_id_A' EXP_ID_B = 'exp_id_B' self.exploration = exp_domain.Exploration.create_default_exploration( EXP_ID_A, 'A title', 'A category') exp_services.save_new_exploration(EXP_ID_A, self.exploration) self.exploration = exp_domain.Exploration.create_default_exploration( EXP_ID_B, 'A title', 'A category') exp_services.save_new_exploration(EXP_ID_B, self.exploration) rating_services.assign_rating_to_exploration( self.USER_ID_1, EXP_ID_A, 1) rating_services.assign_rating_to_exploration( self.USER_ID_1, EXP_ID_B, 3) rating_services.assign_rating_to_exploration( self.USER_ID_2, EXP_ID_A, 2) rating_services.assign_rating_to_exploration( self.USER_ID_2, EXP_ID_B, 5) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, EXP_ID_A), 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, EXP_ID_B), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, EXP_ID_A), 2) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, EXP_ID_B), 5) self.assertEqual( rating_services.get_overall_ratings_for_exploration(EXP_ID_A), {'1': 1, '2': 1, '3': 0, '4': 0, '5': 0}) self.assertEqual( rating_services.get_overall_ratings_for_exploration(EXP_ID_B), {'1': 0, '2': 0, '3': 1, '4': 0, '5': 1})
def test_rating_assignation(self): """Check ratings are correctly assigned to an exploration""" exp_services.save_new_exploration( self.EXP_ID, exp_domain.Exploration.create_default_exploration( self.EXP_ID, 'A title', 'A category')) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0}) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), None) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 2) rating_services.assign_rating_to_exploration( self.USER_ID_2, self.EXP_ID, 4) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, self.EXP_ID), 3) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_2, self.EXP_ID), 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 1, '4': 1, '5': 0}) rating_services.assign_rating_to_exploration( self.USER_ID_1, self.EXP_ID, 4) self.assertEqual( rating_services.get_overall_ratings_for_exploration(self.EXP_ID), {'1': 0, '2': 0, '3': 0, '4': 2, '5': 0})
def test_rating_assignation_with_no_exploration_summary_ratings(self): def _mock_get_exploration_summary_by_id(exp_id): """Assign None to exploration summary ratings.""" exp_summary_model = exp_models.ExpSummaryModel.get(exp_id) exp_summary_model.ratings = None return exp_summary_model with self.swap(exp_fetchers, 'get_exploration_summary_by_id', _mock_get_exploration_summary_by_id): exp_services.save_new_exploration( 'exp_id_a', exp_domain.Exploration.create_default_exploration('exp_id_a')) rating_services.assign_rating_to_exploration( self.USER_ID_1, 'exp_id_a', 1) self.assertEqual( rating_services.get_user_specific_rating_for_exploration( self.USER_ID_1, 'exp_id_a'), 1)