def test_case_sensitive_string_incorrect_answer(self): # Given string_answer = "hello, earth!" answer = Answer(question=self.question2,answer=string_answer) answer.save() self.answerpaper.answers.add(answer) # When json_data = None result = self.answerpaper.validate_answer(string_answer, self.question2, json_data ) # Then self.assertFalse(result['success']) # Regrade # Given answer.correct = True answer.marks = 1 answer.answer = "Hello, EARTH!" answer.save() # When details = self.answerpaper.regrade(self.question2.id) # Then answer = self.answerpaper.answers.filter(question=self.question2)\ .last() self.assertTrue(details[0]) self.assertEqual(answer.marks, 1) self.assertTrue(answer.correct)
def check(request, q_id, attempt_num=None, questionpaper_id=None): """Checks the answers of the user for particular question""" user = request.user paper = get_object_or_404(AnswerPaper, user=request.user, attempt_number=attempt_num, question_paper=questionpaper_id) question = get_object_or_404(Question, pk=q_id) if question in paper.questions_answered.all(): next_q = paper.skip(q_id) return show_question(request, next_q, paper) if request.method == 'POST': snippet_code = request.POST.get('snippet') # Add the answer submitted, regardless of it being correct or not. if question.type == 'mcq': user_answer = request.POST.get('answer') elif question.type == 'mcc': user_answer = request.POST.getlist('answer') elif question.type == 'upload': assign = AssignmentUpload() assign.user = user.profile assign.assignmentQuestion = question # if time-up at upload question then the form is submitted without # validation if 'assignment' in request.FILES: assign.assignmentFile = request.FILES['assignment'] assign.save() user_answer = 'ASSIGNMENT UPLOADED' next_q = paper.completed_question(question.id) return show_question(request, next_q, paper) else: user_code = request.POST.get('answer') user_answer = snippet_code + "\n" + user_code if snippet_code else user_code new_answer = Answer(question=question, answer=user_answer, correct=False) new_answer.save() paper.answers.add(new_answer) # If we were not skipped, we were asked to check. For any non-mcq # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. test_cases = TestCase.objects.filter(question=question) json_data = question.consolidate_answer_data(test_cases, user_answer) \ if question.type == 'code' else None correct, result = validate_answer(user, user_answer, question, json_data) if correct: new_answer.correct = correct new_answer.marks = question.points new_answer.error = result.get('error') else: new_answer.error = result.get('error') new_answer.save() paper.update_marks('inprogress') if not result.get('success'): # Should only happen for non-mcq questions. new_answer.answer = user_code new_answer.save() return show_question(request, question, paper, result.get('error')) else: # Display the same question if user_answer is None if not user_answer: msg = "Please submit a valid option or code" return show_question(request, question, paper, msg) elif question.type == 'code' and user_answer: msg = "Correct Output" paper.completed_question(question.id) return show_question(request, question, paper, msg) else: next_q = paper.completed_question(question.id) return show_question(request, next_q, paper) else: return show_question(request, question, paper)