def testWaitOnInputs(self): """ Tests that we can make the transform wait on its inputs and then continue. """ # Any task that writes 'foo' as an output taskA1 = Transform(self.wf_spec, 'A1', transforms=["my_task.set_attribute(foo=1)"]) self.wf_spec.start.connect(taskA1) # Any task that writes 'bar' as an output after a wait taskA2 = Transform(self.wf_spec, 'A2', transforms=[ "self.bar = getattr(self, 'bar', 0) + 1", "my_task.set_attribute(bar=self.bar)", "return self.bar == 4", ]) self.wf_spec.start.connect(taskA2) function_name = "tests.SpiffWorkflow.specs.SafeTransMergeTest." \ "SomeClass.my_code" taskB = SafeTransMerge(self.wf_spec, 'B', function_name=function_name) taskB.follow(taskA1) taskB.follow(taskA2) task3 = Simple(self.wf_spec, 'Last') taskB.connect(task3) expected = 'Start\n A1\n A2\n B\n Last\n' workflow = run_workflow(self, self.wf_spec, expected, '', max_tries=3) last = [t for t in workflow.get_tasks() if t.get_name() == 'Last'][0] self.assertEqual(last.attributes.get('my_foo'), 1) self.assertEqual(last.attributes.get('my_bar'), 4) self.assertEqual(last.attributes.get('foo'), 1) self.assertEqual(last.attributes.get('bar'), 4)
def create_worlflow(): spec = WorkflowSpec() a1 = Transform(spec, 'do what you want', [ 'from SpiffWorkflow.exceptions import TaskError\n' + 'if len(my_task.parent.children) < 2: raise TaskError("test")']) a1.follow(spec.start) return spec, a1
def create_worlflow(): spec = WorkflowSpec() a1 = Transform(spec, 'do what you want', [ 'from SpiffWorkflow.exceptions import TaskError\n' + 'if len(my_task.parent.children) < 2: raise TaskError("test")' ]) a1.follow(spec.start) return spec, a1
def testPattern(self): """ Tests that we can create a task that executes an shell command and that the workflow can be called to complete such tasks. """ task1 = Transform(self.wf_spec, 'First', transforms=["my_task.set_attribute(foo=1)"]) self.wf_spec.start.connect(task1) task2 = Transform( self.wf_spec, 'Second', transforms=[ "my_task.set_attribute(foo=my_task.attributes['foo']+1)", "my_task.set_attribute(copy=my_task.attributes['foo'])" ]) task1.connect(task2) task3 = Simple(self.wf_spec, 'Last') task2.connect(task3) expected = 'Start\n First\n Second\n Last\n' workflow = run_workflow(self, self.wf_spec, expected, '') first = workflow.get_task(3) last = workflow.get_task(5) self.assertEqual(first.attributes.get('foo'), 1) self.assertEqual(last.attributes.get('copy'), 2)
def testWaitOnInputs(self): """ Tests that we can make the transform wait on its inputs and then continue. """ # Any task that writes 'foo' as an output taskA1 = Transform(self.wf_spec, 'A1', transforms=["my_task.set_attribute(foo=1)"]) self.wf_spec.start.connect(taskA1) # Any task that writes 'bar' as an output after a wait taskA2 = Transform(self.wf_spec, 'A2', transforms=[ "self.bar = getattr(self, 'bar', 0) + 1", "my_task.set_attribute(bar=self.bar)", "return self.bar == 4", ]) self.wf_spec.start.connect(taskA2) # Create our special task that collects outputs and proceeds when it # has all the fields it needs, writing them into the output task my_code = """ fields = ['foo', 'bar'] # Get fields from task and store them in spec for key in fields: if key in my_task.attributes: merge_dictionary(self.properties, dict(collect={ 'my_%s' % key: my_task.attributes[key], key: my_task.attributes[key]})) # Check if we have all fields collected = self.get_property('collect', {}) if any(key for key in fields if key not in collected): return False my_task.set_attribute(**collected) return True""" taskB = TransMerge(self.wf_spec, 'B', transforms=[my_code]) taskB.follow(taskA1) taskB.follow(taskA2) task3 = Simple(self.wf_spec, 'Last') taskB.connect(task3) expected = 'Start\n A1\n A2\n B\n Last\n' workflow = run_workflow(self, self.wf_spec, expected, '', max_tries=3) last = [t for t in workflow.get_tasks() if t.get_name() == 'Last'][0] self.assertEqual(last.attributes.get('my_foo'), 1) self.assertEqual(last.attributes.get('my_bar'), 4) self.assertEqual(last.attributes.get('foo'), 1) self.assertEqual(last.attributes.get('bar'), 4)
def create_instance(self): if 'testtask' in self.wf_spec.task_specs: del self.wf_spec.task_specs['testtask'] return Transform(self.wf_spec, 'testtask', description='foo', transforms=[''])
def testPattern(self): """ Tests that we can create a task that executes an shell command and that the workflow can be called to complete such tasks. """ task1 = Transform(self.wf_spec, 'First', transforms=[ "my_task.set_attribute(foo=1)"]) self.wf_spec.start.connect(task1) task2 = Transform(self.wf_spec, 'Second', transforms=[ "my_task.set_attribute(foo=my_task.attributes['foo']+1)", "my_task.set_attribute(copy=my_task.attributes['foo'])" ]) task1.connect(task2) task3 = Simple(self.wf_spec, 'Last') task2.connect(task3) expected = 'Start\n First\n Second\n Last\n' workflow = run_workflow(self, self.wf_spec, expected, '') first = workflow.get_task(3) last = workflow.get_task(5) self.assertEqual(first.attributes.get('foo'), 1) self.assertEqual(last.attributes.get('copy'), 2)
def testPattern(self): """ Tests that we can create a task that executes an shell command and that the workflow can be called to complete such tasks. """ task1 = Transform(self.wf_spec, "First", transforms=["my_task.set_data(foo=1)"]) self.wf_spec.start.connect(task1) task2 = Transform( self.wf_spec, "Second", transforms=["my_task.set_data(foo=my_task.data['foo']+1)", "my_task.set_data(copy=my_task.data['foo'])"], ) task1.connect(task2) task3 = Simple(self.wf_spec, "Last") task2.connect(task3) expected = "Start\n First\n Second\n Last\n" workflow = run_workflow(self, self.wf_spec, expected, "") first = workflow.get_tasks_from_spec_name("First")[0] last = workflow.get_tasks_from_spec_name("Last")[0] self.assertEqual(first.data.get("foo"), 1) self.assertEqual(last.data.get("foo"), 2) self.assertEqual(last.data.get("copy"), 2)
def create_instance(self): return Transform(self.wf_spec, 'testtask', description='foo', transforms=[''])