def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user_model = get_user_model() self.user = self.user_model.objects.create_user( 'test_user', '*****@*****.**', 'testpw') self.first_name = 'Edward' self.last_name = 'Williams' self.user.first_name = self.first_name self.user.last_name = self.last_name self.user.save() # Re-fetch the user from the DB to avoid issues with the current # object having a stale reference to their corresponding userprofile self.user = self.user_model.objects.get(pk=self.user.pk) self.term = Term(term=Term.SPRING, year=2013, current=True) self.term.save() self.term_old = Term(term=Term.SPRING, year=2012) self.term_old.save() self.committee = OfficerPosition( short_name='it', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.committee.save()
def generate_terms(span=5, include_summer=False): """Create Term instances for current year +/-span(years) inclusive.""" current_year = datetime.date.today().year start_year = max(current_year - span, datetime.MINYEAR) end_year = min(current_year + span, datetime.MAXYEAR) term_codes = [Term.FALL, Term.SPRING] if include_summer: term_codes.append(Term.SUMMER) if settings.TERM_TYPE == 'quarter': term_codes.append(Term.WINTER) new_terms = [] with transaction.atomic(): existing_terms = set([ t.id for t in Term.objects.filter(year__gte=start_year, year__lte=end_year) ]) for year in range(start_year, end_year + 1): for term in term_codes: new_term = Term(term=term, year=year) new_term.id = new_term._calculate_pk() if new_term.id not in existing_terms: new_terms.append(new_term) Term.objects.bulk_create(new_terms) return new_terms
def setUp(self): self.test_exam1 = make_test_exam(10000) self.test_exam2 = make_test_exam(20000) self.test_exam3 = make_test_exam(30000) self.permission1 = InstructorPermission.objects.get(pk=100000) self.permission2 = InstructorPermission.objects.get(pk=200000) self.permission3 = InstructorPermission.objects.get(pk=300000) self.dept_cs = Department(long_name='Computer Science', short_name='CS', abbreviation='COMPSCI') self.dept_cs.save() self.course_cs_1 = Course(department=self.dept_cs, number='1') self.course_cs_1.save() self.instructor_cs = Instructor(first_name='Tau', last_name='Bate', department=self.dept_cs) self.instructor_cs.save() self.term = Term(term='sp', year=2016, current=True) self.term.save() self.course_instance_cs_1 = CourseInstance(term=self.term, course=self.course_cs_1) self.course_instance_cs_1.save() self.course_instance_cs_1.instructors.add(self.instructor_cs) self.course_instance_cs_1.save() self.permission4 = InstructorPermission(instructor=self.instructor_cs, permission_allowed=True) self.permission4.save()
def setUp(self): Group.objects.create(name="Current Candidate") Group.objects.create(name="Member") self.user_model = get_user_model() self.user = self.user_model.objects.create_user("test_user", "*****@*****.**", "testpw") self.first_name = "Edward" self.last_name = "Williams" self.user.first_name = self.first_name self.user.last_name = self.last_name self.user.save() # Re-fetch the user from the DB to avoid issues with the current # object having a stale reference to their corresponding userprofile self.user = self.user_model.objects.get(pk=self.user.pk) self.term = Term(term=Term.SPRING, year=2013, current=True) self.term.save() self.term_old = Term(term=Term.SPRING, year=2012) self.term_old.save() self.committee = OfficerPosition( short_name="it", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) self.committee.save()
def setUp(self): self.department_cs = Department(long_name='Computer Science', short_name='CS', abbreviation='COMP SCI') self.department_cs.save() self.department_me = Department(long_name='Mechanical Engineering', short_name='ME', abbreviation='MEC ENG') self.department_me.save() self.course_cs1 = Course(department=self.department_cs, number='1') self.course_cs1.save() self.course_me50 = Course(department=self.department_me, number='50') self.course_me50.save() self.instructor_test = Instructor(first_name='Tau', last_name='Bate', department=self.department_cs) self.instructor_test.save() self.term_test = Term(term=Term.SPRING, year=2013, current=True) self.term_test.save() self.user_test = get_user_model().objects.create_user( username='******', email='tbp.berkeley.edu', password='******', first_name='Tau', last_name='Bate') self.user_test.save()
class OfficerTest(TestCase): fixtures = ["officer_position.yaml"] def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name="IT_test", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) self.position.save() def test_save(self): it_chair = Officer(user=self.user, position=self.position, term=self.term, is_chair=True) it_chair.save() self.assertTrue(Officer.objects.filter(is_chair=True).exists()) new_term = Term(term=Term.FALL, year=2011, current=False) new_term.save() it_officer = Officer(user=self.user, position=self.position, term=new_term, is_chair=False) it_officer.save() officers = Officer.objects.filter(is_chair=True) self.assertEquals(len(officers), 1) self.assertEquals(officers[0].user, self.user)
def test_comparisons(self): fall = Term(term=Term.FALL, year=2012) spring = Term(term=Term.SPRING, year=2012) self.assertTrue(spring < fall) self.assertTrue(spring <= fall) self.assertFalse(spring > fall) self.assertFalse(spring >= fall) self.assertTrue(fall <= fall) self.assertTrue(fall >= fall)
def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition(short_name='vp', long_name='Vice President', rank=1, mailing_list='vp', executive=True) self.position_exec.save() self.position_regular = OfficerPosition( short_name='it', long_name='Information Technology', rank=2, mailing_list='it') self.position_regular.save() self.position_auxiliary = OfficerPosition(short_name='advisor', long_name='Advisor', rank=3, mailing_list='advisor', auxiliary=True) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name='Officer') self.officer_group_curr = Group.objects.create(name='Current Officer') self.exec_group = Group.objects.create(name='Executive') self.exec_group_curr = Group.objects.create(name='Current Executive') # Groups from officer positions: self.pos_exec_group = Group.objects.create( name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create( name='Current {}'.format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create( name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create( name='Current {}'.format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create( name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create( name='Current {}'.format(self.position_auxiliary.long_name))
class SurveyFormTest(TestCase): def setUp(self): self.department_cs = Department(long_name='Computer Science', short_name='CS', abbreviation='COMP SCI') self.department_cs.save() self.department_me = Department(long_name='Mechanical Engineering', short_name='ME', abbreviation='MEC ENG') self.department_me.save() self.course_cs1 = Course(department=self.department_cs, number='1') self.course_cs1.save() self.course_me50 = Course(department=self.department_me, number='50') self.course_me50.save() self.instructor_test = Instructor(first_name='Tau', last_name='Bate', department=self.department_cs) self.instructor_test.save() self.term_test = Term(term=Term.SPRING, year=2013, current=True) self.term_test.save() self.user_test = get_user_model().objects.create_user( username='******', email='tbp.berkeley.edu', password='******', first_name='Tau', last_name='Bate') self.user_test.save() def test_courses_as_optgroups(self): optgroups = courses_as_optgroups() # optgrous[i] returns [dept.long_name, list of courses in dept] # optgroups[i][1] returns a list of tuples in the form of # (Course, Course.abbreviation()) self.assertEqual(optgroups[0][0], self.department_cs.long_name) self.assertEqual(optgroups[1][1][0][0], self.course_me50) def test_valid_form(self): test_form = SurveyForm() self.assertFalse(test_form.is_valid()) form_data = { 'course': self.course_me50.pk, 'term': self.term_test.pk, 'instructor': self.instructor_test.pk, 'prof_rating': 1, 'course_rating': 2, 'time_commitment': 3, 'comments': 'Test Comment', 'submitter': self.user_test.pk } test_form = SurveyForm(form_data) self.assertTrue(test_form.is_valid())
def test_save(self): it_chair = Officer(user=self.user, position=self.position, term=self.term, is_chair=True) it_chair.save() self.assertTrue(Officer.objects.filter(is_chair=True).exists()) new_term = Term(term=Term.FALL, year=2011, current=False) new_term.save() it_officer = Officer(user=self.user, position=self.position, term=new_term, is_chair=False) it_officer.save() officers = Officer.objects.filter(is_chair=True) self.assertEquals(len(officers), 1) self.assertEquals(officers[0].user, self.user)
def test_get_url_name(self): term = Term(term=Term.WINTER, year=2012, current=True) self.assertEqual(term.get_url_name(), "wi2012") term = Term(term=Term.SPRING, year=2012, current=True) self.assertEqual(term.get_url_name(), "sp2012") term = Term(term=Term.SUMMER, year=2012, current=True) self.assertEqual(term.get_url_name(), "su2012") term = Term(term=Term.FALL, year=2012, current=True) self.assertEqual(term.get_url_name(), "fa2012") term = Term(term=Term.UNKNOWN, year=2012, current=True) self.assertEqual(term.get_url_name(), "un2012")
def test_calculate_pk(self): term = Term(term=Term.WINTER, year=2012, current=True) self.assertEqual(term._calculate_pk(), 20121) term = Term(term=Term.SPRING, year=2012, current=True) self.assertEqual(term._calculate_pk(), 20122) term = Term(term=Term.SUMMER, year=2012, current=True) self.assertEqual(term._calculate_pk(), 20123) term = Term(term=Term.FALL, year=2012, current=True) self.assertEqual(term._calculate_pk(), 20124) term = Term(term=Term.UNKNOWN, year=2012, current=True) self.assertEqual(term._calculate_pk(), 20120)
def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.position.save()
def setUp(self): self.dept_cs = Department(long_name="Computer Science", short_name="CS", abbreviation="COMPSCI") self.dept_cs.save() self.dept_ee = Department(long_name="Electrical Engineering", short_name="EE", abbreviation="EL ENG") self.dept_ee.save() self.course_cs_1 = Course(department=self.dept_cs, number="1") self.course_cs_1.save() self.course_ee_1 = Course(department=self.dept_ee, number="1") self.course_ee_1.save() self.instructor_cs = Instructor(first_name="Tau", last_name="Bate", department=self.dept_cs) self.instructor_cs.save() self.instructor_ee = Instructor(first_name="Pi", last_name="Bent", department=self.dept_ee) self.instructor_ee.save() self.term = Term(term="sp", year=2013, current=True) self.term.save() self.course_instance_cs_1 = CourseInstance(term=self.term, course=self.course_cs_1) self.course_instance_cs_1.save() self.course_instance_cs_1.instructors.add(self.instructor_cs) self.course_instance_cs_1.save() self.course_instance_ee_1 = CourseInstance(term=self.term, course=self.course_ee_1) self.course_instance_ee_1.save() self.course_instance_ee_1.instructors.add(self.instructor_ee) self.course_instance_ee_1.save() self.user = get_user_model().objects.create_user( username="******", email="tbp.berkeley.edu", password="******", first_name="tbp", last_name="user" ) self.user.save() self.survey_cs_1 = Survey( course=self.course_cs_1, term=self.term, instructor=self.instructor_cs, prof_rating=5, course_rating=5, time_commitment=5, comments="Test comments", submitter=self.user, published=True, ) self.survey_cs_1.save() self.survey_cs_1_b = Survey( course=self.course_cs_1, term=self.term, instructor=self.instructor_cs, prof_rating=0, course_rating=5, time_commitment=0, comments="Test comments", submitter=self.user, published=True, ) self.survey_cs_1_b.save() self.exam_ee_1 = Exam( course_instance=self.course_instance_ee_1, submitter=self.user, exam_number=Exam.FINAL, exam_type=Exam.EXAM, file_ext=".pdf", verified=True, ) self.exam_ee_1.save()
def assign_specific_event_achievements(instance): d15_regex = re.compile(r'.*D(istrict)?[\s]?15.*') if d15_regex.match(instance.event.name): d15_achievement = get_object_or_none(Achievement, short_name='attend_d15') if d15_achievement: d15_achievement.assign(instance.user, term=instance.event.term) if 'National Convention' in instance.event.name: natl_achievement = get_object_or_none(Achievement, short_name='attend_convention') if natl_achievement: natl_achievement.assign(instance.user, term=instance.event.term) if 'Envelope Stuffing' in instance.event.name: envelope_achievement = get_object_or_none( Achievement, short_name='attend_envelope_stuffing') if envelope_achievement: envelope_achievement.assign(instance.user, term=instance.event.term) if instance.event.name == 'Candidate Meeting' and (instance.event.term == Term(term=Term.FALL, year=2013)): cm2013_achievement = get_object_or_none( Achievement, short_name='berkeley_explosion') if cm2013_achievement: cm2013_achievement.assign(instance.user, term=instance.event.term)
def test_get_terms_no_future(self): # Intentionally unordered. Term(term=Term.SUMMER, year=2012, current=False).save() Term(term=Term.FALL, year=2011, current=False).save() Term(term=Term.FALL, year=2012, current=False).save() Term(term=Term.SPRING, year=2012, current=True).save() terms = Term.objects.get_terms() self.assertEquals(len(terms), 2) self.assertEquals(terms[0].year, 2011) self.assertEquals(terms[0].term, Term.FALL) self.assertEquals(terms[1].year, 2012) self.assertEquals(terms[1].term, Term.SPRING)
def test_get_terms_semester(self): # Intentionally unordered. Term(term=Term.WINTER, year=2012, current=True).save() terms = Term.objects.get_terms() self.assertEquals(len(terms), 0)
def test_get_terms_no_unknown(self): unknown = Term(term=Term.UNKNOWN, year=2011) unknown.save() spring = Term(term=Term.SPRING, year=2012) spring.save() fall = Term(term=Term.FALL, year=2013, current=True) fall.save() terms = Term.objects.get_terms(include_unknown=False) self.assertEquals(list(terms), [spring, fall]) terms = Term.objects.get_terms(include_unknown=True) self.assertEquals(list(terms), [unknown, spring, fall])
def test_garbage_comparisons(self): term = Term(term=Term.FALL, year=1900) self.assertTrue(term < 'foo') self.assertTrue(term <= 'foo') self.assertFalse(term > 'foo') self.assertFalse(term >= 'foo') self.assertTrue(term != 'foo') self.assertFalse(term == 'foo')
def test_get_upcoming(self): # Create an event that hasn't started yet: start_time = timezone.now() + datetime.timedelta(hours=2) end_time = start_time + datetime.timedelta(hours=3) event = self.create_event(start_time, end_time) upcoming_events = Event.objects.get_upcoming() self.assertIn(event, upcoming_events) # Make the event be set to occur in a future term future_term = Term(term=self.term.term, year=(self.term.year + 1), current=False) future_term.save() event.term = future_term event.save() self.assertIn( event, Event.objects.get_upcoming(current_term_only=False)) self.assertNotIn( event, Event.objects.get_upcoming(current_term_only=True))
def test_term_post_save(self): """Test that when terms are saved, the "Current" groups are kept up-to-date. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add an exec officer position (for which the post-save should add # groups) in the current term: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() expected_groups = set( self.position_exec.get_corresponding_groups(term=self.term)) groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) self.assertItemsEqual(groups, expected_groups) # Make sure saving the current term is a no-op: self.term.save() groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Add a regular officer position for this user in a new term (not # "current"), and the user's group count should increase: term_new = Term(term=Term.FALL, year=2012) term_new.save() officer_reg = Officer(user=self.user, position=self.position_regular, term=term_new) officer_reg.save() expected_groups.update( self.position_regular.get_corresponding_groups(term=term_new)) groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Now change the "new" term to be the current term: term_new.current = True term_new.save() # The self.term object is stale now, so re-fetch it from the database: self.term = Term.objects.get(pk=self.term.pk) # We should expect that the user should still be a "Current Officer", # but no longer "Current" for the groups specific to the exec position. groups = list(self.user.groups.all()) # Get "expected_groups" over again, since the current term has changed: expected_groups = set( self.position_exec.get_corresponding_groups(term=self.term)) expected_groups.update( self.position_regular.get_corresponding_groups(term=term_new)) self.assertItemsEqual(groups, expected_groups) # Double-check some of the "Current" groups: self.assertNotIn(self.exec_group_curr, groups) self.assertNotIn(self.pos_exec_group_curr, groups) self.assertIn(self.officer_group_curr, groups) self.assertIn(self.pos_reg_group_curr, groups)
def test_get_terms_quarter(self): # Intentionally unordered. Term(term=Term.WINTER, year=2012, current=True).save() terms = Term.objects.get_terms() self.assertEquals(len(terms), 1) self.assertEquals(terms[0].year, 2012) self.assertEquals(terms[0].term, Term.WINTER)
def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name="IT_test", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) self.position.save()
def test_get_officer_positions(self): # Note that when given no 'term' kwarg, the method returns positions # from all terms. The order of the list returned is based on term, then # officer position rank # No officer positions for this user yet: self.assertEqual(list(self.profile.get_officer_positions()), []) # One Officer position in the current term: Officer(user=self.user, position=self.committee, term=self.term, is_chair=True).save() self.assertEqual(list(self.profile.get_officer_positions()), [self.committee]) self.assertEqual(list(self.profile.get_officer_positions(term=self.term)), [self.committee]) self.assertEqual(list(self.profile.get_officer_positions(term=self.term_old)), []) # Advisor officer position in an old term: Officer(user=self.user, position=self.advisor_pos, term=self.term_old).save() self.assertEqual(list(self.profile.get_officer_positions()), [self.advisor_pos, self.committee]) self.assertEqual(list(self.profile.get_officer_positions(term=self.term)), [self.committee]) self.assertEqual(list(self.profile.get_officer_positions(term=self.term_old)), [self.advisor_pos]) # Another advisor officer position in the current term Officer(user=self.user, position=self.advisor_pos, term=self.term).save() self.assertEqual( list(self.profile.get_officer_positions()), [self.advisor_pos, self.committee, self.advisor_pos] ) self.assertEqual(list(self.profile.get_officer_positions(term=self.term)), [self.committee, self.advisor_pos]) self.assertEqual(list(self.profile.get_officer_positions(term=self.term_old)), [self.advisor_pos]) # Add a house leader officer position in the current term: # Ensure ordering is correct: Officer(user=self.user, position=self.house_leader, term=self.term).save() self.assertEqual( list(self.profile.get_officer_positions()), [self.advisor_pos, self.committee, self.house_leader, self.advisor_pos], ) older_term = Term(term=Term.SPRING, year=2008) older_term.save() # Add a house leader officer position in an even older term: Officer(user=self.user, position=self.house_leader, term=older_term).save() self.assertEqual( list(self.profile.get_officer_positions()), [self.house_leader, self.advisor_pos, self.committee, self.house_leader, self.advisor_pos], )
def test_save_bad_pk(self): term = Term(term=Term.SPRING, year=2012, current=False) term.save() self.assertEquals(term.pk, 20122) self.assertEquals(term.year, 2012) self.assertEquals(term.term, Term.SPRING) term.year = 2013 self.assertRaisesMessage(ValueError, ('You cannot update the year or term without ' 'also updating the primary key value.'), term.save) terms = Term.objects.all() self.assertEquals(len(terms), 1) term = terms[0] self.assertEquals(term.pk, 20122) self.assertEquals(term.year, 2012) self.assertEquals(term.term, Term.SPRING)
def test_save_bad_pk(self): term = Term(term=Term.SPRING, year=2012, current=False) term.save() self.assertEquals(term.pk, 20122) self.assertEquals(term.year, 2012) self.assertEquals(term.term, Term.SPRING) term.year = 2013 self.assertRaisesMessage( ValueError, ("You cannot update the year or term without " "also updating the primary key value."), term.save, ) terms = Term.objects.all() self.assertEquals(len(terms), 1) term = terms[0] self.assertEquals(term.pk, 20122) self.assertEquals(term.year, 2012) self.assertEquals(term.term, Term.SPRING)
def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition(short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type')
def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition.objects.get(short_name='it') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.project_report = ProjectReport(term=self.term, author=self.user, committee=self.committee, title='Test', date=timezone.now().date()) self.project_report.save() self.mox = mox.Mox() self.tz = timezone.get_current_timezone()
class OfficerTest(TestCase): fixtures = ['officer_position.yaml'] def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.position.save() def test_save(self): it_chair = Officer(user=self.user, position=self.position, term=self.term, is_chair=True) it_chair.save() self.assertTrue(Officer.objects.filter(is_chair=True).exists()) new_term = Term(term=Term.FALL, year=2011, current=False) new_term.save() it_officer = Officer(user=self.user, position=self.position, term=new_term, is_chair=False) it_officer.save() officers = Officer.objects.filter(is_chair=True) self.assertEquals(len(officers), 1) self.assertEquals(officers[0].user, self.user)
def generate_terms(span=5, include_summer=False): """Create Term instances for current year +/-span(years) inclusive.""" current_year = datetime.date.today().year start_year = max(current_year - span, datetime.MINYEAR) end_year = min(current_year + span, datetime.MAXYEAR) term_codes = [Term.FALL, Term.SPRING] if include_summer: term_codes.append(Term.SUMMER) if settings.TERM_TYPE == 'quarter': term_codes.append(Term.WINTER) new_terms = [] with transaction.atomic(): existing_terms = set([t.id for t in Term.objects.filter( year__gte=start_year, year__lte=end_year)]) for year in range(start_year, end_year + 1): for term in term_codes: new_term = Term(term=term, year=year) new_term.id = new_term._calculate_pk() if new_term.id not in existing_terms: new_terms.append(new_term) Term.objects.bulk_create(new_terms) return new_terms
def test_get_terms_reversed(self): # Intentionally unordered. Term(term=Term.FALL, year=2011, current=False).save() Term(term=Term.SUMMER, year=2012, current=True).save() Term(term=Term.SPRING, year=2012, current=False).save() Term(term=Term.SPRING, year=2011, current=False).save() terms = Term.objects.get_terms(include_summer=True, reverse=True) self.assertEquals(len(terms), 4) self.assertEquals(terms[0].year, 2012) self.assertEquals(terms[0].term, Term.SUMMER) self.assertTrue(terms[0].current) self.assertEquals(terms[1].year, 2012) self.assertEquals(terms[1].term, Term.SPRING) self.assertEquals(terms[2].year, 2011) self.assertEquals(terms[2].term, Term.FALL) self.assertEquals(terms[3].year, 2011) self.assertEquals(terms[3].term, Term.SPRING)
def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition( short_name="vp", long_name="Vice President", rank=1, mailing_list="vp", executive=True ) self.position_exec.save() self.position_regular = OfficerPosition( short_name="it", long_name="Information Technology", rank=2, mailing_list="it" ) self.position_regular.save() self.position_auxiliary = OfficerPosition( short_name="advisor", long_name="Advisor", rank=3, mailing_list="advisor", auxiliary=True ) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name="Officer") self.officer_group_curr = Group.objects.create(name="Current Officer") self.exec_group = Group.objects.create(name="Executive") self.exec_group_curr = Group.objects.create(name="Current Executive") # Groups from officer positions: self.pos_exec_group = Group.objects.create(name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create(name="Current {}".format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create(name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create(name="Current {}".format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create(name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create(name="Current {}".format(self.position_auxiliary.long_name))
def test_term_post_save(self): """Test that when terms are saved, the "Current" groups are kept up-to-date. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add an exec officer position (for which the post-save should add # groups) in the current term: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() expected_groups = set(self.position_exec.get_corresponding_groups(term=self.term)) groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) self.assertItemsEqual(groups, expected_groups) # Make sure saving the current term is a no-op: self.term.save() groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Add a regular officer position for this user in a new term (not # "current"), and the user's group count should increase: term_new = Term(term=Term.FALL, year=2012) term_new.save() officer_reg = Officer(user=self.user, position=self.position_regular, term=term_new) officer_reg.save() expected_groups.update(self.position_regular.get_corresponding_groups(term=term_new)) groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Now change the "new" term to be the current term: term_new.current = True term_new.save() # The self.term object is stale now, so re-fetch it from the database: self.term = Term.objects.get(pk=self.term.pk) # We should expect that the user should still be a "Current Officer", # but no longer "Current" for the groups specific to the exec position. groups = list(self.user.groups.all()) # Get "expected_groups" over again, since the current term has changed: expected_groups = set(self.position_exec.get_corresponding_groups(term=self.term)) expected_groups.update(self.position_regular.get_corresponding_groups(term=term_new)) self.assertItemsEqual(groups, expected_groups) # Double-check some of the "Current" groups: self.assertNotIn(self.exec_group_curr, groups) self.assertNotIn(self.pos_exec_group_curr, groups) self.assertIn(self.officer_group_curr, groups) self.assertIn(self.pos_reg_group_curr, groups)
def get(self, request, *args, **kwargs): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = \ 'attachment; filename="candidates.csv"' writer = csv.writer(response) writer.writerow([ 'Last name', 'First name', 'Middle name', 'Email', 'Standing', 'Graduation date', 'Major' ]) current_term = Term.objects.get(id=kwargs['term_pk']) candidates = Candidate.objects.filter( term=current_term).select_related( 'user', 'user__userprofile', 'user__collegestudentinfo__major', 'user__collegestudentinfo__start_term', 'user__collegestudentinfo').order_by('user__last_name') for candidate in candidates: start_term = candidate.user.collegestudentinfo.start_term candidate_year = current_term.year - start_term.year + 1 if Term(year=start_term.year, term=current_term.term) < start_term: candidate_year -= 1 # Not entirely correct, but this is the best we can do with the # information we have. candidate_standing = 'Junior' if candidate_year <= 2 else 'Senior' candidate_majors = candidate.user.collegestudentinfo.major candidate_major_list = candidate_majors.values_list('long_name', flat=True) writer.writerow([ candidate.user.last_name, candidate.user.first_name, candidate.user.userprofile.middle_name, candidate.user.email, candidate_standing, candidate.user.collegestudentinfo.grad_term.verbose_name(), '/'.join(candidate_major_list) ]) return response
def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition.objects.get(short_name='it') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.project_report = ProjectReport( term=self.term, author=self.user, committee=self.committee, title='Test', date=timezone.now().date()) self.project_report.save() self.mox = mox.Mox() self.tz = timezone.get_current_timezone()
def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition( short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type')
def setUp(self): self.department_cs = Department( long_name='Computer Science', short_name='CS', abbreviation='COMP SCI') self.department_cs.save() self.department_me = Department( long_name='Mechanical Engineering', short_name='ME', abbreviation='MEC ENG') self.department_me.save() self.course_cs1 = Course( department=self.department_cs, number='1') self.course_cs1.save() self.course_me50 = Course( department=self.department_me, number='50') self.course_me50.save() self.instructor_test = Instructor( first_name='Tau', last_name='Bate', department=self.department_cs) self.instructor_test.save() self.term_test = Term( term=Term.SPRING, year=2013, current=True) self.term_test.save() self.user_test = get_user_model().objects.create_user( username='******', email='tbp.berkeley.edu', password='******', first_name='Tau', last_name='Bate') self.user_test.save()
class OfficerGroupsTest(TestCase): def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition(short_name='vp', long_name='Vice President', rank=1, mailing_list='vp', executive=True) self.position_exec.save() self.position_regular = OfficerPosition( short_name='it', long_name='Information Technology', rank=2, mailing_list='it') self.position_regular.save() self.position_auxiliary = OfficerPosition(short_name='advisor', long_name='Advisor', rank=3, mailing_list='advisor', auxiliary=True) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name='Officer') self.officer_group_curr = Group.objects.create(name='Current Officer') self.exec_group = Group.objects.create(name='Executive') self.exec_group_curr = Group.objects.create(name='Current Executive') # Groups from officer positions: self.pos_exec_group = Group.objects.create( name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create( name='Current {}'.format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create( name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create( name='Current {}'.format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create( name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create( name='Current {}'.format(self.position_auxiliary.long_name)) def test_get_corresponding_groups(self): """Test the OfficerPosition.get_corresponding_groups method.""" # If we specify the term that is the current term, then the "Current" # groups should be included; otherwise, "Current" groups should not be # included. # Check the corresponding groups for a "regular" (non-auxiliary, # non-exec) officer position. We should expect the corresponding groups # to be Officer and the group specific to the position: groups = [self.officer_group, self.pos_reg_group] self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups()) self.assertItemsEqual( groups, self.position_regular.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([self.officer_group_curr, self.pos_reg_group_curr]) self.assertItemsEqual( groups, self.position_regular.get_corresponding_groups(term=self.term)) # For the executive position, the corresponding groups will also # include the "Executive" groups: groups = [self.officer_group, self.exec_group, self.pos_exec_group] self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups()) self.assertItemsEqual( groups, self.position_exec.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([ self.officer_group_curr, self.exec_group_curr, self.pos_exec_group_curr ]) self.assertItemsEqual( groups, self.position_exec.get_corresponding_groups(term=self.term)) # For the auxiliary position, there should be no "Officer" group or # "Executive" group (since the position is non-exec): groups = [self.pos_aux_group] self.assertItemsEqual( groups, self.position_auxiliary.get_corresponding_groups()) self.assertItemsEqual( groups, self.position_auxiliary.get_corresponding_groups(self.term_old)) # For the current term: groups.append(self.pos_aux_group_curr) self.assertItemsEqual( groups, self.position_auxiliary.get_corresponding_groups(term=self.term)) def test_add_groups(self): # Test the Officer method for adding the user to groups. Note that no # officer objects are saved, as that would activate post-saves, which # are tested seprately. officer = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = self.position_regular.get_corresponding_groups( term=self.term) self.assertFalse(self.user.groups.exists()) officer._add_user_to_officer_groups() # Check that all of the expected groups were added for this user: self.assertTrue(self.user.groups.exists()) for group in expected_groups: self.assertTrue(self.user.groups.filter(pk=group.pk).exists()) def test_remove_groups(self): # Test the Officer method for removing the user from groups. Note that # unlike test_add_groups, this method saves the Officer objets, as the # _remove_user_from_officer_groups method depends on database entries # to work properly. Thus, this method relies on post-save functions for # adding groups for a user. (Post-saves are also tested separately.) self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add the groups for an exec officer position manually so that the # Officer object is not in the database (and does not need to be # deleted here before we can test the removal function), and the user's # group count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec._add_user_to_officer_groups() self.assertTrue(len(groups) < self.user.groups.count()) # Now remove groups from the exec position, and the user's groups # should return to the same positions as from before the exec position # added any: officer_exec._remove_user_from_officer_groups() self.assertItemsEqual(groups, list(self.user.groups.all())) def test_officer_post_save(self): """Test that a user is added to the appropriate groups on post-save.""" self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = set( self.position_regular.get_corresponding_groups(term=self.term)) self.assertFalse(self.user.groups.exists()) officer_reg.save() # Check that all of the expected groups were added for this user: self.assertItemsEqual(expected_groups, self.user.groups.all()) # Add another position, and check that the correct groups are added: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term_old) officer_exec.save() expected_groups.update( self.position_exec.get_corresponding_groups(term=self.term_old)) self.assertItemsEqual(expected_groups, self.user.groups.all()) def test_officer_post_delete(self): """Test that a user is removed from the appropriate groups on post-delete. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add an exec officer position for this user, and the user's group # count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() self.assertTrue(len(groups) < self.user.groups.count()) # Now delete exec officer, and the user's groups should return to the # same positions as from before the exec position added any: officer_exec.delete() self.assertItemsEqual(groups, list(self.user.groups.all())) # And delete the regular officer, and the user should be part of no # more groups: officer_reg.delete() self.assertFalse(self.user.groups.exists()) def test_term_post_save(self): """Test that when terms are saved, the "Current" groups are kept up-to-date. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add an exec officer position (for which the post-save should add # groups) in the current term: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() expected_groups = set( self.position_exec.get_corresponding_groups(term=self.term)) groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) self.assertItemsEqual(groups, expected_groups) # Make sure saving the current term is a no-op: self.term.save() groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Add a regular officer position for this user in a new term (not # "current"), and the user's group count should increase: term_new = Term(term=Term.FALL, year=2012) term_new.save() officer_reg = Officer(user=self.user, position=self.position_regular, term=term_new) officer_reg.save() expected_groups.update( self.position_regular.get_corresponding_groups(term=term_new)) groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Now change the "new" term to be the current term: term_new.current = True term_new.save() # The self.term object is stale now, so re-fetch it from the database: self.term = Term.objects.get(pk=self.term.pk) # We should expect that the user should still be a "Current Officer", # but no longer "Current" for the groups specific to the exec position. groups = list(self.user.groups.all()) # Get "expected_groups" over again, since the current term has changed: expected_groups = set( self.position_exec.get_corresponding_groups(term=self.term)) expected_groups.update( self.position_regular.get_corresponding_groups(term=term_new)) self.assertItemsEqual(groups, expected_groups) # Double-check some of the "Current" groups: self.assertNotIn(self.exec_group_curr, groups) self.assertNotIn(self.pos_exec_group_curr, groups) self.assertIn(self.officer_group_curr, groups) self.assertIn(self.pos_reg_group_curr, groups)
def test_save_invalid_term(self): spring = Term(term=Term.UNKNOWN, year=0, current=True) spring.save() self.assertFalse(Term.objects.filter(current=True).exists())
def test_get_current_term(self): Term(term=Term.SPRING, year=2012, current=False).save() Term(term=Term.FALL, year=2012, current=True).save() term = Term.objects.get_current_term() self.assertEquals(term.term, Term.FALL) self.assertEquals(term.year, 2012)
def test_natural_key(self): term = Term(term=Term.SPRING, year=2012) self.assertEqual(term.natural_key(), (Term.SPRING, 2012))
def test_verbose_name_current(self): term = Term(term=Term.FALL, year=2012, current=True) self.assertEqual(term.verbose_name(), "Fall 2012")
def test_inequality(self): spring = Term(term=Term.SPRING, year=2012) fall = Term(term=Term.FALL, year=2012) self.assertTrue(spring != fall)
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)
class SurveyFormTest(TestCase): def setUp(self): self.department_cs = Department( long_name='Computer Science', short_name='CS', abbreviation='COMP SCI') self.department_cs.save() self.department_me = Department( long_name='Mechanical Engineering', short_name='ME', abbreviation='MEC ENG') self.department_me.save() self.course_cs1 = Course( department=self.department_cs, number='1') self.course_cs1.save() self.course_me50 = Course( department=self.department_me, number='50') self.course_me50.save() self.instructor_test = Instructor( first_name='Tau', last_name='Bate', department=self.department_cs) self.instructor_test.save() self.term_test = Term( term=Term.SPRING, year=2013, current=True) self.term_test.save() self.user_test = get_user_model().objects.create_user( username='******', email='tbp.berkeley.edu', password='******', first_name='Tau', last_name='Bate') self.user_test.save() def test_courses_as_optgroups(self): optgroups = courses_as_optgroups() # optgrous[i] returns [dept.long_name, list of courses in dept] # optgroups[i][1] returns a list of tuples in the form of # (Course, Course.abbreviation()) self.assertEqual(optgroups[0][0], self.department_cs.long_name) self.assertEqual(optgroups[1][1][0][0], self.course_me50) def test_valid_form(self): test_form = SurveyForm() self.assertFalse(test_form.is_valid()) form_data = { 'course': self.course_me50.pk, 'term': self.term_test.pk, 'instructor': self.instructor_test.pk, 'prof_rating': 1, 'course_rating': 2, 'time_commitment': 3, 'comments': 'Test Comment', 'submitter': self.user_test.pk } test_form = SurveyForm(form_data) self.assertTrue(test_form.is_valid())
def test_save(self): spring = Term(term=Term.SPRING, year=2012, current=False) spring.save() fall = Term(term=Term.FALL, year=2012, current=False) fall.save() self.assertFalse(Term.objects.filter(current=True).exists()) spring.current = True spring.save() current = Term.objects.filter(current=True) self.assertEquals(len(current), 1) self.assertEquals(current[0].year, 2012) self.assertEquals(current[0].term, Term.SPRING) fall.current = True fall.save() current = Term.objects.filter(current=True) self.assertEquals(len(current), 1) self.assertEquals(current[0].year, 2012) self.assertEquals(current[0].term, Term.FALL)
def test_get_url_name(self): term = Term(term=Term.WINTER, year=2012, current=True) self.assertEqual(term.get_url_name(), 'wi2012') term = Term(term=Term.SPRING, year=2012, current=True) self.assertEqual(term.get_url_name(), 'sp2012') term = Term(term=Term.SUMMER, year=2012, current=True) self.assertEqual(term.get_url_name(), 'su2012') term = Term(term=Term.FALL, year=2012, current=True) self.assertEqual(term.get_url_name(), 'fa2012') term = Term(term=Term.UNKNOWN, year=2012, current=True) self.assertEqual(term.get_url_name(), 'un2012')
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()
class EventTesting(TestCase): """Define a common setUp and helper method for event testing. Subclassed below for ease of testing various aspects of events. """ def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition( short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type') 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 def assert_can_view(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertTrue( event.can_user_view(self.user), 'Should be able to view {} event'.format( event.get_restriction_display())) def assert_cannot_view(self, event, user): """Assert that the given event cannot be viewed by the given user.""" self.assertFalse( event.can_user_view(self.user), 'Should not be able to view {} event'.format( event.get_restriction_display())) def assert_can_sign_up(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertTrue( event.can_user_sign_up(self.user), 'Should be able to sign up for {} event'.format( event.get_restriction_display())) def assert_cannot_sign_up(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertFalse( event.can_user_sign_up(self.user), 'Should not be able to sign up for {} event'.format( event.get_restriction_display()))
def test_get_current_term_undefined(self): Term(term=Term.SPRING, year=2012, current=False).save() Term(term=Term.FALL, year=2012, current=False).save() term = Term.objects.get_current_term() self.assertIsNone(term)
class OfficerGroupsTest(TestCase): def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition( short_name="vp", long_name="Vice President", rank=1, mailing_list="vp", executive=True ) self.position_exec.save() self.position_regular = OfficerPosition( short_name="it", long_name="Information Technology", rank=2, mailing_list="it" ) self.position_regular.save() self.position_auxiliary = OfficerPosition( short_name="advisor", long_name="Advisor", rank=3, mailing_list="advisor", auxiliary=True ) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name="Officer") self.officer_group_curr = Group.objects.create(name="Current Officer") self.exec_group = Group.objects.create(name="Executive") self.exec_group_curr = Group.objects.create(name="Current Executive") # Groups from officer positions: self.pos_exec_group = Group.objects.create(name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create(name="Current {}".format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create(name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create(name="Current {}".format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create(name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create(name="Current {}".format(self.position_auxiliary.long_name)) def test_get_corresponding_groups(self): """Test the OfficerPosition.get_corresponding_groups method.""" # If we specify the term that is the current term, then the "Current" # groups should be included; otherwise, "Current" groups should not be # included. # Check the corresponding groups for a "regular" (non-auxiliary, # non-exec) officer position. We should expect the corresponding groups # to be Officer and the group specific to the position: groups = [self.officer_group, self.pos_reg_group] self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups()) self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([self.officer_group_curr, self.pos_reg_group_curr]) self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups(term=self.term)) # For the executive position, the corresponding groups will also # include the "Executive" groups: groups = [self.officer_group, self.exec_group, self.pos_exec_group] self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups()) self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([self.officer_group_curr, self.exec_group_curr, self.pos_exec_group_curr]) self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups(term=self.term)) # For the auxiliary position, there should be no "Officer" group or # "Executive" group (since the position is non-exec): groups = [self.pos_aux_group] self.assertItemsEqual(groups, self.position_auxiliary.get_corresponding_groups()) self.assertItemsEqual(groups, self.position_auxiliary.get_corresponding_groups(self.term_old)) # For the current term: groups.append(self.pos_aux_group_curr) self.assertItemsEqual(groups, self.position_auxiliary.get_corresponding_groups(term=self.term)) def test_add_groups(self): # Test the Officer method for adding the user to groups. Note that no # officer objects are saved, as that would activate post-saves, which # are tested seprately. officer = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = self.position_regular.get_corresponding_groups(term=self.term) self.assertFalse(self.user.groups.exists()) officer._add_user_to_officer_groups() # Check that all of the expected groups were added for this user: self.assertTrue(self.user.groups.exists()) for group in expected_groups: self.assertTrue(self.user.groups.filter(pk=group.pk).exists()) def test_remove_groups(self): # Test the Officer method for removing the user from groups. Note that # unlike test_add_groups, this method saves the Officer objets, as the # _remove_user_from_officer_groups method depends on database entries # to work properly. Thus, this method relies on post-save functions for # adding groups for a user. (Post-saves are also tested separately.) self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add the groups for an exec officer position manually so that the # Officer object is not in the database (and does not need to be # deleted here before we can test the removal function), and the user's # group count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec._add_user_to_officer_groups() self.assertTrue(len(groups) < self.user.groups.count()) # Now remove groups from the exec position, and the user's groups # should return to the same positions as from before the exec position # added any: officer_exec._remove_user_from_officer_groups() self.assertItemsEqual(groups, list(self.user.groups.all())) def test_officer_post_save(self): """Test that a user is added to the appropriate groups on post-save.""" self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = set(self.position_regular.get_corresponding_groups(term=self.term)) self.assertFalse(self.user.groups.exists()) officer_reg.save() # Check that all of the expected groups were added for this user: self.assertItemsEqual(expected_groups, self.user.groups.all()) # Add another position, and check that the correct groups are added: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term_old) officer_exec.save() expected_groups.update(self.position_exec.get_corresponding_groups(term=self.term_old)) self.assertItemsEqual(expected_groups, self.user.groups.all()) def test_officer_post_delete(self): """Test that a user is removed from the appropriate groups on post-delete. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add an exec officer position for this user, and the user's group # count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() self.assertTrue(len(groups) < self.user.groups.count()) # Now delete exec officer, and the user's groups should return to the # same positions as from before the exec position added any: officer_exec.delete() self.assertItemsEqual(groups, list(self.user.groups.all())) # And delete the regular officer, and the user should be part of no # more groups: officer_reg.delete() self.assertFalse(self.user.groups.exists()) def test_term_post_save(self): """Test that when terms are saved, the "Current" groups are kept up-to-date. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add an exec officer position (for which the post-save should add # groups) in the current term: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() expected_groups = set(self.position_exec.get_corresponding_groups(term=self.term)) groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) self.assertItemsEqual(groups, expected_groups) # Make sure saving the current term is a no-op: self.term.save() groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Add a regular officer position for this user in a new term (not # "current"), and the user's group count should increase: term_new = Term(term=Term.FALL, year=2012) term_new.save() officer_reg = Officer(user=self.user, position=self.position_regular, term=term_new) officer_reg.save() expected_groups.update(self.position_regular.get_corresponding_groups(term=term_new)) groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Now change the "new" term to be the current term: term_new.current = True term_new.save() # The self.term object is stale now, so re-fetch it from the database: self.term = Term.objects.get(pk=self.term.pk) # We should expect that the user should still be a "Current Officer", # but no longer "Current" for the groups specific to the exec position. groups = list(self.user.groups.all()) # Get "expected_groups" over again, since the current term has changed: expected_groups = set(self.position_exec.get_corresponding_groups(term=self.term)) expected_groups.update(self.position_regular.get_corresponding_groups(term=term_new)) self.assertItemsEqual(groups, expected_groups) # Double-check some of the "Current" groups: self.assertNotIn(self.exec_group_curr, groups) self.assertNotIn(self.pos_exec_group_curr, groups) self.assertIn(self.officer_group_curr, groups) self.assertIn(self.pos_reg_group_curr, groups)
class ProjectReportTest(TestCase): fixtures = ['officer_position.yaml'] def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition.objects.get(short_name='it') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.project_report = ProjectReport( term=self.term, author=self.user, committee=self.committee, title='Test', date=timezone.now().date()) self.project_report.save() self.mox = mox.Mox() self.tz = timezone.get_current_timezone() def tearDown(self): self.mox.UnsetStubs() def test_completed_time(self): self.mox.StubOutWithMock(timezone, 'now') mock_time = timezone.make_aware( datetime.datetime(2012, 01, 01, 01, 01, 01), self.tz) timezone.now().MultipleTimes().AndReturn(mock_time) self.mox.ReplayAll() self.assertFalse(self.project_report.complete) self.assertIsNone(self.project_report.first_completed_at) self.project_report.complete = True self.project_report.save() self.assertEquals(self.project_report.first_completed_at, mock_time) self.assertTrue(self.project_report.complete) # Make sure that we can unset the completed_at date if it's no longer # complete. self.project_report.complete = False self.project_report.save() self.assertFalse(self.project_report.complete) self.assertIsNone(self.project_report.first_completed_at) self.mox.VerifyAll() def test_complete_time_no_overwrite(self): original_time = timezone.make_aware( datetime.datetime(2012, 01, 01, 01, 01, 01), self.tz) self.project_report.complete = True self.project_report.first_completed_at = original_time self.project_report.save() # Make sure that a save doesn't change anything if there is already a # time recorded. self.assertEquals(self.project_report.first_completed_at, original_time) self.assertTrue(self.project_report.complete) def test_word_count(self): self.project_report.description = 'one two' self.project_report.purpose = 'three four' self.project_report.organization = 'five six' self.project_report.cost = 'seven eight' self.project_report.problems = 'nine' self.project_report.results = 'ten' self.assertEquals(self.project_report.word_count(), 10)
def test_equality(self): one = Term(term=Term.FALL, year=2012) two = Term(term=Term.FALL, year=2012) self.assertTrue(one == two)