Beispiel #1
0
class TestCaseTestCases(unittest.TestCase):
    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.question1 = Question(summary='Demo question 1',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  description='Write a function',
                                  points=1.0,
                                  test_case_type="standardtestcase",
                                  user=self.user,
                                  snippet='def myfunc()')
        self.question2 = Question(summary='Demo question 2',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  description='Write to standard output',
                                  points=1.0,
                                  test_case_type="stdiobasedtestcase",
                                  user=self.user,
                                  snippet='def myfunc()')
        self.question1.save()
        self.question2.save()
        self.assertion_testcase = StandardTestCase(
            question=self.question1, test_case='assert myfunc(12, 13) == 15')
        self.stdout_based_testcase = StdioBasedTestCase(
            question=self.question2, expected_output='Hello World')
        self.assertion_testcase.save()
        self.stdout_based_testcase.save()
        answer_data = {
            "user_answer":
            "demo_answer",
            "test_case_data": [{
                "test_case": "assert myfunc(12, 13) == 15",
                "weight": 0.0
            }]
        }
        self.answer_data_json = json.dumps(answer_data)

    def test_assertion_testcase(self):
        """ Test question """
        self.assertEqual(self.assertion_testcase.question, self.question1)
        self.assertEqual(self.assertion_testcase.test_case,
                         'assert myfunc(12, 13) == 15')

    def test_stdout_based_testcase(self):
        """ Test question """
        self.assertEqual(self.stdout_based_testcase.question, self.question2)
        self.assertEqual(self.stdout_based_testcase.expected_output,
                         'Hello World')

    def test_consolidate_answer_data(self):
        """ Test consolidate answer data model method """
        result = self.question1.consolidate_answer_data(
            user_answer="demo_answer")
        actual_data = json.loads(result)
        exp_data = json.loads(self.answer_data_json)
        self.assertEqual(actual_data['user_answer'], exp_data['user_answer'])
        self.assertEqual(actual_data['test_case_data'],
                         exp_data['test_case_data'])
Beispiel #2
0
 def setUp(self):
     self.user = User.objects.get(pk=1)
     self.question1 = Question(summary='Demo question 1',
                               language='Python',
                               type='Code',
                               active=True,
                               description='Write a function',
                               points=1.0,
                               test_case_type="standardtestcase",
                               user=self.user,
                               snippet='def myfunc()')
     self.question2 = Question(summary='Demo question 2',
                               language='Python',
                               type='Code',
                               active=True,
                               description='Write to standard output',
                               points=1.0,
                               test_case_type="stdiobasedtestcase",
                               user=self.user,
                               snippet='def myfunc()')
     self.question1.save()
     self.question2.save()
     self.assertion_testcase = StandardTestCase(
         question=self.question1, test_case='assert myfunc(12, 13) == 15')
     self.stdout_based_testcase = StdioBasedTestCase(
         question=self.question2, expected_output='Hello World')
     self.assertion_testcase.save()
     self.stdout_based_testcase.save()
     answer_data = {
         "user_answer": "demo_answer",
         "test_case_data": [{
             "test_case": "assert myfunc(12, 13) == 15"
         }]
     }
     self.answer_data_json = json.dumps(answer_data)
Beispiel #3
0
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  test_case_type='standardtestcase',
                                  description='Write a function',
                                  points=1.0,
                                  snippet='def myfunc()',
                                  user=self.user1)
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
                                  language='python',
                                  type='code',
                                  active=True,
                                  description='factorial of a no',
                                  test_case_type='standardtestcase',
                                  points=2.0,
                                  snippet='def fact()',
                                  user=self.user2)
        self.question2.save()

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(
            question=self.question1, test_case='assert myfunc(12, 13) == 15')
        self.upload_test_case = StandardTestCase(
            question=self.question2, test_case='assert fact(3) == 6')
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{"test_case": "assert fact(3)==6"}]
        questions_data = [{
            "snippet": "def fact()",
            "active": True,
            "points": 1.0,
            "description": "factorial of a no",
            "language": "Python",
            "type": "Code",
            "test_case_type": "standardtestcase",
            "testcase": self.test_case_upload_data,
            "summary": "Json Demo"
        }]
        self.json_questions_data = json.dumps(questions_data)
