def test_closed_course(self, store): """ Make sure we can force update closed course """ course2 = CourseFactory.create( org='foo', course='bar', run='baz', end=datetime.now(pytz.UTC) - timedelta(days=1), default_store=store ) CourseEnrollment.enroll(self.user, course2.id) CourseEnrollment.enroll(self.user2, course2.id) with patch('social_engagement.engagement._get_course_social_stats') as mock_func: mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(course2.id, compute_if_closed_course=False) # shouldn't be anything in there because course is closed data = StudentSocialEngagementScore.generate_leaderboard(course2.id) self.assertEqual(len(data['queryset']), 0) self.assertEqual(data['course_avg'], 0) mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(course2.id, compute_if_closed_course=True) # shouldn't be anything in there because course is closed data = StudentSocialEngagementScore.generate_leaderboard(course2.id) self.assertEqual(len(data['queryset']), 2) self.assertEqual(data['course_avg'], 85)
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) empty_stats = { 'num_threads': 0, 'num_comments': 0, 'num_replies': 0, 'num_upvotes': 0, 'num_thread_followers': 0, 'num_comments_generated': 0, } with patch('social_engagement.engagement._get_course_social_stats' ) as mock_func: mock_func.return_value = ((user_id, empty_stats) for user_id in self.user_ids) update_course_engagement(self.course.id, self.user.id) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_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)
def test_all_courses(self, store): """ Verifies that we can calculate over all courses """ course2 = CourseFactory.create(org='foo', course='bar', run='baz', default_store=store) CourseEnrollment.enroll(self.user, course2.id) self.assertEqual( CourseEnrollment.objects.filter(course_id=course2.id).count(), 1) for course in (self.course, course2): with patch('social_engagement.engagement._get_course_social_stats' ) as mock_func: mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(course.id) data = StudentSocialEngagementScore.generate_leaderboard( self.course.id) self.assertEqual(len(data['queryset']), 2) self.assertEqual(data['course_avg'], 85) data = StudentSocialEngagementScore.generate_leaderboard(course2.id) self.assertEqual(len(data['queryset']), 1) self.assertEqual(data['course_avg'], 85)
def test_multiple_users(self): """ See if it works with more than one enrollee """ self.assertEqual(get_notifications_count_for_user(self.user.id), 0) self.assertEqual(get_notifications_count_for_user(self.user2.id), 0) stats = ( self.DEFAULT_STATS, { 'num_threads': 2, 'num_comments': 2, 'num_replies': 2, 'num_upvotes': 2, 'num_thread_followers': 2, 'num_comments_generated': 2, } ) with patch('social_engagement.engagement._get_course_social_stats') as mock_func: mock_func.return_value = zip(self.user_ids, stats) update_course_engagement(self.course.id) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_id=self.user.id ) self.assertEqual(leaderboard_position['score'], 85) # user should be in place #2 self.assertEqual(leaderboard_position['position'], 2) self.assertEqual(get_notifications_count_for_user(self.user.id), 1) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_id=self.user2.id ) self.assertEqual( leaderboard_position['score'], 170 ) # user2 should be in place #1 self.assertEqual( leaderboard_position['position'], 1 ) self.assertEqual(get_notifications_count_for_user(self.user2.id), 1)
def test_calc_course(self): """ Verifies that we can calculate the whole course enrollments """ with patch('social_engagement.engagement._get_course_social_stats') as mock_func: mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(self.course.id) data = StudentSocialEngagementScore.generate_leaderboard(self.course.id) self.assertEqual(len(data['queryset']), 2)
def test_calc_course(self): """ Verifies that we can calculate the whole course enrollments """ with patch('social_engagement.engagement._get_course_social_stats' ) as mock_func: mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(self.course.id) data = StudentSocialEngagementScore.generate_leaderboard( self.course.id) self.assertEqual(len(data['queryset']), 2)
def test_update_user_engagement_score(self): """ Run the engagement calculation for a user in a course """ self.assertEqual(get_notifications_count_for_user(self.user.id), 0) with patch('social_engagement.engagement._get_course_social_stats' ) as mock_func: mock_func.return_value = ((self.user.id, self.DEFAULT_STATS), ) update_course_engagement(self.course.id) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_id=self.user.id) self.assertEqual(leaderboard_position['score'], 85) self.assertEqual(leaderboard_position['position'], 1) self.assertEqual(get_notifications_count_for_user(self.user.id), 1)
def test_update_user_engagement_score(self): """ Run the engagement calculation for a user in a course """ self.assertEqual(get_notifications_count_for_user(self.user.id), 0) with patch('social_engagement.engagement._get_course_social_stats') as mock_func: mock_func.return_value = ((self.user.id, self.DEFAULT_STATS),) update_course_engagement(self.course.id) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_id=self.user.id ) self.assertEqual(leaderboard_position['score'], 85) self.assertEqual(leaderboard_position['position'], 1) self.assertEqual(get_notifications_count_for_user(self.user.id), 1)
def test_multiple_users(self): """ See if it works with more than one enrollee """ self.assertEqual(get_notifications_count_for_user(self.user.id), 0) self.assertEqual(get_notifications_count_for_user(self.user2.id), 0) stats = (self.DEFAULT_STATS, { 'num_threads': 2, 'num_comments': 2, 'num_replies': 2, 'num_upvotes': 2, 'num_thread_followers': 2, 'num_comments_generated': 2, }) with patch('social_engagement.engagement._get_course_social_stats' ) as mock_func: mock_func.return_value = zip(self.user_ids, stats) update_course_engagement(self.course.id) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_id=self.user.id) self.assertEqual(leaderboard_position['score'], 85) # user should be in place #2 self.assertEqual(leaderboard_position['position'], 2) self.assertEqual(get_notifications_count_for_user(self.user.id), 1) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_id=self.user2.id) self.assertEqual(leaderboard_position['score'], 170) # user2 should be in place #1 self.assertEqual(leaderboard_position['position'], 1) self.assertEqual(get_notifications_count_for_user(self.user2.id), 1)
def test_closed_course(self, store): """ Make sure we can force update closed course """ course2 = CourseFactory.create(org='foo', course='bar', run='baz', end=datetime.now(pytz.UTC) - timedelta(days=1), default_store=store) CourseEnrollment.enroll(self.user, course2.id) CourseEnrollment.enroll(self.user2, course2.id) with patch('social_engagement.engagement._get_course_social_stats' ) as mock_func: mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(course2.id, compute_if_closed_course=False) # shouldn't be anything in there because course is closed data = StudentSocialEngagementScore.generate_leaderboard( course2.id) self.assertEqual(len(data['queryset']), 0) self.assertEqual(data['course_avg'], 0) mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(course2.id, compute_if_closed_course=True) # shouldn't be anything in there because course is closed data = StudentSocialEngagementScore.generate_leaderboard( course2.id) self.assertEqual(len(data['queryset']), 2) self.assertEqual(data['course_avg'], 85)
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) empty_stats = { 'num_threads': 0, 'num_comments': 0, 'num_replies': 0, 'num_upvotes': 0, 'num_thread_followers': 0, 'num_comments_generated': 0, } with patch('social_engagement.engagement._get_course_social_stats') as mock_func: mock_func.return_value = ((user_id, empty_stats) for user_id in self.user_ids) update_course_engagement(self.course.id, self.user.id) leaderboard_position = StudentSocialEngagementScore.get_user_leaderboard_position( self.course.id, user_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)
def test_all_courses(self, store): """ Verifies that we can calculate over all courses """ course2 = CourseFactory.create(org='foo', course='bar', run='baz', default_store=store) CourseEnrollment.enroll(self.user, course2.id) self.assertEqual(CourseEnrollment.objects.filter(course_id=course2.id).count(), 1) for course in (self.course, course2): with patch('social_engagement.engagement._get_course_social_stats') as mock_func: mock_func.return_value = ((user_id, self.DEFAULT_STATS) for user_id in self.user_ids) # update whole course and re-calc update_course_engagement(course.id) data = StudentSocialEngagementScore.generate_leaderboard(self.course.id) self.assertEqual(len(data['queryset']), 2) self.assertEqual(data['course_avg'], 85) data = StudentSocialEngagementScore.generate_leaderboard(course2.id) self.assertEqual(len(data['queryset']), 1) self.assertEqual(data['course_avg'], 85)
def task_compute_social_scores_in_course(course_id): """ Task to compute social scores in course """ course_key = CourseKey.from_string(course_id) course = modulestore().get_course(course_key, depth=None) if course: score_update_count = update_course_engagement( course_key, compute_if_closed_course=True, course_descriptor=course ) log.info("Social scores updated for %d users in course %s", score_update_count or 0, course_id) else: log.info("Course with course id %s does not exist", course_id)