]> SALOME platform Git repositories - tools/ydefx.git/commitdiff
Salome HOME
Error management when a job is created with wrong parameters.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 9 Oct 2019 11:24:36 +0000 (13:24 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 9 Oct 2019 11:24:36 +0000 (13:24 +0200)
src/cpp/Launcher.hxx
src/cpp/Test/StudyGeneralTest.cxx
src/pydefx/pystudy.py
src/pyexample/test_default.py

index 9396d575d37e908456fafd354e68cf404b9f023e..60c140419a825743e3913885c39e2526735ef66a 100644 (file)
@@ -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";
index 2a3bf7572725dd82d8b7e0c2fa45e0190ae54a21..cac120e3ecac19f852040bfc73ca00a8bb4ac611 100644 (file)
@@ -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();
 }
index cf2c05655fdb52d031e8cac7fb44c4d915c2e9d7..63739268d1534577b99481ad11ff228dbaa65f9c 100644 (file)
@@ -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):
   """
index fa0b6e7392e743355e8231726e5d6464bf8473c9..8d07653819221e4495a4717883adafe11380e409 100644 (file)
@@ -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()