diff --git a/pysyncrosim/scenario.py b/pysyncrosim/scenario.py index b57c5ce..1d5f65a 100644 --- a/pysyncrosim/scenario.py +++ b/pysyncrosim/scenario.py @@ -602,41 +602,56 @@ def run(self, copy_external_inputs=False): if copy_external_inputs is True: args += ["--copyextfiles=yes"] - try: + error_msg = None + + try: print(f"Running Scenario [{self.sid}] {self.name}") - result = self.library.session._Session__call_console(args) - - if result.returncode == 0: - print("Run successful") + self.library.session._Session__call_console(args) except RuntimeError as e: - # TODO: add handling when the error message contains "You must be signed in" or "There has been an issue with your SyncroSim license file" - - print(e) + if "You must be signed in" in str(e): + error_msg = ("you must be signed in to SyncroSim. " + "Use session.sign_in() to sign in.") + + elif "There has been an issue with your SyncroSim license file" in str(e): + error_msg = "there has been an issue with your SyncroSim license file." + + else: + error_msg = str(e) finally: - + # Reset Project Scenarios self.project._Project__scenarios = None # Reset results self.__results = None - + # Retrieve Results Scenario ID # Also resets scenarios and results info results_df = self.results() if (not results_df.empty): - + result_id = results_df["ScenarioId"].values[-1] - + + if error_msg is not None: + raise RuntimeError(f"Run failed for Scenario [{result_id}] " + f"{self.name}: {error_msg}") + else: + print("Run successful") + # Return Results Scenario result_scn = self.library.scenarios(project=self.project, name=None, sid=result_id) - + return result_scn + + elif error_msg is not None: + raise RuntimeError(f"Run failed for Scenario [{self.sid}] " + f"{self.name}: {error_msg}") def run_log(self): """ diff --git a/tests/test_pysyncrosim.py b/tests/test_pysyncrosim.py index 38eda80..d9d613c 100644 --- a/tests/test_pysyncrosim.py +++ b/tests/test_pysyncrosim.py @@ -12,8 +12,7 @@ session_path = None test_lib_path = os.path.join(temp_path.name, "stsimLibrary.ssim") lib_name = "spatial-example.ssim" -git_repo_path = "C:/Users/VickiZhang/Documents/GH_ApexRMS" -# git_repo_path = "C:/gitprojects" +git_repo_path = "C:/GH_ApexRMS" lib_path = os.path.join(git_repo_path, "pysyncrosim/tests", lib_name) lib_backup_path = os.path.join(git_repo_path, "pysyncrosim/tests", "spatial-example.ssimbak") @@ -932,6 +931,22 @@ def test_scenario_run_and_results(): assert not isinstance(myScenario.run_log(), pd.DataFrame) myLibrary.delete(force=True) + +def test_scenario_run_failure(): + + mySession = ps.Session(session_path) + mySession.restore(lib_backup_path) + + myLibrary = ps.library(name=lib_path, + session=mySession, + force_update=True) + + myScenario = myLibrary.scenarios(name="My Scenario") + myFailScenario = myScenario.copy(name="My Scenario - Failure Test") + myFailScenario.delete(force=True) + + with pytest.raises(RuntimeError, match="Run failed for Scenario"): + myFailScenario.run() def test_scenario_copy_dep_delete(): @@ -1026,8 +1041,8 @@ def test_scenario_copy_dep_delete(): # Test delete myNewScn.delete(force=True) - emptyResScn = myNewScn.run() - assert emptyResScn is None + with pytest.raises(RuntimeError, match = "Run failed for Scenario"): + myNewScn.run() # Delete other scenarios sameNameScn.delete(force=True)