diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py index c9921ac3..bc8b68ac 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py @@ -362,15 +362,12 @@ class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore task: SpiffTask | None = None, external_context: dict[str, Any] | None = None, ) -> Any: + """Evaluate the given expression, within the context of the given task and return the result.""" + methods = self.__get_augment_methods(task) if external_context: methods.update(external_context) - if hasattr(self, "method_overrides"): - if self.method_overrides: - methods = {**methods, **self.method_overrides} - - """Evaluate the given expression, within the context of the given task and return the result.""" try: return super()._evaluate(expression, context, external_context=methods) except Exception as exception: @@ -392,10 +389,6 @@ class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore if external_context: methods.update(external_context) - if hasattr(self, "method_overrides"): - if self.method_overrides: - methods = {**methods, **self.method_overrides} - # do not run script if it is blank if script: super().execute(task, script, methods) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_test_runner_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_test_runner_service.py index aacdfa15..fd748894 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_test_runner_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_test_runner_service.py @@ -43,17 +43,39 @@ class BpmnFileMissingExecutableProcessError(Exception): class ProcessModelTestRunnerScriptEngine(PythonScriptEngine): # type: ignore - def execute(self, task: SpiffTask, script: str, external_context: Any = None) -> bool: + def __init__(self, method_overrides: dict | None = None) -> None: + self.method_overrides = method_overrides + super().__init__() + + def _get_all_methods_for_context(self, external_context: dict[str, Any] | None) -> dict: methods = { "get_process_initiator_user": lambda: { "username": "test_username_a", "tenant_specific_field_1": "test_tenant_specific_field_1_a", }, } + + if self.method_overrides: + methods = {**methods, **self.method_overrides} + if external_context: methods.update(external_context) + + return methods + + def evaluate(self, task: SpiffTask, expression: str, external_context: dict[str, Any] | None = None) -> Any: + """ + Evaluate the given expression, within the context of the given task and + return the result. + """ + updated_context = self._get_all_methods_for_context(external_context) + return super().evaluate(task, expression, updated_context) + + def execute(self, task: SpiffTask, script: str, external_context: Any = None) -> bool: if script: + methods = self._get_all_methods_for_context(external_context) super().execute(task, script, methods) + return True def call_service( @@ -146,17 +168,6 @@ class ProcessModelTestRunnerMostlyPureSpiffDelegate(ProcessModelTestRunnerDelega bpmn_process_spec, subprocess_specs=subprocesses, ) - bpmn_process_instance.script_engine = ProcessModelTestRunnerScriptEngine() - - # we do not want to call the real get_process_initiator_user script, since it depends on a process instance - # that does not actually exist - overridden_methods = { - "get_process_initiator_user": lambda: { - "username": "test_username_a", - "tenant_specific_field_1": "test_tenant_specific_field_1_a", - }, - } - bpmn_process_instance.script_engine.method_overrides = overridden_methods return bpmn_process_instance def execute_task(self, spiff_task: SpiffTask, task_data_for_submit: dict | None = None) -> None: @@ -339,6 +350,11 @@ class ProcessModelTestRunner: def run_test_case(self, bpmn_file: str, test_case_identifier: str, test_case_contents: dict) -> None: bpmn_process_instance = self._instantiate_executer(bpmn_file) + method_overrides = {} + if "mocks" in test_case_contents: + for method_name, mock_return_value in test_case_contents["mocks"].items(): + method_overrides[method_name] = lambda value=mock_return_value: value + bpmn_process_instance.script_engine = ProcessModelTestRunnerScriptEngine(method_overrides=method_overrides) next_task = self._get_next_task(bpmn_process_instance) while next_task is not None: test_case_task_properties = None diff --git a/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/process_model.json b/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/process_model.json new file mode 100644 index 00000000..0b972734 --- /dev/null +++ b/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/process_model.json @@ -0,0 +1,9 @@ +{ + "description": "", + "display_name": "Script Task", + "exception_notification_addresses": [], + "fault_or_suspend_on_exception": "fault", + "metadata_extraction_paths": null, + "primary_file_name": "script_task.bpmn", + "primary_process_id": "Process_Script_Task" +} diff --git a/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/script_task.bpmn b/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/script_task.bpmn new file mode 100644 index 00000000..89f1a10a --- /dev/null +++ b/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/script_task.bpmn @@ -0,0 +1,41 @@ + + + + + Flow_0qfycuk + + + + Flow_1auiekw + + + + Flow_0qfycuk + Flow_1auiekw + a = 1 +frontend_url_for_testing = get_frontend_url() + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/test_script_task.json b/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/test_script_task.json new file mode 100644 index 00000000..dbd0ce13 --- /dev/null +++ b/spiffworkflow-backend/tests/data/bpmn_unit_test_process_models/expected-to-pass/script-task-with-backend-script/test_script_task.json @@ -0,0 +1,24 @@ +{ + "test_case_1": { + "mocks": { + "get_frontend_url": "https://spiffworkflow.example.com", + "get_process_initiator_user": { + "username": "test_username_a", + "tenant_specific_field_1": "test_tenant_specific_field_1_a" + } + }, + "tasks": { + "Activity_1vepcwc": { + "data": [ + { + "what": true + } + ] + } + }, + "expected_output_json": { + "a": 1, + "frontend_url_for_testing": "https://spiffworkflow.example.com" + } + } +}