summaryrefslogtreecommitdiff
path: root/yaksh/evaluator_tests
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/evaluator_tests')
-rw-r--r--yaksh/evaluator_tests/test_simple_question_types.py342
1 files changed, 299 insertions, 43 deletions
diff --git a/yaksh/evaluator_tests/test_simple_question_types.py b/yaksh/evaluator_tests/test_simple_question_types.py
index cbf2abd..dfb82a2 100644
--- a/yaksh/evaluator_tests/test_simple_question_types.py
+++ b/yaksh/evaluator_tests/test_simple_question_types.py
@@ -1,10 +1,11 @@
import unittest
from datetime import datetime, timedelta
from django.utils import timezone
+from textwrap import dedent
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 +40,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 +119,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 +129,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 +155,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 +165,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 +251,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 +261,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 +284,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 +294,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 +316,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 +326,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 +349,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 +359,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 +429,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 +441,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 +466,270 @@ class FloatQuestionTestCases(unittest.TestCase):
# Regrade
# Given
- self.answer.correct = True
- self.answer.marks = 1
+ 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)
+
+ # 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)
+class MCQQuestionTestCases(unittest.TestCase):
+ @classmethod
+ def setUpClass(self):
+ #Creating User
+ self.user = User.objects.get(username='demo_user_100')
+ self.user2 = User.objects.get(username='demo_user_101')
+ 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()
- self.answer.answer = 99.9
+ 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)
+
+
+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 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)
+
+ # Try regrade with wrong data structure
+ # When
+ regrade_answer.answer = 1
+ regrade_answer.save()
+ details = self.answerpaper.regrade(self.question1.id)
+ err_msg = dedent("""\
+ User: {0}; Quiz: {1}; Question: {2}.
+ {3} answer not a list.""".format(
+ self.user.username,
+ self.quiz.description,
+ self.question1.summary,
+ self.question1.type
+ ) )
+ self.assertFalse(details[0])
+ self.assertEqual(details[1], err_msg)
+
+
+ # Try regrade with incorrect answer
+ # When
+ regrade_answer.answer = [self.testcase_1_id,
+ self.testcase_3_id,
+ self.testcase_2_id,
+ ]
+ regrade_answer.save()
+ # Then
+ details = self.answerpaper.regrade(self.question1.id)
+ 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)
@@ -482,6 +737,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)