From d953f6f9e62671eeb5d6ea6498475167301dfe91 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 2 May 2016 14:38:13 +0530 Subject: - Fix tests - Fix minor views.py - Fix minor errors caused due to conflict resolution, rebasing - Fix errors in forms, views - Minor Button label change in addquestion.html - Add snippet to question - Remove commented code --- yaksh/code_evaluator.py | 61 +++---------------------- yaksh/evaluator_tests/test_bash_evaluation.py | 40 ---------------- yaksh/evaluator_tests/test_code_evaluation.py | 25 ---------- yaksh/evaluator_tests/test_java_evaluation.py | 43 ----------------- yaksh/evaluator_tests/test_python_evaluation.py | 3 -- yaksh/evaluator_tests/test_scilab_evaluation.py | 20 -------- yaksh/forms.py | 11 ----- yaksh/models.py | 8 ++-- yaksh/python_assertion_evaluator.py | 2 - yaksh/scilab_code_evaluator.py | 4 -- yaksh/templates/yaksh/add_question.html | 2 +- yaksh/tests.py | 59 ++++++++++++++---------- yaksh/urls.py | 2 +- yaksh/views.py | 9 +++- 14 files changed, 57 insertions(+), 232 deletions(-) (limited to 'yaksh') diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py index b7a523c..1c11c00 100644 --- a/yaksh/code_evaluator.py +++ b/yaksh/code_evaluator.py @@ -5,14 +5,13 @@ import os import stat from os.path import isdir, dirname, abspath, join, isfile, exists import signal +import traceback from multiprocessing import Process, Queue import subprocess import re -# import json # Local imports. from settings import SERVER_TIMEOUT - MY_DIR = abspath(dirname(__file__)) @@ -50,36 +49,12 @@ def delete_signal_handler(): class CodeEvaluator(object): """Tests the code obtained from Code Server""" - # def __init__(self, test_case_data, test, language, user_answer, - # ref_code_path=None, in_dir=None): def __init__(self, in_dir=None): 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.test_case_data = test_case_data - # self.language = language.lower() #@@@remove - # self.user_answer = user_answer #@@@specific to check-code - # self.ref_code_path = ref_code_path #@@@specific to check-code - # self.test = test #@@@specific to check-code - self.in_dir = in_dir #@@@Common for all, no change - # self.test_case_args = None #@@@no change - - # Public Protocol ########## - # @classmethod - # def from_json(cls, language, json_data, in_dir): - # json_data = json.loads(json_data) - # # test_case_data = json_data.get("test_case_data") - # user_answer = json_data.get("user_answer") - # ref_code_path = json_data.get("ref_code_path") - # test = json_data.get("test") - - # # instance = cls(test_case_data, test, language, user_answer, ref_code_path, - # # in_dir) - # instance = cls(test, language, user_answer, ref_code_path, - # in_dir) - # return instance - - # def evaluate(self): + self.in_dir = in_dir + def evaluate(self, **kwargs): """Evaluates given code with the test cases based on given arguments in test_case_data. @@ -103,7 +78,6 @@ class CodeEvaluator(object): """ self.setup() - # success, err = self.safe_evaluate(self.test_case_args) success, err = self.safe_evaluate(**kwargs) self.teardown() @@ -114,18 +88,13 @@ class CodeEvaluator(object): def setup(self): self._change_dir(self.in_dir) - # def safe_evaluate(self, args): - # def safe_evaluate(self, **kwargs): #@@@v2 def safe_evaluate(self, user_answer, test_case_data): # Add a new signal handler for the execution of this code. prev_handler = create_signal_handler() success = False - # args = args or [] # Do whatever testing needed. try: - # success, err = self.check_code(*args) - # success, err = self.check_code(**kwargs) #@@@v2 for test_case in test_case_data: self.compile_code(user_answer, **test_case) success, err = self.check_code(user_answer, **test_case) @@ -134,9 +103,9 @@ class CodeEvaluator(object): except TimeoutException: err = self.timeout_msg - except: - _type, value = sys.exc_info()[:2] - err = "Error: {0}".format(repr(value)) + except Exception: + err = "Error: {0}".format(traceback.format_exc(limit=0)) + finally: # Set back any original signal handler. set_original_signal_handler(prev_handler) @@ -198,24 +167,6 @@ class CodeEvaluator(object): raise return proc, stdout, stderr - # def _compile_command(self, cmd, *args, **kw): - # """Compiles C/C++/java code and returns errors if any. - # Run a command in a subprocess while blocking, the process is killed - # if it takes more than 2 seconds to run. Return the Popen object, the - # stderr. - # """ - # try: - # proc_compile = subprocess.Popen(cmd, shell=True, stdin=None, - # stdout=subprocess.PIPE, - # stderr=subprocess.PIPE) - # out, err = proc_compile.communicate() - # except TimeoutException: - # # Runaway code, so kill it. - # proc_compile.kill() - # # Re-raise exception. - # raise - # return proc_compile, err - def _change_dir(self, in_dir): if in_dir is not None and isdir(in_dir): os.chdir(in_dir) diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py index 39247f7..7c58c43 100644 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -10,15 +10,6 @@ class BashEvaluationTestCases(unittest.TestCase): self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) - # def setUp(self): - # self.language = "bash" - # self.ref_code_path = "bash_files/sample.sh,bash_files/sample.args" - # self.in_dir = "/tmp" - # self.test_case_data = [] - # self.timeout_msg = ("Code took more than {0} seconds to run. " - # "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) - # self.test = None - def test_correct_answer(self): user_answer = "#!/bin/bash\n[[ $# -eq 2 ]] && echo $(( $1 + $2 )) && exit $(( $1 + $2 ))" get_class = BashCodeEvaluator(self.in_dir) @@ -49,37 +40,6 @@ class BashEvaluationTestCases(unittest.TestCase): self.assertFalse(result.get("success")) self.assertEquals(result.get("error"), self.timeout_msg) - # def test_infinite_loop(self): - # user_answer = "#!/bin/bash\nwhile [ 1 ] ; do echo "" > /dev/null ; done" - # get_class = BashCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertFalse(result.get("success")) - # self.assertEquals(result.get("error"), self.timeout_msg) - - # def test_correct_answer(self): - # user_answer = "#!/bin/bash\n[[ $# -eq 2 ]] && echo $(( $1 + $2 )) && exit $(( $1 + $2 ))" - # get_class = BashCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertTrue(result.get("success")) - # self.assertEqual(result.get("error"), "Correct answer") - - # def test_error(self): - # user_answer = "#!/bin/bash\n[[ $# -eq 2 ]] && echo $(( $1 - $2 )) && exit $(( $1 - $2 ))" - # get_class = BashCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertFalse(result.get("success")) - # self.assertTrue("Error" in result.get("error")) - - # def test_infinite_loop(self): - # user_answer = "#!/bin/bash\nwhile [ 1 ] ; do echo "" > /dev/null ; done" - # get_class = BashCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertFalse(result.get("success")) - # self.assertEquals(result.get("error"), self.timeout_msg) if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_code_evaluation.py b/yaksh/evaluator_tests/test_code_evaluation.py index e4f129c..ace6115 100644 --- a/yaksh/evaluator_tests/test_code_evaluation.py +++ b/yaksh/evaluator_tests/test_code_evaluation.py @@ -23,28 +23,3 @@ class RegistryTestCase(unittest.TestCase): if __name__ == '__main__': unittest.main() - - -# import unittest -# import os -# from yaksh import cpp_code_evaluator -# from yaksh.language_registry import _LanguageRegistry, get_registry -# from yaksh.settings import SERVER_TIMEOUT - - -# class RegistryTestCase(unittest.TestCase): -# def setUp(self): -# self.registry_object = get_registry() -# self.language_registry = _LanguageRegistry() - -# def test_set_register(self): -# class_name = getattr(cpp_code_evaluator, 'CppCodeEvaluator') -# self.registry_object.register("c", {"standardtestcase": "cpp_code_evaluator.CppCodeEvaluator"}) -# self.assertEquals(self.registry_object.get_class("c", "standardtestcase"), class_name) - -# def tearDown(self): -# self.registry_object = None - - -# if __name__ == '__main__': -# unittest.main() diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index eacd62e..76a3fcf 100644 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -57,49 +57,6 @@ class JavaEvaluationTestCases(unittest.TestCase): self.assertFalse(result.get("success")) self.assertEquals(result.get("error"), self.timeout_msg) - # def setUp(self): - # self.language = "java" - # self.ref_code_path = "java_files/main_square.java" - # self.in_dir = "/tmp" - # self.test_case_data = [] - # evaluator.SERVER_TIMEOUT = 9 - # self.timeout_msg = ("Code took more than {0} seconds to run. " - # "You probably have an infinite loop in " - # "your code.").format(evaluator.SERVER_TIMEOUT) - # self.test = None - - # def tearDown(self): - # evaluator.SERVER_TIMEOUT = 2 - - # def test_correct_answer(self): - # user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a*a;\n\t}\n}" - # get_class = JavaCodeEvaluator(self.test_case_data, self.test, - # self.language, user_answer, - # self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertTrue(result.get("success")) - # self.assertEqual(result.get("error"), "Correct answer") - - # def test_error(self): - # user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a*a" - # get_class = JavaCodeEvaluator(self.test_case_data, self.test, - # self.language, user_answer, - # self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertFalse(result.get("success")) - # self.assertTrue("Error" in result.get("error")) - - # def test_infinite_loop(self): - # user_answer = "class Test {\n\tint square_num(int a) {\n\t\twhile(0==0){\n\t\t}\n\t}\n}" - # get_class = JavaCodeEvaluator(self.test_case_data, self.test, - # self.language, user_answer, - # self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertFalse(result.get("success")) - # self.assertEquals(result.get("error"), self.timeout_msg) if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index 39d4723..f6ac0bf 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -16,7 +16,6 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) def test_correct_answer(self): - # {u'user_answer': u'def adder(a,b):\r\n return a', u'test_case_data': [u'assert(adder(1,2)==3)']} user_answer = "def add(a,b):\n\treturn a + b" get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, @@ -156,7 +155,6 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) def test_correct_answer(self): - # user_answer = "a = 'Hello'\nb = 'World'\nprint '{0} {1}'.format(a, b)" user_answer = "a,b=0,1\nfor i in range(5):\n\tprint a,\n\ta,b=b,a+b" get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, @@ -197,7 +195,6 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): self.assertFalse(result.get('success')) self.assertEqual(result.get('error'), 'Incorrect Answer') ->>>>>>> - Add test cases for multiple python evaluators if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_scilab_evaluation.py b/yaksh/evaluator_tests/test_scilab_evaluation.py index 928cd7e..24d6740 100644 --- a/yaksh/evaluator_tests/test_scilab_evaluation.py +++ b/yaksh/evaluator_tests/test_scilab_evaluation.py @@ -11,16 +11,6 @@ class ScilabEvaluationTestCases(unittest.TestCase): self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) - # def setUp(self): - # self.language = "scilab" - # self.ref_code_path = "scilab_files/test_add.sce" - # self.in_dir = "/tmp" - # self.test_case_data = [] - # self.timeout_msg = ("Code took more than {0} seconds to run. " - # "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) - # self.test = None - - def test_correct_answer(self): user_answer = "funcprot(0)\nfunction[c]=add(a,b)\n\tc=a+b;\nendfunction" get_class = ScilabCodeEvaluator(self.in_dir) @@ -31,16 +21,6 @@ class ScilabEvaluationTestCases(unittest.TestCase): self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) - # def test_correct_answer(self): - # user_answer = "funcprot(0)\nfunction[c]=add(a,b)\n\tc=a+b;\nendfunction" - # get_class = ScilabCodeEvaluator(self.test_case_data, self.test, - # self.language, user_answer, - # self.ref_code_path, self.in_dir) - # result = get_class.evaluate() - - # self.assertTrue(result.get("success")) - # self.assertEqual(result.get("error"), "Correct answer") - def test_error(self): user_answer = "funcprot(0)\nfunction[c]=add(a,b)\n\tc=a+b;\ndis(\tendfunction" get_class = ScilabCodeEvaluator(self.in_dir) diff --git a/yaksh/forms.py b/yaksh/forms.py index 2ce2cba..808262b 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -224,14 +224,3 @@ class ProfileForm(forms.ModelForm): class UploadFileForm(forms.Form): file = forms.FileField() - -class StandardTestCaseForm(forms.ModelForm): - class Meta: - model = StandardTestCase - fields = ['test_case'] - - -class StdoutBasedTestCaseForm(forms.ModelForm): - class Meta: - model = StdoutBasedTestCase - fields = ['output'] diff --git a/yaksh/models.py b/yaksh/models.py index a200ae1..dd2fb5f 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -176,6 +176,9 @@ class Question(models.Model): # Tags for the Question. tags = TaggableManager(blank=True) + # Snippet of code provided to the user. + snippet = models.CharField(max_length=256, blank=True) + # user for particular question user = models.ForeignKey(User, related_name="user") @@ -199,10 +202,9 @@ class Question(models.Model): questions_dict = [] for question in questions: q_dict = {'summary': question.summary, 'description': question.description, - 'points': question.points, 'test': question.test, - 'ref_code_path': question.ref_code_path, - 'options': question.options, 'language': question.language, + 'points': question.points, 'language': question.language, 'type': question.type, 'active': question.active, + 'test_case_type': question.test_case_type, 'snippet': question.snippet} questions_dict.append(q_dict) diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index ff56421..bf6a4be 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -39,8 +39,6 @@ class PythonAssertionEvaluator(CodeEvaluator): err = "{0} {1} in: {2}".format(type.__name__, str(value), text) except TimeoutException: raise - except Exception: - err = traceback.format_exc(limit=0) else: success = True err = 'Correct answer' diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 3af9782..f4aa5f8 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -14,9 +14,7 @@ class ScilabCodeEvaluator(CodeEvaluator): """Tests the Scilab code obtained from Code Server""" def setup(self): super(ScilabCodeEvaluator, self).setup() - # ref_path, test_case_path = self._set_test_code_file_path(self.ref_code_path) self.submit_code_path = self.create_submit_code_file('function.sci') - # return ref_path, # Return as a tuple def teardown(self): super(ScilabCodeEvaluator, self).teardown() @@ -43,8 +41,6 @@ class ScilabCodeEvaluator(CodeEvaluator): shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - # success = False - # proc, stdout, stderr = self.compiled_output proc, stdout, stderr = ret # Get only the error. diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index eb9419c..d38aa1c 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -40,7 +40,7 @@