def setUp(self): self.cs61a = CourseFactory.create(name="CS61A") self.user = UserFactory.create(email="*****@*****.**") mentor = MentorFactory.create(user=self.user) SectionFactory.create(course=self.cs61a, mentor=mentor) # Create a bunch of random guys for the heck of it for section in SectionFactory.create_batch(10, course=self.cs61a): StudentFactory.create(section=section)
def test_mentor_in_other_course(self): """ Tests that enrollment should succeed when the student is a mentor for another course, but not a student in this one. """ mentor = MentorFactory.create(user=self.user) SectionFactory.create(course=self.other_course, mentor=mentor) my_section = SectionFactory.create(course=self.my_course) self.enroll_and_should_succeed(my_section)
def test_student_in_other_course(self): """ Tests that enrollment should succeed when the student is already enrolled in a section in another course, but not this one. """ other_section = SectionFactory.create(course=self.other_course) my_section = SectionFactory.create(course=self.my_course) utils.enroll_user_as_student(self.user, other_section) self.enroll_and_should_succeed(my_section)
def test_already_enrolled(self): """ Tests that enrollment should fail if the user is already enrolled in a section in this course. """ section = SectionFactory.create(course=self.my_course) self.enroll_and_should_succeed(section) self.enroll_and_should_fail(section)
def test_simple(self): """ Tests that enrollment succeeds when the student is enrolled in/is a mentor for no other sections. """ section = SectionFactory.create(course=self.my_course) self.enroll_and_should_succeed(section)
def test_is_mentor(self): """ Tests that a mentor should be unable to enroll in a section in their course. """ mentor = MentorFactory.create(user=self.user) section = SectionFactory.create(course=self.other_course, mentor=mentor) self.enroll_and_should_fail(section)
def test_with_other_sections(self): """ Tests that a student enrolled in multiple sections only drops the desired one. """ student1 = utils.enroll_user_as_student( self.user, SectionFactory.create(course=self.my_course)) student2 = utils.enroll_user_as_student( self.user, SectionFactory.create(course=self.other_course)) self.drop_and_should_succeed(student1) self.assertEqual( self.count_student_profiles_in(self.user, self.my_course), 0) self.assertEqual( self.count_student_profiles_in(self.user, self.other_course), 1) self.drop_and_should_succeed(student2) self.assertEqual( self.count_student_profiles_in(self.user, self.my_course), 0) self.assertEqual( self.count_student_profiles_in(self.user, self.other_course), 0)
def test_simple(self): """ Tests that the student should be able to drop a section they just enrolled in. """ student = utils.enroll_user_as_student( self.user, SectionFactory.create(course=self.my_course)) self.drop_and_should_succeed(student) self.assertEqual( self.count_student_profiles_in(self.user, self.my_course), 0)
def test_mentor_and_student(self): student_section = self.make_default_section() student = StudentFactory.create(user=self.user, section=student_section) mentor = MentorFactory.create(user=self.user) mentor_section = SectionFactory.create( mentor=mentor, course=CourseFactory.create( name='CS70', title='Discrete Mathematics and Probability Theory' ), spacetimes=[ SpacetimeFactory.create( start_time=time(hour=11), day_of_week='Tuesday', duration=timedelta(hours=1.5), location='Cory 7' ), SpacetimeFactory.create( start_time=time(hour=11), day_of_week='Thursday', duration=timedelta(hours=1.5), location='Cory 11' ) ] ) response = self.client.get(self.endpoint) self.assertEqual(len(response.data), 2) self.assertIn(self.get_default_response(student, student_section), response.data) # CS 70 is a special case due to multiple spacetimes self.assertIn({ 'id': mentor.pk, 'section_id': mentor_section.pk, 'section_spacetimes': [ { 'id': mentor_section.spacetimes.all()[0].pk, 'start_time': '11:00:00', 'day_of_week': 'Tuesday', 'duration': '01:30:00', # IMPORTANT: note the AM here to avoid ambiguity 'time': 'Tuesday 11:00 AM-12:30 PM', 'location': 'Cory 7', 'override': None }, { 'id': mentor_section.spacetimes.all()[1].pk, 'start_time': '11:00:00', 'day_of_week': 'Thursday', 'duration': '01:30:00', 'time': 'Thursday 11:00 AM-12:30 PM', 'location': 'Cory 11', 'override': None }, ], 'course': 'CS70', 'course_id': mentor_section.course.id, 'course_title': 'Discrete Mathematics and Probability Theory', 'role': "MENTOR" }, response.data)
def test_reenroll(self): """ Tests that a student that previously dropped a section is able to reenroll in this course. """ section = SectionFactory.create(course=self.my_course) student = utils.enroll_user_as_student(self.user, section) self.drop(student) # New response should return 204 instead of 201 url = get_enroll_url_for(section) self.request(self.client.put, url, exp_code=status.HTTP_204_NO_CONTENT) self.assertNotEqual(self.count_student_profiles_in(section.course), 0)
def make_default_section(self, mentor=None): return SectionFactory.create( mentor=mentor, course=CourseFactory.create(name='CS61C', title='Machine Structures'), spacetimes=[ SpacetimeFactory.create( start_time=time(hour=13), day_of_week='Monday', duration=timedelta(hours=1), location='Soda 1337' ) ] )
def test_double_drop(self): """ Tests that dropping a section twice should fail the second call and make no DB changes. """ student = utils.enroll_user_as_student( self.user, SectionFactory.create(course=self.my_course)) self.drop_and_should_succeed(student) self.assertEqual( self.count_student_profiles_in(self.user, self.my_course), 0) url = get_drop_url_for(student) self.request(self.client.patch, url, exp_code=status.HTTP_403_FORBIDDEN) self.assertEqual( self.count_student_profiles_in(self.user, self.my_course), 0)
def test_without_enroll(self): """ Tests that a student should be unable to drop a student that's not themselves. """ other_student = utils.enroll_user_as_student( UserFactory.create(), SectionFactory.create(course=self.my_course)) url = get_drop_url_for(other_student) self.request(self.client.patch, url, exp_code=status.HTTP_403_FORBIDDEN) self.assertEqual( self.count_student_profiles_in(self.user, self.my_course), 0) self.assertEqual( self.count_student_profiles_in(other_student.user, self.my_course), 1)
def test_full_section(self): """ Tests that enrollment should not fail when the student attempts to enroll in a full section. """ section = SectionFactory.create(course=self.my_course, capacity=0) self.enroll_and_should_fail(section)
def test_last_spot(self): """ Tests that enrollment should succeed if the section is one away from capacity. """ section = SectionFactory.create(course=self.my_course, capacity=1) self.enroll_and_should_succeed(section)
def create_empty_section_for(mentor): """ Creates a section for MENTOR without populated students. """ return SectionFactory.create(course=mentor.course, mentor=mentor)