def test_execute_action_which_raises_exception(self): from tests.apps.HelloWorld.exceptions import CustomException step = Step(app='HelloWorld', action='Buggy') instance = Instance.create(app_name='HelloWorld', device_name='device1') with self.assertRaises(CustomException): step.execute(instance.instance, {})
def test_as_json_after_executed(self): step = Step(app='HelloWorld', action='helloWorld') instance = Instance.create(app_name='HelloWorld', device_name='device1') step.execute(instance.instance, {}) step_json = step.as_json() self.assertDictEqual( step_json['output'], ActionResult({ 'message': 'HELLO WORLD' }, 'Success').as_json())
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 = Instance.create(app_name='HelloWorld', device_name='device1') with self.assertRaises(InvalidInput): step.execute(instance.instance, accumulator)
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 = Instance.create(app_name='HelloWorld', device_name='device1') result = step.execute(instance.instance, {}) self.assertIsInstance(result, tuple) self.assertAlmostEqual(result.result, 11.0) self.assertEqual(result.status, 'Success') self.assertTupleEqual(step.output, result)
def test_execute_no_args(self): step = Step(app='HelloWorld', action='helloWorld') instance = Instance.create(app_name='HelloWorld', device_name='device1') self.assertDictEqual(step.execute(instance.instance, {}), {'message': 'HELLO WORLD'}) self.assertDictEqual(step.output, {'message': 'HELLO WORLD'})
def test_execute_invalid_inputs(self): app = 'HelloWorld' actions = [('invalid_name', { 'call': Argument(key='call', value='HelloWorld', format='str') }), ('repeatBackToMe', { 'number': Argument(key='number', value='6', format='str') }), ('returnPlusOne', {})] for action, inputs in actions: step = Step(app=app, action=action, inputs=inputs) with server.running_context.flask_app.app_context(): instance = Instance.create(app_name=app, device_name='test_device_name') with self.assertRaises(InvalidStepInputError): step.execute(instance=instance())
def test_execute_with_args(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '-5.6', 'num2': '4.3', 'num3': '10.2' }) instance = Instance.create(app_name='HelloWorld', device_name='device1') self.assertAlmostEqual(step.execute(instance.instance, {}), 8.9) self.assertAlmostEqual(step.output, 8.9)
def test_execute_with_accumulator_with_extra_steps(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '@1', 'num2': '@step2', 'num3': '10.2' }) accumulator = {'1': '-5.6', 'step2': '4.3', '3': '45'} instance = Instance.create(app_name='HelloWorld', device_name='device1') self.assertAlmostEqual(step.execute(instance.instance, accumulator), 8.9) self.assertAlmostEqual(step.output, 8.9)
def test_execute_with_args(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '-5.6', 'num2': '4.3', 'num3': '10.2' }) instance = Instance.create(app_name='HelloWorld', device_name='device1') result = step.execute(instance.instance, {}) self.assertIsInstance(result, tuple) self.assertAlmostEqual(result.result, 8.9) self.assertEqual(result.status, 'Success') self.assertTupleEqual(step.output, result)
def test_execute_event(self): step = Step(app='HelloWorld', action='Sample Event', inputs={'arg1': 1}) instance = Instance.create(app_name='HelloWorld', device_name='device1') import time from tests.apps.HelloWorld.events import event1 def sender(): gevent.sleep(0.1) event1.trigger(3) start = time.time() gevent.spawn(sender) result = step.execute(instance.instance, {}) end = time.time() self.assertTupleEqual(result, (4, 'Success')) self.assertTrue((end - start) > 0.1)
def test_execute(self): app = 'HelloWorld' with server.running_context.flask_app.app_context(): instance = Instance.create(app_name=app, device_name='test_device_name') actions = [('helloWorld', {}, { "message": "HELLO WORLD" }), ('repeatBackToMe', { 'call': Argument(key='call', value='HelloWorld', format='str') }, "REPEATING: HelloWorld"), ('returnPlusOne', { 'number': Argument(key='number', value='6', format='str') }, '7')] for action, inputs, output in actions: step = Step(app=app, action=action, inputs=inputs) self.assertEqual(step.execute(instance=instance()), output) self.assertEqual(step.output, output)