Beispiel #4
0
 def setUp(self):
     self.user = User.objects.get(pk=1)
     self.question1 = Question(summary='Demo question 1',
                               language='Python',
                               type='Code',
                               active=True,
                               description='Write a function',
                               points=1.0,
                               user=self.user,
                               snippet='def myfunc()')
     self.question2 = Question(summary='Demo question 2',
                               language='Python',
                               type='Code',
                               active=True,
                               description='Write to standard output',
                               points=1.0,
                               user=self.user,
                               snippet='def myfunc()')
     self.question1.save()
     self.question2.save()
     self.assertion_testcase = StandardTestCase(
         question=self.question1,
         test_case='assert myfunc(12, 13) == 15',
         type='standardtestcase')
     self.stdout_based_testcase = StdIOBasedTestCase(
         question=self.question2,
         expected_output='Hello World',
         type='standardtestcase')
     self.assertion_testcase.save()
     self.stdout_based_testcase.save()
     answer_data = {
         'metadata': {
             'user_answer': 'demo_answer',
             'language': 'python',
             'partial_grading': False
         },
         'test_case_data': [{
             'test_case': 'assert myfunc(12, 13) == 15',
             'test_case_type': 'standardtestcase',
             'test_case_args': "",
             'weight': 1.0
         }]
     }
     self.answer_data_json = json.dumps(answer_data)
Beispiel #5
0
    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.question1 = Question(summary='Demo question 1',
            language='Python',
            type='Code',
            active=True,
            description='Write a function',
            points=1.0,
            test_case_type="standardtestcase",
            user=self.user,
            snippet='def myfunc()'
        )
        self.question2 = Question(summary='Demo question 2',
             language='Python',
             type='Code',
             active=True,
             description='Write to standard output',
             points=1.0,
             test_case_type="stdiobasedtestcase",
             user=self.user,
             snippet='def myfunc()'
        )
        self.question1.save()
        self.question2.save()
        self.assertion_testcase = StandardTestCase(
            question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase'
        )
        self.stdout_based_testcase = StdIOBasedTestCase(
            question=self.question2,
            expected_output='Hello World',
            type='standardtestcase'

        )
        self.assertion_testcase.save()
        self.stdout_based_testcase.save()
        answer_data = {'metadata': { 'user_answer': 'demo_answer',
                        'language': 'python',
                        'partial_grading': False
                        },
                    'test_case_data': [{'test_case': 'assert myfunc(12, 13) == 15',
                        'test_case_type': 'standardtestcase',
                        'weight': 1.0
                        }]
                    }
        self.answer_data_json = json.dumps(answer_data)
Beispiel #6
0
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
            language='Python',
            type='Code',
            active=True,
            test_case_type='standardtestcase',
            description='Write a function',
            points=1.0,
            snippet='def myfunc()',
            user=self.user1
        )
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
            language='python',
            type='code',
            active=True,
            description='factorial of a no',
            test_case_type='standardtestcase',
            points=2.0,
            snippet='def fact()',
            user=self.user2
        )
        self.question2.save()

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(question=self.question1,
            test_case='assert myfunc(12, 13) == 15'
        )
        self.upload_test_case = StandardTestCase(question=self.question2,
            test_case='assert fact(3) == 6'
        )
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{"test_case": "assert fact(3)==6"}]
        questions_data = [{"snippet": "def fact()", "active": True,
                           "points": 1.0,
                           "description": "factorial of a no",
                           "language": "Python", "type": "Code",
                           "test_case_type": "standardtestcase",
                           "testcase": self.test_case_upload_data,
                           "summary": "Json Demo"}]
        self.json_questions_data = json.dumps(questions_data)
Beispiel #7
0
 def setUp(self):
     self.user = User.objects.get(pk=1)
     self.question1 = Question(summary='Demo question 1', 
         language='Python',
         type='Code', 
         active=True,
         description='Write a function', 
         points=1.0,
         test_case_type="standardtestcase", 
         user=self.user,
         snippet='def myfunc()'
     )
     self.question2 = Question(summary='Demo question 2', 
          language='Python',
          type='Code', 
          active=True,
          description='Write to standard output', 
          points=1.0,
          test_case_type="stdoutbasedtestcase", 
          user=self.user,
          snippet='def myfunc()'
     )
     self.question1.save()
     self.question2.save()
     self.assertion_testcase = StandardTestCase(
         question=self.question1,
         test_case='assert myfunc(12, 13) == 15'
     )
     self.stdout_based_testcase = StdoutBasedTestCase(
         question=self.question2,
         expected_output='Hello World'
     )
     self.assertion_testcase.save()
     self.stdout_based_testcase.save()
     answer_data = {"user_answer": "demo_answer",
         "test_case_data": [
             {"test_case": "assert myfunc(12, 13) == 15"}
         ]
     }
     self.answer_data_json = json.dumps(answer_data)
