def testPrizeOutsideOfRound(self): """ Test that a raffle prize outside of the round does not appear in the list.""" deadline = RaffleDeadline( round_name="Round 1", pub_date=datetime.datetime.today() - datetime.timedelta(days=7), end_date=datetime.datetime.today() - datetime.timedelta(days=1), ) deadline.save() raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=deadline, value=5, ) raffle_prize.save() response = self.client.get(reverse("prizes_index")) self.failUnlessEqual(response.status_code, 200) self.assertNotContains(response, "Test raffle prize") # Try allocating a ticket to this prize. response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id, )), follow=True) self.failUnlessEqual(response.status_code, 200) self.assertContains(response, "The raffle for this round is over.")
def testAllocatedTicket(self): """ Test that allocated_ticket works. """ # Create a raffle prize. deadline = RaffleDeadline( round_name="Overall", pub_date=datetime.datetime.today() - datetime.timedelta(hours=1), end_date=datetime.datetime.today() + datetime.timedelta(days=5), ) deadline.save() prize = RafflePrize(title="Super prize!", description="A test prize", deadline=deadline) prize.save() # Test within context of a quest self.quest.unlock_conditions = "allocated_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue( self.quest not in quests["available_quests"], "User should not be able to participate in this quest." ) self.quest.unlock_conditions = "not allocated_ticket()" self.quest.completion_conditions = "allocated_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") self.quest.accept(self.user) # Add a raffle ticket and test that the user completed the quest. ticket = RaffleTicket(raffle_prize=prize, user=self.user) ticket.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def testIndex(self): """Check that we can load the index page.""" raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=self.deadline, value=5, ) raffle_prize.save() response = self.client.get(reverse("prizes_index")) self.failUnlessEqual(response.status_code, 200) self.assertContains(response, "Round 2 Raffle", msg_prefix="We should be in round 2 of the raffle.") self.assertContains(response, "Your total raffle tickets: 0 Allocated right now: 0 Available: 0", msg_prefix="User should not have any raffle tickets.") date_string = self.deadline.end_date.strftime("%A, %B %d, %Y, ") # Workaround since strftime doesn't remove the leading 0 in hours. hour = self.deadline.end_date.hour if hour == 0: hour = hour + 12 elif hour > 12: hour = hour - 12 date_string = date_string + str(hour) + self.deadline.end_date.strftime("%p") # Another workaround for days because of the leading 0 date_string = re.sub(r"\b0", "", date_string) self.assertContains(response, "Deadline for Round 2 submissions: " + date_string, msg_prefix="Raffle should have the correct deadline.") # Give the user some points and see if their tickets update. profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() response = self.client.get(reverse("prizes_index")) self.assertContains(response, "Your total raffle tickets: 1 Allocated right now: 0 Available: 1", msg_prefix="User should have 1 raffle ticket.")
def testAddWithoutTicket(self): """ Test that the user cannot add a ticket to a raffle if they don't have any tickets. """ raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=self.deadline, value=5, ) raffle_prize.save() # Test adding a ticket. response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id, )), follow=True) self.failUnlessEqual(response.status_code, 200) self.assertContains( response, "Your total raffle tickets: 0 Allocated right now: 0 Available: 0", msg_prefix="User should have no tickets available") self.assertNotContains( response, reverse("raffle_add_ticket", args=(raffle_prize.id, )), msg_prefix="There should not be a url to add a ticket.") self.assertNotContains( response, reverse("raffle_remove_ticket", args=(raffle_prize.id, )), msg_prefix="There should not be a url to remove a ticket.")
def testIndex(self): """Check that we can load the index page.""" raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=self.deadline, value=5, ) raffle_prize.save() response = self.client.get(reverse("prizes_index")) self.failUnlessEqual(response.status_code, 200) self.assertContains( response, "Round 2 Raffle", msg_prefix="We should be in round 2 of the raffle.") self.assertContains( response, "Your total raffle tickets: 0 Allocated right now: 0 Available: 0", msg_prefix="User should not have any raffle tickets.") date_string = self.deadline.end_date.strftime("%A, %B %d, %Y, ") # Workaround since strftime doesn't remove the leading 0 in hours. hour = self.deadline.end_date.hour if hour == 0: hour = hour + 12 elif hour > 12: hour = hour - 12 date_string = date_string + str( hour) + self.deadline.end_date.strftime("%p") # Another workaround for days because of the leading 0 date_string = re.sub(r"\b0", "", date_string) self.assertContains( response, "Deadline for Round 2 submissions: " + date_string, msg_prefix="Raffle should have the correct deadline.") # Give the user some points and see if their tickets update. profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() response = self.client.get(reverse("prizes_index")) self.assertContains( response, "Your total raffle tickets: 1 Allocated right now: 0 Available: 1", msg_prefix="User should have 1 raffle ticket.")
def testAddRemoveTicket(self): """Test that we can add and remove a ticket for a prize.""" raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=self.deadline, value=5, ) raffle_prize.save() profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Test that we can add a ticket. response = self.client.get(reverse("prizes_index")) self.assertContains(response, reverse("raffle_add_ticket", args=(raffle_prize.id,)), msg_prefix="There should be a url to add a ticket.") # Test adding a ticket to a prize. response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id,)), follow=True) self.failUnlessEqual(response.status_code, 200) self.assertContains(response, "Your total raffle tickets: 1 Allocated right now: 1 Available: 0", msg_prefix="User should have one allocated ticket.") self.assertContains(response, reverse("raffle_remove_ticket", args=(raffle_prize.id,)), msg_prefix="There should be an url to remove a ticket.") self.assertNotContains(response, reverse("raffle_add_ticket", args=(raffle_prize.id,)), msg_prefix="There should not be an url to add a ticket.") # Test adding another ticket to the prize. profile.add_points(25, datetime.datetime.today(), "test") profile.save() response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id,)), follow=True) self.assertContains(response, "Your total raffle tickets: 2 Allocated right now: 2 Available: 0", msg_prefix="User should have two allocated tickets.") # Test removing a ticket. response = self.client.post(reverse("raffle_remove_ticket", args=(raffle_prize.id,)), follow=True) self.assertContains(response, "Your total raffle tickets: 2 Allocated right now: 1 Available: 1", msg_prefix="User should have one allocated ticket and one available.") self.assertContains(response, reverse("raffle_add_ticket", args=(raffle_prize.id,)), msg_prefix="There should be a url to add a ticket.") self.assertContains(response, reverse("raffle_remove_ticket", args=(raffle_prize.id,)), msg_prefix="There should be an url to remove a ticket.")
def testAddRemoveWithoutTicket(self): """Test that the user cannot remove a ticket from a prize they did not allocate tickets in.""" raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=self.deadline, value=5, ) raffle_prize.save() # Test removing a ticket. response = self.client.post(reverse("raffle_remove_ticket", args=(raffle_prize.id,)), follow=True) self.failUnlessEqual(response.status_code, 200) self.assertContains(response, "Your total raffle tickets: 0 Allocated right now: 0 Available: 0", msg_prefix="User should have no tickets available") self.assertNotContains(response, reverse("raffle_add_ticket", args=(raffle_prize.id,)), msg_prefix="There should not be a url to add a ticket.") self.assertNotContains(response, reverse("raffle_remove_ticket", args=(raffle_prize.id,)), msg_prefix="There should not be a url to remove a ticket.")
def testAllocatedTicket(self): """ Test that allocated_ticket works. """ # Create a raffle prize. deadline = RaffleDeadline( round_name="Overall", pub_date=datetime.datetime.today() - datetime.timedelta(hours=1), end_date=datetime.datetime.today() + datetime.timedelta(days=5), ) deadline.save() prize = RafflePrize( title="Super prize!", description="A test prize", deadline=deadline, value=5, ) prize.save() # Test within context of a quest self.quest.unlock_conditions = "allocated_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest not in quests["available_quests"], "User should not be able to participate in this quest.") self.quest.unlock_conditions = "not allocated_ticket()" self.quest.completion_conditions = "allocated_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") self.quest.accept(self.user) # Add a raffle ticket and test that the user completed the quest. ticket = RaffleTicket(raffle_prize=prize, user=self.user) ticket.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def testPrizeOutsideOfRound(self): """ Test that a raffle prize outside of the round does not appear in the list.""" deadline = RaffleDeadline( round_name="Round 1", pub_date=datetime.datetime.today() - datetime.timedelta(days=7), end_date=datetime.datetime.today() - datetime.timedelta(days=1), ) deadline.save() raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=deadline, value=5, ) raffle_prize.save() response = self.client.get(reverse("prizes_index")) self.failUnlessEqual(response.status_code, 200) self.assertNotContains(response, "Test raffle prize") # Try allocating a ticket to this prize. response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id,)), follow=True) self.failUnlessEqual(response.status_code, 200) self.assertContains(response, "The raffle for this round is over.")
class RafflePrizeTests(TestCase): """ Tests the RafflePrize model. """ 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. """ self.saved_rounds = settings.COMPETITION_ROUNDS 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 self.user = User.objects.create_user("user", "*****@*****.**", password="******") # Set up raffle deadline self.deadline = RaffleDeadline( round_name="Round 1", pub_date=datetime.datetime.today() - datetime.timedelta(hours=1), end_date=datetime.datetime.today() + datetime.timedelta(days=5), ) self.deadline.save() image_path = os.path.join(settings.PROJECT_ROOT, "fixtures", "test_images", "test.jpg") image = ImageFile(open(image_path, "r")) self.prize = RafflePrize( title="Super prize!", description="A test prize", image=image, value=5, deadline=self.deadline, ) def testTicketAllocation(self): """ Tests that a user can allocate a ticket. """ self.prize.round_name = "Round 1" self.prize.save() profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Add a ticket to the prize self.assertEqual(profile.available_tickets(), 1, "User should have one raffle ticket.") self.prize.add_ticket(self.user) self.assertEqual(profile.available_tickets(), 0, "User should not have any raffle tickets.") self.assertEqual(self.prize.allocated_tickets(), 1, "1 ticket should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(self.user), 1, "1 ticket should be allocated by this user to this prize.") # Have another user add a ticket to the prize. user2 = User.objects.create_user("user2", "*****@*****.**", password="******") profile = user2.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Add a ticket to the prize self.prize.add_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 2, "2 tickets should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(user2), 1, "1 ticket should be allocated by this user to this prize.") # Add another ticket to the prize. profile.add_points(25, datetime.datetime.today(), "test") profile.save() self.prize.add_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 3, "3 tickets should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(user2), 2, "2 tickets should be allocated by this user to this prize.") # Remove a ticket from the prize. self.prize.remove_ticket(self.user) self.assertEqual(self.prize.allocated_tickets(), 2, "2 tickets should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(self.user), 0, "No tickets should be allocated by this user to this prize.") self.prize.remove_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 1, "1 ticket should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(user2), 1, "1 ticket should be allocated by this user to this prize.") def tearDown(self): """ Deletes the created image file in prizes. """ settings.COMPETITION_ROUNDS = self.saved_rounds self.prize.image.delete() self.prize.delete()
def testAddRemoveTicket(self): """Test that we can add and remove a ticket for a prize.""" raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", deadline=self.deadline, value=5, ) raffle_prize.save() profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Test that we can add a ticket. response = self.client.get(reverse("prizes_index")) self.assertContains( response, reverse("raffle_add_ticket", args=(raffle_prize.id, )), msg_prefix="There should be a url to add a ticket.") # Test adding a ticket to a prize. response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id, )), follow=True) self.failUnlessEqual(response.status_code, 200) self.assertContains( response, "Your total raffle tickets: 1 Allocated right now: 1 Available: 0", msg_prefix="User should have one allocated ticket.") self.assertContains( response, reverse("raffle_remove_ticket", args=(raffle_prize.id, )), msg_prefix="There should be an url to remove a ticket.") self.assertNotContains( response, reverse("raffle_add_ticket", args=(raffle_prize.id, )), msg_prefix="There should not be an url to add a ticket.") # Test adding another ticket to the prize. profile.add_points(25, datetime.datetime.today(), "test") profile.save() response = self.client.post(reverse("raffle_add_ticket", args=(raffle_prize.id, )), follow=True) self.assertContains( response, "Your total raffle tickets: 2 Allocated right now: 2 Available: 0", msg_prefix="User should have two allocated tickets.") # Test removing a ticket. response = self.client.post(reverse("raffle_remove_ticket", args=(raffle_prize.id, )), follow=True) self.assertContains( response, "Your total raffle tickets: 2 Allocated right now: 1 Available: 1", msg_prefix= "User should have one allocated ticket and one available.") self.assertContains( response, reverse("raffle_add_ticket", args=(raffle_prize.id, )), msg_prefix="There should be a url to add a ticket.") self.assertContains( response, reverse("raffle_remove_ticket", args=(raffle_prize.id, )), msg_prefix="There should be an url to remove a ticket.")