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 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 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 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 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 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 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): self.dorm = Dorm(name="Test dorm") self.dorm.save() self.test_floor = Floor(number="A", dorm=self.dorm) self.test_floor.save()