Beispiel #8
0
    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()
Beispiel #9
0
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)
Beispiel #10
0
class TestCaseTestCases(unittest.TestCase):
    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.question1 = Question(summary='Demo question 1', 
            language='Python',
            type='Code', 
            active=True,
            description='Write a function', 
            points=1.0,
            test_case_type="standardtestcase", 
            user=self.user,
            snippet='def myfunc()'
        )
        self.question2 = Question(summary='Demo question 2', 
             language='Python',
             type='Code', 
             active=True,
             description='Write to standard output', 
             points=1.0,
             test_case_type="stdoutbasedtestcase", 
             user=self.user,
             snippet='def myfunc()'
        )
        self.question1.save()
        self.question2.save()
        self.assertion_testcase = StandardTestCase(
            question=self.question1,
            test_case='assert myfunc(12, 13) == 15'
        )
        self.stdout_based_testcase = StdoutBasedTestCase(
            question=self.question2,
            expected_output='Hello World'
        )
        self.assertion_testcase.save()
        self.stdout_based_testcase.save()
        answer_data = {"user_answer": "demo_answer",
            "test_case_data": [
                {"test_case": "assert myfunc(12, 13) == 15"}
            ]
        }
        self.answer_data_json = json.dumps(answer_data)

    def test_assertion_testcase(self):
        """ Test question """
        self.assertEqual(self.assertion_testcase.question, self.question1)
        self.assertEqual(self.assertion_testcase.test_case, 
                             'assert myfunc(12, 13) == 15')

    def test_stdout_based_testcase(self):
        """ Test question """
        self.assertEqual(self.stdout_based_testcase.question, self.question2)
        self.assertEqual(self.stdout_based_testcase.expected_output, 
            'Hello World'
        )

    def test_consolidate_answer_data(self):
        """ Test consolidate answer data model method """
        result = self.question1.consolidate_answer_data(
            user_answer="demo_answer"
        )
        self.assertEqual(result, self.answer_data_json)
Beispiel #11
0
class QuestionTestCases(unittest.TestCase):
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
            language='Python',
            type='Code',
            active=True,
            test_case_type='standardtestcase',
            description='Write a function',
            points=1.0,
            snippet='def myfunc()',
            user=self.user1
        )
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
            language='python',
            type='code',
            active=True,
            description='factorial of a no',
            test_case_type='standardtestcase',
            points=2.0,
            snippet='def fact()',
            user=self.user2
        )
        self.question2.save()

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(question=self.question1,
            test_case='assert myfunc(12, 13) == 15'
        )
        self.upload_test_case = StandardTestCase(question=self.question2,
            test_case='assert fact(3) == 6'
        )
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{"test_case": "assert fact(3)==6"}]
        questions_data = [{"snippet": "def fact()", "active": True,
                           "points": 1.0,
                           "description": "factorial of a no",
                           "language": "Python", "type": "Code",
                           "test_case_type": "standardtestcase",
                           "testcase": self.test_case_upload_data,
                           "summary": "Json Demo"}]
        self.json_questions_data = json.dumps(questions_data)

    def test_question(self):
        """ Test question """
        self.assertEqual(self.question1.summary, 'Demo question')
        self.assertEqual(self.question1.language, 'Python')
        self.assertEqual(self.question1.type, 'Code')
        self.assertEqual(self.question1.description, 'Write a function')
        self.assertEqual(self.question1.points, 1.0)
        self.assertTrue(self.question1.active)
        self.assertEqual(self.question1.snippet, 'def myfunc()')
        tag_list = []
        for tag in self.question1.tags.all():
                    tag_list.append(tag.name)
        self.assertEqual(tag_list, ['python', 'function'])

    def test_dump_questions_into_json(self):
        """ Test dump questions into json """
        question = Question()
        question_id = [self.question2.id]
        questions = json.loads(question.dump_into_json(question_id, self.user2))
        test_case = self.question2.get_test_cases()
        for q in questions:
            self.assertEqual(self.question2.summary, q['summary'])
            self.assertEqual(self.question2.language, q['language'])
            self.assertEqual(self.question2.type, q['type'])
            self.assertEqual(self.question2.description, q['description'])
            self.assertEqual(self.question2.points, q['points'])
            self.assertTrue(self.question2.active)
            self.assertEqual(self.question2.snippet, q['snippet'])
            self.assertEqual(self.question2.test_case_type, q['test_case_type'])
            self.assertEqual([case.get_field_value() for case in test_case], q['testcase'])

    def test_load_questions_from_json(self):
        """ Test load questions into database from json """
        question = Question()
        result = question.load_from_json(self.json_questions_data, self.user1)
        question_data = Question.objects.get(pk=25)
        test_case = question_data.get_test_cases()
        self.assertEqual(question_data.summary, 'Json Demo')
        self.assertEqual(question_data.language, 'Python')
        self.assertEqual(question_data.type, 'Code')
        self.assertEqual(question_data.description, 'factorial of a no')
        self.assertEqual(question_data.points, 1.0)
        self.assertTrue(question_data.active)
        self.assertEqual(question_data.snippet, 'def fact()')
        self.assertEqual(question_data.test_case_type, 'standardtestcase')
        self.assertEqual([case.get_field_value() for case in test_case], self.test_case_upload_data)
