From 6f6a88d8368e4a3b9e22d53d2ec8b87af89cd8ab Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Wed, 9 Oct 2019 13:24:36 +0200 Subject: [PATCH] Error management when a job is created with wrong parameters. --- src/cpp/Launcher.hxx | 8 +++++ src/cpp/Test/StudyGeneralTest.cxx | 9 ++++++ src/pydefx/pystudy.py | 14 +++++++++ src/pyexample/test_default.py | 50 ++++++++++++++++++++++++++++++- 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/cpp/Launcher.hxx b/src/cpp/Launcher.hxx index 9396d57..60c1404 100644 --- a/src/cpp/Launcher.hxx +++ b/src/cpp/Launcher.hxx @@ -79,6 +79,14 @@ Job* Launcher::submitMonoPyJob(const PyStudyFunction& fnScript, return result; } + if(!result->lastError().empty()) + { + _lastError = result->lastError(); + delete result; + result = nullptr; + return result; + } + if(!result->launch()) { _lastError = "Failed to submit job.\n"; diff --git a/src/cpp/Test/StudyGeneralTest.cxx b/src/cpp/Test/StudyGeneralTest.cxx index 2a3bf75..cac120e 100644 --- a/src/cpp/Test/StudyGeneralTest.cxx +++ b/src/cpp/Test/StudyGeneralTest.cxx @@ -100,6 +100,15 @@ void SampleTest::fullStudy() for(const py2cpp::PyPtr& obj : pyobjResult) CPPUNIT_ASSERT(obj.repr() == "['object which needs pickel protocol']"); delete myJob; + + // test a case of error + std::string wrongScript = "wrong 'script"; + ydefx::PyStudyFunction wrongStudy; + wrongStudy.loadString(wrongScript); + CPPUNIT_ASSERT(!wrongStudy.isValid()); + myJob = l.submitMonoPyJob(wrongStudy, sample, jobParams); + CPPUNIT_ASSERT(myJob == nullptr); + CPPUNIT_ASSERT(l.lastError().find("SyntaxError") != std::string::npos); } Py_Finalize(); } diff --git a/src/pydefx/pystudy.py b/src/pydefx/pystudy.py index cf2c056..6373926 100644 --- a/src/pydefx/pystudy.py +++ b/src/pydefx/pystudy.py @@ -57,6 +57,7 @@ class PyStudy: The result directory will contain all the files needed for a launch and a job is created but not launched. """ + self._check(script,sample) self.sample = sample self.params = params self.params.salome_parameters.job_type = self.jobType() @@ -302,6 +303,19 @@ For further details, see {}/logs directory on {}.""".format( extra_files.extend(inputFiles) return schema_path, extra_files + def _check(self, script, sample): + "Raise StudyUseException if the sample does not match with the sample." + script_params = script.getInputNames() + sample_inputs = sample.getInputNames() + if len(script_params) < 1: + raise StudyUseException("The study function should have at least one parameter. None found.") + if len(script_params) != len(sample_inputs): + m="The study function should have the same number of parameters as the input variables in the sample ({} != {})." + raise StudyUseException(m.format(len(script_params), len(sample_inputs))) + for nm in script_params: + if nm not in sample_inputs: + raise StudyUseException("Parameter {} not found in the sample.".format(nm)) + ### Deprecated!!!! def dumpJob(result_directory, jobString): """ diff --git a/src/pyexample/test_default.py b/src/pyexample/test_default.py index fa0b6e7..8d07653 100644 --- a/src/pyexample/test_default.py +++ b/src/pyexample/test_default.py @@ -25,7 +25,55 @@ class TestYdefx(unittest.TestCase): def test_availableResources(self): import pydefx lr = pydefx.configuration.availableResources() - self.assertIn('localhost', lr) + self.assertIn('localhost', lr) + + def test_invalid_study(self): + import pydefx + myParams = pydefx.Parameters() + myParams.configureResource("localhost") + myScript = pydefx.PyScript() + myStudy = pydefx.PyStudy() + + myScript.loadString("wrong 'script") + mySample = pydefx.Sample([],[]) + try: + myStudy.createNewJob(myScript, mySample, myParams) + self.fail("Excpected pydefx.pyscript.PyScriptException!") + except pydefx.pyscript.PyScriptException: + pass + except pydefx.studyexception.StudyException: + pass + + script=""" +def _exec(): + x=5 + return x +""" + myScript.loadString(script) + try: + myStudy.createNewJob(myScript, mySample, myParams) + self.fail("Excpected pydefx.studyexception.StudyUseException!") + except pydefx.studyexception.StudyException: + pass + + script=""" +def _exec(a): + x=5 + return x +""" + myScript.loadString(script) + try: + myStudy.createNewJob(myScript, mySample, myParams) + self.fail("Excpected pydefx.studyexception.StudyUseException!") + except pydefx.studyexception.StudyException: + pass + + mySample = pydefx.Sample(["b"],[]) + try: + myStudy.createNewJob(myScript, mySample, myParams) + self.fail("Excpected pydefx.studyexception.StudyUseException!") + except pydefx.studyexception.StudyException: + pass if __name__ == '__main__': unittest.main() -- 2.30.2