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 = AppInstance.create(app_name='HelloWorld', device_name='device1') result = action.execute(instance.instance, {}) self.assertAlmostEqual(result.result, 11.0) self.assertEqual(result.status, 'Success') self.assertEqual(action._output, result)
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_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_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_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_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_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_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_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_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'])
def test_execute_return_failure(self): action = Action(app_name='HelloWorld', action_name='dummy action', name='helloWorld', arguments=[Argument('status', value=False)]) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') 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 else: self.assertIn('data', kwargs) data = kwargs['data'] self.assertEqual(data['status'], 'Failure') self.assertEqual(data['result'], False) result['result_triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(instance.instance, {}) self.assertTrue(result['started_triggered']) self.assertTrue(result['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_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_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_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_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_action_which_raises_exception(self): action = Action(app_name='HelloWorld', action_name='Buggy', name='helloWorld') instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.execute(instance.instance, {}) self.assertIsNotNone(action.get_output())
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_no_args(self): action = Action(app_name='HelloWorld', action_name='helloWorld', name='helloWorld') instance = AppInstance.create(app_name='HelloWorld', device_name='device1') self.assertEqual(action.execute(instance.instance, {}), ActionResult({'message': 'HELLO WORLD'}, 'Success')) self.assertEqual(action._output, ActionResult({'message': 'HELLO WORLD'}, '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_global_action(self): action = Action(app_name='HelloWorld', action_name='global2', name='helloWorld', arguments=[Argument('arg1', value='something')]) instance = AppInstance.create(app_name='HelloWorld', device_name='') result = action.execute(instance.instance, {}) self.assertAlmostEqual(result.result, 'something') self.assertEqual(result.status, 'Success') self.assertEqual(action._output, result)
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_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_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 = AppInstance.create(app_name='HelloWorld', device_name='device1') action.execute(instance.instance, accumulator)
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_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) accumulator = {} action._output = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action], branches=[branch]) workflow.get_branch(action, accumulator) self.assertEqual(branch._counter, 1) self.assertIn(branch.id, accumulator) self.assertEqual(accumulator[branch.id], 1)
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_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 = AppInstance.create(app_name='HelloWorld', device_name='device1') result = action.execute(instance.instance, {}) self.assertAlmostEqual(result.result, 8.9) self.assertEqual(result.status, 'Success') self.assertEqual(action._output, result)
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_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_set_args_invalid_format(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') ]) with self.assertRaises(InvalidArgument): action.set_arguments([ Argument('num1', value='-5.62'), Argument('num2', value='5'), Argument('num3', value='invalid') ])
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 = AppInstance.create(app_name='HelloWorld', device_name='device1') result = action.execute(instance.instance, accumulator) self.assertAlmostEqual(result.result, 8.9) self.assertEqual(result.status, 'Success') self.assertEqual(action._output, result)
def convert_action(action, action_map): uid = action.pop('uid') try: action_id = uuid.UUID(uid) action_map[uid] = action_id except Exception: print("Action UID is not valid UUID, creating new UID") action_id = uuid.uuid4() action_map[uid] = action_id arguments = [] if 'arguments' in action: for argument in action['arguments']: arguments.append(convert_arg(argument)) trigger = None if 'triggers' in action and len(action['triggers']) > 0: trigger = ConditionalExpression() for trig in action['triggers']: trigger.conditions.append(convert_condition(trig)) name = action['name'] if 'name' in action else action['action_name'] device_id = action['device_id'] if 'device_id' in action else None x = None y = None if 'position' in action and action['position']: x = action['position']['x'] y = action['position']['y'] position = Position(x, y) if x and y else None action_obj = Action(id=action_id, app_name=action['app_name'], action_name=action['action_name'], name=name, device_id=device_id, position=position, arguments=arguments, trigger=trigger) return action_obj
def test_init_with_arguments_with_conversion(self): action = Action('HelloWorld', 'returnPlusOne', 'returnPlusOne', arguments=[Argument('number', value='-5.6')]) self.__compare_init(action, 'HelloWorld', 'returnPlusOne', 'returnPlusOne', arguments=[Argument('number', value='-5.6')])
def test_init_with_position(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', position=Position(13, 42)) self.__compare_init(action, 'HelloWorld', 'helloWorld', 'helloWorld', position=Position(13, 42))
def test_init_app_action_only_with_device(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', device_id='test') self.__compare_init(action, 'HelloWorld', 'helloWorld', 'helloWorld', device_id='test')
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_get_execution_id(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld') self.assertEqual(action.get_execution_id(), action._execution_id)
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)