Beispiel #12
0
class QuestionTestCases(unittest.TestCase):
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  description='Write a function',
                                  points=1.0,
                                  snippet='def myfunc()',
                                  user=self.user1)
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
                                  language='python',
                                  type='code',
                                  active=True,
                                  description='factorial of a no',
                                  points=2.0,
                                  snippet='def fact()',
                                  user=self.user2)
        self.question2.save()

        # create a temp directory and add files for loading questions test
        file_path = "/tmp/test.txt"
        self.load_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.load_tmp_path)
        file1 = os.path.join(self.load_tmp_path, "test.txt")

        # create a temp directory and add files for dumping questions test
        self.dump_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.dump_tmp_path)
        file2 = os.path.join(self.dump_tmp_path, "test.txt")
        file = open(file2, "r")
        django_file = File(file)
        file = FileUpload.objects.create(file=django_file,
                                         question=self.question2)

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(
            question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase')
        self.upload_test_case = StandardTestCase(
            question=self.question2,
            test_case='assert fact(3) == 6',
            type='standardtestcase')
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{
            "test_case": "assert fact(3)==6",
            "test_case_type": "standardtestcase",
            "test_case_args": "",
            "weight": 1.0
        }]
        questions_data = [{
            "snippet": "def fact()",
            "active": True,
            "points": 1.0,
            "description": "factorial of a no",
            "language": "Python",
            "type": "Code",
            "testcase": self.test_case_upload_data,
            "files": [[file1, 0]],
            "summary": "Json Demo"
        }]
        self.json_questions_data = json.dumps(questions_data)

    def tearDown(self):
        shutil.rmtree(self.load_tmp_path)
        shutil.rmtree(self.dump_tmp_path)

    def test_question(self):
        """ Test question """
        self.assertEqual(self.question1.summary, 'Demo question')
        self.assertEqual(self.question1.language, 'Python')
        self.assertEqual(self.question1.type, 'Code')
        self.assertEqual(self.question1.description, 'Write a function')
        self.assertEqual(self.question1.points, 1.0)
        self.assertTrue(self.question1.active)
        self.assertEqual(self.question1.snippet, 'def myfunc()')
        tag_list = []
        for tag in self.question1.tags.all():
            tag_list.append(tag.name)
        for tag in tag_list:
            self.assertIn(tag, ['python', 'function'])

    def test_dump_questions(self):
        """ Test dump questions into json """
        question = Question()
        question_id = [self.question2.id]
        questions_zip = question.dump_questions(question_id, self.user2)
        que_file = FileUpload.objects.get(question=self.question2.id)
        zip_file = zipfile.ZipFile(questions_zip, "r")
        tmp_path = tempfile.mkdtemp()
        zip_file.extractall(tmp_path)
        test_case = self.question2.get_test_cases()
        with open("{0}/questions_dump.json".format(tmp_path), "r") as f:
            questions = json.loads(f.read())
            for q in questions:
                self.assertEqual(self.question2.summary, q['summary'])
                self.assertEqual(self.question2.language, q['language'])
                self.assertEqual(self.question2.type, q['type'])
                self.assertEqual(self.question2.description, q['description'])
                self.assertEqual(self.question2.points, q['points'])
                self.assertTrue(self.question2.active)
                self.assertEqual(self.question2.snippet, q['snippet'])
                self.assertEqual(os.path.basename(que_file.file.path),
                                 q['files'][0][0])
                self.assertEqual(
                    [case.get_field_value() for case in test_case],
                    q['testcase'])
        for file in zip_file.namelist():
            os.remove(os.path.join(tmp_path, file))

    def test_load_questions(self):
        """ Test load questions into database from json """
        question = Question()
        result = question.load_questions(self.json_questions_data, self.user1)
        question_data = Question.objects.get(pk=25)
        file = FileUpload.objects.get(question=25)
        test_case = question_data.get_test_cases()
        self.assertEqual(question_data.summary, 'Json Demo')
        self.assertEqual(question_data.language, 'Python')
        self.assertEqual(question_data.type, 'Code')
        self.assertEqual(question_data.description, 'factorial of a no')
        self.assertEqual(question_data.points, 1.0)
        self.assertTrue(question_data.active)
        self.assertEqual(question_data.snippet, 'def fact()')
        self.assertEqual(os.path.basename(file.file.path), "test.txt")
        self.assertEqual([case.get_field_value() for case in test_case],
                         self.test_case_upload_data)
