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 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 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 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_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 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_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_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 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): 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 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 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_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_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 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_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.position = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.position.save()
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_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_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): 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 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 test_verbose_name_current(self): term = Term(term=Term.FALL, year=2012, current=True) self.assertEqual(term.verbose_name(), 'Fall 2012')
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)
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_inequality(self): spring = Term(term=Term.SPRING, year=2012) fall = Term(term=Term.FALL, year=2012) self.assertTrue(spring != fall)
def test_equality(self): one = Term(term=Term.FALL, year=2012) two = Term(term=Term.FALL, year=2012) self.assertTrue(one == two)