diff options
-rw-r--r-- | yaksh/forms.py | 1 | ||||
-rw-r--r-- | yaksh/models.py | 28 | ||||
-rw-r--r-- | yaksh/tests.py | 35 |
3 files changed, 42 insertions, 22 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py index 8b864d0..a443e34 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -226,4 +226,3 @@ class ProfileForm(forms.ModelForm): class UploadFileForm(forms.Form): file = forms.FileField() - diff --git a/yaksh/models.py b/yaksh/models.py index 2c1554a..7035d1e 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -184,7 +184,7 @@ class Question(models.Model): # The type of evaluator test_case_type = models.CharField(max_length=24, choices=test_case_types) - + # Is this question active or not. If it is inactive it will not be used # when creating a QuestionPaper. active = models.BooleanField(default=True) @@ -214,17 +214,19 @@ class Question(models.Model): return json.dumps(question_data) def dump_into_json(self, question_ids, user): - questions = Question.objects.filter(id__in = question_ids, user_id = user.id) + questions = Question.objects.filter(id__in=question_ids, user_id=user.id) questions_dict = [] for question in questions: - q_dict = {'summary': question.summary, - 'description': question.description, - 'points': question.points, - 'language': question.language, - 'type': question.type, - 'active': question.active, - 'test_case_type': question.test_case_type, - 'snippet': question.snippet} + test_case = question.get_test_cases() + q_dict = {'summary': question.summary, + 'description': question.description, + 'points': question.points, + 'language': question.language, + 'type': question.type, + 'active': question.active, + 'test_case_type': question.test_case_type, + 'snippet': question.snippet, + 'testcase': [case.get_field_value() for case in test_case]} questions_dict.append(q_dict) return json.dumps(questions_dict, indent=2) @@ -233,7 +235,11 @@ class Question(models.Model): questions = json.loads(questions_list) for question in questions: question['user'] = user - Question.objects.get_or_create(**question) + test_cases = question.pop('testcase') + que, result = Question.objects.get_or_create(**question) + model_class = get_model_class(que.test_case_type) + for test_case in test_cases: + model_class.objects.get_or_create(question=que, **test_case) def get_test_cases(self, **kwargs): test_case_ctype = ContentType.objects.get(app_label="yaksh", diff --git a/yaksh/tests.py b/yaksh/tests.py index 441f0f6..e65e699 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -94,13 +94,14 @@ class QuestionTestCases(unittest.TestCase): ) self.question1.save() - self.question2 = Question(summary='Demo Json', + self.question2 = Question(summary='Demo Json', language='python', - type='code', + type='code', active=True, - description='factorial of a no', + description='factorial of a no', + test_case_type='standardtestcase', points=2.0, - snippet='def fact()', + snippet='def fact()', user=self.user2 ) self.question2.save() @@ -109,11 +110,19 @@ class QuestionTestCases(unittest.TestCase): 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" - questions_data = [{"snippet": "def fact()", "active": True, "points": 1.0, - "description": "factorial of a no", - "language": "Python", "type": "Code", - "summary": "Json Demo"}] + 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): @@ -133,8 +142,9 @@ class QuestionTestCases(unittest.TestCase): def test_dump_questions_into_json(self): """ Test dump questions into json """ question = Question() - question_id = ['24'] - questions = json.loads(question.dump_into_json(question_id, self.user1)) + 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']) @@ -143,12 +153,15 @@ class QuestionTestCases(unittest.TestCase): 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') @@ -156,6 +169,8 @@ class QuestionTestCases(unittest.TestCase): 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) ############################################################################### |