From cccd523ea39499c10f7f3c2e3f061852970973ac Mon Sep 17 00:00:00 2001 From: burnettk Date: Fri, 30 Sep 2022 18:35:20 -0400 Subject: [PATCH] use _execute and avoid passing an actual task around --- .../routes/process_api_blueprint.py | 21 +------- .../services/script_unit_test_runner.py | 15 +++--- tests/data/hello_world/hello_world.bpmn | 18 ++----- .../unit/test_process_instance_processor.py | 49 ------------------- .../unit/test_script_unit_test_runner.py | 4 +- 5 files changed, 15 insertions(+), 92 deletions(-) diff --git a/src/spiffworkflow_backend/routes/process_api_blueprint.py b/src/spiffworkflow_backend/routes/process_api_blueprint.py index c6e974c1..cbb74d38 100644 --- a/src/spiffworkflow_backend/routes/process_api_blueprint.py +++ b/src/spiffworkflow_backend/routes/process_api_blueprint.py @@ -1083,31 +1083,12 @@ def script_unit_test_run( # FIXME: We should probably clear this somewhere else but this works current_app.config["THREAD_LOCAL_DATA"].process_instance_id = None - bpmn_task_identifier = get_required_parameter_or_raise("bpmn_task_identifier", body) python_script = get_required_parameter_or_raise("python_script", body) input_json = get_required_parameter_or_raise("input_json", body) expected_output_json = get_required_parameter_or_raise("expected_output_json", body) - bpmn_process_instance = ( - ProcessInstanceProcessor.get_bpmn_process_instance_from_process_model( - process_model_id, process_group_id - ) - ) - spiff_task = ProcessInstanceProcessor.get_task_by_bpmn_identifier( - bpmn_task_identifier, bpmn_process_instance - ) - - if spiff_task is None: - raise ( - ApiError( - code="task_not_found", - message=f"Could not find task with identifier: {bpmn_task_identifier}", - status_code=400, - ) - ) - result = ScriptUnitTestRunner.run_with_task_and_script_and_pre_post_contexts( - spiff_task, python_script, input_json, expected_output_json + python_script, input_json, expected_output_json ) return make_response(jsonify(result), 200) diff --git a/src/spiffworkflow_backend/services/script_unit_test_runner.py b/src/spiffworkflow_backend/services/script_unit_test_runner.py index f1a11378..5136f3fe 100644 --- a/src/spiffworkflow_backend/services/script_unit_test_runner.py +++ b/src/spiffworkflow_backend/services/script_unit_test_runner.py @@ -33,16 +33,18 @@ class ScriptUnitTestRunner: @classmethod def run_with_task_and_script_and_pre_post_contexts( cls, - task: SpiffTask, script: str, input_context: PythonScriptContext, expected_output_context: PythonScriptContext, ) -> ScriptUnitTestResult: """Run_task.""" - task.data = input_context + + # make a new variable just for clarity, since we are going to update this dict in place + # with the output variables from the script. + context = input_context.copy() try: - cls._script_engine.execute(task, script) + cls._script_engine._execute(context=context, script=script) except WorkflowTaskExecException as ex: return ScriptUnitTestResult( result=False, @@ -56,9 +58,10 @@ class ScriptUnitTestRunner: error=f"Failed to execute script: {str(ex)}", ) - result_as_boolean = task.data == expected_output_context + result_as_boolean = context == expected_output_context + script_unit_test_result = ScriptUnitTestResult( - result=result_as_boolean, context=task.data + result=result_as_boolean, context=context ) return script_unit_test_result @@ -95,5 +98,5 @@ class ScriptUnitTestRunner: script = task.task_spec.script return cls.run_with_task_and_script_and_pre_post_contexts( - task, script, input_context, expected_output_context + script, input_context, expected_output_context ) diff --git a/tests/data/hello_world/hello_world.bpmn b/tests/data/hello_world/hello_world.bpmn index d50e674f..1e5bc853 100644 --- a/tests/data/hello_world/hello_world.bpmn +++ b/tests/data/hello_world/hello_world.bpmn @@ -5,12 +5,12 @@ SequenceFlow_0qyd2b7 - + Flow_1d27j6f - Flow_06uueri + SequenceFlow_0qyd2b7 Flow_1d27j6f Flow_1s17tt0 @@ -32,11 +32,6 @@ - - SequenceFlow_0qyd2b7 - Flow_06uueri - - @@ -46,25 +41,18 @@ - - - - + - - - - diff --git a/tests/spiffworkflow_backend/unit/test_process_instance_processor.py b/tests/spiffworkflow_backend/unit/test_process_instance_processor.py index 591c03db..7875c1c6 100644 --- a/tests/spiffworkflow_backend/unit/test_process_instance_processor.py +++ b/tests/spiffworkflow_backend/unit/test_process_instance_processor.py @@ -26,52 +26,3 @@ class TestProcessInstanceProcessor(BaseTest): result = script_engine._evaluate("a", {"a": 1}) assert result == 1 - - # FIXME: print statements for debugging - def test_get_bpmn_process_instance_from_process_model_can_access_tasks_from_subprocesses( - self, - app: Flask, - with_db_and_bpmn_file_cleanup: None, - ) -> None: - """Test_get_bpmn_process_instance_from_process_model_can_access_tasks_from_subprocesses.""" - app.config["THREAD_LOCAL_DATA"].process_instance_id = None - process_model = load_test_spec( - "hello_world", - process_model_source_directory="hello_world", - ) - - # BpmnWorkflow instance - bpmn_process_instance = ( - ProcessInstanceProcessor.get_bpmn_process_instance_from_process_model( - process_group_identifier="test_process_group_id", - process_model_identifier="hello_world", - ) - ) - - tasks = bpmn_process_instance.get_tasks(TaskState.ANY_MASK) - subprocess_specs = bpmn_process_instance.subprocess_specs - task_ids = [t.task_spec.name for t in tasks] - print("\nWITHOUT INSTANCE") - print(f"task_ids: {task_ids}\n") - print(f"task_ids length: {len(task_ids)}\n") - print(f"subprocess_specs: {subprocess_specs}") - - user = self.find_or_create_user() - - process_instance = ProcessInstanceService.create_process_instance( - process_model.id, - user, - process_group_identifier=process_model.process_group_id, - ) - - processor = ProcessInstanceProcessor(process_instance) - processor.do_engine_steps() - processor.save() - - print("\nWITH INSTANCE") - bpmn_process_instance = processor.bpmn_process_instance - tasks = bpmn_process_instance.get_tasks(TaskState.ANY_MASK) - subprocess_specs = bpmn_process_instance.subprocess_specs - task_ids = [t.task_spec.name for t in tasks] - print(f"task_ids: {task_ids}\n") - print(f"subprocess_specs: {subprocess_specs}") diff --git a/tests/spiffworkflow_backend/unit/test_script_unit_test_runner.py b/tests/spiffworkflow_backend/unit/test_script_unit_test_runner.py index 336e7188..e60be2a2 100644 --- a/tests/spiffworkflow_backend/unit/test_script_unit_test_runner.py +++ b/tests/spiffworkflow_backend/unit/test_script_unit_test_runner.py @@ -40,7 +40,7 @@ class TestScriptUnitTestRunner(BaseTest): unit_test_result = ( ScriptUnitTestRunner.run_with_task_and_script_and_pre_post_contexts( - task, script, input_context, expected_output_context + script, input_context, expected_output_context ) ) @@ -74,7 +74,7 @@ class TestScriptUnitTestRunner(BaseTest): unit_test_result = ( ScriptUnitTestRunner.run_with_task_and_script_and_pre_post_contexts( - task, script, input_context, expected_output_context + script, input_context, expected_output_context ) )