Beispiel #13
0
class QuestionTestCases(unittest.TestCase):
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
            language='Python',
            type='Code',
            active=True,
            test_case_type='standardtestcase',
            description='Write a function',
            points=1.0,
            snippet='def myfunc()',
            user=self.user1
        )
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
            language='python',
            type='code',
            active=True,
            description='factorial of a no',
            test_case_type='standardtestcase',
            points=2.0,
            snippet='def fact()',
            user=self.user2
        )
        self.question2.save()

        # create a temp directory and add files for loading questions test
        file_path = "/tmp/test.txt"
        self.load_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.load_tmp_path)
        file1 = os.path.join(self.load_tmp_path, "test.txt")

        # create a temp directory and add files for dumping questions test
        self.dump_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.dump_tmp_path)
        file2 = os.path.join(self.dump_tmp_path, "test.txt")
        file = open(file2, "r")
        django_file = File(file)
        file = FileUpload.objects.create(file=django_file, question=self.question2)

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase'
        )
        self.upload_test_case = StandardTestCase(question=self.question2,
            test_case='assert fact(3) == 6',
            type='standardtestcase'
        )
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{"test_case": "assert fact(3)==6",
                                        "test_case_type": "standardtestcase",
                                        "weight": 1.0
                                        }]
        questions_data = [{"snippet": "def fact()", "active": True,
                           "points": 1.0,
                           "description": "factorial of a no",
                           "language": "Python", "type": "Code",
                           "testcase": self.test_case_upload_data,
                           "files": [[file1, 0]],
                           "summary": "Json Demo"}]
        self.json_questions_data = json.dumps(questions_data)

    def tearDown(self):
        shutil.rmtree(self.load_tmp_path)
        shutil.rmtree(self.dump_tmp_path)

    def test_question(self):
        """ Test question """
        self.assertEqual(self.question1.summary, 'Demo question')
        self.assertEqual(self.question1.language, 'Python')
        self.assertEqual(self.question1.type, 'Code')
        self.assertEqual(self.question1.description, 'Write a function')
        self.assertEqual(self.question1.points, 1.0)
        self.assertTrue(self.question1.active)
        self.assertEqual(self.question1.snippet, 'def myfunc()')
        tag_list = []
        for tag in self.question1.tags.all():
                    tag_list.append(tag.name)
        for tag in tag_list:
            self.assertIn(tag, ['python', 'function'])

    def test_dump_questions(self):
        """ Test dump questions into json """
        question = Question()
        question_id = [self.question2.id]
        questions_zip = question.dump_questions(question_id, self.user2)
        que_file = FileUpload.objects.get(question=self.question2.id)
        zip_file = zipfile.ZipFile(questions_zip, "r")
        tmp_path = tempfile.mkdtemp()
        zip_file.extractall(tmp_path)
        test_case = self.question2.get_test_cases()
        with open("{0}/questions_dump.json".format(tmp_path), "r") as f:
            questions = json.loads(f.read())
            for q in questions:
                self.assertEqual(self.question2.summary, q['summary'])
                self.assertEqual(self.question2.language, q['language'])
                self.assertEqual(self.question2.type, q['type'])
                self.assertEqual(self.question2.description, q['description'])
                self.assertEqual(self.question2.points, q['points'])
                self.assertTrue(self.question2.active)
                self.assertEqual(self.question2.snippet, q['snippet'])
                self.assertEqual(self.question2.test_case_type, q['test_case_type'])
                self.assertEqual(os.path.basename(que_file.file.path), q['files'][0][0])
                self.assertEqual([case.get_field_value() for case in test_case], q['testcase'])
        for file in zip_file.namelist():
            os.remove(os.path.join(tmp_path, file))

    def test_load_questions(self):
        """ Test load questions into database from json """
        question = Question()
        result = question.load_questions(self.json_questions_data, self.user1)
        question_data = Question.objects.get(pk=25)
        file = FileUpload.objects.get(question=25)
        test_case = question_data.get_test_cases()
        self.assertEqual(question_data.summary, 'Json Demo')
        self.assertEqual(question_data.language, 'Python')
        self.assertEqual(question_data.type, 'Code')
        self.assertEqual(question_data.description, 'factorial of a no')
        self.assertEqual(question_data.points, 1.0)
        self.assertTrue(question_data.active)
        self.assertEqual(question_data.snippet, 'def fact()')
        self.assertEqual(os.path.basename(file.file.path), "test.txt")
        self.assertEqual([case.get_field_value() for case in test_case], self.test_case_upload_data)
