def test_execute_multiple_triggers(self): triggers = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ] action = Action(app_name='HelloWorld', action_name='helloWorld', triggers=triggers) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.send_data_to_trigger({"data_in": {"data": 'a'}}) trigger_taken = {'triggered': 0} trigger_not_taken = {'triggered': 0} def callback_is_sent(sender, **kwargs): if kwargs['event'] == WalkoffEvent.TriggerActionTaken: trigger_taken['triggered'] += 1 elif kwargs['event'] == WalkoffEvent.TriggerActionNotTaken: action.send_data_to_trigger({"data_in": {"data": 'aaa'}}) trigger_not_taken['triggered'] += 1 WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(instance.instance, {}) self.assertEqual(trigger_taken['triggered'], 1) self.assertEqual(trigger_not_taken['triggered'], 1)
def test_execute_with_accumulator_missing_action_callbacks(self): action = Action(app_name='HelloWorld', action_name='Add Three', 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') 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(instance.instance, accumulator) 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', 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 = {'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(instance.instance, {}) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_generates_uid(self): action = Action(app_name='HelloWorld', action_name='helloWorld') original_execution_uid = action.get_execution_uid() instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.execute(instance.instance, {}) self.assertNotEqual(action.get_execution_uid(), original_execution_uid)
def test_execute_default_return_success(self): action = Action(app_name='HelloWorld', action_name='dummy action', arguments=[ Argument('status', value=True), Argument('other', value=True) ]) 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.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(instance.instance, {}) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_with_accumulator_missing_action(self): action = Action(app_name='HelloWorld', action_name='Add Three', 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_execute_with_complex_args(self): action = Action(app_name='HelloWorld', action_name='Json Sample', 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_execute_no_args(self): action = Action(app_name='HelloWorld', action_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_global_action(self): action = Action(app_name='HelloWorld', action_name='global2', 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_triggers(self): triggers = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ] action = Action(app_name='HelloWorld', action_name='helloWorld', triggers=triggers) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.send_data_to_trigger({"data_in": {"data": 'aaa'}}) result = {'triggered': False} def callback_is_sent(sender, **kwargs): if kwargs['event'] == WalkoffEvent.TriggerActionTaken: result['triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(instance.instance, {}) self.assertTrue(result['triggered'])
def test_execute_with_args(self): action = Action(app_name='HelloWorld', action_name='Add Three', 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_action_which_raises_exception_sends_callbacks(self): action = Action(app_name='HelloWorld', action_name='Buggy') 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 elif kwargs['event'] == WalkoffEvent.ActionExecutionError: 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_with_accumulator_with_extra_actions(self): action = Action(app_name='HelloWorld', action_name='Add Three', 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 test_execute_action_which_raises_exception(self): action = Action(app_name='HelloWorld', action_name='Buggy') instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.execute(instance.instance, {}) self.assertIsNotNone(action.get_output())