def test_execute_generates_uid(self): step = Step(app='HelloWorld', action='helloWorld') original_execution_uid = step.get_execution_uid() instance = AppInstance.create(app_name='HelloWorld', device_name='device1') step.execute(instance.instance, {}) self.assertNotEqual(step.get_execution_uid(), original_execution_uid)
def test_execute_action_which_raises_exception(self): from tests.testapps.HelloWorld.exceptions import CustomException step = Step(app='HelloWorld', action='Buggy') instance = AppInstance.create(app_name='HelloWorld', device_name='device1') with self.assertRaises(CustomException): step.execute(instance.instance, {})
def test_execute_with_accumulator_missing_step_callbacks(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '@1', 'num2': '@step2', 'num3': '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} @callbacks.data_sent.connect def callback_is_sent(sender, **kwargs): if isinstance(sender, Step): self.assertIs(sender, step) self.assertIn('callback_name', kwargs) self.assertIn(kwargs['callback_name'], ('Step Started', 'Step Input Invalid')) self.assertIn('object_type', kwargs) self.assertEqual(kwargs['object_type'], 'Step') if kwargs['callback_name'] == 'Step Started': result['started_triggered'] = True else: result['result_triggered'] = True with self.assertRaises(InvalidInput): step.execute(instance.instance, accumulator) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_with_accumulator_missing_step(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '@1', 'num2': '@step2', 'num3': '10.2' }) accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'} instance = AppInstance.create(app_name='HelloWorld', device_name='device1') with self.assertRaises(InvalidInput): step.execute(instance.instance, accumulator)
def test_execute_with_triggers(self): triggers = [Flag(action='regMatch', args={'regex': 'aaa'})] step = Step(app='HelloWorld', action='helloWorld', triggers=triggers) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') step.send_data_to_trigger({"data_in": {"data": 'aaa'}}) result = {'triggered': False} @callbacks.data_sent.connect def callback_is_sent(sender, **kwargs): if kwargs['callback_name'] == "Trigger Step Taken": result['triggered'] = True step.execute(instance.instance, {}) self.assertTrue(result['triggered'])
def test_execute_with_complex_inputs(self): step = Step(app='HelloWorld', action='Json Sample', inputs={ 'json_in': { '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 = step.execute(instance.instance, {}) self.assertAlmostEqual(result.result, 11.0) self.assertEqual(result.status, 'Success') self.assertEqual(step._output, result)
def test_execute_no_args(self): step = Step(app='HelloWorld', action='helloWorld') instance = AppInstance.create(app_name='HelloWorld', device_name='device1') self.assertEqual(step.execute(instance.instance, {}), ActionResult({'message': 'HELLO WORLD'}, 'Success')) self.assertEqual(step._output, ActionResult({'message': 'HELLO WORLD'}, 'Success'))
def test_execute_global_action(self): step = Step(app='HelloWorld', action='global2', inputs={'arg1': 'something'}) instance = AppInstance.create(app_name='HelloWorld', device_name='') result = step.execute(instance.instance, {}) self.assertAlmostEqual(result.result, 'something') self.assertEqual(result.status, 'Success') self.assertEqual(step._output, result)
def test_execute_multiple_triggers(self): triggers = [Flag(action='regMatch', args={'regex': 'aaa'})] step = Step(app='HelloWorld', action='helloWorld', triggers=triggers) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') step.send_data_to_trigger({"data_in": {"data": 'a'}}) trigger_taken = {'triggered': 0} trigger_not_taken = {'triggered': 0} @callbacks.data_sent.connect def callback_is_sent(sender, **kwargs): if kwargs['callback_name'] == "Trigger Step Taken": trigger_taken['triggered'] += 1 elif kwargs['callback_name'] == "Trigger Step Not Taken": step.send_data_to_trigger({"data_in": {"data": 'aaa'}}) trigger_not_taken['triggered'] += 1 step.execute(instance.instance, {}) self.assertEqual(trigger_taken['triggered'], 1) self.assertEqual(trigger_not_taken['triggered'], 1)
def test_execute_action_which_raises_exception_sends_callbacks(self): from tests.testapps.HelloWorld.exceptions import CustomException step = Step(app='HelloWorld', action='Buggy') instance = AppInstance.create(app_name='HelloWorld', device_name='device1') result = {'started_triggered': False} @callbacks.data_sent.connect def callback_is_sent(sender, **kwargs): if isinstance(sender, Step): self.assertIs(sender, step) self.assertIn('callback_name', kwargs) self.assertEqual(kwargs['callback_name'], 'Step Started') self.assertIn('object_type', kwargs) self.assertEqual(kwargs['object_type'], 'Step') result['started_triggered'] = True with self.assertRaises(CustomException): step.execute(instance.instance, {}) self.assertTrue(result['started_triggered'])
def test_execute_with_args(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '-5.6', 'num2': '4.3', 'num3': '10.2' }) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') result = step.execute(instance.instance, {}) self.assertAlmostEqual(result.result, 8.9) self.assertEqual(result.status, 'Success') self.assertEqual(step._output, result)
def test_execute_with_accumulator_with_conversion(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '@1', 'num2': '@step2', 'num3': '10.2' }) accumulator = {'1': '-5.6', 'step2': '4.3'} instance = AppInstance.create(app_name='HelloWorld', device_name='device1') result = step.execute(instance.instance, accumulator) self.assertAlmostEqual(result.result, 8.9) self.assertEqual(result.status, 'Success') self.assertEqual(step._output, result)
def test_execute_sends_callbacks(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '-5.6', 'num2': '4.3', 'num3': '10.2' }) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') result = {'started_triggered': False, 'result_triggered': False} @callbacks.data_sent.connect def callback_is_sent(sender, **kwargs): if isinstance(sender, Step): self.assertIs(sender, step) self.assertIn('callback_name', kwargs) self.assertIn(kwargs['callback_name'], ('Step Started', 'Function Execution Success')) self.assertIn('object_type', kwargs) self.assertEqual(kwargs['object_type'], 'Step') if kwargs['callback_name'] == 'Step Started': result['started_triggered'] = True else: self.assertIn('data', kwargs) data = json.loads(kwargs['data']) self.assertIn('result', data) data = data['result'] self.assertEqual(data['status'], 'Success') self.assertAlmostEqual(data['result'], 8.9) result['result_triggered'] = True step.execute(instance.instance, {}) self.assertTrue(result['started_triggered']) self.assertTrue(result['result_triggered'])
def test_execute_event(self): step = Step(app='HelloWorld', action='Sample Event', inputs={'arg1': 1}) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') import time from tests.testapps.HelloWorld.events import event1 import threading def sender(): time.sleep(0.1) event1.trigger(3) thread = threading.Thread(target=sender) start = time.time() thread.start() result = step.execute(instance.instance, {}) end = time.time() thread.join() self.assertEqual(result, ActionResult(4, 'Success')) self.assertGreater((end - start), 0.1)