Beispiel #14
0
class QuestionTestCases(unittest.TestCase):
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  test_case_type='standardtestcase',
                                  description='Write a function',
                                  points=1.0,
                                  snippet='def myfunc()',
                                  user=self.user1)
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
                                  language='python',
                                  type='code',
                                  active=True,
                                  description='factorial of a no',
                                  test_case_type='standardtestcase',
                                  points=2.0,
                                  snippet='def fact()',
                                  user=self.user2)
        self.question2.save()

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(
            question=self.question1, test_case='assert myfunc(12, 13) == 15')
        self.upload_test_case = StandardTestCase(
            question=self.question2, test_case='assert fact(3) == 6')
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{"test_case": "assert fact(3)==6"}]
        questions_data = [{
            "snippet": "def fact()",
            "active": True,
            "points": 1.0,
            "description": "factorial of a no",
            "language": "Python",
            "type": "Code",
            "test_case_type": "standardtestcase",
            "testcase": self.test_case_upload_data,
            "summary": "Json Demo"
        }]
        self.json_questions_data = json.dumps(questions_data)

    def test_question(self):
        """ Test question """
        self.assertEqual(self.question1.summary, 'Demo question')
        self.assertEqual(self.question1.language, 'Python')
        self.assertEqual(self.question1.type, 'Code')
        self.assertEqual(self.question1.description, 'Write a function')
        self.assertEqual(self.question1.points, 1.0)
        self.assertTrue(self.question1.active)
        self.assertEqual(self.question1.snippet, 'def myfunc()')
        tag_list = []
        for tag in self.question1.tags.all():
            tag_list.append(tag.name)
        self.assertEqual(tag_list, ['python', 'function'])

    def test_dump_questions_into_json(self):
        """ Test dump questions into json """
        question = Question()
        question_id = [self.question2.id]
        questions = json.loads(question.dump_into_json(question_id,
                                                       self.user2))
        test_case = self.question2.get_test_cases()
        for q in questions:
            self.assertEqual(self.question2.summary, q['summary'])
            self.assertEqual(self.question2.language, q['language'])
            self.assertEqual(self.question2.type, q['type'])
            self.assertEqual(self.question2.description, q['description'])
            self.assertEqual(self.question2.points, q['points'])
            self.assertTrue(self.question2.active)
            self.assertEqual(self.question2.snippet, q['snippet'])
            self.assertEqual(self.question2.test_case_type,
                             q['test_case_type'])
            self.assertEqual([case.get_field_value() for case in test_case],
                             q['testcase'])

    def test_load_questions_from_json(self):
        """ Test load questions into database from json """
        question = Question()
        result = question.load_from_json(self.json_questions_data, self.user1)
        question_data = Question.objects.get(pk=25)
        test_case = question_data.get_test_cases()
        self.assertEqual(question_data.summary, 'Json Demo')
        self.assertEqual(question_data.language, 'Python')
        self.assertEqual(question_data.type, 'Code')
        self.assertEqual(question_data.description, 'factorial of a no')
        self.assertEqual(question_data.points, 1.0)
        self.assertTrue(question_data.active)
        self.assertEqual(question_data.snippet, 'def fact()')
        self.assertEqual(question_data.test_case_type, 'standardtestcase')
        self.assertEqual([case.get_field_value() for case in test_case],
                         self.test_case_upload_data)
