Example #1
0
    def test_exam_endpoints(self):
        user = rand.user()
        course = Course(name='World Lit')
        section1 = Section(name='Hour 1')
        section2 = Section(name='Hour 2')
        course.sections = [section1, section2]
        exam1 = Exam(name='Exam 1', exam_format='DDD..EEDD')
        exam2 = Exam(name='Exam 2', exam_format='..FFFFFFFFFFFF')
        course.exams = [exam1, exam2]
        user.courses.append(course)
        db.session.add(course)
        db.session.add_all([user, section1, section2, exam1, exam2])
        db.session.commit()
        db.session.refresh(course)
        db.session.refresh(exam1)

        ##############################################
        # Verify that we have two exams in this course
        exams_list_response = self.client.get(
            '/api/course/{course_id}/exams'.format(course_id=course.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(exams_list_response.status_code, HTTPStatus.OK)
        exam_list_data = json.loads(exams_list_response.data.decode())
        self.assertEqual(len(exam_list_data['exams']), 2)

        ###############################################
        # Verify that a specific exam has the correct name and format
        self.assertIn('Exam 1',
                      [exam['name'] for exam in exam_list_data['exams']])
        self.assertNotIn('Exam Foo',
                         [exam['name'] for exam in exam_list_data['exams']])
        self.assertIn(
            'DDD..EEDD',
            [exam['exam_format'] for exam in exam_list_data['exams']])

        ###############################################
        # Add an Exam
        new_exam = Exam(name='Exam 3', exam_format='DDDDD')
        new_exam_request_response = self.client.post(
            '/api/course/{course_id}/exam/add'.format(course_id=course.id),
            data=json.dumps(new_exam.toJSON()),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(new_exam_request_response.status_code, HTTPStatus.OK)
        exams_list_response = self.client.get(
            '/api/course/{course_id}/exams'.format(course_id=course.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam_list_data = json.loads(exams_list_response.data.decode())
        # we now have 3 exams
        self.assertEqual(len(exam_list_data['exams']), 3)
        new_exam_id = json.loads(
            new_exam_request_response.data.decode())['exam']['id']

        ###############################################
        # Edit an exam
        update_exam_response = self.client.post(
            '/api/course/{course_id}/exam/{exam_id}/update'.format(
                course_id=course.id, exam_id=new_exam_id),
            data=json.dumps({
                'name': 'Exam 4',
                'exam_format': 'CCC'
            }),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        self.assertEqual(update_exam_response.status_code, HTTPStatus.OK)
        update_exam_response_data = json.loads(
            update_exam_response.data.decode())
        self.assertEqual('CCC',
                         update_exam_response_data['exam']['exam_format'])
        self.assertEqual('Exam 4', update_exam_response_data['exam']['name'])

        ###############################################
        # Delete an Exam
        delete_exam_response = self.client.delete(
            '/api/course/{course_id}/exam/{exam_id}'.format(
                course_id=course.id, exam_id=new_exam_id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exams_list_response = self.client.get(
            '/api/course/{course_id}/exams'.format(course_id=course.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam_list_data = json.loads(exams_list_response.data.decode())
        # we should be back to two exams
        self.assertEqual(len(exam_list_data['exams']), 2)

        ###############################################
        ###############################################
        # Create three student exams
        for answers in ['DDDTTEEDD', 'ABCTFDEAC', 'DCBFFEAAA']:
            create_student_exam_response = self.client.post(
                '/api/course/{course_id}/exam/{exam_id}/student_exam'.format(
                    course_id=course.id, exam_id=exam1.id),
                data=json.dumps({'answers': answers}),
                content_type='application/json',
                headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam1 = Exam.query.get(
            exam1.id
        )  # 'db.session.refresh' doesn't work here, as we're on to a different db session
        self.assertEqual(len(exam1.student_exams), 3)
        exam_response = self.client.get(
            '/api/course/{course_id}/exam/{exam_id}'.format(
                course_id=course.id, exam_id=exam1.id),
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam_response_data = json.loads(exam_response.data.decode())
        self.assertEqual(len(exam_response_data['exam']['student_exams']), 3)

        ###############################################
        # Update one of those student exams
        student_exam_id = exam_response_data['exam']['student_exams'][0]['id']
        self.client.post(
            '/api/course/{course_id}/exam/{exam_id}/student_exam/{student_exam_id}'
            .format(course_id=course.id,
                    exam_id=exam1.id,
                    student_exam_id=student_exam_id),
            data=json.dumps({'answers': 'AAAFFAAAA'}),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        student_exam = StudentExam.query.get(student_exam_id)
        self.assertEqual(student_exam.answers, 'AAAFFAAAA')

        ###############################################
        # Delete a student exam
        delete_student_exam_response = self.client.delete(
            '/api/course/{course_id}/exam/{exam_id}/student_exam/{student_exam_id}'
            .format(course_id=course.id,
                    exam_id=exam1.id,
                    student_exam_id=student_exam_id),
            content_type='application/json',
            headers={'Authorization': 'Bearer {}'.format(jwt_token)})
        exam1 = Exam.query.get(exam1.id)
        self.assertEqual(len(exam1.student_exams), 2)