use _execute and avoid passing an actual task around
This commit is contained in:
parent
89faaf0c9a
commit
cccd523ea3
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -5,12 +5,12 @@
|
|||
<bpmn:startEvent id="StartEvent_1">
|
||||
<bpmn:outgoing>SequenceFlow_0qyd2b7</bpmn:outgoing>
|
||||
</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:incoming>Flow_1d27j6f</bpmn:incoming>
|
||||
</bpmn:endEvent>
|
||||
<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:startEvent id="Event_0jubmia">
|
||||
<bpmn:outgoing>Flow_1s17tt0</bpmn:outgoing>
|
||||
|
@ -32,11 +32,6 @@
|
|||
</bpmn:userTask>
|
||||
</bpmn:subProcess>
|
||||
<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>
|
||||
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
|
||||
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_HelloWorld">
|
||||
|
@ -46,25 +41,18 @@
|
|||
<bpmndi:BPMNShape id="EndEvent_1l03lqw_di" bpmnElement="EndEvent_1l03lqw">
|
||||
<dc:Bounds x="752" y="99" width="36" height="36" />
|
||||
</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">
|
||||
<dc:Bounds x="410" y="77" width="100" height="80" />
|
||||
<bpmndi:BPMNLabel />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNEdge id="SequenceFlow_0qyd2b7_di" bpmnElement="SequenceFlow_0qyd2b7">
|
||||
<di:waypoint x="215" y="117" />
|
||||
<di:waypoint x="260" y="117" />
|
||||
<di:waypoint x="410" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge id="Flow_1d27j6f_di" bpmnElement="Flow_1d27j6f">
|
||||
<di:waypoint x="510" y="117" />
|
||||
<di:waypoint x="752" y="117" />
|
||||
</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:BPMNDiagram>
|
||||
<bpmndi:BPMNDiagram id="BPMNDiagram_04r6k7u">
|
||||
|
|
|
@ -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}")
|
||||
|
|
|
@ -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
|
||||
)
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in New Issue