Beispiel #15
0
    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()
Beispiel #16
0
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)
Beispiel #17
0
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  description='Write a function',
                                  points=1.0,
                                  snippet='def myfunc()',
                                  user=self.user1)
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
                                  language='python',
                                  type='code',
                                  active=True,
                                  description='factorial of a no',
                                  points=2.0,
                                  snippet='def fact()',
                                  user=self.user2)
        self.question2.save()

        # create a temp directory and add files for loading questions test
        file_path = "/tmp/test.txt"
        self.load_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.load_tmp_path)
        file1 = os.path.join(self.load_tmp_path, "test.txt")

        # create a temp directory and add files for dumping questions test
        self.dump_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.dump_tmp_path)
        file2 = os.path.join(self.dump_tmp_path, "test.txt")
        file = open(file2, "r")
        django_file = File(file)
        file = FileUpload.objects.create(file=django_file,
                                         question=self.question2)

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(
            question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase')
        self.upload_test_case = StandardTestCase(
            question=self.question2,
            test_case='assert fact(3) == 6',
            type='standardtestcase')
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{
            "test_case": "assert fact(3)==6",
            "test_case_type": "standardtestcase",
            "test_case_args": "",
            "weight": 1.0
        }]
        questions_data = [{
            "snippet": "def fact()",
            "active": True,
            "points": 1.0,
            "description": "factorial of a no",
            "language": "Python",
            "type": "Code",
            "testcase": self.test_case_upload_data,
            "files": [[file1, 0]],
            "summary": "Json Demo"
        }]
        self.json_questions_data = json.dumps(questions_data)
Beispiel #18
0
class TestCaseTestCases(unittest.TestCase):
    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.question1 = Question(summary='Demo question 1',
            language='Python',
            type='Code',
            active=True,
            description='Write a function',
            points=1.0,
            test_case_type="standardtestcase",
            user=self.user,
            snippet='def myfunc()'
        )
        self.question2 = Question(summary='Demo question 2',
             language='Python',
             type='Code',
             active=True,
             description='Write to standard output',
             points=1.0,
             test_case_type="stdiobasedtestcase",
             user=self.user,
             snippet='def myfunc()'
        )
        self.question1.save()
        self.question2.save()
        self.assertion_testcase = StandardTestCase(
            question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase'
        )
        self.stdout_based_testcase = StdIOBasedTestCase(
            question=self.question2,
            expected_output='Hello World',
            type='standardtestcase'

        )
        self.assertion_testcase.save()
        self.stdout_based_testcase.save()
        answer_data = {'metadata': { 'user_answer': 'demo_answer',
                        'language': 'python',
                        'partial_grading': False
                        },
                    'test_case_data': [{'test_case': 'assert myfunc(12, 13) == 15',
                        'test_case_type': 'standardtestcase',
                        'weight': 1.0
                        }]
                    }
        self.answer_data_json = json.dumps(answer_data)

    def test_assertion_testcase(self):
        """ Test question """
        self.assertEqual(self.assertion_testcase.question, self.question1)
        self.assertEqual(self.assertion_testcase.test_case,
                             'assert myfunc(12, 13) == 15')

    def test_stdout_based_testcase(self):
        """ Test question """
        self.assertEqual(self.stdout_based_testcase.question, self.question2)
        self.assertEqual(self.stdout_based_testcase.expected_output,
            'Hello World'
        )

    def test_consolidate_answer_data(self):
        """ Test consolidate answer data model method """
        result = self.question1.consolidate_answer_data(
            user_answer="demo_answer"
        )
        actual_data = json.loads(result)
        exp_data = json.loads(self.answer_data_json)
        self.assertEqual(actual_data['metadata']['user_answer'], exp_data['metadata']['user_answer'])
        self.assertEqual(actual_data['test_case_data'], exp_data['test_case_data'])
