From 81d373983806b78d651e17b966bae0a8ccf3c4bb Mon Sep 17 00:00:00 2001
From: ankitjavalkar
Date: Mon, 19 Dec 2016 17:43:41 +0530
Subject: Fix model test cases

---
 yaksh/code_evaluator.py         |  2 +-
 yaksh/models.py                 | 56 ++++++++++++++++++++++++++++++++---------
 yaksh/test_models.py            | 45 ++++++++++++++++++++-------------
 yaksh/tests/test_code_server.py | 48 ++++++++++++++++++++++-------------
 4 files changed, 103 insertions(+), 48 deletions(-)

diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py
index f1ac5b7..52720df 100644
--- a/yaksh/code_evaluator.py
+++ b/yaksh/code_evaluator.py
@@ -74,7 +74,7 @@ class CodeEvaluator(object):
         msg = 'Code took more than %s seconds to run. You probably '\
               'have an infinite loop in your code.' % SERVER_TIMEOUT
         self.timeout_msg = msg
-        self.in_dir = in_dir
+        self.in_dir = in_dir if in_dir else MY_DIR
 
 
     def evaluate(self, kwargs): #language, test_case_type, 
diff --git a/yaksh/models.py b/yaksh/models.py
index 4951836..272ec75 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -306,9 +306,13 @@ class Question(models.Model):
             que, result = Question.objects.get_or_create(**question)
             if file_names:
                 que._add_files_to_db(file_names, file_path)
-            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)
+                test_case_type = test_case.pop('test_case_type')
+                model_class = get_model_class(test_case_type)
+                # TestCase.objects.get_or_create(question=que, type=)
+                new_test_case, obj_create_status = model_class.objects.get_or_create(question=que, **test_case)
+                new_test_case.type = test_case_type
+                new_test_case.save()
         if files_list:
             delete_files(files_list, file_path)
 
@@ -320,22 +324,50 @@ class Question(models.Model):
         #     question=self,
         #     **kwargs
         # )
+        # tc_list = []
+        # for tc in self.testcase_set.filter(**kwargs):
+        #     tc_type = str(tc.type)
+        #     obj = getattr(tc, tc_type)
+        #     tc_list.append(obj)
+
+        # return tc_list
+
         tc_list = []
         for tc in self.testcase_set.all():
-            tc_type = tc.type
-            obj = getattr(tc, tc_type)
-            tc_list.append(obj)
+            test_case_type = tc.type
+            test_case_ctype = ContentType.objects.get(app_label="yaksh",
+                model=test_case_type
+            )
+            test_case = test_case_ctype.get_object_for_this_type(
+                question=self,
+                **kwargs
+            )
+            tc_list.append(test_case)
 
         return tc_list
 
     def get_test_case(self, **kwargs):
-        test_case_ctype = ContentType.objects.get(app_label="yaksh",
-            model=self.test_case_type
-        )
-        test_case = test_case_ctype.get_object_for_this_type(
-            question=self,
-            **kwargs
-        )
+        # test_case_ctype = ContentType.objects.get(app_label="yaksh",
+        #     model=self.test_case_type
+        # )
+        # test_case = test_case_ctype.get_object_for_this_type(
+        #     question=self,
+        #     **kwargs
+        # )
+        # tc = self.testcase_set.get(**kwargs)
+        # tc_type = str(tc.type)
+        # test_case = getattr(tc, tc_type)
+
+        # return test_case
+        for tc in self.testcase_set.all():
+            test_case_type = tc.type
+            test_case_ctype = ContentType.objects.get(app_label="yaksh",
+                model=self.test_case_type
+            )
+            test_case = test_case_ctype.get_object_for_this_type(
+                question=self,
+                **kwargs
+            )
 
         return test_case
 
diff --git a/yaksh/test_models.py b/yaksh/test_models.py
index 522da89..0db1b82 100644
--- a/yaksh/test_models.py
+++ b/yaksh/test_models.py
@@ -134,21 +134,23 @@ class QuestionTestCases(unittest.TestCase):
 
         self.question1.tags.add('python', 'function')
         self.assertion_testcase = StandardTestCase(question=self.question1,
-            test_case='assert myfunc(12, 13) == 15'
+            test_case='assert myfunc(12, 13) == 15',
+            type='standardtestcase'
         )
         self.upload_test_case = StandardTestCase(question=self.question2,
-            test_case='assert fact(3) == 6'
+            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",
-                           "test_case_type": "standardtestcase",
                            "testcase": self.test_case_upload_data,
                            "files": [[file1, 0]],
                            "summary": "Json Demo"}]
