def test_awarding(self): """ Tests that the fully committed badge is awarded to a user. """ user = User(username="******", password="******") user.save() commitments = [] # Create 5 test commitments. for i in range(0, 5): commitment = Commitment( title="Test commitment %i" % i, description="A commitment!", point_value=10, ) commitment.save() commitments.append(commitment) # Add the commitments one by one and see if the user earned a badge. # The badge should not be awarded until the very end. for index, commitment in enumerate(commitments): self.assertEqual(user.badges_earned.count(), 0, "Badge should not be awarded after %i commitments" % index) member = CommitmentMember(user=user, commitment=commitment, award_date=datetime.datetime.today()) member.save() self.assertEqual(user.badges_earned.count(), 0, "Badge should not be awarded after commitment %i is awarded" % index) member.award_date = None member.save() self.assertEqual(user.badges_earned.count(), 1, "A badge should have been awarded.") self.assertEqual(user.badges_earned.all()[0].slug, "fully_committed", "Check that the Fully Committed badge was awarded.")
def testCommitmentsNumCompleted(self): """Tests that num_tasks_completed works for a completed commitment.""" commitment = Commitment( title="Test commitment", type="commitment", name="Test", description="A commitment!", point_value=10, ) commitment.save() member = CommitmentMember(user=self.user, commitment=commitment) member.save() self.assertFalse(num_tasks_completed(self.user, 1), "User with commitment in progress should not have completed a task.") # Test as an unlock condition self.quest.unlock_conditions = "num_tasks_completed(1)" self.quest.save() self.assertTrue(self.quest not in get_quests(self.user), "User should not be able to participate in this quest.") member.award_date = datetime.datetime.today() member.save() self.assertTrue(num_tasks_completed(self.user, 1), "User that has a completed commitment did not complete a task.") self.assertTrue(self.quest in get_quests(self.user)["available_quests"], "User should be able to participate in this quest.") # Test as a completion condition self.quest.accept(self.user) self.quest.completion_conditions = "num_tasks_completed(2)" self.quest.save() self.assertTrue(self.quest not in possibly_completed_quests(self.user), "User should not be able to complete this quest.") self.quest.completion_conditions = "num_tasks_completed(1)" self.quest.save() self.assertTrue(self.quest in possibly_completed_quests(self.user), "User should be able to complete this quest.")
def testCommitmentAchievement(self): """Check that the user's commitment achievements are loaded.""" commitment = Commitment( title="Test commitment", description="A commitment!", point_value=10, type="commitment", slug="test-commitment", ) commitment.save() # Test that profile page has a pending activity. member = CommitmentMember(user=self.user, commitment=commitment) member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(commitment.type, commitment.slug,))) self.assertContains(response, "In Progress") self.assertContains(response, "Commitment:") self.assertNotContains(response, "You have nothing in progress or pending.") # Test that the profile page has a rejected activity member.award_date = datetime.datetime.today() member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(commitment.type, commitment.slug,))) self.assertNotContains(response, "You have not been awarded anything yet!") self.assertContains(response, "You have nothing in progress or pending.")
def testIndexMostPopular(self): posts = self.floor.post_set.count() commitment = Commitment( type="commitment", title="Test commitment", description="A commitment!", point_value=10, ) commitment.save() member = CommitmentMember(commitment=commitment, user=self.user, award_date=datetime.datetime.today()) member.save() response = self.client.get(reverse("news_index")) self.failUnlessEqual(response.status_code, 200) self.assertEqual(posts + 2, self.floor.post_set.count(), "Two posts should have been posted to the wall (commit and award).") self.assertContains(response, commitment.title, 3, msg_prefix=""" Commitment title should appear in the wall twice and in the most popular box. Note, may fail because of caching. """ )
def testIndexCommitment(self): """Tests that a commitment shows up in public commitments and in the wall.""" posts = self.floor.post_set.count() # Create a commitment that will appear on the news page. commitment = Commitment( type="commitment", title="Test commitment", description="A commitment!", point_value=10, ) commitment.save() member = CommitmentMember(commitment=commitment, user=self.user) member.save() response = self.client.get(reverse("news_index")) self.failUnlessEqual(response.status_code, 200) self.assertEqual(posts + 1, self.floor.post_set.count(), "One post should have been posted to the wall (public commitment).") self.assertContains(response, commitment.title, 2, msg_prefix="Commitment title should only appear in the wall and the public commitments box." )
def testIndexMostPopular(self): posts = self.floor.post_set.count() commitment = Commitment(type="commitment", title="Test commitment", description="A commitment!", point_value=10) commitment.save() member = CommitmentMember(commitment=commitment, user=self.user, award_date=datetime.datetime.today()) member.save() response = self.client.get(reverse("news_index")) self.failUnlessEqual(response.status_code, 200) self.assertEqual( posts + 2, self.floor.post_set.count(), "Two posts should have been posted to the wall (commit and award)." ) self.assertContains( response, commitment.title, 3, msg_prefix=""" Commitment title should appear in the wall twice and in the most popular box. Note, may fail because of caching. """, )
def testIndexCommitment(self): """Tests that a commitment shows up in public commitments and in the wall.""" posts = self.floor.post_set.count() # Create a commitment that will appear on the news page. commitment = Commitment(type="commitment", title="Test commitment", description="A commitment!", point_value=10) commitment.save() member = CommitmentMember(commitment=commitment, user=self.user) member.save() response = self.client.get(reverse("news_index")) self.failUnlessEqual(response.status_code, 200) self.assertEqual( posts + 1, self.floor.post_set.count(), "One post should have been posted to the wall (public commitment)." ) self.assertContains( response, commitment.title, 2, msg_prefix="Commitment title should only appear in the wall and the public commitments box.", )
def simulate_commitments(self): competition_start = datetime.datetime.strptime( settings.COMPETITION_START, "%Y-%m-%d").date() today = datetime.date.today() date_delta = (today - competition_start).days self.stdout.write("Simulating commitment participation.\n") commitments = Commitment.objects.all() for profile in Profile.objects.exclude(user__username="******"): # Assume users will participate in between 0 and 7 commitments. for i in range(0, random.randint(0, 7)): commitment = None while (True): commitment = commitments.order_by("?")[0] # Check if user is already participating in this commitment. if commitment not in get_current_commitments(profile.user): break member = CommitmentMember(user=profile.user, commitment=commitment) member.completion_date = competition_start + datetime.timedelta( days=random.randint(5, date_delta + 5)) if member.completion_date < today: member.award_date = datetime.datetime.combine( member.completion_date, datetime.time()) member.save()
def testCommitmentAchievement(self): """Check that the user's achievements are loaded.""" commitment = Commitment( title="Test commitment", description="A commitment!", point_value=10, type="commitment", ) commitment.save() # Test that profile page has a pending activity. member = CommitmentMember(user=self.user, commitment=commitment) member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(commitment.id,))) self.assertContains(response, "In Progress") self.assertContains(response, "Commitment:") self.assertContains(response, "Made commitment:") self.assertNotContains(response, "You have nothing in progress or pending.") # Test that the profile page has a rejected activity member.award_date = datetime.datetime.today() member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(commitment.id,))) self.assertNotContains(response, "You have not been awarded anything yet!") self.assertContains(response, "You have nothing in progress or pending.")
def simulate_commitments(self): competition_start = datetime.datetime.strptime(settings.COMPETITION_START, "%Y-%m-%d").date() today = datetime.date.today() date_delta = (today - competition_start).days self.stdout.write("Simulating commitment participation.\n") commitments = Commitment.objects.all() for profile in Profile.objects.exclude(user__username="******"): # Assume users will participate in between 0 and 7 commitments. for i in range(0, random.randint(0, 7)): commitment = None while(True): commitment = commitments.order_by("?")[0] # Check if user is already participating in this commitment. if commitment not in get_current_commitments(profile.user): break member = CommitmentMember(user=profile.user, commitment=commitment) member.completion_date = competition_start + datetime.timedelta(days=random.randint(5, date_delta + 5)) if member.completion_date < today: member.award_date = datetime.datetime.combine(member.completion_date, datetime.time()) member.save()
def testHasCommitment(self): """Tests that has_task works for a commitment in progress.""" commitment = Commitment( title="Test commitment", type="commitment", name="Test", slug="test-commitment", description="A commitment!", point_value=10, ) commitment.save() # Test as an unlock condition. self.quest.unlock_conditions = "has_task(slug='test-commitment')" self.quest.save() self.assertTrue( self.quest not in get_quests(self.user), "User should not be able to participate in this quest." ) member = CommitmentMember(user=self.user, commitment=commitment) member.save() self.assertTrue(has_task(self.user, slug="test-commitment"), "User should have a commitment in progress.") self.assertTrue(has_task(self.user, task_type="commitment"), "User should have a commitment in progress.") self.assertTrue( self.quest in get_quests(self.user)["available_quests"], "User should be able to participate in this quest." ) self.quest.unlock_conditions = "has_task(task_type='commitment')" self.quest.save() self.assertTrue( self.quest in get_quests(self.user)["available_quests"], "User should be able to participate in this quest." ) member.award_date = datetime.datetime.today() member.save() self.assertTrue(has_task(self.user, slug="test-commitment"), "User should have a completed commitment.") self.assertTrue(has_task(self.user, task_type="commitment"), "User should have a completed commitment.") # Test as a completion condition self.quest.accept(self.user) self.quest.completion_conditions = "not has_task(slug='test-commitment')" self.quest.save() self.assertTrue( self.quest not in possibly_completed_quests(self.user), "User should not be able to complete this quest." ) self.quest.completion_conditions = "not has_task(task_type='commitment')" self.quest.save() self.assertTrue( self.quest not in possibly_completed_quests(self.user), "User should not be able to complete this quest." ) self.quest.completion_conditions = "has_task(slug='test-commitment')" self.quest.save() self.assertTrue( self.quest in possibly_completed_quests(self.user), "User should be able to complete this quest." )
def testHasCommitment(self): """Tests that has_task works for a commitment in progress.""" commitment = Commitment( title="Test commitment", type="commitment", name="Test", slug="test-commitment", description="A commitment!", point_value=10, ) commitment.save() # Test as an unlock condition. self.quest.unlock_conditions = "has_task(slug='test-commitment')" self.quest.save() self.assertTrue(self.quest not in get_quests(self.user), "User should not be able to participate in this quest.") member = CommitmentMember(user=self.user, commitment=commitment) member.save() self.assertTrue(has_task(self.user, slug='test-commitment'), "User should have a commitment in progress.") self.assertTrue(has_task(self.user, task_type="commitment"), "User should have a commitment in progress.") self.assertTrue(self.quest in get_quests(self.user)["available_quests"], "User should be able to participate in this quest.") self.quest.unlock_conditions = "has_task(task_type='commitment')" self.quest.save() self.assertTrue(self.quest in get_quests(self.user)["available_quests"], "User should be able to participate in this quest.") member.award_date = datetime.datetime.today() member.save() self.assertTrue(has_task(self.user, slug='test-commitment'), "User should have a completed commitment.") self.assertTrue(has_task(self.user, task_type="commitment"), "User should have a completed commitment.") # Test as a completion condition self.quest.accept(self.user) self.quest.completion_conditions = "not has_task(slug='test-commitment')" self.quest.save() self.assertTrue(self.quest not in possibly_completed_quests(self.user), "User should not be able to complete this quest.") self.quest.completion_conditions = "not has_task(task_type='commitment')" self.quest.save() self.assertTrue(self.quest not in possibly_completed_quests(self.user), "User should not be able to complete this quest.") self.quest.completion_conditions = "has_task(slug='test-commitment')" self.quest.save() self.assertTrue(self.quest in possibly_completed_quests(self.user), "User should be able to complete this quest.")