def testRoundRankWithoutEntry(self): """Tests that the overall rank calculation is correct even if a user has not done anything yet.""" dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() profile = self.user.get_profile() # Rank will be the number of users who have points plus one. overall_rank = Profile.objects.filter(points__gt=0).count() + 1 floor_rank = Profile.objects.filter(points__gt=0, floor=floor).count() + 1 self.assertEqual(ScoreboardEntry.user_round_overall_rank(self.user, self.current_round), overall_rank, "Check user is last overallfor the current round.") self.assertEqual(ScoreboardEntry.user_round_floor_rank(self.user, self.current_round), floor_rank, "Check user is last in their floor for the current round.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() entry2, created = ScoreboardEntry.objects.get_or_create( profile=profile2, round_name=self.current_round, ) entry2.points = 10 entry2.last_awarded_submission = datetime.datetime.today() entry2.floor = floor entry2.save() self.assertEqual(ScoreboardEntry.user_round_overall_rank(self.user, self.current_round), overall_rank + 1, "Check that the user's overall rank has moved down.") self.assertEqual(ScoreboardEntry.user_round_floor_rank(self.user, self.current_round), floor_rank + 1, "Check that the user's floor rank has moved down.")
def testOverallRankWithSubmissionDate(self): """Check that rank calculation is correct in the case of ties.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().add_points( user_points, datetime.datetime.today() - datetime.timedelta(days=3), "test") user.get_profile().save() # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user = User(username="******", password="******") user.save() user.get_profile().floor = test_floor2 user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is ranked second.")
def testFloorRankWithSubmissionDate(self): """Tests that the floor_rank method accurately computes the rank when users have the same number of points.""" user = User(username="******", password="******") user.save() dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() profile = user.get_profile() profile.floor = floor top_user = Profile.objects.all().order_by("-points")[0] profile.add_points(top_user.points + 1, datetime.datetime.today() - datetime.timedelta(minutes=1), "Test") profile.save() self.assertEqual(profile.floor_rank(), 1, "Check that the user is number 1.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.add_points(profile.points, datetime.datetime.today(), "Test") profile2.save() profile2.floor = floor profile2.save() print profile.points print profile2.points self.assertEqual(profile.floor_rank(), 2, "Check that the user is now rank 2.")
def testOverallRankWithPoints(self): """Check that calculating the rank is correct based on point value.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor # Test the floor is ranked last if they haven't done anything yet. floor_rank = ( Floor.objects.annotate( floor_points=Sum("profile__points"), last_awarded_submission=Max("profile__last_awarded_submission") ) .filter(floor_points__gt=self.test_floor.points) .count() + 1 ) self.assertEqual(self.test_floor.rank(), floor_rank, "Check the floor is ranked last.") user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual(self.test_floor.rank(), 1, "Check the floor is now ranked number 1.") # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today()) user2.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is now ranked number 2.")
def testFloorRankWithSubmissionDate(self): """Tests that the floor_rank method accurately computes the rank when users have the same number of points.""" user = User(username="******", password="******") user.save() dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() profile = user.get_profile() profile.floor = floor top_user = Profile.objects.all().order_by("-points")[0] profile.add_points( top_user.points + 1, datetime.datetime.today() - datetime.timedelta(minutes=1), "Test") profile.save() self.assertEqual(profile.floor_rank(), 1, "Check that the user is number 1.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.add_points(profile.points, datetime.datetime.today(), "Test") profile2.save() profile2.floor = floor profile2.save() print profile.points print profile2.points self.assertEqual(profile.floor_rank(), 2, "Check that the user is now rank 2.")
def testFloorPointsInRound(self): """ Tests calculating the floor points leaders in a round. """ profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile.save() self.assertEqual(Floor.floor_points_leaders(round_name=self.current_round)[0], profile.floor, "The user's floor is not leading in the prize.") # Test that a user in a different floor but same dorm changes the leader for the original user. profile2 = self.users[2].get_profile() profile2.add_points(profile.points + 1, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile2.save() self.assertEqual(Floor.floor_points_leaders(round_name=self.current_round)[0], profile2.floor, "The user's floor should have changed.") # Test that adding points outside of the round does not affect the leaders. profile.add_points(10, datetime.datetime.today() - datetime.timedelta(days=2), "test") profile.save() self.assertEqual(Floor.floor_points_leaders(round_name=self.current_round)[0], profile2.floor, "The leader of the floor should not change.") # Test that a tie is handled properly. profile.add_points(1, datetime.datetime.today(), "test") profile.save() self.assertEqual(Floor.floor_points_leaders(round_name=self.current_round)[0], profile.floor, "The leader of the floor should have changed back.")
def testRoundRank(self): """Check that the rank calculation is correct for the current round.""" # Save the round information and set up a test round. saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().save() ScoreboardEntry.objects.values("profile__floor").filter( round_name=current_round).annotate( floor_points=Sum("points"), last_awarded=Max("last_awarded_submission")).filter( floor_points__gt=self.test_floor.points).count() + 1 self.assertEqual( self.test_floor.current_round_rank(), 1, "Check the calculation works even if there's no submission.") user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 1, "Check the floor is now ranked number 1.") test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today(), "test") user2.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is now ranked number 2.") user.get_profile().add_points( user_points, datetime.datetime.today() - datetime.timedelta(days=3), "test") user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is still ranked number 2.") settings.COMPETITION_ROUNDS = saved_rounds
def generate_floors(): from components.floors.models import Dorm, Floor dorms = Dorm.objects.all() floors = ["3-4", "5-6", "7-8", "9-10", "11-12"] for dorm in dorms: for floor in floors: dorm_floor = Floor(number=floor, dorm=dorm) dorm_floor.save()
def testRoundRank(self): """Check that the rank calculation is correct for the current round.""" # Save the round information and set up a test round. saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1" : { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().save() ScoreboardEntry.objects.values("profile__floor").filter( round_name=current_round ).annotate( floor_points=Sum("points"), last_awarded=Max("last_awarded_submission") ).filter(floor_points__gt=self.test_floor.points).count() + 1 self.assertEqual(self.test_floor.current_round_rank(), 1, "Check the calculation works even if there's no submission.") user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 1, "Check the floor is now ranked number 1.") test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today(), "test") user2.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is now ranked number 2.") user.get_profile().add_points(user_points, datetime.datetime.today() - datetime.timedelta(days=3), "test") user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is still ranked number 2.") settings.COMPETITION_ROUNDS = saved_rounds
def points_scoreboard(request): profiles = Profile.objects.filter( points__gt=0, ).order_by("-points", "-last_awarded_submission").values("name", "points", 'user__username') canopy_members = Profile.objects.filter( canopy_member=True, ).order_by("-canopy_karma").values("name", "canopy_karma") floor_standings = Floor.floor_points_leaders(num_results=20) # Find referrals. referrals = Profile.objects.filter( referring_user__isnull=False, ).values('referring_user__profile__name', 'referring_user__username').annotate( referrals=Count('referring_user') ) round_individuals = {} round_floors = {} for round_name in settings.COMPETITION_ROUNDS: round_individuals[round_name] = ScoreboardEntry.objects.filter( points__gt=0, round_name=round_name, ).order_by("-points", "-last_awarded_submission").values("profile__name", "points", 'profile__user__username') round_floors[round_name] = Floor.floor_points_leaders( num_results=20, round_name=round_name ) # Calculate active participation. floor_participation = Floor.objects.filter(profile__points__gte=50).annotate( user_count=Count('profile'), ).order_by('-user_count').select_related('dorm') for floor in floor_participation: floor.active_participation = (floor.user_count * 100) / floor.profile_set.count() return render_to_response("status/points.html", { "profiles": profiles, "canopy_members": canopy_members, "round_individuals": round_individuals, "floor_standings": floor_standings, "round_floors": round_floors, "floor_participation": floor_participation, "referrals": referrals, }, context_instance=RequestContext(request))
def setUp(self): self.dorm = Dorm(name="Test Dorm") self.dorm.save() self.floors = [ Floor(number=str(i), dorm=self.dorm) for i in range(0, 2) ] map(lambda f: f.save(), self.floors) self.users = [ User.objects.create_user("test%d" % i, "*****@*****.**") for i in range(0, 4) ] # Assign users to floors. for index, user in enumerate(self.users): user.get_profile().floor = self.floors[index % 2] user.get_profile().save() self.saved_rounds = settings.COMPETITION_ROUNDS self.current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, }
def testFloorRankForCurrentRound(self): """Test that we can retrieve the rank for the user in the current round.""" saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() user = User(username="******", password="******") user.save() profile = user.get_profile() top_user = Profile.objects.all().order_by("-points")[0] profile.add_points(top_user.points + 1, datetime.datetime.today(), "Test") profile.floor = floor profile.save() self.assertEqual(profile.current_round_floor_rank(), 1, "Check that the user is number 1.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.add_points(profile.points + 1, datetime.datetime.today(), "Test") profile2.floor = floor profile2.save() self.assertEqual(profile.current_round_floor_rank(), 2, "Check that the user is now number 2.") # Restore saved rounds. settings.COMPETITION_ROUNDS = saved_rounds
def testFloorRankWithPoints(self): """Tests that the floor_rank method accurately computes the rank based on points.""" user = User(username="******", password="******") user.save() dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() profile = user.get_profile() profile.floor = floor # Check that the user is ranked last if they haven't done anything. rank = Profile.objects.filter(floor=floor, points__gt=profile.points).count() + 1 self.assertEqual(profile.floor_rank(), rank, "Check that the user is ranked last.") # Make the user number 1 overall. top_user = Profile.objects.all().order_by("-points")[0] profile.add_points(top_user.points + 1, datetime.datetime.today(), "Test") profile.save() self.assertEqual(profile.floor_rank(), 1, "Check that the user is number 1.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.add_points(profile.points + 1, datetime.datetime.today(), "Test") profile2.save() self.assertEqual( profile.floor_rank(), 1, "Check that the user is still number 1 if the new profile is not on the same floor." ) profile2.floor = floor profile2.save() self.assertEqual(profile.floor_rank(), 2, "Check that the user is now rank 2.")
def testFloorPointsInRound(self): """ Tests calculating the floor points leaders in a round. """ profile = self.users[0].get_profile() profile.add_points( 10, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile.save() self.assertEqual( Floor.floor_points_leaders(round_name=self.current_round)[0], profile.floor, "The user's floor is not leading in the prize.") # Test that a user in a different floor but same dorm changes the leader for the original user. profile2 = self.users[2].get_profile() profile2.add_points( profile.points + 1, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile2.save() self.assertEqual( Floor.floor_points_leaders(round_name=self.current_round)[0], profile2.floor, "The user's floor should have changed.") # Test that adding points outside of the round does not affect the leaders. profile.add_points( 10, datetime.datetime.today() - datetime.timedelta(days=2), "test") profile.save() self.assertEqual( Floor.floor_points_leaders(round_name=self.current_round)[0], profile2.floor, "The leader of the floor should not change.") # Test that a tie is handled properly. profile.add_points(1, datetime.datetime.today(), "test") profile.save() self.assertEqual( Floor.floor_points_leaders(round_name=self.current_round)[0], profile.floor, "The leader of the floor should have changed back.")
def testFloorRankForCurrentRound(self): """Test that we can retrieve the rank for the user in the current round.""" saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1" : { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() user = User(username="******", password="******") user.save() profile = user.get_profile() top_user = Profile.objects.all().order_by("-points")[0] profile.add_points(top_user.points + 1, datetime.datetime.today(), "Test") profile.floor = floor profile.save() self.assertEqual(profile.current_round_floor_rank(), 1, "Check that the user is number 1.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.add_points(profile.points + 1, datetime.datetime.today(), "Test") profile2.floor = floor profile2.save() self.assertEqual(profile.current_round_floor_rank(), 2, "Check that the user is now number 2.") # Restore saved rounds. settings.COMPETITION_ROUNDS = saved_rounds
def testUserFloorRoundRankWithPoints(self): """Tests that the floor rank calculation for a round is correct based on points.""" # Setup dorm dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() profile = self.user.get_profile() profile.floor = floor profile.save() # Set up entry top_entry = ScoreboardEntry.objects.filter(round_name=self.current_round).order_by("-points")[0] entry, created = ScoreboardEntry.objects.get_or_create( profile=self.user.get_profile(), round_name=self.current_round, ) entry.points = top_entry.points + 1 entry.last_awarded_submission = datetime.datetime.today() entry.save() self.assertEqual(ScoreboardEntry.user_round_floor_rank(self.user, self.current_round), 1, "Check user is ranked #1 for the current round.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.floor = floor profile2.save() entry2, created = ScoreboardEntry.objects.get_or_create( profile=profile2, round_name=self.current_round, ) entry2.points = entry.points + 1 entry2.last_awarded_submission = entry.last_awarded_submission entry2.save() self.assertEqual(ScoreboardEntry.user_round_floor_rank(self.user, self.current_round), 2, "Check user is now second.")
def testOverallRankWithSubmissionDate(self): """Check that rank calculation is correct in the case of ties.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().add_points(user_points, datetime.datetime.today() - datetime.timedelta(days=3)) user.get_profile().save() # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user = User(username="******", password="******") user.save() user.get_profile().floor = test_floor2 user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is ranked second.")
def testFloorRankWithPoints(self): """Tests that the floor_rank method accurately computes the rank based on points.""" user = User(username="******", password="******") user.save() dorm = Dorm(name="Test dorm") dorm.save() floor = Floor(number="A", dorm=dorm) floor.save() profile = user.get_profile() profile.floor = floor # Check that the user is ranked last if they haven't done anything. rank = Profile.objects.filter(floor=floor, points__gt=profile.points).count() + 1 self.assertEqual(profile.floor_rank(), rank, "Check that the user is ranked last.") # Make the user number 1 overall. top_user = Profile.objects.all().order_by("-points")[0] profile.add_points(top_user.points + 1, datetime.datetime.today()) profile.save() self.assertEqual(profile.floor_rank(), 1, "Check that the user is number 1.") user2 = User(username="******", password="******") user2.save() profile2 = user2.get_profile() profile2.add_points(profile.points + 1, datetime.datetime.today()) profile2.save() self.assertEqual( profile.floor_rank(), 1, "Check that the user is still number 1 if the new profile is not on the same floor.", ) profile2.floor = floor profile2.save() self.assertEqual(profile.floor_rank(), 2, "Check that the user is now rank 2.")
def _points_leader(self, floor=None): round_name = None if self.round_name == "Overall" else self.round_name if self.award_to == "individual_overall": return Profile.points_leaders(num_results=1, round_name=round_name)[0] elif self.award_to == "floor_dorm": return floor.dorm.floor_points_leaders(num_results=1, round_name=round_name)[0] elif self.award_to == "floor_overall": return Floor.floor_points_leaders(num_results=1, round_name=round_name)[0] elif self.award_to == "individual_floor": return floor.points_leaders(num_results=1, round_name=round_name)[0] raise Exception("Not implemented yet.")
def testOverallRankWithPoints(self): """Check that calculating the rank is correct based on point value.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor # Test the floor is ranked last if they haven't done anything yet. floor_rank = Floor.objects.annotate( floor_points=Sum("profile__points"), last_awarded_submission=Max("profile__last_awarded_submission") ).filter(floor_points__gt=self.test_floor.points).count() + 1 self.assertEqual(self.test_floor.rank(), floor_rank, "Check the floor is ranked last.") user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.rank(), 1, "Check the floor is now ranked number 1.") # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today(), "test") user2.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is now ranked number 2.")
def setUp(self): """ Sets up a test individual prize for the rest of the tests. This prize is not saved, as the round field is not yet set. """ image_path = os.path.join(settings.PROJECT_ROOT, "fixtures", "test_images", "test.jpg") image = ImageFile(open(image_path, "r")) self.prize = Prize( title="Super prize!", short_description="A test prize", long_description="A test prize", image=image, award_to="individual_floor", competition_type="points", value=5, ) self.saved_rounds = settings.COMPETITION_ROUNDS self.current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } # Create test dorms, floors, and users. self.dorm = Dorm(name="Test Dorm") self.dorm.save() self.floors = [ Floor(number=str(i), dorm=self.dorm) for i in range(0, 2) ] map(lambda f: f.save(), self.floors) self.users = [ User.objects.create_user("test%d" % i, "*****@*****.**") for i in range(0, 4) ] # Assign users to floors. for index, user in enumerate(self.users): user.get_profile().floor = self.floors[index % 2] user.get_profile().save()
def _points_leader(self, floor=None): round_name = None if self.round_name == "Overall" else self.round_name if self.award_to == "individual_overall": return Profile.points_leaders(num_results=1, round_name=round_name)[0] elif self.award_to == "floor_dorm": return floor.dorm.floor_points_leaders(num_results=1, round_name=round_name)[0] elif self.award_to == "floor_overall": return Floor.floor_points_leaders(num_results=1, round_name=round_name)[0] elif self.award_to == "individual_floor": if floor: return floor.points_leaders(num_results=1, round_name=round_name)[0] return None raise Exception("'%s' is not implemented yet." % self.award_to)
class FloorsUnitTestCase(TestCase): def setUp(self): self.dorm = Dorm(name="Test dorm") self.dorm.save() self.test_floor = Floor(number="A", dorm=self.dorm) self.test_floor.save() def testOverallPoints(self): """Check that retrieving the points for the floor is correct.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor self.assertEqual(self.test_floor.points(), 0, "Check that the floor does not have any points yet.") user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual( self.test_floor.points(), user_points, "Check that the number of points are equal for one user.") # Create another test user and check again. user = User(username="******", password="******") user.save() user.get_profile().floor = self.test_floor user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual( self.test_floor.points(), 2 * user_points, "Check that the number of points are equal for two users.") def testPointsInRound(self): """Tests that we can accurately compute the amount of points in a round.""" # Save the round information and set up a test round. saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } user = User(username="******", password="******") user.save() profile = user.get_profile() profile.floor = self.test_floor profile.save() self.assertEqual(self.test_floor.current_round_points(), 0, "Check that the floor does not have any points yet.") profile.add_points(10, datetime.datetime.today(), "test") profile.save() self.assertEqual( self.test_floor.current_round_points(), 10, "Check that the number of points are correct in this round.") profile.add_points( 10, datetime.datetime.today() - datetime.timedelta(days=3), "test") profile.save() self.assertEqual(self.test_floor.current_round_points(), 10, "Check that the number of points did not change.") # Restore saved rounds. settings.COMPETITION_ROUNDS = saved_rounds def testOverallRankWithPoints(self): """Check that calculating the rank is correct based on point value.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor # Test the floor is ranked last if they haven't done anything yet. floor_rank = Floor.objects.annotate( floor_points=Sum("profile__points"), last_awarded_submission=Max("profile__last_awarded_submission") ).filter(floor_points__gt=self.test_floor.points).count() + 1 self.assertEqual(self.test_floor.rank(), floor_rank, "Check the floor is ranked last.") user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.rank(), 1, "Check the floor is now ranked number 1.") # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today(), "test") user2.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is now ranked number 2.") def testRoundRank(self): """Check that the rank calculation is correct for the current round.""" # Save the round information and set up a test round. saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().save() ScoreboardEntry.objects.values("profile__floor").filter( round_name=current_round).annotate( floor_points=Sum("points"), last_awarded=Max("last_awarded_submission")).filter( floor_points__gt=self.test_floor.points).count() + 1 self.assertEqual( self.test_floor.current_round_rank(), 1, "Check the calculation works even if there's no submission.") user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 1, "Check the floor is now ranked number 1.") test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today(), "test") user2.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is now ranked number 2.") user.get_profile().add_points( user_points, datetime.datetime.today() - datetime.timedelta(days=3), "test") user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is still ranked number 2.") settings.COMPETITION_ROUNDS = saved_rounds def testOverallRankWithSubmissionDate(self): """Check that rank calculation is correct in the case of ties.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().add_points( user_points, datetime.datetime.today() - datetime.timedelta(days=3), "test") user.get_profile().save() # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user = User(username="******", password="******") user.save() user.get_profile().floor = test_floor2 user.get_profile().add_points(user_points, datetime.datetime.today(), "test") user.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is ranked second.")
def setUp(self): self.dorm = Dorm(name="Test dorm") self.dorm.save() self.test_floor = Floor(number="A", dorm=self.dorm) self.test_floor.save()
class FloorsUnitTestCase(TestCase): def setUp(self): self.dorm = Dorm(name="Test dorm") self.dorm.save() self.test_floor = Floor(number="A", dorm=self.dorm) self.test_floor.save() def testOverallPoints(self): """Check that retrieving the points for the floor is correct.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor self.assertEqual(self.test_floor.points(), 0, "Check that the floor does not have any points yet.") user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual( self.test_floor.points(), user_points, "Check that the number of points are equal for one user." ) # Create another test user and check again. user = User(username="******", password="******") user.save() user.get_profile().floor = self.test_floor user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual( self.test_floor.points(), 2 * user_points, "Check that the number of points are equal for two users." ) def testPointsInRound(self): """Tests that we can accurately compute the amount of points in a round.""" # Save the round information and set up a test round. saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": {"start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d")} } user = User(username="******", password="******") user.save() profile = user.get_profile() profile.floor = self.test_floor profile.save() self.assertEqual( self.test_floor.current_round_points(), 0, "Check that the floor does not have any points yet." ) profile.add_points(10, datetime.datetime.today()) profile.save() self.assertEqual( self.test_floor.current_round_points(), 10, "Check that the number of points are correct in this round." ) profile.add_points(10, datetime.datetime.today() - datetime.timedelta(days=3)) profile.save() self.assertEqual(self.test_floor.current_round_points(), 10, "Check that the number of points did not change.") # Restore saved rounds. settings.COMPETITION_ROUNDS = saved_rounds def testOverallRankWithPoints(self): """Check that calculating the rank is correct based on point value.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor # Test the floor is ranked last if they haven't done anything yet. floor_rank = ( Floor.objects.annotate( floor_points=Sum("profile__points"), last_awarded_submission=Max("profile__last_awarded_submission") ) .filter(floor_points__gt=self.test_floor.points) .count() + 1 ) self.assertEqual(self.test_floor.rank(), floor_rank, "Check the floor is ranked last.") user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual(self.test_floor.rank(), 1, "Check the floor is now ranked number 1.") # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today()) user2.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is now ranked number 2.") def testRoundRank(self): """Check that the rank calculation is correct for the current round.""" # Save the round information and set up a test round. saved_rounds = settings.COMPETITION_ROUNDS current_round = "Round 1" start = datetime.date.today() end = start + datetime.timedelta(days=7) settings.COMPETITION_ROUNDS = { "Round 1": {"start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d")} } # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().save() ScoreboardEntry.objects.values("profile__floor").filter(round_name=current_round).annotate( floor_points=Sum("points"), last_awarded=Max("last_awarded_submission") ).filter(floor_points__gt=self.test_floor.points).count() + 1 self.assertEqual( self.test_floor.current_round_rank(), 1, "Check the calculation works even if there's no submission." ) user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 1, "Check the floor is now ranked number 1.") test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user2 = User(username="******", password="******") user2.save() user2.get_profile().floor = test_floor2 user2.get_profile().add_points(user_points + 1, datetime.datetime.today()) user2.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is now ranked number 2.") user.get_profile().add_points(user_points, datetime.datetime.today() - datetime.timedelta(days=3)) user.get_profile().save() self.assertEqual(self.test_floor.current_round_rank(), 2, "Check the floor is still ranked number 2.") settings.COMPETITION_ROUNDS = saved_rounds def testOverallRankWithSubmissionDate(self): """Check that rank calculation is correct in the case of ties.""" # Create a test user. user = User(username="******", password="******") user.save() user_points = 10 user.get_profile().floor = self.test_floor user.get_profile().add_points(user_points, datetime.datetime.today() - datetime.timedelta(days=3)) user.get_profile().save() # Create a test user on a different floor. test_floor2 = Floor(number="B", dorm=self.dorm) test_floor2.save() user = User(username="******", password="******") user.save() user.get_profile().floor = test_floor2 user.get_profile().add_points(user_points, datetime.datetime.today()) user.get_profile().save() self.assertEqual(self.test_floor.rank(), 2, "Check that the floor is ranked second.")