diff options
author | maheshgudi | 2017-02-09 00:48:34 +0530 |
---|---|---|
committer | maheshgudi | 2017-03-02 20:38:41 +0530 |
commit | a7a54b43f36571adc5bb999f454fe57e1dcb5c0f (patch) | |
tree | 9e02033a00439ab4ff37a33f352b9eae95974a04 | |
parent | 23018fa38afcdfec93f1338bd783a769a0fe91e7 (diff) | |
download | online_test-a7a54b43f36571adc5bb999f454fe57e1dcb5c0f.tar.gz online_test-a7a54b43f36571adc5bb999f454fe57e1dcb5c0f.tar.bz2 online_test-a7a54b43f36571adc5bb999f454fe57e1dcb5c0f.zip |
added test cases for float,integer and string type questions
-rw-r--r-- | yaksh/evaluator_tests/test_simple_question_types.py | 296 | ||||
-rw-r--r-- | yaksh/models.py | 2 |
2 files changed, 297 insertions, 1 deletions
diff --git a/yaksh/evaluator_tests/test_simple_question_types.py b/yaksh/evaluator_tests/test_simple_question_types.py new file mode 100644 index 0000000..ca93220 --- /dev/null +++ b/yaksh/evaluator_tests/test_simple_question_types.py @@ -0,0 +1,296 @@ +import unittest +from datetime import datetime, timedelta +from django.utils import timezone +import pytz +from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ + QuestionSet, AnswerPaper, Answer, Course, IntegerTestCase, FloatTestCase,\ + StringTestCase + + +def setUpModule(): + # create user profile + user = User.objects.create_user(pk=1,username='demo_user', + password='demo', + email='demo@test.com') + Profile.objects.create(user=user, roll_number=1, + institute='IIT', department='Aerospace', + position='Student') + + # create 2 questions + for i in range(101, 103): + Question.objects.create(summary='Q%d' % (i), points=1, + type='code', user=user) + + # create a course + course = Course.objects.create(name="Python Course", + enrollment="Enroll Request", creator=user) + + quiz = Quiz.objects.create(start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), + end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, + tzinfo=pytz.utc), + duration=30, active=True, attempts_allowed=1, + time_between_attempts=0, description='demo quiz', + pass_criteria=0,language='Python', + prerequisite=None,course=course, + instructions="Demo Instructions" + ) + question_paper = QuestionPaper.objects.create(pk=1, quiz=quiz, + total_marks=1.0) + + answerpaper = AnswerPaper.objects.create(user=user, user_ip='101.0.0.1', + start_time=timezone.now(), + question_paper=question_paper, + end_time=timezone.now() + +timedelta(minutes=5), + attempt_number=1 + ) + + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + +def tearDownModule(): + User.objects.all().delete() + Question.objects.all().delete() + Quiz.objects.all().delete() + + +class IntegerQuestionTestCases(unittest.TestCase): + @classmethod + def setUpClass(self): + # Creating Quiz + # Creating Question paper + self.question_paper = QuestionPaper.objects.get(pk=1) + + #Creating User + self.user = User.objects.get(pk=1) + + #Creating Question + self.question1 = Question.objects.get(summary='Q101') + self.question1.language = 'python' + self.question1.type = "integer" + self.question1.test_case_type = 'integertestcase' + self.question1.description = 'sum of 12+13?' + self.question1.save() + + #Creating answerpaper + self.answerpaper = AnswerPaper.objects.get(question_paper\ + =self.question_paper) + self.answerpaper.attempt_number = 1 + self.answerpaper.save() + # For question + self.integer_based_testcase = IntegerTestCase(question=self.question1, + correct=25, + type = 'integertestcase', + ) + self.integer_based_testcase.save() + + def test_integer_correct_answer(self): + # Given + integer_answer = 25 + self.answer = Answer(question=self.question1, + answer=integer_answer, + ) + self.answer.save() + self.answerpaper.answers.add(self.answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(integer_answer, + self.question1, + json_data, + ) + # Then + self.assertTrue(result['success']) + + def test_integer_incorrect_answer(self): + # Given + integer_answer = 26 + self.answer = Answer(question=self.question1, + answer=integer_answer, + ) + self.answer.save() + self.answerpaper.answers.add(self.answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(integer_answer, + self.question1, json_data + ) + + # Then + self.assertFalse(result['success']) + + +class StringQuestionTestCases(unittest.TestCase): + @classmethod + def setUpClass(self): + # Creating Question paper + self.question_paper = QuestionPaper.objects.get(pk=1) + + #Creating User + self.user = User.objects.get(pk=1) + + #Creating Question + self.question1 = Question.objects.get(summary='Q101') + self.question1.language = 'python' + self.question1.type = "string" + self.question1.test_case_type = 'stringtestcase' + self.question1.description = 'Write Hello, EARTH!' + self.question1.save() + + self.question2 = Question.objects.get(summary='Q102') + self.question2.language = 'python' + self.question2.type = "string" + self.question2.test_case_type = 'stringtestcase' + self.question2.description = 'Write Hello, EARTH!' + self.question2.save() + + #Creating answerpaper + self.answerpaper = AnswerPaper.objects.get(question_paper\ + =self.question_paper) + self.answerpaper.attempt_number = 1 + self.answerpaper.save() + + # For question + self.lower_string_testcase = StringTestCase(question=self.question1, + correct="Hello, EARTH!", + string_check="lower", + type = 'stringtestcase', + ) + self.lower_string_testcase.save() + + self.exact_string_testcase = StringTestCase(question=self.question2, + correct="Hello, EARTH!", + string_check="exact", + type = 'stringtestcase', + ) + self.exact_string_testcase.save() + + def test_case_insensitive_string_correct_answer(self): + # Given + string_answer = "hello, earth!" + answer = Answer(question=self.question1,answer=string_answer) + answer.save() + self.answerpaper.answers.add(answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(string_answer, + self.question1, json_data + ) + # Then + self.assertTrue(result['success']) + + def test_case_insensitive_string_incorrect_answer(self): + # Given + string_answer = "hello, mars!" + answer = Answer(question=self.question1,answer=string_answer) + answer.save() + self.answerpaper.answers.add(answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(string_answer, + self.question1, json_data + ) + + # Then + self.assertFalse(result['success']) + + def test_case_sensitive_string_correct_answer(self): + # Given + string_answer = "Hello, EARTH!" + answer = Answer(question=self.question2,answer=string_answer) + answer.save() + self.answerpaper.answers.add(answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(string_answer, + self.question2, json_data + ) + # Then + self.assertTrue(result['success']) + + def test_case_sensitive_string_incorrect_answer(self): + # Given + string_answer = "hello, earth!" + answer = Answer(question=self.question2,answer=string_answer) + answer.save() + self.answerpaper.answers.add(answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(string_answer, + self.question2, json_data + ) + + # Then + self.assertFalse(result['success']) + + +class FloatQuestionTestCases(unittest.TestCase): + @classmethod + def setUpClass(self): + #Creating User + self.user = User.objects.get(pk=1) + + # Creating Question paper + self.question_paper = QuestionPaper.objects.get(pk=1) + + #Creating Question + self.question1 = Question.objects.get(summary='Q101') + self.question1.language = 'python' + self.question1.type = "float" + self.question1.test_case_type = 'floattestcase' + self.question1.save() + + #Creating answerpaper + self.answerpaper = AnswerPaper.objects.get(question_paper\ + =self.question_paper) + self.answerpaper.attempt_number = 1 + self.answerpaper.save() + # For question + self.float_based_testcase = FloatTestCase(question=self.question1, + correct=100, + error_margin=0.1, + type = 'floattestcase', + ) + self.float_based_testcase.save() + + def test_float_correct_answer(self): + # Given + float_answer = 99.9 + self.answer = Answer(question=self.question1, + answer=float_answer, + ) + self.answer.save() + self.answerpaper.answers.add(self.answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(float_answer, + self.question1, + json_data, + ) + # Then + self.assertTrue(result['success']) + + def test_integer_incorrect_answer(self): + # Given + float_answer = 99.8 + self.answer = Answer(question=self.question1, + answer=float_answer, + ) + self.answer.save() + self.answerpaper.answers.add(self.answer) + + # When + json_data = None + result = self.answerpaper.validate_answer(float_answer, + self.question1, json_data + ) + + # Then + self.assertFalse(result['success']) diff --git a/yaksh/models.py b/yaksh/models.py index 665805e..a356ec1 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1350,4 +1350,4 @@ class FloatTestCase(TestCase): def __str__(self): return u'Testcase | Correct: {0} | Error Margin: +or- {1}'.format(self.correct, self.error_margin - )
\ No newline at end of file + ) |