Beispiel #1
0
    def test_execute(self):
        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, input_str, expect_name in inputs:
            branch = Branch(source_id=1, destination_id=2, condition=condition)
            if expect_name:
                expected_name = branch.destination_id
                self.assertEqual(branch.execute(input_str, {}), expected_name)
            else:
                self.assertIsNone(branch.execute(input_str, {}))
Beispiel #2
0
 def test_init_with_conditions(self):
     condition = ConditionalExpression(
         'and',
         conditions=[
             Condition('HelloWorld', 'Top Condition'),
             Condition('HelloWorld', 'mod1_flag1')
         ])
     branch = Branch(1, 2, condition=condition)
     self.__compare_init(branch, 1, 2, condition=condition)
Beispiel #3
0
    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._output = ActionResult(result='aaa', status='Success')
        workflow = Workflow('test',
                            1,
                            actions=[action, action2, action3],
                            branches=[branch_one, branch_two])

        self.assertEqual(workflow.get_branch(action, {}), 1)
 def test_init_with_arguments_with_routing(self):
     condition = Condition(
         'HelloWorld',
         action_name='mod1_flag2',
         arguments=[Argument('arg1', reference='action2')])
     self.__compare_init(condition, 'HelloWorld', 'mod1_flag2', [],
                         [Argument('arg1', reference="action2")])
Beispiel #5
0
    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_action_with_valid_arguments_invalid_data(self):
     self.assertFalse(
         Condition('HelloWorld',
                   action_name='mod1_flag2',
                   arguments=[Argument('arg1', value=3)
                              ]).execute(LocalActionExecutionStrategy(),
                                         'invalid', {}))
Beispiel #7
0
    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._output = ActionResult(result='aaa', status='Success')
        workflow = Workflow("helloWorld",
                            action.id,
                            actions=[action, action2],
                            branches=[branch])

        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(workflow.get_branch(action, {}), 2)
        self.assertTrue(result['triggered'])
Beispiel #8
0
 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
     with self.assertRaises(InvalidArgument):
         Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=4)],
                   transforms=transforms).execute('invalid', {})
Beispiel #9
0
    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))
Beispiel #10
0
 def test_eq(self):
     condition = ConditionalExpression(
         'and',
         conditions=[
             Condition('HelloWorld', 'mod1_flag1'),
             Condition('HelloWorld', 'Top Condition')
         ])
     branches = [
         Branch(source_id=1, destination_id=2),
         Branch(source_id=1, destination_id=2, status='TestStatus'),
         Branch(source_id=1, destination_id=2, condition=condition)
     ]
     for i in range(len(branches)):
         for j in range(len(branches)):
             if i == j:
                 self.assertEqual(branches[i], branches[j])
             else:
                 self.assertNotEqual(branches[i], branches[j])
 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_init_with_triggers(self):
     trigger = ConditionalExpression(
         'and',
         conditions=[
             Condition('HelloWorld',
                       action_name='regMatch',
                       arguments=[Argument('regex', value='(.*)')]),
             Condition('HelloWorld',
                       action_name='regMatch',
                       arguments=[Argument('regex', value='a')])
         ])
     action = Action('HelloWorld',
                     'helloWorld',
                     'helloWorld',
                     trigger=trigger)
     self.__compare_init(action,
                         'HelloWorld',
                         'helloWorld',
                         'helloWorld',
                         trigger=trigger)
 def test_init_with_transforms(self):
     transforms = [
         Transform('HelloWorld',
                   action_name='mod1_filter2',
                   arguments=[Argument('arg1', value='5.4')]),
         Transform(app_name='HelloWorld', action_name='Top Transform')
     ]
     condition = Condition('HelloWorld',
                           action_name='Top Condition',
                           transforms=transforms)
     self.__compare_init(condition, 'HelloWorld', 'Top Condition',
                         transforms, [])
Beispiel #14
0
 def test_get_branch_invalid_action(self):
     condition = ConditionalExpression(
         'and',
         conditions=[
             Condition('HelloWorld',
                       action_name='regMatch',
                       arguments=[Argument('regex', value='aaa')])
         ])
     branch = Branch(source_id=1, destination_id=2, condition=condition)
     action = Action('HelloWorld', 'helloWorld', 'helloWorld')
     action._output = ActionResult(result='bbb', status='Success')
     with self.assertRaises(InvalidExecutionElement):
         Workflow('test', 1, actions=[action], branches=[branch])
    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'])
Beispiel #16
0
    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({"data_in": {"data": 'aaa'}}, {})

        self.assertTrue(ret)
 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', {}))
Beispiel #18
0
def convert_condition(condition):
    arguments = []
    if 'arguments' in condition:
        for argument in condition['arguments']:
            arguments.append(convert_arg(argument))

    transforms = []
    if 'transforms' in condition:
        for transform in condition['transforms']:
            transforms.append(convert_transform(transform))

    condition_obj = Condition(app_name=condition['app_name'], action_name=condition['action_name'],
                              id=condition.get('uid', None), arguments=arguments, transforms=transforms)

    return condition_obj
 def test_execute_action_with_valid_arguments_and_transforms_invalid_data_and_routing(
         self):
     transforms = [
         Transform('HelloWorld',
                   action_name='mod1_filter2',
                   arguments=[Argument('arg1', reference='action1')]),
         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
     accumulator = {'action1': '5', 'action2': 4}
     self.assertFalse(
         Condition('HelloWorld',
                   action_name='mod1_flag2',
                   arguments=[Argument('arg1', value=4)],
                   transforms=transforms).execute(
                       LocalActionExecutionStrategy(), 'invalid',
                       accumulator))
Beispiel #20
0
 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)
     AppInstance.create(app_name='HelloWorld', device_name='device1')
     self.assertFalse(action.execute_trigger({"data_in": {
         "data": 'a'
     }}, {}))
     self.assertTrue(
         action.execute_trigger({"data_in": {
             "data": 'aaa'
         }}, {}))
 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'
                                }}, {}))
Beispiel #22
0
    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'])
 def test_init_with_conditions(self):
     conditions = [self.get_always_true_condition(), Condition('HelloWorld', 'mod1_flag1')]
     expression = ConditionalExpression(operator='or', conditions=conditions)
     self.assert_construction(expression, operator='or', conditions=conditions)
 def get_always_true_condition():
     return Condition('HelloWorld', 'Top Condition')
 def get_regex_condition(pattern='(.*)'):
     return Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value=pattern)])
Beispiel #26
0
 def test_execute_action_only_no_arguments_valid_data_with_conversion(self):
     self.assertTrue(Condition('HelloWorld', 'Top Condition').execute('3.4', {}))
Beispiel #27
0
 def test_execute_action_only_no_arguments_valid_data_with_conversion_inverted(self):
     self.assertFalse(Condition('HelloWorld', 'Top Condition', is_negated=True).execute('3.4', {}))
Beispiel #28
0
 def test_execute_action_only_no_arguments_invalid_data(self):
     with self.assertRaises(InvalidArgument):
         Condition('HelloWorld', 'Top Condition').execute('invalid', {})
Beispiel #29
0
 def test_execute_action_with_valid_arguments_valid_data(self):
     self.assertTrue(
         Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=3)]).execute('5', {}))
Beispiel #30
0
 def test_execute_action_with_valid_arguments_invalid_data(self):
     with self.assertRaises(InvalidArgument):
         Condition('HelloWorld',
                   action_name='mod1_flag2',
                   arguments=[Argument('arg1', value=3)]).execute('invalid', {})