Commit 7da5e1c3 by Afzal Wali

Improved coverage. Started using the ExamAttempt serializer.

parent d168dd5f
...@@ -149,7 +149,8 @@ def get_exam_attempt(exam_id, user_id): ...@@ -149,7 +149,8 @@ def get_exam_attempt(exam_id, user_id):
Return an existing exam attempt for the given student Return an existing exam attempt for the given student
""" """
exam_attempt_obj = ProctoredExamStudentAttempt.get_exam_attempt(exam_id, user_id) exam_attempt_obj = ProctoredExamStudentAttempt.get_exam_attempt(exam_id, user_id)
return exam_attempt_obj.__dict__ if exam_attempt_obj else None serialized_attempt_obj = ProctoredExamStudentAttemptSerializer(exam_attempt_obj)
return serialized_attempt_obj.data if exam_attempt_obj else None
def create_exam_attempt(exam_id, user_id, external_id): def create_exam_attempt(exam_id, user_id, external_id):
......
...@@ -46,14 +46,16 @@ class ProctoredExamStudentAttemptSerializer(serializers.ModelSerializer): ...@@ -46,14 +46,16 @@ class ProctoredExamStudentAttemptSerializer(serializers.ModelSerializer):
""" """
Serializer for the ProctoredExamStudentAttempt Model. Serializer for the ProctoredExamStudentAttempt Model.
""" """
proctored_exam_id = serializers.IntegerField(source="proctored_exam_id")
class Meta: class Meta:
""" """
Meta Class Meta Class
""" """
model = ProctoredExamStudentAttempt model = ProctoredExamStudentAttempt
fields = ( fields = (
"id", "created", "modified", "user_id", "started_at", "completed_at", "id", "created", "modified", "user_id", "started_at", "completed_at",
"external_id", "status" "external_id", "status", "proctored_exam_id"
) )
......
...@@ -14,14 +14,14 @@ from edx_proctoring.api import ( ...@@ -14,14 +14,14 @@ from edx_proctoring.api import (
stop_exam_attempt, stop_exam_attempt,
get_active_exams_for_user, get_active_exams_for_user,
get_exam_attempt, get_exam_attempt,
create_exam_attempt create_exam_attempt,
) get_student_view)
from edx_proctoring.exceptions import ( from edx_proctoring.exceptions import (
ProctoredExamAlreadyExists, ProctoredExamAlreadyExists,
ProctoredExamNotFoundException, ProctoredExamNotFoundException,
StudentExamAttemptAlreadyExistsException, StudentExamAttemptAlreadyExistsException,
StudentExamAttemptDoesNotExistsException StudentExamAttemptDoesNotExistsException,
) StudentExamAttemptedAlreadyStarted)
from edx_proctoring.models import ( from edx_proctoring.models import (
ProctoredExam, ProctoredExam,
ProctoredExamStudentAllowance, ProctoredExamStudentAllowance,
...@@ -64,7 +64,17 @@ class ProctoredExamApiTests(LoggedInTestCase): ...@@ -64,7 +64,17 @@ class ProctoredExamApiTests(LoggedInTestCase):
time_limit_mins=self.default_time_limit time_limit_mins=self.default_time_limit
) )
def _create_student_exam_attempt(self): def _create_unstarted_exam_attempt(self):
"""
Creates the ProctoredExamStudentAttempt object.
"""
return ProctoredExamStudentAttempt.objects.create(
proctored_exam_id=self.proctored_exam_id,
user_id=self.user_id,
external_id=self.external_id
)
def _create_started_exam_attempt(self):
""" """
Creates the ProctoredExamStudentAttempt object. Creates the ProctoredExamStudentAttempt object.
""" """
...@@ -195,30 +205,54 @@ class ProctoredExamApiTests(LoggedInTestCase): ...@@ -195,30 +205,54 @@ class ProctoredExamApiTests(LoggedInTestCase):
attempt_id = create_exam_attempt(self.proctored_exam_id, self.user_id, '') attempt_id = create_exam_attempt(self.proctored_exam_id, self.user_id, '')
self.assertGreater(attempt_id, 0) self.assertGreater(attempt_id, 0)
def test_recreate_an_exam_attempt(self):
"""
Start an exam attempt that has already been created.
Raises StudentExamAttemptAlreadyExistsException
"""
proctored_exam_student_attempt = self._create_unstarted_exam_attempt()
with self.assertRaises(StudentExamAttemptAlreadyExistsException):
create_exam_attempt(proctored_exam_student_attempt.proctored_exam, self.user_id, self.external_id)
def test_get_exam_attempt(self): def test_get_exam_attempt(self):
""" """
Test to get the already made exam attempt. Test to get the already made exam attempt.
""" """
self._create_student_exam_attempt() self._create_unstarted_exam_attempt()
exam_attempt = get_exam_attempt(self.proctored_exam_id, self.user_id) exam_attempt = get_exam_attempt(self.proctored_exam_id, self.user_id)
self.assertEqual(exam_attempt['proctored_exam_id'], self.proctored_exam_id) self.assertEqual(exam_attempt['proctored_exam_id'], self.proctored_exam_id)
self.assertEqual(exam_attempt['user_id'], self.user_id) self.assertEqual(exam_attempt['user_id'], self.user_id)
def test_restart_exam_attempt(self): def test_start_uncreated_attempt(self):
""" """
Start an exam attempt that has already been started. Test to attempt starting an attempt which has not been created yet.
Raises StudentExamAttemptAlreadyExistsException should raise an exception.
""" """
proctored_exam_student_attempt = self._create_student_exam_attempt() with self.assertRaises(StudentExamAttemptDoesNotExistsException):
with self.assertRaises(StudentExamAttemptAlreadyExistsException): start_exam_attempt(self.proctored_exam_id, self.user_id)
create_exam_attempt(proctored_exam_student_attempt.proctored_exam, self.user_id, self.external_id)
def test_start_a_created_attempt(self):
"""
Test to attempt starting an attempt which has been created but not started.
"""
self._create_unstarted_exam_attempt()
start_exam_attempt(self.proctored_exam_id, self.user_id)
def test_restart_a_started_attempt(self):
"""
Test to attempt starting an attempt which has been created but not started.
"""
self._create_unstarted_exam_attempt()
start_exam_attempt(self.proctored_exam_id, self.user_id)
with self.assertRaises(StudentExamAttemptedAlreadyStarted):
start_exam_attempt(self.proctored_exam_id, self.user_id)
def test_stop_exam_attempt(self): def test_stop_exam_attempt(self):
""" """
Stop an exam attempt. Stop an exam attempt.
""" """
proctored_exam_student_attempt = self._create_student_exam_attempt() proctored_exam_student_attempt = self._create_unstarted_exam_attempt()
self.assertIsNone(proctored_exam_student_attempt.completed_at) self.assertIsNone(proctored_exam_student_attempt.completed_at)
proctored_exam_attempt_id = stop_exam_attempt( proctored_exam_attempt_id = stop_exam_attempt(
proctored_exam_student_attempt.proctored_exam, self.user_id proctored_exam_student_attempt.proctored_exam, self.user_id
...@@ -237,7 +271,7 @@ class ProctoredExamApiTests(LoggedInTestCase): ...@@ -237,7 +271,7 @@ class ProctoredExamApiTests(LoggedInTestCase):
Test to get the all the active Test to get the all the active
exams for the user. exams for the user.
""" """
active_exam_attempt = self._create_student_exam_attempt() active_exam_attempt = self._create_started_exam_attempt()
self.assertEqual(active_exam_attempt.is_active, True) self.assertEqual(active_exam_attempt.is_active, True)
exam_id = create_exam( exam_id = create_exam(
course_id=self.course_id, course_id=self.course_id,
...@@ -260,3 +294,7 @@ class ProctoredExamApiTests(LoggedInTestCase): ...@@ -260,3 +294,7 @@ class ProctoredExamApiTests(LoggedInTestCase):
self.assertEqual(len(student_active_exams), 2) self.assertEqual(len(student_active_exams), 2)
self.assertEqual(len(student_active_exams[0]['allowances']), 2) self.assertEqual(len(student_active_exams[0]['allowances']), 2)
self.assertEqual(len(student_active_exams[1]['allowances']), 0) self.assertEqual(len(student_active_exams[1]['allowances']), 0)
def test_get_student_view(self):
context = {'default_time_limit_mins': 90}
get_student_view(self.user_id, self.course_id, self.content_id, context)
...@@ -486,6 +486,14 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase): ...@@ -486,6 +486,14 @@ class TestStudentProctoredExamAttempt(LoggedInTestCase):
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
ProctoredExamStudentAttempt.objects.filter(
proctored_exam_id=proctored_exam.id,
user_id=self.user.id,
external_id=proctored_exam.external_id,
).update(
started_at=datetime.now(pytz.UTC)
)
response = self.client.get( response = self.client.get(
reverse('edx_proctoring.proctored_exam.attempt') reverse('edx_proctoring.proctored_exam.attempt')
) )
......
...@@ -239,7 +239,7 @@ class StudentProctoredExamAttempt(AuthenticatedAPIView): ...@@ -239,7 +239,7 @@ class StudentProctoredExamAttempt(AuthenticatedAPIView):
def post(self, request): def post(self, request):
""" """
HTTP POST handler. To create an exam. HTTP POST handler. To create an exam attempt.
""" """
start_immediately = request.DATA.get('start_clock', 'false').lower() == 'true' start_immediately = request.DATA.get('start_clock', 'false').lower() == 'true'
exam_id = request.DATA.get('exam_id', None) exam_id = request.DATA.get('exam_id', None)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment