def testGeneratingMultipleGoals(self): """ Tests that generate_floor_goals does not generate multiple goals and that multiple goals cannot be created. """ start = datetime.date.today() - datetime.timedelta(days=2) voting_end = datetime.date.today() end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() # Try executing twice to see if an exception occurs. generate_floor_goals() try: generate_floor_goals() except ValidationError: self.fail("generate_floor_goals should not cause an exception.") for floor in Floor.objects.all(): self.assertEqual(floor.floorenergygoal_set.count(), 1, "Check that there is only one goal.")
def testGenerateFloorGoals(self): """Tests the generation of floor goals.""" generate_floor_goals() for floor in Floor.objects.all(): self.assertEqual(floor.floorenergygoal_set.count(), 0, "Test that nothing happens if there is no goal.") start = datetime.date.today() - datetime.timedelta(days=2) voting_end = datetime.date.today() + datetime.timedelta(days=1) end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() # Test that this goal does not generate any floor goals because the voting period is not up. generate_floor_goals() for floor in Floor.objects.all(): self.assertEqual(floor.floorenergygoal_set.count(), 0, "Test that no goals are created before the voting end date.") goal.voting_end_date = datetime.date.today() goal.save() # Create a test vote for a user. user = User.objects.get(username="******") vote = EnergyGoalVote(user=user, goal=goal, percent_reduction=10) vote.save() # Generate floor goals. generate_floor_goals() for floor in Floor.objects.all(): floor_goal = FloorEnergyGoal.objects.get(floor=floor, goal=goal) # Our test user's floor should have a 10 percent reduction goal. if floor == user.get_profile().floor: self.assertEqual(floor_goal.percent_reduction, 10, "Check that test user's vote counts.") else: self.assertEqual(floor_goal.percent_reduction, 0, "Check that default goal is 0.")