def test_async_call_with_defaults(self): """Change a task's asynchronousity at runtime.""" # Import the task first to make sure it is decorated whilst the # environment is unpatched. async_me = import_and_get_task("tests.test_app.async_me") lambda_async_mock = mock.Mock() lambda_async_mock.return_value.send.return_value = "Running async!" with mock.patch.dict('zappa.async.ASYNC_CLASSES', {'lambda': lambda_async_mock}): # First check that it still runs synchronously by default self.assertEqual(async_me("123"), "run async when on lambda 123") # Now patch the environment to make it look like we are running on # AWS Lambda options = { 'AWS_LAMBDA_FUNCTION_NAME': 'MyLambda', 'AWS_REGION': 'us-east-1' } with mock.patch.dict(os.environ, options): self.assertEqual(async_me("qux"), "Running async!") # And check the dispatching class got called correctly lambda_async_mock.assert_called_once_with( aws_region='us-east-1', capture_response=False, delay_seconds=0, lambda_function_name="MyLambda") lambda_async_mock.return_value.send.assert_called_with( get_func_task_path(async_me), ("qux", ), {})
def test_async_call_with_defaults(self): """Change a task's asynchronousity at runtime.""" # Import the task first to make sure it is decorated whilst the # environment is unpatched. async_me = import_and_get_task("tests.test_app.async_me") lambda_async_mock = mock.Mock() lambda_async_mock.return_value.send.return_value = "Running async!" with mock.patch.dict('zappa.async.ASYNC_CLASSES', {'lambda': lambda_async_mock}): # First check that it still runs synchronously by default self.assertEqual(async_me("123"), "run async when on lambda 123") # Now patch the environment to make it look like we are running on # AWS Lambda options = { 'AWS_LAMBDA_FUNCTION_NAME': 'MyLambda', 'AWS_REGION': 'us-east-1' } with mock.patch.dict(os.environ, options): self.assertEqual(async_me("qux"), "Running async!") # And check the dispatching class got called correctly lambda_async_mock.assert_called_once() lambda_async_mock.assert_called_with(aws_region='us-east-1', capture_response=False, lambda_function_name="MyLambda") lambda_async_mock.return_value.send.assert_called_with( get_func_task_path(async_me), ("qux",), {})
def function_in_lambda(encoded_data, map_function, compression): """ Helper function that is actually called in the lambda (instead of the map_function directly), because the input as well as the output data must be encoded/decoded. :param encoded_data: The encoded data that will be decoded before feeding into the map_function. :param map_function: The function that is called on the data. :param compression: Turn on compression during streaming. :return: The encoded result of the function call. """ map_function = import_and_get_task(map_function) data = decode_payload(encoded_data, compression) result = feature_calculation_on_chunks(data, map_function) return encode_payload(result, compression)
def test_async_sqs_call(self): """ Call a task with sqs async service. """ async_sqs_me = import_and_get_task("tests.test_app.async_sqs_me") sqs_client_mock = mock.Mock() sqs_client_mock.get_queue_url = mock.MagicMock( return_value={ 'QueueUrl': 'https://us-east-1.queue.amazonaws.com/1' }) sqs_client_mock.send_message = mock.MagicMock( return_value={ 'MD5OfMessageBody': 'string', 'MD5OfMessageAttributes': 'string', 'MessageId': '1234', 'SequenceNumber': '1' }) with mock.patch('zappa.async.SQS_CLIENT', sqs_client_mock, create=True): # First check that it still runs synchronously by default self.assertEqual(async_sqs_me("123"), "run async with sqs service when on lambda 123") # Now patch the environment to make it look like we are running on # AWS Lambda options = { 'AWS_LAMBDA_FUNCTION_NAME': 'MyLambda', 'AWS_REGION': 'us-east-1' } with mock.patch.dict(os.environ, options): async_sqs_me("qux") # And check the sqs client got invoked correctly sqs_client_mock.get_queue_url.assert_called_once_with( QueueName='MyLambda-zappa-async') sqs_client_mock.send_message.assert_called_once_with( QueueUrl='https://us-east-1.queue.amazonaws.com/1', MessageBody=json.dumps({ "task_path": get_func_task_path(async_sqs_me), "capture_response": False, "response_id": None, "args": ["qux"], "kwargs": {}, "zappaAsyncCommand": "zappa.async.route_sqs_task" }), DelaySeconds=0)
def test_nofails_funcs(self): funk = import_and_get_task("tests.test_app.schedule_me") get_func_task_path(funk) is_from_router()
def test_sync_call(self): funk = import_and_get_task("tests.test_app.async_me") self.assertEqual(funk.sync('123'), "run async when on lambda 123")
def test_nofails_funcs(self): funk = import_and_get_task("tests.test_app.async_me") get_func_task_path(funk) self.assertEqual(funk.__name__, 'async_me')