def test_execute_xor_conditions_only(self): expression = ConditionalExpression( operator='xor', conditions=[self.get_regex_condition('bb'), self.get_regex_condition('aa')]) for true_pattern in ('aa', 'bb'): self.assertTrue(expression.execute(LocalActionExecutionStrategy(), true_pattern, {})) for false_pattern in ('aabb', 'cc'): self.assertFalse(expression.execute(LocalActionExecutionStrategy(), false_pattern, {}))
def test_execute_and_expressions_only(self): expression = ConditionalExpression( child_expressions=[ ConditionalExpression(conditions=[self.get_regex_condition('aa')]), ConditionalExpression(conditions=[self.get_regex_condition('bb')])]) self.assertTrue(expression.execute(LocalActionExecutionStrategy(), 'aabb', {})) for false_pattern in ('aa', 'bb', 'cc'): self.assertFalse(expression.execute(LocalActionExecutionStrategy(), false_pattern, {}))
def test_execute_or_with_conditions_and_expressions(self): expression = ConditionalExpression( operator='or', conditions=[self.get_regex_condition('aa')], child_expressions=[ ConditionalExpression(conditions=[self.get_regex_condition('bb')]), ConditionalExpression(conditions=[self.get_regex_condition('cc')])]) for true_pattern in ('aa', 'bb', 'cc', 'aabb', 'bbcc', 'aacc'): self.assertTrue(expression.execute(LocalActionExecutionStrategy(), true_pattern, {})) self.assertFalse(expression.execute(LocalActionExecutionStrategy(), 'd', {}))
def test_execute_sends_callbacks(self): action = Action(app_name='HelloWorld', action_name='Add Three', name='helloWorld', arguments=[ Argument('num1', value='-5.6'), Argument('num2', value='4.3'), Argument('num3', value='10.2') ]) instance = TestAction._make_app_instance() result = {'started_triggered': False, 'result_triggered': False} def callback_is_sent(sender, **kwargs): if isinstance(sender, Action): self.assertIn('event', kwargs) self.assertIn(kwargs['event'], (WalkoffEvent.ActionStarted, WalkoffEvent.ActionExecutionSuccess)) if kwargs['event'] == WalkoffEvent.ActionStarted: result['started_triggered'] = True else: self.assertIn('data', kwargs) data = kwargs['data'] self.assertEqual(data['status'], 'Success') self.assertAlmostEqual(data['result'], 8.9) result['result_triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(LocalActionExecutionStrategy(), {}, instance.instance) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_action_with_valid_arguments_invalid_data(self): self.assertFalse( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=3) ]).execute(LocalActionExecutionStrategy(), 'invalid', {}))
def test_branch_with_priority(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=10) action2 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=5) action3 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=1) condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch_one = Branch(source_id=action.id, destination_id=5, condition=condition, priority=5) branch_two = Branch(source_id=action.id, destination_id=1, condition=condition, priority=1) action_result = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action, action2, action3], branches=[branch_one, branch_two]) wf_ctx = WorkflowExecutionContext(workflow, {}, None, None) wf_ctx.accumulator[action.id] = action_result.result wf_ctx.last_status = action_result.status wf_ctx.executing_action = action self.assertEqual( Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()), 1)
def test_execute_with_complex_args(self): action = Action(app_name='HelloWorld', action_name='Json Sample', name='helloWorld', arguments=[ Argument('json_in', value={ 'a': '-5.6', 'b': { 'a': '4.3', 'b': 5.3 }, 'c': ['1', '2', '3'], 'd': [{ 'a': '', 'b': 3 }, { 'a': '', 'b': -1.5 }, { 'a': '', 'b': -0.5 }] }) ]) instance = TestAction._make_app_instance() acc = {} result = action.execute(LocalActionExecutionStrategy(), acc, instance.instance) self.assertAlmostEqual(acc[action.id], 11.0) self.assertEqual(result, 'Success')
def test_execute_with_complex_args(self): original_filter = Transform( 'HelloWorld', action_name='sub1_filter1', arguments=[Argument('arg1', value={'a': '5.4', 'b': 'string_in'})] ) self.assertEqual(original_filter.execute(LocalActionExecutionStrategy(), 3, {}), '3.0 5.4 string_in')
def test_execute_default_return_success(self): action = Action(app_name='HelloWorld', action_name='dummy action', name='helloWorld', arguments=[ Argument('status', value=True), Argument('other', value=True) ]) instance = TestAction._make_app_instance() result = {'started_triggered': False, 'result_triggered': False} def callback_is_sent(sender, **kwargs): if isinstance(sender, Action): self.assertIn('event', kwargs) self.assertIn(kwargs['event'], (WalkoffEvent.ActionStarted, WalkoffEvent.ActionExecutionSuccess)) if kwargs['event'] == WalkoffEvent.ActionStarted: result['started_triggered'] = True else: self.assertIn('data', kwargs) data = kwargs['data'] self.assertEqual(data['status'], 'Success') self.assertEqual(data['result'], None) result['result_triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(LocalActionExecutionStrategy(), {}, instance.instance) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_action_which_raises_exception_sends_callbacks(self): action = Action(app_name='HelloWorld', action_name='Buggy', name='helloWorld') instance = TestAction._make_app_instance() result = {'started_triggered': False, 'result_triggered': False} def callback_is_sent(sender, **kwargs): if isinstance(sender, Action): self.assertIn('event', kwargs) self.assertIn(kwargs['event'], (WalkoffEvent.ActionStarted, WalkoffEvent.ActionExecutionError)) if kwargs['event'] == WalkoffEvent.ActionStarted: result['started_triggered'] = True elif kwargs['event'] == WalkoffEvent.ActionExecutionError: result['result_triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(LocalActionExecutionStrategy(), {}, instance.instance) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_with_accumulator_missing_action_callbacks(self): action = Action(app_name='HelloWorld', action_name='Add Three', name='helloWorld', arguments=[ Argument('num1', reference='1'), Argument('num2', reference='action2'), Argument('num3', value='10.2') ]) accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'} instance = TestAction._make_app_instance() result = {'started_triggered': False, 'result_triggered': False} def callback_is_sent(sender, **kwargs): if isinstance(sender, Action): self.assertIn('event', kwargs) self.assertIn(kwargs['event'], (WalkoffEvent.ActionStarted, WalkoffEvent.ActionArgumentsInvalid)) if kwargs['event'] == WalkoffEvent.ActionStarted: result['started_triggered'] = True else: result['result_triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(LocalActionExecutionStrategy(), accumulator, instance.instance) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_action_which_raises_exception(self): action = Action(app_name='HelloWorld', action_name='Buggy', name='helloWorld') instance = TestAction._make_app_instance() acc = {} action.execute(LocalActionExecutionStrategy(), acc, instance.instance) self.assertIn(action.id, acc)
def test_execute_generates_id(self): action = Action(app_name='HelloWorld', action_name='helloWorld', name='helloWorld') original_execution_id = action.get_execution_id() instance = TestAction._make_app_instance() action.execute(LocalActionExecutionStrategy(), {}, instance.instance) self.assertNotEqual(action.get_execution_id(), original_execution_id)
def test_execute(self): class MockAction(object): id = 13 condition1 = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='(.*)')]) ]) condition2 = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='(.*)')]), Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='a')]) ]) inputs = [('name1', None, ActionResult('aaaa', 'Success'), True), ('name2', condition1, ActionResult('anyString', 'Success'), True), ('name3', condition2, ActionResult('anyString', 'Success'), True), ('name4', condition2, ActionResult('bbbb', 'Success'), False), ('name4', condition2, ActionResult('aaaa', 'Custom'), False)] for name, condition, previous_result, expect_name in inputs: branch = Branch(source_id=1, destination_id=2, condition=condition) acc = {MockAction.id: previous_result.result} status = previous_result.status action = MockAction() if expect_name: expected_name = branch.destination_id self.assertEqual( branch.execute(LocalActionExecutionStrategy(), status, action, acc), expected_name) else: self.assertIsNone( branch.execute(LocalActionExecutionStrategy(), status, action, acc))
def test_execute_with_args_with_routing(self): self.assertAlmostEqual( Transform( 'HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', reference="action1")] ).execute(LocalActionExecutionStrategy(), 5.4, {'action1': 10.3}), 15.7 )
def test_execute_action_with_valid_complex_arguments_valid_data(self): self.assertTrue( Condition('HelloWorld', action_name='mod1_flag3', arguments=[Argument('arg1', value={ 'a': '1', 'b': '5' })]).execute(LocalActionExecutionStrategy(), 'some_long_string', {}))
def test_execute_no_args(self): action = Action('HelloWorld', action_name='helloWorld', name='helloWorld') instance = TestAction._make_app_instance() acc = {} result = action.execute(LocalActionExecutionStrategy(), acc, instance.instance) expected = ActionResult({'message': 'HELLO WORLD'}, 'Success') self.assertEqual(result, expected.status) self.assertEqual(acc[action.id], expected.result)
def test_execute_global_action(self): action = Action(app_name='HelloWorld', action_name='global2', name='helloWorld', arguments=[Argument('arg1', value='something')]) instance = TestAction._make_app_instance() acc = {} result = action.execute(LocalActionExecutionStrategy(), acc, instance.instance) self.assertAlmostEqual(acc[action.id], 'something') self.assertEqual(result, 'Success')
def test_execute_with_accumulator_missing_action(self): action = Action(app_name='HelloWorld', action_name='Add Three', name='helloWorld', arguments=[ Argument('num1', reference='1'), Argument('num2', reference='action2'), Argument('num3', value='10.2') ]) accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'} instance = TestAction._make_app_instance() action.execute(LocalActionExecutionStrategy(), accumulator, instance.instance)
def test_execute_error_sends_event(self): expression = ConditionalExpression(conditions=[Condition('HelloWorld', 'mod1_flag1')]) result = {'triggered': False} @WalkoffEvent.CommonWorkflowSignal.connect def callback_is_sent(sender, **kwargs): if isinstance(sender, ConditionalExpression): self.assertIn('event', kwargs) self.assertEqual(kwargs['event'], WalkoffEvent.ConditionalExpressionFalse) result['triggered'] = True self.assertFalse(expression.execute(LocalActionExecutionStrategy(), 'any', {})) self.assertTrue(result['triggered'])
def test_execute_multiple_triggers(self): trigger = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) action = Action(app_name='HelloWorld', action_name='helloWorld', name='helloWorld', trigger=trigger) TestAction._make_app_instance() self.assertFalse( action.execute_trigger(LocalActionExecutionStrategy(), {"data_in": { "data": 'a' }}, {})) self.assertTrue( action.execute_trigger(LocalActionExecutionStrategy(), {"data_in": { "data": 'aaa' }}, {}))
def test_execute_false_sends_event(self): expression = ConditionalExpression(conditions=[self.get_always_true_condition()], is_negated=True) result = {'triggered': False} @WalkoffEvent.CommonWorkflowSignal.connect def callback_is_sent(sender, **kwargs): if isinstance(sender, ConditionalExpression): self.assertIn('event', kwargs) self.assertEqual(kwargs['event'], WalkoffEvent.ConditionalExpressionFalse) result['triggered'] = True expression.execute(LocalActionExecutionStrategy(), '3.4', {}) self.assertTrue(result['triggered'])
def setUpClass(cls): initialize_test_config() execution_db_help.setup_dbs() app = create_app() cls.context = app.test_request_context() cls.context.push() multiprocessedexecutor.MultiprocessedExecutor.initialize_threading = mock_initialize_threading multiprocessedexecutor.MultiprocessedExecutor.wait_and_reset = mock_wait_and_reset multiprocessedexecutor.MultiprocessedExecutor.shutdown_pool = mock_shutdown_pool cls.executor = multiprocessedexecutor.MultiprocessedExecutor( MockRedisCacheAdapter(), LocalActionExecutionStrategy()) cls.executor.initialize_threading(app)
def test_execute_action_with_valid_arguments_and_transforms_valid_data( self): transforms = [ Transform('HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', value='5')]), Transform('HelloWorld', action_name='Top Transform') ] # should go <input = 1> -> <mod1_filter2 = 5+1 = 6> -> <Top Transform 6=6> -> <mod1_flag2 4+6%2==0> -> True self.assertTrue( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=4)], transforms=transforms).execute( LocalActionExecutionStrategy(), '1', {}))
def test_execute_with_args(self): action = Action(app_name='HelloWorld', action_name='Add Three', name='helloWorld', arguments=[ Argument('num1', value='-5.6'), Argument('num2', value='4.3'), Argument('num3', value='10.2') ]) instance = TestAction._make_app_instance() acc = {} result = action.execute(LocalActionExecutionStrategy(), acc, instance.instance) self.assertAlmostEqual(acc[action.id], 8.9) self.assertEqual(result, 'Success')
def test_execute_with_accumulator_with_extra_actions(self): action = Action(app_name='HelloWorld', action_name='Add Three', name='helloWorld', arguments=[ Argument('num1', reference='1'), Argument('num2', reference='action2'), Argument('num3', value='10.2') ]) accumulator = {'1': '-5.6', 'action2': '4.3', '3': '45'} instance = TestAction._make_app_instance() result = action.execute(LocalActionExecutionStrategy(), accumulator, instance.instance) self.assertAlmostEqual(accumulator[action.id], 8.9) self.assertEqual(result, 'Success')
def test_execute_action_with_valid_arguments_and_transforms_invalid_data( self): transforms = [ Transform('HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', value='5')]), Transform('HelloWorld', action_name='Top Transform') ] # should go <input = invalid> -> <mod1_filter2 with error = invalid> -> <Top Transform with error = invalid> # -> <mod1_flag2 4+invalid throws error> -> False self.assertFalse( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=4)], transforms=transforms).execute( LocalActionExecutionStrategy(), 'invalid', {}))
def test_branch_counter(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=1) branch = Branch(source_id=action.id, destination_id=action.id) self.assertEqual(branch._counter, 0) action_result = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action], branches=[branch]) wf_ctx = WorkflowExecutionContext(workflow, None, None) wf_ctx.accumulator[action.id] = action_result.result wf_ctx.last_status = action_result.status wf_ctx.executing_action = action Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()) self.assertEqual(branch._counter, 1) self.assertIn(branch.id, wf_ctx.accumulator) self.assertEqual(wf_ctx.accumulator[branch.id], '1')
def test_execute_with_triggers(self): trigger = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) action = Action(app_name='HelloWorld', action_name='helloWorld', name='helloWorld', trigger=trigger) ret = action.execute_trigger(LocalActionExecutionStrategy(), {"data_in": { "data": 'aaa' }}, {}) self.assertTrue(ret)
def test_get_branch(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=10) action2 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=2) condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch = Branch(source_id=action.id, destination_id=2, condition=condition) action_result = ActionResult(result='aaa', status='Success') workflow = Workflow("helloWorld", action.id, actions=[action, action2], branches=[branch]) wf_ctx = WorkflowExecutionContext(workflow, {}, None, None) wf_ctx.accumulator[action.id] = action_result.result wf_ctx.last_status = action_result.status wf_ctx.executing_action = action result = {'triggered': False} def validate_sent_data(sender, **kwargs): if isinstance(sender, Branch): self.assertIn('event', kwargs) self.assertEqual(kwargs['event'], WalkoffEvent.BranchTaken) result['triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(validate_sent_data) self.assertEqual( Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()), 2) self.assertTrue(result['triggered'])