@@ -213,7 +215,6 @@ 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(os.path.basename(file.file.path), "test.txt")
         self.assertEqual([case.get_field_value() for case in test_case], self.test_case_upload_data)
 
@@ -511,19 +512,24 @@ class AnswerPaperTestCases(unittest.TestCase):
         self.question3.save()
         self.assertion_testcase = StandardTestCase(
             question=self.question1,
-            test_case='assert add(1, 3) == 4'
+            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
+            correct = True,
+            type = 'mcqtestcase'
+
         )
         self.mcq_based_testcase.save()
         self.mcc_based_testcase = McqTestCase(
             question=self.question3,
             options = 'a',
-            correct = True
+            correct = True,
+            type = 'mcqtestcase'
         )
         self.mcc_based_testcase.save()
 
@@ -870,21 +876,26 @@ class TestCaseTestCases(unittest.TestCase):
         self.question2.save()
         self.assertion_testcase = StandardTestCase(
             question=self.question1,
-            test_case='assert myfunc(12, 13) == 15'
+            test_case='assert myfunc(12, 13) == 15',
+            type='standardtestcase'
         )
         self.stdout_based_testcase = StdioBasedTestCase(
             question=self.question2,
-            expected_output='Hello World'
+            expected_output='Hello World',
+            type='standardtestcase'
+
         )
         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": 1.0
-                }
-            ]
-        }
+        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):
@@ -907,5 +918,5 @@ class TestCaseTestCases(unittest.TestCase):
         )
         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['metadata']['user_answer'], exp_data['metadata']['user_answer'])
         self.assertEqual(actual_data['test_case_data'], exp_data['test_case_data'])
diff --git a/yaksh/tests/test_code_server.py b/yaksh/tests/test_code_server.py
index 7efd20b..d46c9dd 100644
--- a/yaksh/tests/test_code_server.py
+++ b/yaksh/tests/test_code_server.py
@@ -37,12 +37,15 @@ class TestCodeServer(unittest.TestCase):
 
     def test_infinite_loop(self):
         # Given
-        testdata = {'user_answer': 'while True: pass',
-                    'partial_grading': False,
+        testdata = {'metadata': {'user_answer': 'while True: pass',
+                        'language': 'python',
+                        'partial_grading': False
+                        },
                     'test_case_data': [{'test_case':'assert 1==2', 
+                        'test_case_type': 'standardtestcase',
                         'weight': 0.0
-                        }
-                    ]}
+                        }]
+                    }
 
         # When
         result = self.code_server.run_code(
@@ -56,12 +59,15 @@ class TestCodeServer(unittest.TestCase):
 
     def test_correct_answer(self):
         # Given
-        testdata = {'user_answer': 'def f(): return 1',
-                    'partial_grading': False,
+        testdata = {'metadata': { 'user_answer': 'def f(): return 1',
+                        'language': 'python',
+                        'partial_grading': False
+                        },
                     'test_case_data': [{'test_case':'assert f() == 1',
+                        'test_case_type': 'standardtestcase',
                         'weight': 0.0
-                        }
-                    ]}
+                        }]
+                    }
 
         # When
         result = self.code_server.run_code(
@@ -75,12 +81,15 @@ class TestCodeServer(unittest.TestCase):
 
     def test_wrong_answer(self):
         # Given
-        testdata = {'user_answer': 'def f(): return 1',
-                    'partial_grading': False,
+        testdata = {'metadata': { 'user_answer': 'def f(): return 1',
+                        'language': 'python',
+                        'partial_grading': False
+                        },
                     'test_case_data': [{'test_case':'assert f() == 2',
+                        'test_case_type': 'standardtestcase',
                         'weight': 0.0
-                        }
-                    ]}
+                        }]
+                    }
 
         # When
         result = self.code_server.run_code(
@@ -98,12 +107,15 @@ class TestCodeServer(unittest.TestCase):
 
         def run_code():
             """Run an infinite loop."""
-            testdata = {'user_answer': 'while True: pass',
-                        'partial_grading': False,
-                        'test_case_data': [{'test_case':'assert 1==2',
-                            'weight': 0.0
-                            }
-                        ]}
+            testdata = {'metadata': { 'user_answer': 'while True: pass',
+                            'language': 'python',
+                            'partial_grading': False
+                        },
+                    'test_case_data': [{'test_case':'assert 1==2',
+                        'test_case_type': 'standardtestcase',
+                        'weight': 0.0
+                        }]
+                    }
             result = self.code_server.run_code(
                 'python', 'standardtestcase', json.dumps(testdata), ''
             )
-- 
cgit