diff --git a/lms/djangoapps/social_engagement/engagement.py b/lms/djangoapps/social_engagement/engagement.py index a7d040fd93ae..53cfc7652a27 100644 --- a/lms/djangoapps/social_engagement/engagement.py +++ b/lms/djangoapps/social_engagement/engagement.py @@ -207,6 +207,12 @@ def handle_progress_post_save_signal(sender, instance, **kwargs): get_aggregate_exclusion_user_ids(instance.course_id) )['position'] + if leaderboard_rank == 0: + # quick escape when user is not in the leaderboard + # which means rank = 0. Trouble is 0 < 3, so unfortunately + # the semantics around 0 don't match the logic below + return + # logic for Notification trigger is when a user enters into the Leaderboard leaderboard_size = getattr(settings, 'LEADERBOARD_SIZE', 3) presave_leaderboard_rank = instance.presave_leaderboard_rank if instance.presave_leaderboard_rank else sys.maxint diff --git a/lms/djangoapps/social_engagement/tests/test_engagement.py b/lms/djangoapps/social_engagement/tests/test_engagement.py index 864f5f06cbbc..6739e2625a56 100644 --- a/lms/djangoapps/social_engagement/tests/test_engagement.py +++ b/lms/djangoapps/social_engagement/tests/test_engagement.py @@ -383,3 +383,39 @@ def test_closed_course(self): # shouldn't be anything in there because course is closed leaderboard = StudentSocialEngagementScore.generate_leaderboard(course2.id) self.assertEqual(len(leaderboard), 2) + + def test_no_score(self): + """ + Run the engagement calculation for a user in a course who has no score + """ + + self.assertEqual(get_notifications_count_for_user(self.user.id), 0) + + with patch('social_engagement.engagement._get_user_social_stats') as mock_func: + mock_func.return_value = { + 'num_threads': 0, + 'num_comments': 0, + 'num_replies': 0, + 'num_upvotes': 0, + 'num_thread_followers': 0, + 'num_comments_generated': 0, + } + + update_user_engagement_score(self.course.id, self.user.id) + + leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( + self.course.id, + self.user.id + ) + + self.assertEqual( + leaderboard_position['score'], + 0 + ) + + self.assertEqual( + leaderboard_position['position'], + 0 + ) + + self.assertEqual(get_notifications_count_for_user(self.user.id), 0)