diff options
author | mahesh | 2018-02-07 15:58:54 +0530 |
---|---|---|
committer | maheshgudi | 2018-03-16 15:15:50 +0530 |
commit | 3fb6c4219c7c288fdf47ca300a1d859653c358b3 (patch) | |
tree | baf860a4d47d7f53c2feec9d1a0a794bf2cd6185 /yaksh | |
parent | 537d0fc5c5884ab620e587ff977ed61a2a50504d (diff) | |
download | online_test-3fb6c4219c7c288fdf47ca300a1d859653c358b3.tar.gz online_test-3fb6c4219c7c288fdf47ca300a1d859653c358b3.tar.bz2 online_test-3fb6c4219c7c288fdf47ca300a1d859653c358b3.zip |
Add test cases for Arrange question type
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/evaluator_tests/test_simple_question_types.py | 228 | ||||
-rw-r--r-- | yaksh/models.py | 18 | ||||
-rw-r--r-- | yaksh/test_models.py | 18 |
3 files changed, 208 insertions, 56 deletions
diff --git a/yaksh/evaluator_tests/test_simple_question_types.py b/yaksh/evaluator_tests/test_simple_question_types.py index c952884..d9d60fe 100644 --- a/yaksh/evaluator_tests/test_simple_question_types.py +++ b/yaksh/evaluator_tests/test_simple_question_types.py @@ -4,7 +4,7 @@ from django.utils import timezone import pytz from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, IntegerTestCase, FloatTestCase,\ - StringTestCase, McqTestCase + StringTestCase, McqTestCase, ArrangeTestCase def setUpModule(): @@ -39,15 +39,17 @@ def setUpModule(): duration=30, active=True, attempts_allowed=1, time_between_attempts=0, pass_criteria=0, description='demo quiz 100', - instructions="Demo Instructions" + instructions="Demo Instructions", + creator=user ) question_paper = QuestionPaper.objects.create(quiz=quiz, total_marks=1.0) def tearDownModule(): - User.objects.get(username="demo_user_100").delete() - User.objects.get(username="demo_user_101").delete() + User.objects.filter(username__in=["demo_user_100", "demo_user_101"])\ + .delete() + class IntegerQuestionTestCases(unittest.TestCase): @classmethod @@ -116,11 +118,9 @@ class IntegerQuestionTestCases(unittest.TestCase): # Regrade # Given - self.answer.correct = True - self.answer.marks = 1 - - self.answer.answer = 200 - self.answer.save() + regrade_answer = Answer.objects.get(id=self.answer.id) + regrade_answer.answer = 200 + regrade_answer.save() # When details = self.answerpaper.regrade(self.question1.id) @@ -128,6 +128,7 @@ class IntegerQuestionTestCases(unittest.TestCase): # Then self.answer = self.answerpaper.answers.filter(question=self.question1 ).last() + self.assertEqual(self.answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) @@ -153,11 +154,9 @@ class IntegerQuestionTestCases(unittest.TestCase): # Regrade # Given - self.answer.correct = True - self.answer.marks = 1 - - self.answer.answer = 25 - self.answer.save() + regrade_answer = Answer.objects.get(id=self.answer.id) + regrade_answer.answer = 25 + regrade_answer.save() # When details = self.answerpaper.regrade(self.question1.id) @@ -165,6 +164,7 @@ class IntegerQuestionTestCases(unittest.TestCase): # Then self.answer = self.answerpaper.answers.filter(question=self.question1 ).last() + self.assertEqual(self.answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 1) self.assertTrue(self.answer.correct) @@ -250,11 +250,9 @@ class StringQuestionTestCases(unittest.TestCase): # Regrade # Given - answer.correct = True - answer.marks = 1 - - answer.answer = "hello, mars!" - answer.save() + regrade_answer = Answer.objects.get(id=answer.id) + regrade_answer.answer = "hello, mars!" + regrade_answer.save() # When details = self.answerpaper.regrade(self.question1.id) @@ -262,6 +260,7 @@ class StringQuestionTestCases(unittest.TestCase): # Then answer = self.answerpaper.answers.filter(question=self.question1)\ .last() + self.assertEqual(answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(answer.marks, 0) self.assertFalse(answer.correct) @@ -284,11 +283,9 @@ class StringQuestionTestCases(unittest.TestCase): # Regrade # Given - answer.correct = True - answer.marks = 1 - - answer.answer = "hello, earth!" - answer.save() + regrade_answer = Answer.objects.get(id=answer.id) + regrade_answer.answer = "hello, earth!" + regrade_answer.save() # When details = self.answerpaper.regrade(self.question1.id) @@ -296,6 +293,7 @@ class StringQuestionTestCases(unittest.TestCase): # Then answer = self.answerpaper.answers.filter(question=self.question1)\ .last() + self.assertEqual(answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(answer.marks, 1) self.assertTrue(answer.correct) @@ -317,11 +315,9 @@ class StringQuestionTestCases(unittest.TestCase): # Regrade # Given - answer.correct = True - answer.marks = 1 - - answer.answer = "hello, earth!" - answer.save() + regrade_answer = Answer.objects.get(id=answer.id) + regrade_answer.answer = "hello, earth!" + regrade_answer.save() # When details = self.answerpaper.regrade(self.question2.id) @@ -329,6 +325,7 @@ class StringQuestionTestCases(unittest.TestCase): # Then answer = self.answerpaper.answers.filter(question=self.question2)\ .last() + self.assertEqual(answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(answer.marks, 0) self.assertFalse(answer.correct) @@ -351,11 +348,9 @@ class StringQuestionTestCases(unittest.TestCase): # Regrade # Given - answer.correct = True - answer.marks = 1 - - answer.answer = "Hello, EARTH!" - answer.save() + regrade_answer = Answer.objects.get(id=answer.id) + regrade_answer.answer = "Hello, EARTH!" + regrade_answer.save() # When details = self.answerpaper.regrade(self.question2.id) @@ -363,6 +358,7 @@ class StringQuestionTestCases(unittest.TestCase): # Then answer = self.answerpaper.answers.filter(question=self.question2)\ .last() + self.assertEqual(answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(answer.marks, 1) self.assertTrue(answer.correct) @@ -432,13 +428,11 @@ class FloatQuestionTestCases(unittest.TestCase): # Then self.assertTrue(result['success']) - # Regrade + # Regrade with wrong answer # Given - self.answer.correct = True - self.answer.marks = 1 - - self.answer.answer = 0.0 - self.answer.save() + regrade_answer = Answer.objects.get(id=self.answer.id) + regrade_answer.answer = 0.0 + regrade_answer.save() # When details = self.answerpaper.regrade(self.question1.id) @@ -446,6 +440,7 @@ class FloatQuestionTestCases(unittest.TestCase): # Then self.answer = self.answerpaper.answers.filter(question=self.question1 ).last() + self.assertEqual(self.answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 0) self.assertFalse(self.answer.correct) @@ -470,11 +465,9 @@ class FloatQuestionTestCases(unittest.TestCase): # Regrade # Given - self.answer.correct = True - self.answer.marks = 1 - - self.answer.answer = 99.9 - self.answer.save() + regrade_answer = Answer.objects.get(id=self.answer.id) + regrade_answer.answer = 99.9 + regrade_answer.save() # When details = self.answerpaper.regrade(self.question1.id) @@ -482,6 +475,7 @@ class FloatQuestionTestCases(unittest.TestCase): # Then self.answer = self.answerpaper.answers.filter(question=self.question1 ).last() + self.assertEqual(self.answer, regrade_answer) self.assertTrue(details[0]) self.assertEqual(self.answer.marks, 1) self.assertTrue(self.answer.correct) @@ -586,4 +580,146 @@ class MCQQuestionTestCases(unittest.TestCase): # Then self.assertNotEqual(order1, order2) self.assertEqual(get_test_cases, not_ordered_testcase) - answerpaper3.delete() + + +class ArrangeQuestionTestCases(unittest.TestCase): + @classmethod + def setUpClass(self): + # 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, + total_marks=1.0) + + #Creating User + self.user = User.objects.get(username='demo_user_100') + #Creating Question + self.question1 = Question.objects.create(summary='arrange1', + points=1.0, + user=self.user + ) + self.question1.language = 'python' + self.question1.type = "arrange" + self.question1.description = "Arrange the alphabets in ascending order" + self.question1.test_case_type = 'arrangetestcase' + self.question1.save() + + #Creating answerpaper + + self.answerpaper = AnswerPaper.objects.create(user=self.user, + user_ip='101.0.0.1', + start_time=timezone.now(), + question_paper=self.question_paper, + end_time=timezone.now() + +timedelta(minutes=5), + attempt_number=1, + course=self.course + ) + self.answerpaper.questions.add(self.question1) + self.answerpaper.save() + # For question + self.arrange_testcase_1 = ArrangeTestCase(question=self.question1, + options="A", + type = 'arrangetestcase', + ) + self.arrange_testcase_1.save() + self.testcase_1_id = self.arrange_testcase_1.id + self.arrange_testcase_2 = ArrangeTestCase(question=self.question1, + options="B", + type = 'arrangetestcase', + ) + self.arrange_testcase_2.save() + self.testcase_2_id = self.arrange_testcase_2.id + self.arrange_testcase_3 = ArrangeTestCase(question=self.question1, + options="C", + type = 'arrangetestcase', + ) + self.arrange_testcase_3.save() + self.testcase_3_id = self.arrange_testcase_3.id + @classmethod + def tearDownClass(self): + self.question1.delete() + self.answerpaper.delete() + + def test_validate_regrade_arrange_correct_answer(self): + # Given + arrange_answer = [self.testcase_1_id, + self.testcase_2_id, + self.testcase_3_id, + ] + self.answer = Answer(question=self.question1, + answer=arrange_answer, + ) + self.answer.save() + self.answerpaper.answers.add(self.answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(arrange_answer, + self.question1, + json_data, + ) + # Then + self.assertTrue(result['success']) + + # Regrade with wrong answer + # Given + regrade_answer = Answer.objects.get(id=self.answer.id) + regrade_answer.answer = [self.testcase_1_id, + self.testcase_3_id, + self.testcase_2_id, + ] + regrade_answer.save() + + # When + details = self.answerpaper.regrade(self.question1.id) + + # Then + self.answer = self.answerpaper.answers.filter(question=self.question1 + ).last() + self.assertEqual(self.answer, regrade_answer) + self.assertTrue(details[0]) + self.assertEqual(self.answer.marks, 0) + self.assertFalse(self.answer.correct) + + def test_validate_regrade_arrange_incorrect_answer(self): + # Given + arrange_answer = [self.testcase_1_id, + self.testcase_3_id, + self.testcase_2_id, + ] + self.answer = Answer(question=self.question1, + answer=arrange_answer, + ) + self.answer.save() + self.answerpaper.answers.add(self.answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(arrange_answer, + self.question1, json_data + ) + + # Then + self.assertFalse(result['success']) + # Regrade with wrong answer + # Given + regrade_answer = Answer.objects.get(id=self.answer.id) + regrade_answer.answer = [self.testcase_1_id, + self.testcase_2_id, + self.testcase_3_id, + ] + regrade_answer.save() + + # When + details = self.answerpaper.regrade(self.question1.id) + + # Then + self.answer = self.answerpaper.answers.filter(question=self.question1 + ).last() + self.assertEqual(self.answer, regrade_answer) + self.assertTrue(details[0]) + self.assertEqual(self.answer.marks, 1) + self.assertTrue(self.answer.correct) diff --git a/yaksh/models.py b/yaksh/models.py index 7c5bb85..a79f2f6 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1822,13 +1822,21 @@ class AnswerPaper(models.Model): user_answer = self.answers.filter(question=question).last() if not user_answer: return False, msg + 'Did not answer.' - if question.type == 'mcc': + if question.type in ['mcc', 'arrange']: try: - answer = eval(user_answer.answer) + answer = literal_eval(user_answer.answer) if type(answer) is not list: - return False, msg + 'MCC answer not a list.' + return (False, + msg + '{0} answer not a list.'.format( + question.type + ) + ) except Exception: - return False, msg + 'MCC answer submission error' + return (False, + msg + '{0} answer submission error'.format( + question.type + ) + ) else: answer = user_answer.answer json_data = question.consolidate_answer_data(answer) \ @@ -2048,4 +2056,4 @@ class TestCaseOrder(models.Model): #Order of the test case for a question. order = models.TextField() - +############################################################################## diff --git a/yaksh/test_models.py b/yaksh/test_models.py index a0ccd49..bebc929 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1372,12 +1372,20 @@ class AnswerPaperTestCases(unittest.TestCase): def test_get_question_answer(self): """ Test get_question_answer() method of Answer Paper""" + questions = self.answerpaper.questions.all() 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) + for question in questions: + answers_saved = Answer.objects.filter(question=question) + error_list = [json.loads(ans.error) for ans in answers_saved] + if answers_saved: + self.assertEqual(len(answered[question]), len(answers_saved)) + ans_list = [] + err_list = [] + for val in answered[question]: + ans_list.append(val.get('answer')) + err_list.append(val.get('error_list')) + self.assertEqual(set(ans_list), set(answers_saved)) + self.assertEqual(error_list, err_list) def test_is_answer_correct(self): self.assertTrue(self.answerpaper.is_answer_correct(self.questions[0])) |