def test_failed_activate_next(self): job_task_mock = mock.Mock(spec=Task()) gate_task_mock = mock.Mock(spec=Task()) next_flow_task_mock = mock.Mock() next_flow_task_mock.flow_cls.task_cls = mock.Mock( return_value=gate_task_mock) next_flow_task_mock.dst.activate = \ lambda prev_activation, token: activation.GateActivation().activate(next_flow_task_mock, prev_activation, token) # NOQA flow_task_mock = mock.MagicMock(spec=flow.Job(lambda p: None)) flow_task_mock.flow_cls.task_cls.objects.get = mock.Mock( return_value=job_task_mock) flow_task_mock.activation_cls = mock.Mock( return_value=activation.JobActivation()) flow_task_mock._outgoing = mock.Mock( return_value=[next_flow_task_mock]) with mock.patch('viewflow.flow.job.import_task_by_ref', return_value=flow_task_mock): successful_job(flow_task_strref='unit/TestFlow.job', process_pk=-1, task_pk=-1) job_task_mock.prepare.assert_called_once_with() job_task_mock.start.assert_called_once_with() job_task_mock.done.assert_called_once_with() self.assertFalse(job_task_mock.error.called) gate_task_mock.prepare.assert_called_once_with() gate_task_mock.error.assert_called_once_with() self.assertFalse(gate_task_mock.done.called)
class HelloWorldFlow(Flow): """ Hello world process This process demonstrates hello world approval request flow. 1. User with *helloworld.can_start_process* permission creates hello world request 2. Manager, who have *helloworld.can_approve_request* approves it 3. And if request was approved, background celery job sends it to the world 4. Elsewhere, request became cancelled """ process_cls = HelloWorldProcess lock_impl = select_for_update_lock start = flow.Start(StartView, fields=['text']) \ .Permission(auto_create=True) \ .Next(this.approve) approve = flow.View(ProcessView, fields=['approved']) \ .Permission(auto_create=True) \ .Next(this.send) check_approve = flow.If(cond=lambda p: p.approved) \ .OnTrue(this.send) \ .OnFalse(this.end) send = flow.Job(send_hello_world_request) \ .Next(this.end) end = flow.End()
class FailedGateFlow(Flow): """ Test that failed If gate not reflects on finished job """ lock_impl = lock.cache_lock start = flow.Start().Next(this.job) job = flow.Job(tasks.dummy_job).Next(this.iff) iff = flow.If(lambda p: 2 / 0).OnTrue(this.end).OnFalse(this.end) end = flow.End()
class FailedJobFlow(Flow): """ Test that failed job gate stored task in error state """ lock_impl = lock.cache_lock start = flow.Start().Next(this.job) job = flow.Job(tasks.dummy_job).Next(this.iff) iff = flow.If(lambda p: 2 / (1 - 1)).OnTrue(this.end).OnFalse(this.end) end = flow.End()
def test_job_activation_activate(self): flow_task_mock = mock.Mock(spec=flow.Job(lambda *args, **kwargs: None)) flow_task_mock._outgoing = mock.Mock(return_value=[]) prev_activation_mock = mock.Mock(spec=activation.StartActivation()) with mock.patch('viewflow.activation.get_task_ref'): act = activation.JobActivation.activate(flow_task_mock, prev_activation_mock, Token('start')) act.task.save.assert_has_calls(()) self.assertEqual(1, flow_task_mock.job.apply_async.call_count)
class AllTaskFlow(Flow): lock_impl = lock.cache_lock start = flow.Start().Next(this.view) view = flow.View(perform_task).Next(this.job) job = flow.Job(tasks.dummy_job).Next(this.iff) iff = flow.If(lambda act: True).OnTrue(this.switch).OnFalse(this.switch) switch = flow.Switch().Default(this.split) split = flow.Split().Always(this.join) join = flow.Join().Next(this.first) first = flow.First().Of(this.end) end = flow.End()
def test_job_activation_lifecycle(self): flow_task_mock = mock.Mock(spec=flow.Job(lambda *args, **kwargs: None)) flow_task_mock._outgoing = mock.Mock(return_value=[]) task_mock = mock.Mock(spec=Task()) act = activation.JobActivation() act.initialize(flow_task_mock, task_mock) act.prepare() act.start() act.done(result=None) act.task.done.assert_called_once_with() flow_task_mock._outgoing.assert_any_call()
def test_failed_job(self): task_mock = mock.Mock(spec=Task()) flow_task_mock = mock.MagicMock(spec=flow.Job(lambda p: None)) flow_task_mock.flow_cls.task_cls.objects.get = mock.Mock( return_value=task_mock) flow_task_mock.activation_cls = mock.Mock( return_value=activation.JobActivation()) with mock.patch('viewflow.flow.job.import_task_by_ref', return_value=flow_task_mock): with self.assertRaises(NotImplementedError): failure_job(flow_task_strref='unit/TestFlow.job', process_pk=-1, task_pk=-1) task_mock.prepare.assert_called_once_with() task_mock.start.assert_called_once_with() task_mock.error.assert_called_once_with() self.assertFalse(task_mock.done.called)