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 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 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 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 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 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 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 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 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="floor_overall", 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()
class FloorLeadersTestCase(TestCase): 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 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)) 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)) 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)) 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()) 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 testIndividualPointsInRound(self): """ Tests calculating the individual points leaders in a round. """ profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today() - datetime.timedelta(minutes=1)) profile.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile, "The user should be in the lead in his own floor.", ) # Test that a user in a different floor but same dorm does not change the leader for the original floor. profile1 = self.users[1].get_profile() profile1.add_points(15, datetime.datetime.today() - datetime.timedelta(minutes=1)) profile1.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile, "The leader for the user's floor should not have changed.", ) self.assertEqual( profile1.floor.points_leaders(round_name=self.current_round)[0], profile1, "User 1 should be leading in their own floor.", ) # Test another user going ahead in the user's floor. profile2 = self.users[2].get_profile() profile2.add_points(15, datetime.datetime.today() - datetime.timedelta(minutes=1)) profile2.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile2, "User 2 should be in the lead in the user's floor.", ) # Test that adding points outside of the round does not affect the leaders. profile.add_points(10, datetime.datetime.today() - datetime.timedelta(days=2)) profile.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile2, "The leader of the floor should not change.", ) # Test that a tie is handled properly. profile.add_points(5, datetime.datetime.today()) profile.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile, "The leader of the floor should have changed back.", ) def testFloorPointsOverall(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)) profile.save() self.assertEqual(profile.floor.points_leaders()[0], profile, "The user should be in the lead in his own floor.") # Test that a user in a different floor but same dorm does not change the leader for the original floor. profile1 = self.users[1].get_profile() profile1.add_points(15, datetime.datetime.today() - datetime.timedelta(minutes=1)) profile1.save() self.assertEqual( profile.floor.points_leaders()[0], profile, "The leader for the user's floor should not have changed." ) self.assertEqual(profile1.floor.points_leaders()[0], profile1, "User 1 should be leading in their own floor.") # Test another user going ahead in the user's floor. profile2 = self.users[2].get_profile() profile2.add_points(15, datetime.datetime.today() - datetime.timedelta(minutes=1)) profile2.save() self.assertEqual( profile.floor.points_leaders()[0], profile2, "User 2 should be in the lead in the user's floor." ) # Test that a tie is handled properly. profile.add_points(5, datetime.datetime.today()) profile.save() self.assertEqual( profile.floor.points_leaders()[0], profile, "The leader of the floor should have changed back." ) def tearDown(self): settings.COMPETITION_ROUNDS = self.saved_rounds
class FloorLeadersTestCase(TestCase): 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 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 testIndividualPointsInRound(self): """ Tests calculating the individual 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( profile.floor.points_leaders(round_name=self.current_round)[0], profile, "The user should be in the lead in his own floor.") # Test that a user in a different floor but same dorm does not change the leader for the original floor. profile1 = self.users[1].get_profile() profile1.add_points( 15, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile1.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile, "The leader for the user's floor should not have changed.") self.assertEqual( profile1.floor.points_leaders(round_name=self.current_round)[0], profile1, "User 1 should be leading in their own floor.") # Test another user going ahead in the user's floor. profile2 = self.users[2].get_profile() profile2.add_points( 15, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile2.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile2, "User 2 should be in the lead in the user's floor.") # 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( profile.floor.points_leaders(round_name=self.current_round)[0], profile2, "The leader of the floor should not change.") # Test that a tie is handled properly. profile.add_points(5, datetime.datetime.today(), "test") profile.save() self.assertEqual( profile.floor.points_leaders(round_name=self.current_round)[0], profile, "The leader of the floor should have changed back.") def testFloorPointsOverall(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(profile.floor.points_leaders()[0], profile, "The user should be in the lead in his own floor.") # Test that a user in a different floor but same dorm does not change the leader for the original floor. profile1 = self.users[1].get_profile() profile1.add_points( 15, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile1.save() self.assertEqual( profile.floor.points_leaders()[0], profile, "The leader for the user's floor should not have changed.") self.assertEqual(profile1.floor.points_leaders()[0], profile1, "User 1 should be leading in their own floor.") # Test another user going ahead in the user's floor. profile2 = self.users[2].get_profile() profile2.add_points( 15, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile2.save() self.assertEqual(profile.floor.points_leaders()[0], profile2, "User 2 should be in the lead in the user's floor.") # Test that a tie is handled properly. profile.add_points(5, datetime.datetime.today(), "test") profile.save() self.assertEqual(profile.floor.points_leaders()[0], profile, "The leader of the floor should have changed back.") def tearDown(self): settings.COMPETITION_ROUNDS = self.saved_rounds
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.")
class OverallFloorPrizeTest(TestCase): """ Tests awarding a prize to a dorm floor points winner. """ 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="floor_overall", 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 testNumAwarded(self): """ Simple test to check that the number of prizes to be awarded is one. """ self.prize.round_name = "Round 1" self.prize.save() self.assertEqual(self.prize.num_awarded(), 1, "This prize should not be awarded to more than one user.") def testRoundLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round_name = "Round 1" self.prize.save() # Test one user will go ahead in points. profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile.save() self.assertEqual(self.prize.leader(profile.floor), profile.floor, "The user's floor is not leading in the prize.") # Test that a user in a different floor 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(self.prize.leader(profile.floor), profile2.floor, "The leader for this prize did not change.") def testOverallLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round = "Overall" self.prize.save() # Test one user will go ahead in points. profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile.save() self.assertEqual(self.prize.leader(profile.floor), 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(self.prize.leader(profile.floor), profile2.floor, "The leader for this prize did not change.") def tearDown(self): """ Deletes the created image file in prizes. """ settings.COMPETITION_ROUNDS = self.saved_rounds self.prize.image.delete() self.prize.delete()
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 FloorPrizeTest(TestCase): """ Tests awarding a prize to the individual on each floor with the most points. """ 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 testNumAwarded(self): """ Tests that the number of prizes awarded corresponds to the number of floors. """ self.prize.round_name = "Round 1" self.prize.save() self.assertEqual(self.prize.num_awarded(), len(self.floors), "This should correspond to the number of floors.") def testRoundLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round_name = "Round 1" self.prize.save() # Test one user profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today(), "test") profile.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile, "Current prize leader is not the leading user.") # Have a user on the same floor move ahead in points. profile3 = self.users[2].get_profile() profile3.add_points(11, datetime.datetime.today(), "test") profile3.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should be the the leader.") # Have the first user earn more points outside of the round. profile.add_points( 2, datetime.datetime.today() - datetime.timedelta(days=2), "test") profile.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should still be the leading profile.") # Try a user on a different floor. profile2 = self.users[1].get_profile() profile2.add_points(20, datetime.datetime.today(), "test") profile2.save() self.assertEqual( self.prize.leader(floor=profile.floor), profile3, "User 3 should be the leading profile on user 1's floor.") self.assertEqual( self.prize.leader(floor=profile2.floor), profile2, "User 2 should be the leading profile on user 2's floor.") def testOverallLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round_name = "Overall" self.prize.save() # Test one user profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today(), "test") profile.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile, "Current prize leader is not the leading user.") # Have a user on the same floor move ahead in points. profile3 = self.users[2].get_profile() profile3.add_points(11, datetime.datetime.today(), "test") profile3.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should be the the leader.") # Try a user on a different floor. profile2 = self.users[1].get_profile() profile2.add_points(20, datetime.datetime.today(), "test") profile2.save() self.assertEqual( self.prize.leader(floor=profile.floor), profile3, "User 3 should be the leading profile on user 1's floor.") self.assertEqual( self.prize.leader(floor=profile2.floor), profile2, "User 2 should be the leading profile on user 2's floor.") def tearDown(self): """ Deletes the created image file in prizes. """ settings.COMPETITION_ROUNDS = self.saved_rounds self.prize.image.delete() self.prize.delete()
class FloorPrizeTest(TestCase): """ Tests awarding a prize to the individual on each floor with the most points. """ 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 testNumAwarded(self): """ Tests that the number of prizes awarded corresponds to the number of floors. """ self.prize.round_name = "Round 1" self.prize.save() self.assertEqual(self.prize.num_awarded(), len(self.floors), "This should correspond to the number of floors.") def testRoundLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round_name = "Round 1" self.prize.save() # Test one user profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today(), "test") profile.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile, "Current prize leader is not the leading user.") # Have a user on the same floor move ahead in points. profile3 = self.users[2].get_profile() profile3.add_points(11, datetime.datetime.today(), "test") profile3.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should be the the leader.") # Have the first user earn more points outside of the round. profile.add_points(2, datetime.datetime.today() - datetime.timedelta(days=2), "test") profile.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should still be the leading profile.") # Try a user on a different floor. profile2 = self.users[1].get_profile() profile2.add_points(20, datetime.datetime.today(), "test") profile2.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should be the leading profile on user 1's floor.") self.assertEqual(self.prize.leader(floor=profile2.floor), profile2, "User 2 should be the leading profile on user 2's floor.") def testOverallLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round_name = "Overall" self.prize.save() # Test one user profile = self.users[0].get_profile() profile.add_points(10, datetime.datetime.today(), "test") profile.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile, "Current prize leader is not the leading user.") # Have a user on the same floor move ahead in points. profile3 = self.users[2].get_profile() profile3.add_points(11, datetime.datetime.today(), "test") profile3.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should be the the leader.") # Try a user on a different floor. profile2 = self.users[1].get_profile() profile2.add_points(20, datetime.datetime.today(), "test") profile2.save() self.assertEqual(self.prize.leader(floor=profile.floor), profile3, "User 3 should be the leading profile on user 1's floor.") self.assertEqual(self.prize.leader(floor=profile2.floor), profile2, "User 2 should be the leading profile on user 2's floor.") def tearDown(self): """ Deletes the created image file in prizes. """ settings.COMPETITION_ROUNDS = self.saved_rounds self.prize.image.delete() self.prize.delete()
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.")
class OverallFloorPrizeTest(TestCase): """ Tests awarding a prize to a dorm floor points winner. """ 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="floor_overall", 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 testNumAwarded(self): """ Simple test to check that the number of prizes to be awarded is one. """ self.prize.round_name = "Round 1" self.prize.save() self.assertEqual( self.prize.num_awarded(), 1, "This prize should not be awarded to more than one user.") def testRoundLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round_name = "Round 1" self.prize.save() # Test one user will go ahead in points. profile = self.users[0].get_profile() profile.add_points( 10, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile.save() self.assertEqual(self.prize.leader(profile.floor), profile.floor, "The user's floor is not leading in the prize.") # Test that a user in a different floor 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(self.prize.leader(profile.floor), profile2.floor, "The leader for this prize did not change.") def testOverallLeader(self): """ Tests that we can retrieve the overall individual points leader for a round prize. """ self.prize.round = "Overall" self.prize.save() # Test one user will go ahead in points. profile = self.users[0].get_profile() profile.add_points( 10, datetime.datetime.today() - datetime.timedelta(minutes=1), "test") profile.save() self.assertEqual(self.prize.leader(profile.floor), 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(self.prize.leader(profile.floor), profile2.floor, "The leader for this prize did not change.") def tearDown(self): """ Deletes the created image file in prizes. """ settings.COMPETITION_ROUNDS = self.saved_rounds self.prize.image.delete() self.prize.delete()