spiff-arena/tests/SpiffWorkflow/bpmn/PythonScriptEngineEnvironmentTest.py
burnettk c237e218b2 Squashed 'SpiffWorkflow/' changes from 98c6294f..0e61be85
0e61be85 Merge pull request #289 from sartography/improvement/execution-and-serialization-cleanup
527684da fix some typos in the class & method docs
0dff44a4 Merge branch 'main' into improvement/execution-and-serialization-cleanup
64737498 Allow for other PythonScriptEngine environments besides task data (#288)
dd63e916 remove some unused tests & diagrams
24aae519 clean up various small stuff
3b2dc35d use context when opening files for parsing
69eec3eb update class/method docs
24528dfb move all spec conversion classes to top level
5af33b11 remove some unused methods related to old serializer
931b90fb reorganize serializer
4e81ed29 consolidate pointless serializer classes
d62acf02 change task_spec._update_hook to return a boolean indicating whether the task is ready

git-subtree-dir: SpiffWorkflow
git-subtree-split: 0e61be85c47474a33037e6f398e64c96e02f13ad
2023-02-02 20:59:28 -05:00

81 lines
3.2 KiB
Python

import json
from tests.SpiffWorkflow.bpmn.BpmnWorkflowTestCase import BpmnWorkflowTestCase
from SpiffWorkflow.bpmn.PythonScriptEngine import PythonScriptEngine
from SpiffWorkflow.bpmn.PythonScriptEngineEnvironment import BasePythonScriptEngineEnvironment
from SpiffWorkflow.bpmn.workflow import BpmnWorkflow
from SpiffWorkflow.task import TaskState
def example_global():
pass
class NonTaskDataExampleEnvironment(BasePythonScriptEngineEnvironment):
def __init__(self, environment_globals, environment):
self.environment = environment
self.environment.update(environment_globals)
super().__init__(environment_globals)
def evaluate(self, expression, context, external_methods=None):
pass
def execute(self, script, context, external_methods=None):
self.environment.update(context)
self.environment.update(external_methods or {})
exec(script, self.environment)
self.environment = {k: v for k, v in self.environment.items() if k not in external_methods}
def user_defined_values(self):
return {k: v for k, v in self.environment.items() if k not in self.globals}
class PythonScriptEngineEnvironmentTest(BpmnWorkflowTestCase):
def setUp(self):
spec, subprocesses = self.load_workflow_spec('task_data_size.bpmn', 'Process_ccz6oq2')
self.workflow = BpmnWorkflow(spec, subprocesses)
def testTaskDataSizeWithDefaultPythonScriptEngine(self):
self.workflow.do_engine_steps()
self.assertIn("a", self.workflow.data)
self.assertIn("b", self.workflow.data)
self.assertIn("c", self.workflow.data)
self.assertIn("d", self.workflow.data)
task_data_len = self._get_task_data_len()
d_uniques = set(self.workflow.data["d"])
d_len = len(self.workflow.data["d"])
self.assertGreater(task_data_len, 15000)
self.assertEqual(d_len, 512*3)
self.assertEqual(d_uniques, {"a", "b", "c"})
def testTaskDataSizeWithNonTaskDataEnvironmentBasedPythonScriptEngine(self):
script_engine_environment = NonTaskDataExampleEnvironment({"example_global": example_global}, {})
script_engine = PythonScriptEngine(environment=script_engine_environment)
self.workflow.script_engine = script_engine
self.workflow.do_engine_steps()
self.workflow.data.update(script_engine.environment.user_defined_values())
self.assertIn("a", self.workflow.data)
self.assertIn("b", self.workflow.data)
self.assertIn("c", self.workflow.data)
self.assertIn("d", self.workflow.data)
self.assertNotIn("example_global", self.workflow.data)
task_data_len = self._get_task_data_len()
d_uniques = set(self.workflow.data["d"])
d_len = len(self.workflow.data["d"])
self.assertEqual(task_data_len, 2)
self.assertEqual(d_len, 512*3)
self.assertEqual(d_uniques, {"a", "b", "c"})
def _get_task_data_len(self):
tasks_to_check = self.workflow.get_tasks(TaskState.FINISHED_MASK)
task_data = [task.data for task in tasks_to_check]
task_data_to_check = list(filter(len, task_data))
task_data_len = len(json.dumps(task_data_to_check))
return task_data_len