Beispiel #19
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 TestCaseTestCases(unittest.TestCase):
    def setUp(self):
        self.user = User.objects.get(username="******")
        self.question1 = Question(summary='Demo question 1',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  description='Write a function',
                                  points=1.0,
                                  user=self.user,
                                  snippet='def myfunc()')
        self.question2 = Question(summary='Demo question 2',
                                  language='Python',
                                  type='Code',
                                  active=True,
                                  description='Write to standard output',
                                  points=1.0,
                                  user=self.user,
                                  snippet='def myfunc()')
        self.question1.save()
        self.question2.save()
        self.assertion_testcase = StandardTestCase(
            question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase')
        self.stdout_based_testcase = StdIOBasedTestCase(
            question=self.question2,
            expected_output='Hello World',
            type='standardtestcase')
        self.assertion_testcase.save()
        self.stdout_based_testcase.save()
        answer_data = {
            'metadata': {
                'user_answer': 'demo_answer',
                'language': 'python',
                'partial_grading': False
            },
            'test_case_data': [{
                'test_case': 'assert myfunc(12, 13) == 15',
                'test_case_type': 'standardtestcase',
                'test_case_args': "",
                'weight': 1.0
            }]
        }
        self.answer_data_json = json.dumps(answer_data)

    def test_assertion_testcase(self):
        """ Test question """
        self.assertEqual(self.assertion_testcase.question, self.question1)
        self.assertEqual(self.assertion_testcase.test_case,
                         'assert myfunc(12, 13) == 15')

    def test_stdout_based_testcase(self):
        """ Test question """
        self.assertEqual(self.stdout_based_testcase.question, self.question2)
        self.assertEqual(self.stdout_based_testcase.expected_output,
                         'Hello World')

    def test_consolidate_answer_data(self):
        """ Test consolidate answer data model method """
        result = self.question1.consolidate_answer_data(
            user_answer="demo_answer")
        actual_data = json.loads(result)
        exp_data = json.loads(self.answer_data_json)
        self.assertEqual(actual_data['metadata']['user_answer'],
                         exp_data['metadata']['user_answer'])
        self.assertEqual(actual_data['test_case_data'],
                         exp_data['test_case_data'])
Beispiel #21
0
    def setUp(self):
        # Single question details
        self.user1 = User.objects.get(pk=1)
        self.user2 = User.objects.get(pk=2)
        self.question1 = Question(summary='Demo question',
            language='Python',
            type='Code',
            active=True,
            test_case_type='standardtestcase',
            description='Write a function',
            points=1.0,
            snippet='def myfunc()',
            user=self.user1
        )
        self.question1.save()

        self.question2 = Question(summary='Demo Json',
            language='python',
            type='code',
            active=True,
            description='factorial of a no',
            test_case_type='standardtestcase',
            points=2.0,
            snippet='def fact()',
            user=self.user2
        )
        self.question2.save()

        # create a temp directory and add files for loading questions test
        file_path = "/tmp/test.txt"
        self.load_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.load_tmp_path)
        file1 = os.path.join(self.load_tmp_path, "test.txt")

        # create a temp directory and add files for dumping questions test
        self.dump_tmp_path = tempfile.mkdtemp()
        shutil.copy(file_path, self.dump_tmp_path)
        file2 = os.path.join(self.dump_tmp_path, "test.txt")
        file = open(file2, "r")
        django_file = File(file)
        file = FileUpload.objects.create(file=django_file, question=self.question2)

        self.question1.tags.add('python', 'function')
        self.assertion_testcase = StandardTestCase(question=self.question1,
            test_case='assert myfunc(12, 13) == 15',
            type='standardtestcase'
        )
        self.upload_test_case = StandardTestCase(question=self.question2,
            test_case='assert fact(3) == 6',
            type='standardtestcase'
        )
        self.upload_test_case.save()
        self.user_answer = "demo_answer"
        self.test_case_upload_data = [{"test_case": "assert fact(3)==6",
                                        "test_case_type": "standardtestcase",
                                        "weight": 1.0
                                        }]
        questions_data = [{"snippet": "def fact()", "active": True,
                           "points": 1.0,
                           "description": "factorial of a no",
                           "language": "Python", "type": "Code",
                           "testcase": self.test_case_upload_data,
                           "files": [[file1, 0]],
                           "summary": "Json Demo"}]
        self.json_questions_data = json.dumps(questions_data)
Beispiel #22
0
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')