def create_event(self, start_time, end_time, name='My Test Event', restriction=Event.OFFICER): """Create, save, and return a new event.""" event = Event(name=name, event_type=self.event_type, start_datetime=start_time, end_datetime=end_time, term=self.term, location='A test location', contact=self.user, committee=self.committee, restriction=restriction) event.save() return event
class CandidateTest(TestCase): fixtures = ['test/course_instance.yaml'] def setUp(self): self.candidate_group = Group.objects.create(name='Current Candidate') self.member_group = Group.objects.create(name='Member') user_model = get_user_model() # Create candidate self.user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Random', last_name='Candidate') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.candidate = Candidate(user=self.user, term=self.term) self.candidate.save() # Create officer officer_user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Joe', last_name='Officer') committee = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') committee.save() self.officer = Officer(user=officer_user, position=committee, term=self.term, is_chair=False) self.officer.save() # Create some manual candidate requirements self.manual_req1 = ManualCandidateRequirement( name='Manual 1', requirement_type=CandidateRequirement.MANUAL, credits_needed=2, term=self.term) self.manual_req1.save() self.manual_req2 = ManualCandidateRequirement( name='Manual 2', requirement_type=CandidateRequirement.MANUAL, credits_needed=5, term=self.term) self.manual_req2.save() # Create a challenge type self.individual_challenge_type = ChallengeType(name='Individual') self.individual_challenge_type.save() # Create a challenge requirement self.challenge_req = ChallengeCandidateRequirement( challenge_type=self.individual_challenge_type, credits_needed=3, term=self.term) self.challenge_req.save() # Create some events and event requirement self.event_type1 = EventType(name='Fun') self.event_type1.save() self.event_type2 = EventType(name='Not Fun') self.event_type2.save() self.fun_event1 = Event(name='Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event1.save() self.fun_event2 = Event(name='Big Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), requirements_credit=2, term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event2.save() self.notfun_event = Event(name='Not Fun Event', event_type=self.event_type2, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.notfun_event.save() self.event_req = EventCandidateRequirement( event_type=self.event_type1, credits_needed=4, term=self.term) self.event_req.save() # Create some exam files and exam files requirement test_file = open('test.txt', 'w+') test_file.write('This is a test file.') self.test_exam1 = Exam( course_instance=CourseInstance.objects.get(pk=10000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam1.save() self.test_exam1.course_instance.course.department.save() self.test_exam2 = Exam( course_instance=CourseInstance.objects.get(pk=20000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam2.save() self.test_exam2.course_instance.course.department.save() self.exam_req = ExamFileCandidateRequirement( credits_needed=2, term=self.term) self.exam_req.save() @classmethod def tearDownClass(cls): os.remove('test.txt') shutil.rmtree(os.path.join(settings.WORKSPACE_ROOT, 'media', 'tests'), ignore_errors=True) def test_candidate_post_save(self): student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) # Candidate has already been saved for this user created above, so # StudentOrgUserProfile should exist: self.assertIsNotNone(student_org_profile) # Candidate has not been marked as initiated, so initiation_term should # be None in their profile, and the candidate should be in the Current # Candidate group and not in the Member group: self.assertIsNone(student_org_profile.initiation_term) candidate_groups = self.candidate.user.groups.all() self.assertIn(self.candidate_group, candidate_groups) self.assertNotIn(self.member_group, candidate_groups) # Mark candidate as initiated, so profile and groups should update to # match: self.candidate.initiated = True self.candidate.save() student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) self.assertEqual(student_org_profile.initiation_term, self.term) candidate_groups = self.candidate.user.groups.all() self.assertNotIn(self.candidate_group, candidate_groups) self.assertIn(self.member_group, candidate_groups) def test_manual_requirements(self): """Test that credits for manual requirements are counted correctly.""" # Create some candidate progress CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req1, manually_recorded_credits=2, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 2) self.assertEqual(progress['required'], 7) # Test the simple summing of credits progress2 = CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req2, manually_recorded_credits=3, alternate_credits_needed=5) progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 7) # Test alternate credit requirement progress2.alternate_credits_needed = 4 progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 6) def test_challenge_requirements(self): """Test that credits for challenges are counted correctly.""" # Make sure unverified challenges don't count challenge = Challenge( candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, challenge_type=self.individual_challenge_type) challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 0) self.assertEqual(progress['required'], 3) # ...and verified challenges do count challenge.verified = True challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 3) # Test alternate credit requirement CandidateRequirementProgress( candidate=self.candidate, requirement=self.challenge_req, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 2) def test_event_requirements(self): """Test that credits for events are counted correctly based on attendance. """ # Attend Fun Event EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 1) # Attend Big Fun Event (worth 2) EventAttendance(event=self.fun_event2, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) # Attend Not Fun Event (not worth any requirements) EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) def test_exams_requirements(self): """Test that credits for exam files are counted correctly.""" # Upload 1 verified exam self.test_exam1.submitter = self.user self.test_exam1.save() self.assertEqual(self.user, self.test_exam1.submitter) progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) # Upload 2 verified exams self.test_exam2.submitter = self.user self.test_exam2.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 2) # Unverify an exam (doesn't count anymore) self.test_exam1.verified = False self.test_exam1.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) def test_get_total_progress(self): """Test get_progress where requirement_type is not specified and total progress for all requirements is returned. """ # Get all the requirements that were created: requirements = CandidateRequirement.objects.filter(term=self.term) num_required = 0 for requirement in requirements: num_required += requirement.credits_needed progress = self.candidate.get_progress() # Ensure that the method returns the same value as calculated manually. # This value should not change over the course of the tests. self.assertEqual(progress['required'], num_required) # Haven't assigned any completions yet: total_completed = 0 self.assertEqual(progress['completed'], total_completed) # Attend Fun Event for fun event requirement EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) total_completed += 1 self.assertEqual(progress['completed'], total_completed) # Complete a challenge: Challenge(candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, verified=True, challenge_type=self.individual_challenge_type).save() progress = self.candidate.get_progress() total_completed += 1 self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed) # Attend an event not worth any requirements EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed)