-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from edx/fix/foldit-leaderboard
Fix/foldit leaderboard
- Loading branch information
Showing
3 changed files
with
80 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,14 @@ | |
import logging | ||
from functools import partial | ||
|
||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
from django.test.client import RequestFactory | ||
from django.core.urlresolvers import reverse | ||
|
||
from foldit.views import foldit_ops, verify_code | ||
from foldit.models import PuzzleComplete, Score | ||
from student.models import UserProfile, unique_id_for_user | ||
from student.models import unique_id_for_user | ||
from student.tests.factories import CourseEnrollmentFactory, UserFactory, UserProfileFactory | ||
|
||
from datetime import datetime, timedelta | ||
from pytz import UTC | ||
|
@@ -23,17 +23,25 @@ def setUp(self): | |
self.factory = RequestFactory() | ||
self.url = reverse('foldit_ops') | ||
|
||
pwd = 'abc' | ||
self.user = User.objects.create_user('testuser', '[email protected]', pwd) | ||
self.user2 = User.objects.create_user('testuser2', '[email protected]', pwd) | ||
self.unique_user_id = unique_id_for_user(self.user) | ||
self.unique_user_id2 = unique_id_for_user(self.user2) | ||
self.course_id = 'course/id/1' | ||
self.course_id2 = 'course/id/2' | ||
|
||
self.user = UserFactory.create() | ||
self.user2 = UserFactory.create() | ||
|
||
self.course_enrollment = CourseEnrollmentFactory.create( | ||
user=self.user, course_id=self.course_id | ||
) | ||
self.course_enrollment2 = CourseEnrollmentFactory.create( | ||
user=self.user2, course_id=self.course_id2 | ||
) | ||
|
||
now = datetime.now(UTC) | ||
self.tomorrow = now + timedelta(days=1) | ||
self.yesterday = now - timedelta(days=1) | ||
|
||
UserProfile.objects.create(user=self.user) | ||
UserProfile.objects.create(user=self.user2) | ||
self.user.profile | ||
self.user2.profile | ||
|
||
def make_request(self, post_data, user=None): | ||
request = self.factory.post(self.url, post_data) | ||
|
@@ -150,6 +158,38 @@ def test_SetPlayerPuzzleScores_multiple(self): | |
delta=0.5 | ||
) | ||
|
||
def test_SetPlayerPuzzleScores_multiplecourses(self): | ||
puzzle_id = "1" | ||
|
||
player1_score = 0.05 | ||
player2_score = 0.06 | ||
|
||
course_list_1 = [self.course_id] | ||
course_list_2 = [self.course_id2] | ||
|
||
response1 = self.make_puzzle_score_request( | ||
puzzle_id, player1_score, self.user | ||
) | ||
course_1_top_10 = Score.get_tops_n(10, puzzle_id, course_list_1) | ||
course_2_top_10 = Score.get_tops_n(10, puzzle_id, course_list_2) | ||
total_top_10 = Score.get_tops_n(10, puzzle_id) | ||
|
||
# player1 should now be in the top 10 of course 1 and not in course 2 | ||
self.assertEqual(len(course_1_top_10), 1) | ||
self.assertEqual(len(course_2_top_10), 0) | ||
self.assertEqual(len(total_top_10), 1) | ||
|
||
response2 = self.make_puzzle_score_request( | ||
puzzle_id, player2_score, self.user2 | ||
) | ||
course_2_top_10 = Score.get_tops_n(10, puzzle_id, course_list_2) | ||
total_top_10 = Score.get_tops_n(10, puzzle_id) | ||
|
||
# player2 should now be in the top 10 of course 2 and not in course 1 | ||
self.assertEqual(len(course_1_top_10), 1) | ||
self.assertEqual(len(course_2_top_10), 1) | ||
self.assertEqual(len(total_top_10), 2) | ||
|
||
def test_SetPlayerPuzzleScores_manyplayers(self): | ||
""" | ||
Check that when we send scores from multiple users, the correct order | ||
|
@@ -306,7 +346,7 @@ def test_SetPlayerPuzzlesComplete_level_complete(self): | |
self.set_puzzle_complete_response([13, 14, 15, 53524])) | ||
|
||
is_complete = partial( | ||
PuzzleComplete.is_level_complete, self.unique_user_id) | ||
PuzzleComplete.is_level_complete, unique_id_for_user(self.user)) | ||
|
||
self.assertTrue(is_complete(1, 1)) | ||
self.assertTrue(is_complete(1, 3)) | ||
|