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", round=RoundSetting.objects.get(name="Round 1"), 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: <em class=\"raffle-ticket-num\">0</em>", msg_prefix="User should have no tickets available") self.assertContains(response, "Allocated: <em class=\"raffle-ticket-num\">0</em>,", msg_prefix="User should have no tickets available") self.assertContains(response, "Available: <em class=\"raffle-ticket-num\">0</em>", 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. r = RoundSetting.objects.get(name="Round 1") prize = RafflePrize( title="Super prize!", description="A test prize", round=r, 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 testIndex(self): """Check that we can load the index page.""" raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", round_name="Round 2", value=5, ) raffle_prize.save() response = self.client.get(reverse("win_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.") deadline = challenge_mgr.get_round_info()["end"] date_string = deadline.strftime("%b. %d, %Y, %I:%M ") 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("win_index")) self.assertContains(response, "Your total raffle tickets: 1 Allocated right now: 0 Available: 1", msg_prefix="User should have 1 raffle 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", round=RoundSetting.objects.get(name="Round 3"), value=5, ) raffle_prize.save() response = self.client.get(reverse("win_index")) self.failUnlessEqual(response.status_code, 200) self.assertContains( response, "Round 2 Raffle", msg_prefix="We should be in round 2 of the raffle.") print response self.assertContains( response, "Your Total Raffle Tickets: <em class=\"raffle-ticket-num\">0</em>,", msg_prefix="User should not have any raffle tickets.") self.assertContains( response, "Allocated: <em class=\"raffle-ticket-num\">0</em>,", msg_prefix="User should not have any raffle tickets.") self.assertContains( response, "Available: <em class=\"raffle-ticket-num\">0</em>", msg_prefix="User should not have any raffle tickets.") deadline = challenge_mgr.get_round_info()["end"] date_string = deadline.strftime("%b. %d, %Y, %I:%M ") 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("win_index")) self.assertContains( response, "Your Total Raffle Tickets: <em class=\"raffle-ticket-num\">1</em>", msg_prefix="User should have 1 raffle ticket.") self.assertContains( response, "Allocated: <em class=\"raffle-ticket-num\">0</em>,", msg_prefix="User should have 1 raffle ticket.") self.assertContains( response, "Available: <em class=\"raffle-ticket-num\">1</em>", 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", round_name="Round 2", 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("win_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 testPrizeOutsideOfRound(self): """ Test that a raffle prize outside of the round does not appear in the list. """ raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", round=RoundSetting.objects.get(name="Round 1"), value=5, ) raffle_prize.save() response = self.client.get(reverse("win_index")) self.failUnlessEqual(response.status_code, 200) self.assertNotContains(response, "Test raffle prize")
def testPrizeOutsideOfRound(self): """ Test that a raffle prize outside of the round does not appear in the list. """ raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", round_name="Round 1", value=5, ) raffle_prize.save() response = self.client.get(reverse("win_index")) self.failUnlessEqual(response.status_code, 200) self.assertNotContains(response, "Test raffle prize") # Try allocating a ticket to this prize. raffle_prize = RafflePrize( title="Test raffle prize", description="A raffle prize for testing", round_name="Round 2", value=5, ) raffle_prize.save() end = datetime.datetime.today() - datetime.timedelta(hours=1) rounds = RoundSetting.objects.get(name="Round 2") rounds.end = end rounds.save() 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 is now over.")
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", round_name="Round 1", 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 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. """ test_utils.set_competition_round() # Create a test user self.user = User.objects.create_user("user", "*****@*****.**", password="******") 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, round=RoundSetting.objects.get(name="Round 1"), value=5, )
def testAllocatedTicket(self): """ Test that allocated_ticket works. """ # Create a raffle prize. r = RoundSetting.objects.get(name="Round 1") prize = RafflePrize( title="Super prize!", description="A test prize", round=r, value=5, ) prize.save() # Test within context of a quest self.quest.unlock_conditions = "allocated_raffle_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_raffle_ticket()" self.quest.completion_conditions = "allocated_raffle_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.")
class RafflePrizeTests(TransactionTestCase): """ 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. """ test_utils.set_competition_round() # Create a test user self.user = User.objects.create_user("user", "*****@*****.**", password="******") 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, round=RoundSetting.objects.get(name="Round 1"), value=5, ) def testTicketAllocation(self): """ Tests that a user can allocate a ticket. """ self.prize.round = RoundSetting.objects.get(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(RaffleTicket.available_tickets(self.user), 1, "User should have one raffle ticket.") self.prize.add_ticket(self.user) self.assertEqual(RaffleTicket.available_tickets(self.user), 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. """ self.prize.image.delete() self.prize.delete()
class RafflePrizeTests(TransactionTestCase): """ 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. """ test_utils.set_competition_round() # Create a test user self.user = User.objects.create_user("user", "*****@*****.**", password="******") 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, round=RoundSetting.objects.get(name="Round 1"), value=5, ) def testTicketAllocation(self): """ Tests that a user can allocate a ticket. """ self.prize.round = RoundSetting.objects.get(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(RaffleTicket.available_tickets(self.user), 1, "User should have one raffle ticket.") self.prize.add_ticket(self.user) self.assertEqual(RaffleTicket.available_tickets(self.user), 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. """ 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", round=RoundSetting.objects.get(name="Round 2"), 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("win_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: <em class=\"raffle-ticket-num\">1</em>", msg_prefix="User should have one allocated ticket.") self.assertContains( response, "Allocated: <em class=\"raffle-ticket-num\">1</em>,", msg_prefix="User should have one allocated ticket.") self.assertContains( response, "Available: <em class=\"raffle-ticket-num\">0</em>", 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: <em class=\"raffle-ticket-num\">2</em>", msg_prefix="User should have two allocated tickets.") self.assertContains( response, "Allocated: <em class=\"raffle-ticket-num\">2</em>,", msg_prefix="User should have two allocated tickets.") self.assertContains( response, "Available: <em class=\"raffle-ticket-num\">0</em>", 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: <em class=\"raffle-ticket-num\">2</em>", msg_prefix= "User should have one allocated ticket and one available.") self.assertContains( response, "Allocated: <em class=\"raffle-ticket-num\">1</em>,", msg_prefix= "User should have one allocated ticket and one available.") self.assertContains( response, "Available: <em class=\"raffle-ticket-num\">1</em>", 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.")