spiff-arena/tests/SpiffWorkflow/bpmn/CustomScriptTest.py
burnettk c4f4e008d8 Squashed 'SpiffWorkflow/' changes from 96ad2a2b0..73886584b
73886584b Merge pull request #315 from sartography/feature/new-task-states
a04fdd311 Merge remote-tracking branch 'origin/main' into feature/new-task-states
3d376bf9a documentation and comment updates
7e368ee4b copy edit
e7b9fe91c typo
eceef15a7 Called elements (#316)
5a0fd2774 add serialization migration
7211e67ee Merge pull request #314 from sartography/bugfix/data-object-references
403acc1f5 use same data objects & references in subprocesses after deserialization
c54091dee add two more states to spiff
fa04a14a7 clean up script engine a little bit
162a1c5f5 override create parser in spiff package to allow duplicate process names
98a1b37e0 Merge pull request #312 from sartography/bugfix/run-boundary-events-from-engine-steps
067d6a723 do not execute boundary events in catch
1c877dd76 send external events to top level workflow

git-subtree-dir: SpiffWorkflow
git-subtree-split: 73886584b17c7d11a9713d0c4526ed41e411fc45
2023-04-21 11:22:52 -04:00

63 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
import unittest
from SpiffWorkflow.exceptions import WorkflowTaskException
from SpiffWorkflow.task import TaskState
from SpiffWorkflow.bpmn.PythonScriptEngine import PythonScriptEngine
from SpiffWorkflow.bpmn.PythonScriptEngineEnvironment import TaskDataEnvironment
from SpiffWorkflow.bpmn.workflow import BpmnWorkflow
from tests.SpiffWorkflow.bpmn.BpmnWorkflowTestCase import BpmnWorkflowTestCase
__author__ = 'McDonald, danfunk'
def my_custom_function(txt):
return str(txt).upper()
class CustomBpmnScriptEngine(PythonScriptEngine):
"""This is a custom script processor that can be easily injected into Spiff Workflow.
It will execute python code read in from the bpmn. It will also make any scripts in the
scripts directory available for execution. """
def __init__(self):
environment = TaskDataEnvironment({'custom_function': my_custom_function})
super().__init__(environment=environment)
class CustomInlineScriptTest(BpmnWorkflowTestCase):
def setUp(self):
spec, subprocesses = self.load_workflow_spec('custom_function_test*', 'top_workflow')
script_engine = CustomBpmnScriptEngine()
self.workflow = BpmnWorkflow(spec, subprocesses, script_engine=script_engine)
def testRunThroughHappy(self):
self.actual_test(save_restore=False)
def testRunThroughSaveRestore(self):
self.actual_test(save_restore=False)
def actual_test(self, save_restore):
if save_restore: self.save_restore()
self.workflow.do_engine_steps()
self.complete_subworkflow()
self.complete_subworkflow()
if save_restore: self.save_restore()
data = self.workflow.last_task.data
self.assertEqual(data['c1'], 'HELLO')
self.assertEqual(data['c2'], 'GOODBYE')
self.assertEqual(data['c3'], 'ARRIVEDERCI')
def test_overwrite_function_with_local_variable(self):
ready_task = self.workflow.get_tasks(TaskState.READY)[0]
ready_task.data = {'custom_function': "bill"}
with self.assertRaises(WorkflowTaskException) as e:
self.workflow.do_engine_steps()
self.assertTrue('custom_function' in str(e.exception))
task = self.workflow.get_tasks_from_spec_name('Activity_1y303ko')[0]
self.assertEqual(task.state, TaskState.ERROR)
def suite():
return unittest.TestLoader().loadTestsFromTestCase(CustomInlineScriptTest)
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())