use _execute and avoid passing an actual task around

This commit is contained in:
burnettk 2022-09-30 18:35:20 -04:00
parent 89faaf0c9a
commit cccd523ea3
5 changed files with 15 additions and 92 deletions

View File

@ -1083,31 +1083,12 @@ def script_unit_test_run(
# FIXME: We should probably clear this somewhere else but this works # FIXME: We should probably clear this somewhere else but this works
current_app.config["THREAD_LOCAL_DATA"].process_instance_id = None 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) python_script = get_required_parameter_or_raise("python_script", body)
input_json = get_required_parameter_or_raise("input_json", body) input_json = get_required_parameter_or_raise("input_json", body)
expected_output_json = get_required_parameter_or_raise("expected_output_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( 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) return make_response(jsonify(result), 200)

View File

@ -33,16 +33,18 @@ class ScriptUnitTestRunner:
@classmethod @classmethod
def run_with_task_and_script_and_pre_post_contexts( def run_with_task_and_script_and_pre_post_contexts(
cls, cls,
task: SpiffTask,
script: str, script: str,
input_context: PythonScriptContext, input_context: PythonScriptContext,
expected_output_context: PythonScriptContext, expected_output_context: PythonScriptContext,
) -> ScriptUnitTestResult: ) -> ScriptUnitTestResult:
"""Run_task.""" """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: try:
cls._script_engine.execute(task, script) cls._script_engine._execute(context=context, script=script)
except WorkflowTaskExecException as ex: except WorkflowTaskExecException as ex:
return ScriptUnitTestResult( return ScriptUnitTestResult(
result=False, result=False,
@ -56,9 +58,10 @@ class ScriptUnitTestRunner:
error=f"Failed to execute script: {str(ex)}", 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( script_unit_test_result = ScriptUnitTestResult(
result=result_as_boolean, context=task.data result=result_as_boolean, context=context
) )
return script_unit_test_result return script_unit_test_result
@ -95,5 +98,5 @@ class ScriptUnitTestRunner:
script = task.task_spec.script script = task.task_spec.script
return cls.run_with_task_and_script_and_pre_post_contexts( return cls.run_with_task_and_script_and_pre_post_contexts(
task, script, input_context, expected_output_context script, input_context, expected_output_context
) )

View File

@ -5,12 +5,12 @@
<bpmn:startEvent id="StartEvent_1"> <bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>SequenceFlow_0qyd2b7</bpmn:outgoing> <bpmn:outgoing>SequenceFlow_0qyd2b7</bpmn:outgoing>
</bpmn:startEvent> </bpmn:startEvent>
<bpmn:sequenceFlow id="SequenceFlow_0qyd2b7" sourceRef="StartEvent_1" targetRef="Activity_10l590e" /> <bpmn:sequenceFlow id="SequenceFlow_0qyd2b7" sourceRef="StartEvent_1" targetRef="Activity_subprocess" />
<bpmn:endEvent id="EndEvent_1l03lqw"> <bpmn:endEvent id="EndEvent_1l03lqw">
<bpmn:incoming>Flow_1d27j6f</bpmn:incoming> <bpmn:incoming>Flow_1d27j6f</bpmn:incoming>
</bpmn:endEvent> </bpmn:endEvent>
<bpmn:subProcess id="Activity_subprocess" name="Hot Subprocess"> <bpmn:subProcess id="Activity_subprocess" name="Hot Subprocess">
<bpmn:incoming>Flow_06uueri</bpmn:incoming> <bpmn:incoming>SequenceFlow_0qyd2b7</bpmn:incoming>
<bpmn:outgoing>Flow_1d27j6f</bpmn:outgoing> <bpmn:outgoing>Flow_1d27j6f</bpmn:outgoing>
<bpmn:startEvent id="Event_0jubmia"> <bpmn:startEvent id="Event_0jubmia">
<bpmn:outgoing>Flow_1s17tt0</bpmn:outgoing> <bpmn:outgoing>Flow_1s17tt0</bpmn:outgoing>
@ -32,11 +32,6 @@
</bpmn:userTask> </bpmn:userTask>
</bpmn:subProcess> </bpmn:subProcess>
<bpmn:sequenceFlow id="Flow_1d27j6f" sourceRef="Activity_subprocess" targetRef="EndEvent_1l03lqw" /> <bpmn:sequenceFlow id="Flow_1d27j6f" sourceRef="Activity_subprocess" targetRef="EndEvent_1l03lqw" />
<bpmn:manualTask id="Activity_10l590e">
<bpmn:incoming>SequenceFlow_0qyd2b7</bpmn:incoming>
<bpmn:outgoing>Flow_06uueri</bpmn:outgoing>
</bpmn:manualTask>
<bpmn:sequenceFlow id="Flow_06uueri" sourceRef="Activity_10l590e" targetRef="Activity_subprocess" />
</bpmn:process> </bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1"> <bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_HelloWorld"> <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_HelloWorld">
@ -46,25 +41,18 @@
<bpmndi:BPMNShape id="EndEvent_1l03lqw_di" bpmnElement="EndEvent_1l03lqw"> <bpmndi:BPMNShape id="EndEvent_1l03lqw_di" bpmnElement="EndEvent_1l03lqw">
<dc:Bounds x="752" y="99" width="36" height="36" /> <dc:Bounds x="752" y="99" width="36" height="36" />
</bpmndi:BPMNShape> </bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0ewyvhj_di" bpmnElement="Activity_10l590e">
<dc:Bounds x="260" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_subprocess_di" bpmnElement="Activity_subprocess" isExpanded="false"> <bpmndi:BPMNShape id="Activity_subprocess_di" bpmnElement="Activity_subprocess" isExpanded="false">
<dc:Bounds x="410" y="77" width="100" height="80" /> <dc:Bounds x="410" y="77" width="100" height="80" />
<bpmndi:BPMNLabel /> <bpmndi:BPMNLabel />
</bpmndi:BPMNShape> </bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0qyd2b7_di" bpmnElement="SequenceFlow_0qyd2b7"> <bpmndi:BPMNEdge id="SequenceFlow_0qyd2b7_di" bpmnElement="SequenceFlow_0qyd2b7">
<di:waypoint x="215" y="117" /> <di:waypoint x="215" y="117" />
<di:waypoint x="260" y="117" /> <di:waypoint x="410" y="117" />
</bpmndi:BPMNEdge> </bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1d27j6f_di" bpmnElement="Flow_1d27j6f"> <bpmndi:BPMNEdge id="Flow_1d27j6f_di" bpmnElement="Flow_1d27j6f">
<di:waypoint x="510" y="117" /> <di:waypoint x="510" y="117" />
<di:waypoint x="752" y="117" /> <di:waypoint x="752" y="117" />
</bpmndi:BPMNEdge> </bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_06uueri_di" bpmnElement="Flow_06uueri">
<di:waypoint x="360" y="117" />
<di:waypoint x="410" y="117" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane> </bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram> </bpmndi:BPMNDiagram>
<bpmndi:BPMNDiagram id="BPMNDiagram_04r6k7u"> <bpmndi:BPMNDiagram id="BPMNDiagram_04r6k7u">

View File

@ -26,52 +26,3 @@ class TestProcessInstanceProcessor(BaseTest):
result = script_engine._evaluate("a", {"a": 1}) result = script_engine._evaluate("a", {"a": 1})
assert result == 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}")

View File

@ -40,7 +40,7 @@ class TestScriptUnitTestRunner(BaseTest):
unit_test_result = ( unit_test_result = (
ScriptUnitTestRunner.run_with_task_and_script_and_pre_post_contexts( 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 = ( unit_test_result = (
ScriptUnitTestRunner.run_with_task_and_script_and_pre_post_contexts( ScriptUnitTestRunner.run_with_task_and_script_and_pre_post_contexts(
task, script, input_context, expected_output_context script, input_context, expected_output_context
) )
) )