def setUpClass(self): self.ip = '101.0.0.1' self.user = User.objects.get(id=1) self.profile = self.user.profile self.quiz = Quiz.objects.get(pk=1) self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() self.questions = Question.objects.filter(id__in=[1,2,3]) self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) self.question1 = self.questions.get(id=1) self.question2 = self.questions.get(id=2) self.question3 = self.questions.get(id=3) # create answerpaper self.answerpaper = AnswerPaper(user=self.user, question_paper=self.question_paper, start_time=self.start_time, end_time=self.end_time, user_ip=self.ip ) self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, user=self.user ) already_attempted = self.attempted_papers.count() self.answerpaper.attempt_number = already_attempted + 1 self.answerpaper.save() self.answerpaper.questions.add(*self.questions) self.answerpaper.questions_unanswered.add(*self.questions) self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), answer="Demo answer", correct=True, marks=1, error=json.dumps([]) ) self.answer_wrong = Answer(question=Question.objects.get(id=2), answer="My answer", correct=False, marks=0, error=json.dumps(['error1', 'error2']) ) self.answer_right.save() self.answer_wrong.save() self.answerpaper.answers.add(self.answer_right) self.answerpaper.answers.add(self.answer_wrong) self.question1.language = 'python' self.question1.test_case_type = 'standardtestcase' self.question1.save() self.question2.language = 'python' self.question2.type = 'mcq' self.question2.test_case_type = 'mcqtestcase' self.question2.save() self.question3.language = 'python' self.question3.type = 'mcc' self.question3.test_case_type = 'mcqtestcase' self.question3.save() self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', type = 'standardtestcase' ) self.assertion_testcase.save() self.mcq_based_testcase = McqTestCase( options = 'a', question=self.question2, correct = True, type = 'mcqtestcase' ) self.mcq_based_testcase.save() self.mcc_based_testcase = McqTestCase( question=self.question3, options = 'a', correct = True, type = 'mcqtestcase' ) self.mcc_based_testcase.save()
def setUpClass(self): # Creating User self.user = User.objects.get(username='******') self.user2 = User.objects.get(username='******') self.user_ip = '127.0.0.1' # Creating Course self.course = Course.objects.get(name="Python Course 100") # Creating Quiz self.quiz = Quiz.objects.get(description="demo quiz 100") # Creating Question paper self.question_paper = QuestionPaper.objects.get(quiz=self.quiz) self.question_paper.shuffle_testcases = True self.question_paper.save() # Creating Question self.question1 = Question.objects.create(summary='mcq1', points=1, type='code', user=self.user, ) self.question1.language = 'python' self.question1.type = "mcq" self.question1.test_case_type = 'Mcqtestcase' self.question1.description = 'Which option is Correct?' self.question1.save() # For questions self.mcq_based_testcase_1 = McqTestCase(question=self.question1, options="Correct", correct=True, type='mcqtestcase', ) self.mcq_based_testcase_1.save() self.mcq_based_testcase_2 = McqTestCase(question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_2.save() self.mcq_based_testcase_3 = McqTestCase(question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_3.save() self.mcq_based_testcase_4 = McqTestCase(question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_4.save() self.question_paper.fixed_questions.add(self.question1) self.answerpaper = self.question_paper.make_answerpaper( user=self.user, ip=self.user_ip, attempt_num=1, course_id=self.course.id ) # Answerpaper for user 2 self.answerpaper2 = self.question_paper.make_answerpaper( user=self.user2, ip=self.user_ip, attempt_num=1, course_id=self.course.id )
class AnswerPaperTestCases(unittest.TestCase): @classmethod def setUpClass(self): self.ip = '101.0.0.1' self.user = User.objects.get(id=1) self.profile = self.user.profile self.quiz = Quiz.objects.get(pk=1) self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() self.questions = Question.objects.filter(id__in=[1,2,3]) self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) self.question1 = self.questions.get(id=1) self.question2 = self.questions.get(id=2) self.question3 = self.questions.get(id=3) # create answerpaper self.answerpaper = AnswerPaper(user=self.user, question_paper=self.question_paper, start_time=self.start_time, end_time=self.end_time, user_ip=self.ip ) self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, user=self.user ) already_attempted = self.attempted_papers.count() self.answerpaper.attempt_number = already_attempted + 1 self.answerpaper.save() self.answerpaper.questions.add(*self.questions) self.answerpaper.questions_unanswered.add(*self.questions) self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), answer="Demo answer", correct=True, marks=1, error=json.dumps([]) ) self.answer_wrong = Answer(question=Question.objects.get(id=2), answer="My answer", correct=False, marks=0, error=json.dumps(['error1', 'error2']) ) self.answer_right.save() self.answer_wrong.save() self.answerpaper.answers.add(self.answer_right) self.answerpaper.answers.add(self.answer_wrong) self.question1.language = 'python' self.question1.test_case_type = 'standardtestcase' self.question1.save() self.question2.language = 'python' self.question2.type = 'mcq' self.question2.test_case_type = 'mcqtestcase' self.question2.save() self.question3.language = 'python' self.question3.type = 'mcc' self.question3.test_case_type = 'mcqtestcase' self.question3.save() self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', type = 'standardtestcase' ) self.assertion_testcase.save() self.mcq_based_testcase = McqTestCase( options = 'a', question=self.question2, correct = True, type = 'mcqtestcase' ) self.mcq_based_testcase.save() self.mcc_based_testcase = McqTestCase( question=self.question3, options = 'a', correct = True, type = 'mcqtestcase' ) self.mcc_based_testcase.save() def test_validate_and_regrade_mcc_question(self): # Given mcc_answer = ['a'] self.answer = Answer(question=self.question3, answer=mcc_answer, ) self.answer.save() self.answerpaper.answers.add(self.answer) # When json_data = None correct, result = self.answerpaper.validate_answer(mcc_answer, self.question3, json_data) # Then self.assertTrue(correct) self.assertTrue(result['success']) self.assertEqual(result['error'], ['Correct answer']) self.answer.correct = True self.answer.marks = 1 # Given self.answer.correct = True self.answer.marks = 1 self.answer.answer = ['a', 'b'] self.answer.save() # When details = self.answerpaper.regrade(self.question3.id) # Then self.answer = self.answerpaper.answers.filter(question=self.question3).last() self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) def test_validate_and_regrade_mcq_question(self): # Given mcq_answer = 'a' self.answer = Answer(question=self.question2, answer=mcq_answer, ) self.answer.save() self.answerpaper.answers.add(self.answer) # When json_data = None correct, result = self.answerpaper.validate_answer(mcq_answer, self.question2, json_data) # Then self.assertTrue(correct) self.assertTrue(result['success']) self.answer.correct = True self.answer.marks = 1 # Given self.answer.correct = True self.answer.marks = 1 self.answer.answer = 'b' self.answer.save() # When details = self.answerpaper.regrade(self.question2.id) # Then self.answer = self.answerpaper.answers.filter(question=self.question2).last() self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) def test_answerpaper(self): """ Test Answer Paper""" self.assertEqual(self.answerpaper.user.username, 'demo_user') self.assertEqual(self.answerpaper.user_ip, self.ip) questions = self.answerpaper.get_questions() num_questions = len(questions) self.assertSequenceEqual(list(questions), list(self.questions)) self.assertEqual(num_questions, 3) self.assertEqual(self.answerpaper.question_paper, self.question_paper) self.assertEqual(self.answerpaper.start_time, self.start_time) self.assertEqual(self.answerpaper.status, 'inprogress') def test_questions(self): # Test questions_left() method of Answer Paper self.assertEqual(self.answerpaper.questions_left(), 3) # Test current_question() method of Answer Paper current_question = self.answerpaper.current_question() self.assertEqual(current_question.id, 1) # Test completed_question() method of Answer Paper question = self.answerpaper.completed_question(1) self.assertEqual(self.answerpaper.questions_left(), 2) # Test next_question() method of Answer Paper current_question = self.answerpaper.current_question() self.assertEqual(current_question.id, 2) # When next_question_id = self.answerpaper.next_question(current_question.id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 3) # Given, here question is already answered current_question_id = 1 # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 2) # Given, wrong question id current_question_id = 12 # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 2) # Given, last question in the list current_question_id = 3 # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 2) # Test get_questions_answered() method # When questions_answered = self.answerpaper.get_questions_answered() # Then self.assertEqual(questions_answered.count(), 1) self.assertSequenceEqual(questions_answered, [self.questions[0]]) # When questions_unanswered = self.answerpaper.get_questions_unanswered() # Then self.assertEqual(questions_unanswered.count(), 2) self.assertSequenceEqual(questions_unanswered, [self.questions[1], self.questions[2]]) # Test completed_question and next_question # When all questions are answered current_question = self.answerpaper.completed_question(2) # Then self.assertEqual(self.answerpaper.questions_left(), 1) self.assertEqual(current_question.id, 3) # When current_question = self.answerpaper.completed_question(3) # Then self.assertEqual(self.answerpaper.questions_left(), 0) self.assertTrue(current_question is None) # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is None) def test_update_marks(self): """ Test update_marks method of AnswerPaper""" self.answerpaper.update_marks('inprogress') self.assertEqual(self.answerpaper.status, 'inprogress') self.assertTrue(self.answerpaper.is_attempt_inprogress()) self.answerpaper.update_marks() self.assertEqual(self.answerpaper.status, 'completed') self.assertEqual(self.answerpaper.marks_obtained, 1.0) self.assertEqual(self.answerpaper.percent, 33.33) self.assertTrue(self.answerpaper.passed) self.assertFalse(self.answerpaper.is_attempt_inprogress()) def test_set_end_time(self): current_time = timezone.now() self.answerpaper.set_end_time(current_time) self.assertEqual(self.answerpaper.end_time,current_time) def test_get_question_answer(self): """ Test get_question_answer() method of Answer Paper""" answered = self.answerpaper.get_question_answers() first_answer = list(answered.values())[0][0] first_answer_obj = first_answer['answer'] self.assertEqual(first_answer_obj.answer, 'Demo answer') self.assertTrue(first_answer_obj.correct) self.assertEqual(len(answered), 2) def test_is_answer_correct(self): self.assertTrue(self.answerpaper.is_answer_correct(self.questions[0])) self.assertFalse(self.answerpaper.is_answer_correct(self.questions[1])) def test_get_previous_answers(self): answers = self.answerpaper.get_previous_answers(self.questions[0]) self.assertEqual(answers.count(), 1) self.assertTrue(answers[0], self.answer_right) answers = self.answerpaper.get_previous_answers(self.questions[1]) self.assertEqual(answers.count(), 1) self.assertTrue(answers[0], self.answer_wrong) def test_set_marks (self): self.answer_wrong.set_marks(0.5) self.assertEqual(self.answer_wrong.marks, 0.5) self.answer_wrong.set_marks(10.0) self.assertEqual(self.answer_wrong.marks,1.0)
def setUpClass(self): # Creating User self.user = User.objects.get(username='******') self.user2 = User.objects.get(username='******') self.user_ip = '127.0.0.1' # Creating Course self.course = Course.objects.get(name="Python Course 100") # Creating Quiz self.quiz = Quiz.objects.get(description="demo quiz 100") # Creating Question paper self.question_paper = QuestionPaper.objects.get(quiz=self.quiz) self.question_paper.shuffle_testcases = True self.question_paper.save() # Creating Question self.question1 = Question.objects.create( summary='mcq1', points=1, type='code', user=self.user, ) self.question1.language = 'python' self.question1.type = "mcq" self.question1.test_case_type = 'Mcqtestcase' self.question1.description = 'Which option is Correct?' self.question1.save() # For questions self.mcq_based_testcase_1 = McqTestCase( question=self.question1, options="Correct", correct=True, type='mcqtestcase', ) self.mcq_based_testcase_1.save() self.mcq_based_testcase_2 = McqTestCase( question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_2.save() self.mcq_based_testcase_3 = McqTestCase( question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_3.save() self.mcq_based_testcase_4 = McqTestCase( question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_4.save() self.question_paper.fixed_questions.add(self.question1) self.answerpaper = self.question_paper.make_answerpaper( user=self.user, ip=self.user_ip, attempt_num=1, course_id=self.course.id) # Answerpaper for user 2 self.answerpaper2 = self.question_paper.make_answerpaper( user=self.user2, ip=self.user_ip, attempt_num=1, course_id=self.course.id)
class MCQShuffleTestCases(unittest.TestCase): @classmethod def setUpClass(self): # Creating User self.user = User.objects.get(username='******') self.user2 = User.objects.get(username='******') self.user_ip = '127.0.0.1' # Creating Course self.course = Course.objects.get(name="Python Course 100") # Creating Quiz self.quiz = Quiz.objects.get(description="demo quiz 100") # Creating Question paper self.question_paper = QuestionPaper.objects.get(quiz=self.quiz) self.question_paper.shuffle_testcases = True self.question_paper.save() # Creating Question self.question1 = Question.objects.create(summary='mcq1', points=1, type='code', user=self.user, ) self.question1.language = 'python' self.question1.type = "mcq" self.question1.test_case_type = 'Mcqtestcase' self.question1.description = 'Which option is Correct?' self.question1.save() # For questions self.mcq_based_testcase_1 = McqTestCase(question=self.question1, options="Correct", correct=True, type='mcqtestcase', ) self.mcq_based_testcase_1.save() self.mcq_based_testcase_2 = McqTestCase(question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_2.save() self.mcq_based_testcase_3 = McqTestCase(question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_3.save() self.mcq_based_testcase_4 = McqTestCase(question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_4.save() self.question_paper.fixed_questions.add(self.question1) self.answerpaper = self.question_paper.make_answerpaper( user=self.user, ip=self.user_ip, attempt_num=1, course_id=self.course.id ) # Answerpaper for user 2 self.answerpaper2 = self.question_paper.make_answerpaper( user=self.user2, ip=self.user_ip, attempt_num=1, course_id=self.course.id ) @classmethod def tearDownClass(self): self.question1.delete() self.answerpaper.delete() self.answerpaper2.delete() def test_shuffle_test_cases(self): # Given # When user_testcase = self.question1.get_ordered_test_cases( self.answerpaper ) order1 = [tc.id for tc in user_testcase] user2_testcase = self.question1.get_ordered_test_cases( self.answerpaper2 ) order2 = [tc.id for tc in user2_testcase] self.question_paper.shuffle_testcases = False self.question_paper.save() answerpaper3 = self.question_paper.make_answerpaper( user=self.user2, ip=self.user_ip, attempt_num=self.answerpaper.attempt_number+1, course_id=self.course.id ) not_ordered_testcase = self.question1.get_ordered_test_cases( answerpaper3 ) get_test_cases = self.question1.get_test_cases() # Then self.assertNotEqual(order1, order2) self.assertEqual(get_test_cases, not_ordered_testcase) answerpaper3.delete()
class MCQShuffleTestCases(unittest.TestCase): @classmethod def setUpClass(self): # Creating User self.user = User.objects.get(username='******') self.user2 = User.objects.get(username='******') self.user_ip = '127.0.0.1' # Creating Course self.course = Course.objects.get(name="Python Course 100") # Creating Quiz self.quiz = Quiz.objects.get(description="demo quiz 100") # Creating Question paper self.question_paper = QuestionPaper.objects.get(quiz=self.quiz) self.question_paper.shuffle_testcases = True self.question_paper.save() # Creating Question self.question1 = Question.objects.create( summary='mcq1', points=1, type='code', user=self.user, ) self.question1.language = 'python' self.question1.type = "mcq" self.question1.test_case_type = 'Mcqtestcase' self.question1.description = 'Which option is Correct?' self.question1.save() # For questions self.mcq_based_testcase_1 = McqTestCase( question=self.question1, options="Correct", correct=True, type='mcqtestcase', ) self.mcq_based_testcase_1.save() self.mcq_based_testcase_2 = McqTestCase( question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_2.save() self.mcq_based_testcase_3 = McqTestCase( question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_3.save() self.mcq_based_testcase_4 = McqTestCase( question=self.question1, options="Incorrect", correct=False, type='mcqtestcase', ) self.mcq_based_testcase_4.save() self.question_paper.fixed_questions.add(self.question1) self.answerpaper = self.question_paper.make_answerpaper( user=self.user, ip=self.user_ip, attempt_num=1, course_id=self.course.id) # Answerpaper for user 2 self.answerpaper2 = self.question_paper.make_answerpaper( user=self.user2, ip=self.user_ip, attempt_num=1, course_id=self.course.id) @classmethod def tearDownClass(self): self.question1.delete() self.answerpaper.delete() self.answerpaper2.delete() def test_shuffle_test_cases(self): # Given # When user_testcase = self.question1.get_ordered_test_cases(self.answerpaper) order1 = [tc.id for tc in user_testcase] user2_testcase = self.question1.get_ordered_test_cases( self.answerpaper2) order2 = [tc.id for tc in user2_testcase] self.question_paper.shuffle_testcases = False self.question_paper.save() answerpaper3 = self.question_paper.make_answerpaper( user=self.user2, ip=self.user_ip, attempt_num=self.answerpaper.attempt_number + 1, course_id=self.course.id) not_ordered_testcase = self.question1.get_ordered_test_cases( answerpaper3) get_test_cases = self.question1.get_test_cases() # Then self.assertNotEqual(order1, order2) self.assertEqual(get_test_cases, not_ordered_testcase) answerpaper3.delete()
def setUpClass(self): self.ip = '101.0.0.1' self.user = User.objects.get(id=1) self.profile = self.user.profile self.quiz = Quiz.objects.get(pk=1) self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() self.questions = Question.objects.filter(id__in=[1, 2, 3]) self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) self.question1 = self.questions.get(id=1) self.question2 = self.questions.get(id=2) self.question3 = self.questions.get(id=3) # create answerpaper self.answerpaper = AnswerPaper(user=self.user, question_paper=self.question_paper, start_time=self.start_time, end_time=self.end_time, user_ip=self.ip) self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, user=self.user) already_attempted = self.attempted_papers.count() self.answerpaper.attempt_number = already_attempted + 1 self.answerpaper.save() self.answerpaper.questions.add(*self.questions) self.answerpaper.questions_unanswered.add(*self.questions) self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), answer="Demo answer", correct=True, marks=1, error=json.dumps([])) self.answer_wrong = Answer(question=Question.objects.get(id=2), answer="My answer", correct=False, marks=0, error=json.dumps(['error1', 'error2'])) self.answer_right.save() self.answer_wrong.save() self.answerpaper.answers.add(self.answer_right) self.answerpaper.answers.add(self.answer_wrong) self.question1.language = 'python' self.question1.test_case_type = 'standardtestcase' self.question1.save() self.question2.language = 'python' self.question2.type = 'mcq' self.question2.test_case_type = 'mcqtestcase' self.question2.save() self.question3.language = 'python' self.question3.type = 'mcc' self.question3.test_case_type = 'mcqtestcase' self.question3.save() self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', type='standardtestcase') self.assertion_testcase.save() self.mcq_based_testcase = McqTestCase(options='a', question=self.question2, correct=True, type='mcqtestcase') self.mcq_based_testcase.save() self.mcc_based_testcase = McqTestCase(question=self.question3, options='a', correct=True, type='mcqtestcase') self.mcc_based_testcase.save()
class AnswerPaperTestCases(unittest.TestCase): @classmethod def setUpClass(self): self.ip = '101.0.0.1' self.user = User.objects.get(id=1) self.profile = self.user.profile self.quiz = Quiz.objects.get(pk=1) self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() self.questions = Question.objects.filter(id__in=[1, 2, 3]) self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) self.question1 = self.questions.get(id=1) self.question2 = self.questions.get(id=2) self.question3 = self.questions.get(id=3) # create answerpaper self.answerpaper = AnswerPaper(user=self.user, question_paper=self.question_paper, start_time=self.start_time, end_time=self.end_time, user_ip=self.ip) self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, user=self.user) already_attempted = self.attempted_papers.count() self.answerpaper.attempt_number = already_attempted + 1 self.answerpaper.save() self.answerpaper.questions.add(*self.questions) self.answerpaper.questions_unanswered.add(*self.questions) self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), answer="Demo answer", correct=True, marks=1, error=json.dumps([])) self.answer_wrong = Answer(question=Question.objects.get(id=2), answer="My answer", correct=False, marks=0, error=json.dumps(['error1', 'error2'])) self.answer_right.save() self.answer_wrong.save() self.answerpaper.answers.add(self.answer_right) self.answerpaper.answers.add(self.answer_wrong) self.question1.language = 'python' self.question1.test_case_type = 'standardtestcase' self.question1.save() self.question2.language = 'python' self.question2.type = 'mcq' self.question2.test_case_type = 'mcqtestcase' self.question2.save() self.question3.language = 'python' self.question3.type = 'mcc' self.question3.test_case_type = 'mcqtestcase' self.question3.save() self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', type='standardtestcase') self.assertion_testcase.save() self.mcq_based_testcase = McqTestCase(options='a', question=self.question2, correct=True, type='mcqtestcase') self.mcq_based_testcase.save() self.mcc_based_testcase = McqTestCase(question=self.question3, options='a', correct=True, type='mcqtestcase') self.mcc_based_testcase.save() def test_validate_and_regrade_mcc_correct_answer(self): # Given mcc_answer = ['a'] self.answer = Answer( question=self.question3, answer=mcc_answer, ) self.answer.save() self.answerpaper.answers.add(self.answer) # When json_data = None result = self.answerpaper.validate_answer(mcc_answer, self.question3, json_data) # Then self.assertTrue(result['success']) self.assertEqual(result['error'], ['Correct answer']) self.answer.correct = True self.answer.marks = 1 # Given self.answer.correct = True self.answer.marks = 1 self.answer.answer = ['a', 'b'] self.answer.save() # When details = self.answerpaper.regrade(self.question3.id) # Then self.answer = self.answerpaper.answers.filter( question=self.question3).last() self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) def test_validate_and_regrade_mcq_correct_answer(self): # Given mcq_answer = 'a' self.answer = Answer( question=self.question2, answer=mcq_answer, ) self.answer.save() self.answerpaper.answers.add(self.answer) # When json_data = None result = self.answerpaper.validate_answer(mcq_answer, self.question2, json_data) # Then self.assertTrue(result['success']) self.answer.correct = True self.answer.marks = 1 # Given self.answer.correct = True self.answer.marks = 1 self.answer.answer = 'b' self.answer.save() # When details = self.answerpaper.regrade(self.question2.id) # Then self.answer = self.answerpaper.answers.filter( question=self.question2).last() self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) def test_mcq_incorrect_answer(self): # Given mcq_answer = 'b' self.answer = Answer( question=self.question2, answer=mcq_answer, ) self.answer.save() self.answerpaper.answers.add(self.answer) # When json_data = None result = self.answerpaper.validate_answer(mcq_answer, self.question2, json_data) # Then self.assertFalse(result['success']) def test_mcc_incorrect_answer(self): # Given mcc_answer = ['b'] self.answer = Answer( question=self.question3, answer=mcc_answer, ) self.answer.save() self.answerpaper.answers.add(self.answer) # When json_data = None result = self.answerpaper.validate_answer(mcc_answer, self.question3, json_data) # Then self.assertFalse(result['success']) def test_answerpaper(self): """ Test Answer Paper""" self.assertEqual(self.answerpaper.user.username, 'demo_user') self.assertEqual(self.answerpaper.user_ip, self.ip) questions = self.answerpaper.get_questions() num_questions = len(questions) self.assertSequenceEqual(list(questions), list(self.questions)) self.assertEqual(num_questions, 3) self.assertEqual(self.answerpaper.question_paper, self.question_paper) self.assertEqual(self.answerpaper.start_time, self.start_time) self.assertEqual(self.answerpaper.status, 'inprogress') def test_questions(self): # Test questions_left() method of Answer Paper self.assertEqual(self.answerpaper.questions_left(), 3) # Test current_question() method of Answer Paper current_question = self.answerpaper.current_question() self.assertEqual(current_question.id, 1) # Test completed_question() method of Answer Paper question = self.answerpaper.add_completed_question(1) self.assertEqual(self.answerpaper.questions_left(), 2) # Test next_question() method of Answer Paper current_question = self.answerpaper.current_question() self.assertEqual(current_question.id, 2) # When next_question_id = self.answerpaper.next_question(current_question.id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 3) # Given, here question is already answered current_question_id = 1 # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 2) # Given, wrong question id current_question_id = 12 # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 1) # Given, last question in the list current_question_id = 3 # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 1) # Test get_questions_answered() method # When questions_answered = self.answerpaper.get_questions_answered() # Then self.assertEqual(questions_answered.count(), 1) self.assertSequenceEqual(questions_answered, [self.questions[0]]) # When questions_unanswered = self.answerpaper.get_questions_unanswered() # Then self.assertEqual(questions_unanswered.count(), 2) self.assertSequenceEqual(questions_unanswered, [self.questions[1], self.questions[2]]) # Test completed_question and next_question # When all questions are answered current_question = self.answerpaper.add_completed_question(2) # Then self.assertEqual(self.answerpaper.questions_left(), 1) self.assertEqual(current_question.id, 3) # When current_question = self.answerpaper.add_completed_question(3) # Then self.assertEqual(self.answerpaper.questions_left(), 0) self.assertTrue(current_question is None) # When next_question_id = self.answerpaper.next_question(current_question_id) # Then self.assertTrue(next_question_id is None) def test_update_marks(self): """ Test update_marks method of AnswerPaper""" self.answerpaper.update_marks('inprogress') self.assertEqual(self.answerpaper.status, 'inprogress') self.assertTrue(self.answerpaper.is_attempt_inprogress()) self.answerpaper.update_marks() self.assertEqual(self.answerpaper.status, 'completed') self.assertEqual(self.answerpaper.marks_obtained, 1.0) self.assertEqual(self.answerpaper.percent, 33.33) self.assertTrue(self.answerpaper.passed) self.assertFalse(self.answerpaper.is_attempt_inprogress()) def test_set_end_time(self): current_time = timezone.now() self.answerpaper.set_end_time(current_time) self.assertEqual(self.answerpaper.end_time, current_time) def test_get_question_answer(self): """ Test get_question_answer() method of Answer Paper""" answered = self.answerpaper.get_question_answers() first_answer = list(answered.values())[0][0] first_answer_obj = first_answer['answer'] self.assertEqual(first_answer_obj.answer, 'Demo answer') self.assertTrue(first_answer_obj.correct) self.assertEqual(len(answered), 2) def test_is_answer_correct(self): self.assertTrue(self.answerpaper.is_answer_correct(self.questions[0])) self.assertFalse(self.answerpaper.is_answer_correct(self.questions[1])) def test_get_previous_answers(self): answers = self.answerpaper.get_previous_answers(self.questions[0]) self.assertEqual(answers.count(), 1) self.assertTrue(answers[0], self.answer_right) answers = self.answerpaper.get_previous_answers(self.questions[1]) self.assertEqual(answers.count(), 1) self.assertTrue(answers[0], self.answer_wrong) def test_set_marks(self): self.answer_wrong.set_marks(0.5) self.assertEqual(self.answer_wrong.marks, 0.5) self.answer_wrong.set_marks(10.0) self.assertEqual(self.answer_wrong.marks, 1.0)
def setUpClass(self): self.client = APIClient() self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, password=self.password) Profile.objects.create(user=self.user) self.quiz = Quiz.objects.create(description='Quiz', creator=self.user) self.questionpaper = QuestionPaper.objects.create(quiz=self.quiz) self.question1 = Question.objects.create(summary='Q1', user=self.user, points=1.0, language='python', type='code') self.question2 = Question.objects.create(summary='Q2', user=self.user, points=1.0, language='python', type='mcq') self.question3 = Question.objects.create(summary='Q3', user=self.user, points=1.0, language='python', type='mcc') self.question4 = Question.objects.create(summary='Q4', user=self.user, points=1.0, language='python', type='mcq') self.question5 = Question.objects.create(summary='Q5', user=self.user, points=1.0, language='python', type='mcq') self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', type='standardtestcase') self.assertion_testcase.save() self.mcq_based_testcase1 = McqTestCase(options='a', question=self.question2, correct=True, type='mcqtestcase') self.mcq_based_testcase1.save() self.mcq_based_testcase2 = McqTestCase(options='b', question=self.question2, correct=False, type='mcqtestcase') self.mcq_based_testcase2.save() self.mcc_based_testcase = McqTestCase(question=self.question3, options='a', correct=True, type='mcqtestcase') self.mcc_based_testcase.save() self.questionset = QuestionSet.objects.create(marks=1, num_questions=1) self.questionset.questions.add(self.question3) self.questionset.questions.add(self.question4) self.questionset.save() self.questionpaper.fixed_questions.add(self.question1) self.questionpaper.fixed_questions.add(self.question2) self.questionpaper.random_questions.add(self.questionset) self.questionpaper.save() self.questionpaper.update_total_marks() self.course = Course.objects.create(name="Python Course", enrollment="Enroll Request", creator=self.user) # Learing module learning_module = LearningModule.objects.create( name='LM1', description='module one', creator=self.user) learning_unit_quiz = LearningUnit.objects.create(quiz=self.quiz, type='quiz', order=1) learning_module.learning_unit.add(learning_unit_quiz) learning_module.save() self.course.learning_module.add(learning_module) self.course.students.add(self.user) self.course.save() self.ip = '127.0.0.1' self.answerpaper = self.questionpaper.make_answerpaper( self.user, self.ip, 1, self.course.id) settings.code_evaluators['python']['standardtestcase'] = \ "yaksh.python_assertion_evaluator.PythonAssertionEvaluator" server_pool = ServerPool(n=1, pool_port=SERVER_POOL_PORT) self.server_pool = server_pool self.server_thread = t = Thread(target=server_pool.run) t.start()
class AnswerValidatorTestCase(TestCase): @classmethod def setUpClass(self): self.client = APIClient() self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, password=self.password) Profile.objects.create(user=self.user) self.quiz = Quiz.objects.create(description='Quiz', creator=self.user) self.questionpaper = QuestionPaper.objects.create(quiz=self.quiz) self.question1 = Question.objects.create(summary='Q1', user=self.user, points=1.0, language='python', type='code') self.question2 = Question.objects.create(summary='Q2', user=self.user, points=1.0, language='python', type='mcq') self.question3 = Question.objects.create(summary='Q3', user=self.user, points=1.0, language='python', type='mcc') self.question4 = Question.objects.create(summary='Q4', user=self.user, points=1.0, language='python', type='mcq') self.question5 = Question.objects.create(summary='Q5', user=self.user, points=1.0, language='python', type='mcq') self.assertion_testcase = StandardTestCase( question=self.question1, test_case='assert add(1, 3) == 4', type='standardtestcase') self.assertion_testcase.save() self.mcq_based_testcase1 = McqTestCase(options='a', question=self.question2, correct=True, type='mcqtestcase') self.mcq_based_testcase1.save() self.mcq_based_testcase2 = McqTestCase(options='b', question=self.question2, correct=False, type='mcqtestcase') self.mcq_based_testcase2.save() self.mcc_based_testcase = McqTestCase(question=self.question3, options='a', correct=True, type='mcqtestcase') self.mcc_based_testcase.save() self.questionset = QuestionSet.objects.create(marks=1, num_questions=1) self.questionset.questions.add(self.question3) self.questionset.questions.add(self.question4) self.questionset.save() self.questionpaper.fixed_questions.add(self.question1) self.questionpaper.fixed_questions.add(self.question2) self.questionpaper.random_questions.add(self.questionset) self.questionpaper.save() self.questionpaper.update_total_marks() self.course = Course.objects.create(name="Python Course", enrollment="Enroll Request", creator=self.user) # Learing module learning_module = LearningModule.objects.create( name='LM1', description='module one', creator=self.user) learning_unit_quiz = LearningUnit.objects.create(quiz=self.quiz, type='quiz', order=1) learning_module.learning_unit.add(learning_unit_quiz) learning_module.save() self.course.learning_module.add(learning_module) self.course.students.add(self.user) self.course.save() self.ip = '127.0.0.1' self.answerpaper = self.questionpaper.make_answerpaper( self.user, self.ip, 1, self.course.id) settings.code_evaluators['python']['standardtestcase'] = \ "yaksh.python_assertion_evaluator.PythonAssertionEvaluator" server_pool = ServerPool(n=1, pool_port=SERVER_POOL_PORT) self.server_pool = server_pool self.server_thread = t = Thread(target=server_pool.run) t.start() @classmethod def tearDownClass(self): self.client.logout() User.objects.all().delete() Question.objects.all().delete() QuestionPaper.objects.all().delete() Quiz.objects.all().delete() AnswerPaper.objects.all().delete() self.server_pool.stop() self.server_thread.join() settings.code_evaluators['python']['standardtestcase'] = \ "python_assertion_evaluator.PythonAssertionEvaluator" def test_correct_mcq(self): # Given data = {'answer': str(self.mcq_based_testcase1.id)} answerpaper_id = self.answerpaper.id question_id = self.question2.id # When self.client.login(username=self.username, password=self.password) response = self.client.post( reverse('api:validators', kwargs={ 'answerpaper_id': answerpaper_id, 'question_id': question_id }), data) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data.get('success')) answerpaper = AnswerPaper.objects.get( user=self.user, course=self.course, attempt_number=1, question_paper=self.questionpaper) self.assertTrue(answerpaper.marks_obtained > 0) def test_wrong_mcq(self): # Given data = {'answer': str(self.mcq_based_testcase2.id)} answerpaper_id = self.answerpaper.id question_id = self.question2.id # When self.client.login(username=self.username, password=self.password) response = self.client.post( reverse('api:validators', kwargs={ 'answerpaper_id': answerpaper_id, 'question_id': question_id }), data) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) self.assertFalse(response.data.get('success')) def test_correct_mcc(self): # Given data = {'answer': str(self.mcc_based_testcase.id)} answerpaper_id = self.answerpaper.id question_id = self.question3.id # When self.client.login(username=self.username, password=self.password) response = self.client.post( reverse('api:validators', kwargs={ 'answerpaper_id': answerpaper_id, 'question_id': question_id }), data) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data.get('success')) answerpaper = AnswerPaper.objects.get( user=self.user, course=self.course, attempt_number=1, question_paper=self.questionpaper) self.assertTrue(answerpaper.marks_obtained > 0) def test_correct_code(self): # Given answer = dedent("""\ def add(a,b): return a+b """) data = {'answer': answer} answerpaper_id = self.answerpaper.id question_id = self.question1.id # When self.client.login(username=self.username, password=self.password) response = self.client.post( reverse('api:validators', kwargs={ 'answerpaper_id': answerpaper_id, 'question_id': question_id }), data) # Then self.assertTrue(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get('status'), 'running') uid = response.data['uid'] time.sleep(2) response = self.client.get( reverse('api:validator', kwargs={'uid': uid})) self.assertTrue(response.status_code, status.HTTP_200_OK) answerpaper = AnswerPaper.objects.get( user=self.user, course=self.course, attempt_number=1, question_paper=self.questionpaper) if response.data.get('status') == 'done': result = json.loads(response.data.get('result')) self.assertTrue(result.get('success')) else: self.assertEqual(response.data.get('status'), 'running')