def test_get_assignments(self): """ Test getting the assignments of a course """ sample.sample_data_few() course = Course.objects.get(name="c1") view = CourseViewSet.as_view(actions={'get': 'get_assignments'}) request = self.factory.get('/course/' + str(course.id) + '/get_assignments/') response = view(request, pk=course.id) # Check the response self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() assignments = json.loads(response.content.decode('utf-8')) # Check that the course has two assignments, hw1 and hw2. self.assertTrue('assignments' in assignments, "Did not get valid response") assignments = assignments['assignments'] self.assertEqual(len(assignments), 2, "Course should have 2 assignments.") self.assertTrue( assignments[0]['description'] == 'Homework 1' or assignments[1]['description'] == 'Homework 1', 'Homework 1 should be returned') self.assertTrue( assignments[0]['description'] == 'Homework 2' or assignments[1]['description'] == 'Homework 2', 'Homework 2 should be returned')
def test_create_with_users(self): """ Try creating a new course with users """ sample.sample_data_few() data = { 'name': 'test_name', 'description': 'test_desc', 'students': [1, 2], 'tas': [2, 3], 'instructors': [3], } view = CourseViewSet.as_view(actions={'post': 'create'}) request = self.factory.post('/course/', data) response = view(request) self.assertEqual(response.status_code, 201, "Did not get a 201 HTTP response.") response.render() # get the course then check its students, tas, and instructors course = Course.objects.get(name='test_name') self.check_get_access_codes(course) self.assertEqual(len(course.students.all()), 2, "Course should have two students.") self.assertEqual(len(course.tas.all()), 2, "Course should have two tas.") self.assertEqual(len(course.instructors.all()), 1, "Course should have one instructor.") self.assertEqual(course.instructors.all()[0].id, 3, "Course has wrong instructor.")
def test_get_submission(self): """ Try getting a submission (just testing models, not code) """ sample.sample_data_few() submission = Submission.objects.all() self.assertTrue(len(submission) != 0, "Failed getting submission")
def test_save_course_many(self): """ Try saving many users with possible duplicate whitespace, commas, or emails """ sample.sample_data_few() course = Course.objects.get(name="c1") data = { "course_description": "new_description", "students": "[email protected],,\n,[email protected]", "tas": "[email protected] [email protected] [email protected]", "regenerate_access": True, } view = CourseViewSet.as_view(actions={'post': 'save_course'}) request = self.factory.post( '/course/' + str(course.id) + "/save_course/", data) response = view(request, pk=course.pk) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() # Check the course course = Course.objects.get(name='c1') self.assertEqual(len(course.students.all()), 2, "Should have 2 students") self.assertEqual(len(course.tas.all()), 2, "Should have 2 tas")
def test_delete_submission(self): """ Try deleting a submission (just testing models, not code) """ sample.sample_data_few() submission = Submission.objects.get(pk=1) submission.delete() try: Submission.objects.get(pk=1) self.assertFalse(True, "Exception should have been thrown") except Submission.DoesNotExist: pass
def test_get_submissions_no_filter(self): """ test get_submissions with no filter """ sample.sample_data_few() view = SubmissionViewSet.as_view(actions={'post': 'get_submissions'}) data = {} request = self.factory.post('/submission/get_submissions/', data) response = view(request) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() data = json.loads(response.content.decode('utf-8')) self.assertEqual(len(data['submissions']), 2, "Should have two submissions.")
def test_get_assignments_due_week_none(self): """ Create sample data with a user with no assignments due in a week """ sample.sample_data_few() me = User.objects.get(email="*****@*****.**") view = UserViewSet.as_view(actions={'get': 'assignments_due_week'}) request = self.factory.get('/user/' + str(me.id) + '/assignments_due_week/') response = view(request, pk=me.id) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() satisfying_assignments = json.loads(response.content.decode('utf-8')) # No assignments should be returned self.assertEqual(len(satisfying_assignments), 0, "Should not have received an assignment back for u3 " "(they are only an instructor).")
def test_get_submissions_assignment_filter(self): """ Test get_submissions using assignment filter """ sample.sample_data_few() view = SubmissionViewSet.as_view(actions={'post': 'get_submissions'}) data = {'assignmentid': 4} request = self.factory.post('/submission/get_submissions/', data) response = view(request) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() data = json.loads(response.content.decode('utf-8')) # Should have one submission self.assertEqual(len(data['submissions']), 1, "Should have one submission.") self.assertEqual(data['submissions'][0]['assignment'], "c23hw1", "Incorrect submission.")
def test_get_submissions_userid(self): """ Test get_submissions using userid filter """ sample.sample_data_few() view = SubmissionViewSet.as_view(actions={'post': 'get_submissions'}) data = {'userid': 1} request = self.factory.post('/submission/get_submissions/', data) response = view(request) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() data = json.loads(response.content.decode('utf-8')) # should have 1 submission with 1 assignment self.assertEqual(len(data['submissions']), 1, "Incorrect number of submissions returned.") self.assertEqual(data['submissions'][0]['assignment'], 'c3hw1', "Received wrong data.")
def test_get_course_student_multiple(self): """ Check getting the coruses for a user in multiple courses """ sample.sample_data_few() user = User.objects.get(email="*****@*****.**") view = UserViewSet.as_view(actions={'get': 'get_course'}) request = self.factory.get('/user/' + str(user.id) + '/get_course/') response = view(request, pk=user.id) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() courses = json.loads(response.content.decode('utf-8')) # check two courses back, 'c2' and 'c23' self.assertEqual(len(courses['student']), 2, "User should be a student in 2 classes.") self.assertTrue(courses['student'][0]['name'] == 'c2' or courses['student'][1]['name'] == 'c2', "User u2 not in course c2.") self.assertTrue(courses['student'][0]['name'] == 'c23' or courses['student'][1]['name'] == 'c23', "User u2 not in course c23.") self.assertEqual(len(courses['ta']), 0, "User should not be a TA.") self.assertEqual(len(courses['instructor']), 0, "User should not be an Instructor.")
def test_get_course_instructor(self): """ Check get_course for a user that is an instructor """ sample.sample_data_few() user = User.objects.get(email="*****@*****.**") view = UserViewSet.as_view(actions={'get': 'get_course'}) request = self.factory.get('/user/' + str(user.id) + '/get_course/') response = view(request, pk=user.id) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() courses = json.loads(response.content.decode('utf-8')) # shoulld only be an instructor of two courses self.assertEqual(len(courses['student']), 0, "User should not be a student.") self.assertEqual(len(courses['ta']), 0, "User should not be a ta.") self.assertEqual(len(courses['instructor']), 2, "User should be an instructor for 2 courses.") self.assertTrue(courses['instructor'][0]['name'] == 'c23' or courses['instructor'][1]['name'] == 'c23', "User u3 not in course c23.") self.assertTrue(courses['instructor'][0]['name'] == 'c3' or courses['instructor'][1]['name'] == 'c3', "User u3 not in course c3.")
def test_get_users_multiple(self): """ Test getting the users of a course with a student and instructor """ sample.sample_data_few() course = Course.objects.get(name="c23") view = CourseViewSet.as_view(actions={'get': 'get_users'}) request = self.factory.get('/course/' + str(course.id) + '/get_assignments/') response = view(request, pk=course.id) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() users = json.loads(response.content.decode('utf-8')) # should have 1 student and 1 instructor self.assertTrue('students' in users, "Did not get valid response") self.assertEqual(len(users['students']), 1, "Did not get one user back.") self.assertEqual(len(users['tas']), 0, "Course should have no TAs.") self.assertEqual(len(users['instructors']), 1, "Did not got one instructor back.")
def test_get_assignments_empty(self): """ Test getting the assignments of ac ourse with no assignments """ sample.sample_data_few() course = Course.objects.get(name="c4") view = CourseViewSet.as_view(actions={'get': 'get_assignments'}) request = self.factory.get('/course/' + str(course.id) + '/get_assignments/') response = view(request, pk=course.id) # Check the response self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() assignments = json.loads(response.content.decode('utf-8')) self.assertTrue('assignments' in assignments, "Did not get valid response") assignments = assignments['assignments'] # should be empty self.assertEqual(len(assignments), 0, "Course should have no assignments.")
def test_save_course_empty(self): """ try saving no changes (nothing should change) """ sample.sample_data_few() course = Course.objects.get(name="c1") data = {} view = CourseViewSet.as_view(actions={'post': 'save_course'}) request = self.factory.post( '/course/' + str(course.id) + "/save_course/", data) response = view(request, pk=course.pk) self.assertEqual(response.status_code, 200, "Did not get a 200 HTTP response.") response.render() # check properties are the same course = Course.objects.get(name='c1') self.assertEqual(course.description, "Course One", "Course description should be unchanged") self.assertEqual(len(course.students.all()), 1, "Should have 2 student") self.assertEqual(len(course.tas.all()), 0, "Should have 0 tas")
def test_get_models(self): """ Test getting the instructors, students, and assignments of a course. This is checking the models are working, not anything implemented. """ sample.sample_data_few() course = Course.objects.get(name="c23") # Get a course, check its instructors and students self.assertEqual(course.instructors.count(), 1, "Course doesn't have exactly one instructor.") self.assertEqual(course.students.count(), 1, "Course doesn't have exactly one student.") self.assertEqual(course.assignment_set.count(), 1, "Course doesn't have exactly one assignment.") # check its assignments ass = course.assignment_set.get(name="c23hw1") self.assertEqual(ass.submission_set.count(), 1, "assignment doesn't have exactly one submission") # check the assignment submission u2 = User.objects.get(email="*****@*****.**") sub = ass.submission_set.get(submitter=u2) self.assertEqual(sub.text, "My submission", "Incorrect submission text.")
def test_create_with_instructor(self): """ Try creating a new course with 1 instructor (the common use case) Even though not passed in, students and tas should be init to empty """ sample.sample_data_few() data = { 'name': 'test_name', 'description': 'test_desc', 'instructors': [2], } view = CourseViewSet.as_view(actions={'post': 'create'}) request = self.factory.post('/course/', data) response = view(request) self.assertEqual(response.status_code, 201, "Did not get a 201 HTTP response.") response.render() course = Course.objects.get(name='test_name') # check the access codes self.check_get_access_codes(course) insts = course.instructors.all() self.assertEqual(len(insts), 1, "Course should have one instructor.") self.assertEqual(insts[0].email, "*****@*****.**", "Instructor should be u2")