From 5b23647de575fd90552807260a4b8e0a96ab6afe Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 12 May 2015 17:35:16 +0530 Subject: Seperated tests into seperate folder, formatting changes --- testapp/exam/bash_code_evaluator.py | 4 +- testapp/exam/code_evaluator.py | 8 +--- testapp/exam/code_server.py | 6 --- testapp/exam/cpp_code_evaluator.py | 19 ++++---- testapp/exam/java_code_evaluator.py | 11 +++-- testapp/exam/models.py | 4 +- testapp/exam/python_code_evaluator.py | 7 ++- testapp/exam/scilab_code_evaluator.py | 5 ++- testapp/test_bash_evaluation.py | 42 ------------------ testapp/test_c_cpp_evaluation.py | 77 --------------------------------- testapp/test_code_evaluation.py | 24 ---------- testapp/test_java_evaluation.py | 41 ------------------ testapp/test_python_evaluation.py | 54 ----------------------- testapp/test_scilab_evaluation.py | 39 ----------------- testapp/tests/__init__.py | 0 testapp/tests/test_bash_evaluation.py | 41 ++++++++++++++++++ testapp/tests/test_c_cpp_evaluation.py | 77 +++++++++++++++++++++++++++++++++ testapp/tests/test_code_evaluation.py | 24 ++++++++++ testapp/tests/test_java_evaluation.py | 41 ++++++++++++++++++ testapp/tests/test_python_evaluation.py | 54 +++++++++++++++++++++++ testapp/tests/test_scilab_evaluation.py | 39 +++++++++++++++++ 21 files changed, 304 insertions(+), 313 deletions(-) delete mode 100644 testapp/test_bash_evaluation.py delete mode 100644 testapp/test_c_cpp_evaluation.py delete mode 100644 testapp/test_code_evaluation.py delete mode 100644 testapp/test_java_evaluation.py delete mode 100644 testapp/test_python_evaluation.py delete mode 100644 testapp/test_scilab_evaluation.py create mode 100644 testapp/tests/__init__.py create mode 100644 testapp/tests/test_bash_evaluation.py create mode 100644 testapp/tests/test_c_cpp_evaluation.py create mode 100644 testapp/tests/test_code_evaluation.py create mode 100644 testapp/tests/test_java_evaluation.py create mode 100644 testapp/tests/test_python_evaluation.py create mode 100644 testapp/tests/test_scilab_evaluation.py (limited to 'testapp') diff --git a/testapp/exam/bash_code_evaluator.py b/testapp/exam/bash_code_evaluator.py index 7fcfb0f..23c0ae5 100644 --- a/testapp/exam/bash_code_evaluator.py +++ b/testapp/exam/bash_code_evaluator.py @@ -23,11 +23,11 @@ class BashCodeEvaluator(CodeEvaluator): def _setup(self): super(BashCodeEvaluator, self)._setup() - self.set_file_as_executable(self.submit_path) + self._set_file_as_executable(self.submit_path) get_ref_path, get_test_case_path = self.ref_code_path.strip().split(',') get_ref_path = get_ref_path.strip() get_test_case_path = get_test_case_path.strip() - ref_path, test_case_path = self.set_test_code_file_path(get_ref_path, + ref_path, test_case_path = self._set_test_code_file_path(get_ref_path, get_test_case_path) return ref_path, self.submit_path, test_case_path diff --git a/testapp/exam/code_evaluator.py b/testapp/exam/code_evaluator.py index 1efd519..2a57257 100644 --- a/testapp/exam/code_evaluator.py +++ b/testapp/exam/code_evaluator.py @@ -22,7 +22,6 @@ class TimeoutException(Exception): pass -# Private Protocol ########## def timeout_handler(signum, frame): """A handler for the ALARM signal.""" raise TimeoutException('Code took too long to run.') @@ -49,9 +48,6 @@ def delete_signal_handler(): return -############################################################################### -# `TestCode` class. -############################################################################### class CodeEvaluator(object): """Tests the code obtained from Code Server""" def __init__(self, test_case_data, test, language, user_answer, @@ -150,12 +146,12 @@ class CodeEvaluator(object): return submit_path - def set_file_as_executable(self, fname): + def _set_file_as_executable(self, fname): os.chmod(fname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH) - def set_test_code_file_path(self, ref_path=None, test_case_path=None): + def _set_test_code_file_path(self, ref_path=None, test_case_path=None): if ref_path and not ref_path.startswith('/'): ref_path = join(MY_DIR, ref_path) diff --git a/testapp/exam/code_server.py b/testapp/exam/code_server.py index 3dd5072..580379f 100755 --- a/testapp/exam/code_server.py +++ b/testapp/exam/code_server.py @@ -32,12 +32,6 @@ import json # Local imports. from settings import SERVER_PORTS, SERVER_TIMEOUT, SERVER_POOL_PORT from language_registry import set_registry -# from evaluate_python_code import EvaluatePythonCode -# from evaluate_c_code import EvaluateCCode -# from evaluate_cpp_code import EvaluateCppCode -# from evaluate_java_code import EvaluateJavaCode -# from evaluate_scilab_code import EvaluateScilabCode -# from evaluate_bash_code import EvaluateBashCode MY_DIR = abspath(dirname(__file__)) diff --git a/testapp/exam/cpp_code_evaluator.py b/testapp/exam/cpp_code_evaluator.py index 04efba8..15e2b13 100644 --- a/testapp/exam/cpp_code_evaluator.py +++ b/testapp/exam/cpp_code_evaluator.py @@ -12,19 +12,20 @@ from code_evaluator import CodeEvaluator class CppCodeEvaluator(CodeEvaluator): """Tests the C code obtained from Code Server""" - def __init__(self, test_case_data, test, language, user_answer, - ref_code_path=None, in_dir=None): - super(CppCodeEvaluator, self).__init__(test_case_data, test, language, user_answer, - ref_code_path, in_dir) + def __init__(self, test_case_data, test, language, user_answer, + ref_code_path=None, in_dir=None): + super(CppCodeEvaluator, self).__init__(test_case_data, test, language, + user_answer, ref_code_path, + in_dir) self.submit_path = self.create_submit_code_file('submit.c') - self.test_case_args = self._setup() + self.test_case_args = self._setup() # Private Protocol ########## def _setup(self): super(CppCodeEvaluator, self)._setup() get_ref_path = self.ref_code_path - ref_path, test_case_path = self.set_test_code_file_path(get_ref_path) + ref_path, test_case_path = self._set_test_code_file_path(get_ref_path) # Set file paths c_user_output_path = os.getcwd() + '/output' @@ -40,7 +41,8 @@ class CppCodeEvaluator(CodeEvaluator): remove_user_output = c_user_output_path remove_ref_output = c_ref_output_path - return ref_path, self.submit_path, compile_command, compile_main, run_command_args, remove_user_output, remove_ref_output + return (ref_path, self.submit_path, compile_command, compile_main, + run_command_args, remove_user_output, remove_ref_output) def _teardown(self): # Delete the created file. @@ -75,10 +77,8 @@ class CppCodeEvaluator(CodeEvaluator): return False, 'No file at %s or Incorrect path' % submit_code_path success = False - # output_path = os.getcwd() + '/output' ret = self._compile_command(compile_command) proc, stdnt_stderr = ret - # if self.language == "java": stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) # Only if compilation is successful, the program is executed @@ -86,7 +86,6 @@ class CppCodeEvaluator(CodeEvaluator): if stdnt_stderr == '': ret = self._compile_command(compile_main) proc, main_err = ret - # if self.language == "java": main_err = self._remove_null_substitute_char(main_err) if main_err == '': diff --git a/testapp/exam/java_code_evaluator.py b/testapp/exam/java_code_evaluator.py index 709a0a1..08ae208 100644 --- a/testapp/exam/java_code_evaluator.py +++ b/testapp/exam/java_code_evaluator.py @@ -14,7 +14,8 @@ class JavaCodeEvaluator(CodeEvaluator): """Tests the Java code obtained from Code Server""" def __init__(self, test_case_data, test, language, user_answer, ref_code_path=None, in_dir=None): - super(JavaCodeEvaluator, self).__init__(test_case_data, test, language, user_answer, + super(JavaCodeEvaluator, self).__init__(test_case_data, test, + language, user_answer, ref_code_path, in_dir) self.submit_path = self.create_submit_code_file('Test.java') self.test_case_args = self._setup() @@ -23,7 +24,7 @@ class JavaCodeEvaluator(CodeEvaluator): def _setup(self): super(JavaCodeEvaluator, self)._setup() - ref_path, test_case_path = self.set_test_code_file_path(self.ref_code_path) + ref_path, test_case_path = self._set_test_code_file_path(self.ref_code_path) # Set file paths java_student_directory = os.getcwd() + '/' @@ -42,7 +43,8 @@ class JavaCodeEvaluator(CodeEvaluator): remove_ref_output = "{0}{1}.class".format(java_student_directory, java_ref_file_name) - return ref_path, self.submit_path, compile_command, compile_main, run_command_args, remove_user_output, remove_ref_output + return (ref_path, self.submit_path, compile_command, compile_main, + run_command_args, remove_user_output, remove_ref_output) def _teardown(self): # Delete the created file. @@ -77,10 +79,8 @@ class JavaCodeEvaluator(CodeEvaluator): return False, 'No file at %s or Incorrect path' % submit_code_path success = False - # output_path = os.getcwd() + '/output' ret = self._compile_command(compile_command) proc, stdnt_stderr = ret - # if self.language == "java": stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) # Only if compilation is successful, the program is executed @@ -88,7 +88,6 @@ class JavaCodeEvaluator(CodeEvaluator): if stdnt_stderr == '': ret = self._compile_command(compile_main) proc, main_err = ret - # if self.language == "java": main_err = self._remove_null_substitute_char(main_err) if main_err == '': diff --git a/testapp/exam/models.py b/testapp/exam/models.py index a60550c..c5043dc 100644 --- a/testapp/exam/models.py +++ b/testapp/exam/models.py @@ -20,8 +20,8 @@ class Profile(models.Model): languages = ( ("python", "Python"), ("bash", "Bash"), - ("C", "C Language"), - ("CPP", "C++ Language"), + ("c", "C Language"), + ("cpp", "C++ Language"), ("java", "Java Language"), ("scilab", "Scilab"), ) diff --git a/testapp/exam/python_code_evaluator.py b/testapp/exam/python_code_evaluator.py index 61eee66..0c473cf 100644 --- a/testapp/exam/python_code_evaluator.py +++ b/testapp/exam/python_code_evaluator.py @@ -52,7 +52,10 @@ class PythonCodeEvaluator(CodeEvaluator): if test_case.get('kw_args') else "" args = pos_args + ", " + kw_args if pos_args and kw_args \ else pos_args or kw_args - tcode = "assert {0}({1}) == {2}".format(test_case.get('func_name'), - args, test_case.get('expected_answer')) + function_name = test_case.get('func_name') + expected_answer = test_case.get('expected_answer') + + tcode = "assert {0}({1}) == {2}".format(function_name, args, + expected_answer) test_code += tcode + "\n" return test_code diff --git a/testapp/exam/scilab_code_evaluator.py b/testapp/exam/scilab_code_evaluator.py index a4628a2..53640cc 100644 --- a/testapp/exam/scilab_code_evaluator.py +++ b/testapp/exam/scilab_code_evaluator.py @@ -14,7 +14,8 @@ class ScilabCodeEvaluator(CodeEvaluator): """Tests the Scilab code obtained from Code Server""" def __init__(self, test_case_data, test, language, user_answer, ref_code_path=None, in_dir=None): - super(ScilabCodeEvaluator, self).__init__(test_case_data, test, language, user_answer, + super(ScilabCodeEvaluator, self).__init__(test_case_data, test, + language, user_answer, ref_code_path, in_dir) self.submit_path = self.create_submit_code_file('function.sci') self.test_case_args = self._setup() @@ -23,7 +24,7 @@ class ScilabCodeEvaluator(CodeEvaluator): def _setup(self): super(ScilabCodeEvaluator, self)._setup() - ref_path, test_case_path = self.set_test_code_file_path(self.ref_code_path) + ref_path, test_case_path = self._set_test_code_file_path(self.ref_code_path) return ref_path, # Return as a tuple diff --git a/testapp/test_bash_evaluation.py b/testapp/test_bash_evaluation.py deleted file mode 100644 index 5dce5fa..0000000 --- a/testapp/test_bash_evaluation.py +++ /dev/null @@ -1,42 +0,0 @@ - -import unittest -import os -from exam.bash_code_evaluator import BashCodeEvaluator -from exam.settings import SERVER_TIMEOUT - -class BashEvaluationTestCases(unittest.TestCase): - 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.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() \ No newline at end of file diff --git a/testapp/test_c_cpp_evaluation.py b/testapp/test_c_cpp_evaluation.py deleted file mode 100644 index b820963..0000000 --- a/testapp/test_c_cpp_evaluation.py +++ /dev/null @@ -1,77 +0,0 @@ -import unittest -import os -from exam.cpp_code_evaluator import CppCodeEvaluator -from exam.settings import SERVER_TIMEOUT - -class CEvaluationTestCases(unittest.TestCase): - def setUp(self): - self.language = "C" - self.ref_code_path = "c_cpp_files/main.cpp" - 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 = "int add(int a, int b)\n{return a+b;}" - get_class = CppCodeEvaluator(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_compilation_error(self): - user_answer = "int add(int a, int b)\n{return a+b}" - get_class = CppCodeEvaluator(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("Compilation Error" in result.get("error")) - - def test_infinite_loop(self): - user_answer = "int add(int a, int b)\n{while(1>0){}}" - get_class = CppCodeEvaluator(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) - - -############################################################################### -class CppEvaluationTestCases(unittest.TestCase): - def setUp(self): - self.language = "CPP" - self.ref_code_path = "c_cpp_files/main.cpp" - 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 = "int add(int a, int b)\n{return a+b;}" - get_class = CppCodeEvaluator(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_compilation_error(self): - user_answer = "int add(int a, int b)\n{return a+b}" - get_class = CppCodeEvaluator(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("Compilation Error" in result.get("error")) - - def test_infinite_loop(self): - user_answer = "int add(int a, int b)\n{while(1>0){}}" - get_class = CppCodeEvaluator(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() \ No newline at end of file diff --git a/testapp/test_code_evaluation.py b/testapp/test_code_evaluation.py deleted file mode 100644 index fa2b1fa..0000000 --- a/testapp/test_code_evaluation.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest -import os -from exam import python_code_evaluator -from exam.language_registry import _LanguageRegistry, set_registry, get_registry -from exam.settings import SERVER_TIMEOUT - - -class RegistryTestCase(unittest.TestCase): - def setUp(self): - set_registry() - self.registry_object = get_registry() - self.language_registry = _LanguageRegistry() - - def test_set_register(self): - class_name = getattr(python_code_evaluator, 'PythonCodeEvaluator') - self.registry_object.register("python", "exam.python_code_evaluator.PythonCodeEvaluator") - self.assertEquals(self.registry_object.get_class("python"), class_name) - - def tearDown(self): - self.registry_object = None - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/testapp/test_java_evaluation.py b/testapp/test_java_evaluation.py deleted file mode 100644 index d86d7b3..0000000 --- a/testapp/test_java_evaluation.py +++ /dev/null @@ -1,41 +0,0 @@ -import unittest -import os -from exam.java_code_evaluator import JavaCodeEvaluator -from exam.settings import SERVER_TIMEOUT - -class JavaEvaluationTestCases(unittest.TestCase): - def setUp(self): - self.language = "java" - self.ref_code_path = "java_files/main_square.java" - 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 = "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() \ No newline at end of file diff --git a/testapp/test_python_evaluation.py b/testapp/test_python_evaluation.py deleted file mode 100644 index 57e111c..0000000 --- a/testapp/test_python_evaluation.py +++ /dev/null @@ -1,54 +0,0 @@ - -import unittest -import os -from exam.python_code_evaluator import PythonCodeEvaluator -from exam.settings import SERVER_TIMEOUT - -class PythonEvaluationTestCases(unittest.TestCase): - def setUp(self): - self.language = "Python" - self.test = None - self.test_case_data = [{"func_name": "add", - "expected_answer": "5", - "test_id": u'null', - "pos_args": ["3", "2"], - "kw_args": {} - }] - self.timeout_msg = ("Code took more than {0} seconds to run. " - "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) - - def test_correct_answer(self): - user_answer = "def add(a, b):\n\treturn a + b""" - get_class = PythonCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, ref_code_path=None, in_dir=None) - result = get_class.evaluate() - self.assertTrue(result.get("success")) - self.assertEqual(result.get("error"), "Correct answer") - - def test_incorrect_answer(self): - user_answer = "def add(a, b):\n\treturn a - b" - test_case_data = [{"func_name": "add", - "expected_answer": "5", - "test_id": u'null', - "pos_args": ["3", "2"], - "kw_args": {} - }] - get_class = PythonCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, ref_code_path=None, in_dir=None) - result = get_class.evaluate() - self.assertFalse(result.get("success")) - self.assertEqual(result.get("error"), "AssertionError in: assert add(3, 2) == 5") - - def test_infinite_loop(self): - user_answer = "def add(a, b):\n\twhile True:\n\t\tpass""" - test_case_data = [{"func_name": "add", - "expected_answer": "5", - "test_id": u'null', - "pos_args": ["3", "2"], - "kw_args": {} - }] - get_class = PythonCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, ref_code_path=None, in_dir=None) - result = get_class.evaluate() - self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/testapp/test_scilab_evaluation.py b/testapp/test_scilab_evaluation.py deleted file mode 100644 index 072fff6..0000000 --- a/testapp/test_scilab_evaluation.py +++ /dev/null @@ -1,39 +0,0 @@ -import unittest -import os -from exam.scilab_code_evaluator import ScilabCodeEvaluator -from exam.settings import SERVER_TIMEOUT - -class ScilabEvaluationTestCases(unittest.TestCase): - def setUp(self): - self.language = "scilab" - self.ref_code_path = "scilab_files/test_add.sce" - self.in_dir = "/tmp" - self.test_case_data = [] - 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.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_correct_answer_2(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\t c=a+b;\ndis(\tendfunction" - 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.assertFalse(result.get("success")) - self.assertTrue("Error" in result.get("error")) - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/testapp/tests/__init__.py b/testapp/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testapp/tests/test_bash_evaluation.py b/testapp/tests/test_bash_evaluation.py new file mode 100644 index 0000000..3ae3b0b --- /dev/null +++ b/testapp/tests/test_bash_evaluation.py @@ -0,0 +1,41 @@ +import unittest +import os +from exam.bash_code_evaluator import BashCodeEvaluator +from exam.settings import SERVER_TIMEOUT + +class BashEvaluationTestCases(unittest.TestCase): + 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.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() \ No newline at end of file diff --git a/testapp/tests/test_c_cpp_evaluation.py b/testapp/tests/test_c_cpp_evaluation.py new file mode 100644 index 0000000..b820963 --- /dev/null +++ b/testapp/tests/test_c_cpp_evaluation.py @@ -0,0 +1,77 @@ +import unittest +import os +from exam.cpp_code_evaluator import CppCodeEvaluator +from exam.settings import SERVER_TIMEOUT + +class CEvaluationTestCases(unittest.TestCase): + def setUp(self): + self.language = "C" + self.ref_code_path = "c_cpp_files/main.cpp" + 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 = "int add(int a, int b)\n{return a+b;}" + get_class = CppCodeEvaluator(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_compilation_error(self): + user_answer = "int add(int a, int b)\n{return a+b}" + get_class = CppCodeEvaluator(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("Compilation Error" in result.get("error")) + + def test_infinite_loop(self): + user_answer = "int add(int a, int b)\n{while(1>0){}}" + get_class = CppCodeEvaluator(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) + + +############################################################################### +class CppEvaluationTestCases(unittest.TestCase): + def setUp(self): + self.language = "CPP" + self.ref_code_path = "c_cpp_files/main.cpp" + 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 = "int add(int a, int b)\n{return a+b;}" + get_class = CppCodeEvaluator(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_compilation_error(self): + user_answer = "int add(int a, int b)\n{return a+b}" + get_class = CppCodeEvaluator(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("Compilation Error" in result.get("error")) + + def test_infinite_loop(self): + user_answer = "int add(int a, int b)\n{while(1>0){}}" + get_class = CppCodeEvaluator(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() \ No newline at end of file diff --git a/testapp/tests/test_code_evaluation.py b/testapp/tests/test_code_evaluation.py new file mode 100644 index 0000000..fa2b1fa --- /dev/null +++ b/testapp/tests/test_code_evaluation.py @@ -0,0 +1,24 @@ +import unittest +import os +from exam import python_code_evaluator +from exam.language_registry import _LanguageRegistry, set_registry, get_registry +from exam.settings import SERVER_TIMEOUT + + +class RegistryTestCase(unittest.TestCase): + def setUp(self): + set_registry() + self.registry_object = get_registry() + self.language_registry = _LanguageRegistry() + + def test_set_register(self): + class_name = getattr(python_code_evaluator, 'PythonCodeEvaluator') + self.registry_object.register("python", "exam.python_code_evaluator.PythonCodeEvaluator") + self.assertEquals(self.registry_object.get_class("python"), class_name) + + def tearDown(self): + self.registry_object = None + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/testapp/tests/test_java_evaluation.py b/testapp/tests/test_java_evaluation.py new file mode 100644 index 0000000..d86d7b3 --- /dev/null +++ b/testapp/tests/test_java_evaluation.py @@ -0,0 +1,41 @@ +import unittest +import os +from exam.java_code_evaluator import JavaCodeEvaluator +from exam.settings import SERVER_TIMEOUT + +class JavaEvaluationTestCases(unittest.TestCase): + def setUp(self): + self.language = "java" + self.ref_code_path = "java_files/main_square.java" + 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 = "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() \ No newline at end of file diff --git a/testapp/tests/test_python_evaluation.py b/testapp/tests/test_python_evaluation.py new file mode 100644 index 0000000..57e111c --- /dev/null +++ b/testapp/tests/test_python_evaluation.py @@ -0,0 +1,54 @@ + +import unittest +import os +from exam.python_code_evaluator import PythonCodeEvaluator +from exam.settings import SERVER_TIMEOUT + +class PythonEvaluationTestCases(unittest.TestCase): + def setUp(self): + self.language = "Python" + self.test = None + self.test_case_data = [{"func_name": "add", + "expected_answer": "5", + "test_id": u'null', + "pos_args": ["3", "2"], + "kw_args": {} + }] + self.timeout_msg = ("Code took more than {0} seconds to run. " + "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) + + def test_correct_answer(self): + user_answer = "def add(a, b):\n\treturn a + b""" + get_class = PythonCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, ref_code_path=None, in_dir=None) + result = get_class.evaluate() + self.assertTrue(result.get("success")) + self.assertEqual(result.get("error"), "Correct answer") + + def test_incorrect_answer(self): + user_answer = "def add(a, b):\n\treturn a - b" + test_case_data = [{"func_name": "add", + "expected_answer": "5", + "test_id": u'null', + "pos_args": ["3", "2"], + "kw_args": {} + }] + get_class = PythonCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, ref_code_path=None, in_dir=None) + result = get_class.evaluate() + self.assertFalse(result.get("success")) + self.assertEqual(result.get("error"), "AssertionError in: assert add(3, 2) == 5") + + def test_infinite_loop(self): + user_answer = "def add(a, b):\n\twhile True:\n\t\tpass""" + test_case_data = [{"func_name": "add", + "expected_answer": "5", + "test_id": u'null', + "pos_args": ["3", "2"], + "kw_args": {} + }] + get_class = PythonCodeEvaluator(self.test_case_data, self.test, self.language, user_answer, ref_code_path=None, in_dir=None) + result = get_class.evaluate() + self.assertFalse(result.get("success")) + self.assertEquals(result.get("error"), self.timeout_msg) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/testapp/tests/test_scilab_evaluation.py b/testapp/tests/test_scilab_evaluation.py new file mode 100644 index 0000000..072fff6 --- /dev/null +++ b/testapp/tests/test_scilab_evaluation.py @@ -0,0 +1,39 @@ +import unittest +import os +from exam.scilab_code_evaluator import ScilabCodeEvaluator +from exam.settings import SERVER_TIMEOUT + +class ScilabEvaluationTestCases(unittest.TestCase): + def setUp(self): + self.language = "scilab" + self.ref_code_path = "scilab_files/test_add.sce" + self.in_dir = "/tmp" + self.test_case_data = [] + 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.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_correct_answer_2(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\t c=a+b;\ndis(\tendfunction" + 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.assertFalse(result.get("success")) + self.assertTrue("Error" in result.get("error")) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file -- cgit