Skip to content

Added generic tests for http and servicebus #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import logging

import azure.functions as func

app = func.FunctionApp()


@app.function_name(name="default_template")
@app.generic_trigger(arg_name="req",
type="httpTrigger",
route="default_template")
@app.generic_output_binding(arg_name="$return", type="http")
def default_template(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')

if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function "
f"executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. "
"Pass a name in the query string or in the request body for a"
" personalized response.",
status_code=200
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import json

import azure.functions as func

app = func.FunctionApp()


@app.function_name(name="put_message")
@app.generic_trigger(arg_name="req", type="httpTrigger", route="put_message")
@app.generic_output_binding(arg_name="msg",
type="serviceBus",
connection="AzureWebJobsServiceBusConnectionString",
queue_name="testqueue")
@app.generic_output_binding(arg_name="$return", type="http")
def put_message(req: func.HttpRequest, msg: func.Out[str]):
msg.set(req.get_body().decode('utf-8'))
return 'OK'


@app.function_name(name="get_servicebus_triggered")
@app.generic_trigger(arg_name="req", type="httpTrigger",
route="get_servicebus_triggered")
@app.generic_input_binding(arg_name="file",
type="blob",
path="python-worker-tests/test-servicebus-triggered.txt", # NoQA
connection="AzureWebJobsStorage")
@app.generic_output_binding(arg_name="$return", type="http")
def get_servicebus_triggered(req: func.HttpRequest,
file: func.InputStream) -> str:
return func.HttpResponse(
file.read().decode('utf-8'), mimetype='application/json')


@app.generic_trigger(
arg_name="msg",
type="serviceBusTrigger",
connection="AzureWebJobsServiceBusConnectionString",
queue_name="testqueue")
@app.generic_output_binding(arg_name="$return",
path="python-worker-tests/test-servicebus-triggered.txt", # NoQA
type="blob",
connection="AzureWebJobsStorage")
def servicebus_trigger(msg: func.ServiceBusMessage) -> str:
result = json.dumps({
'message_id': msg.message_id,
'body': msg.get_body().decode('utf-8'),
'content_type': msg.content_type,
'delivery_count': msg.delivery_count,
'expiration_time': (msg.expiration_time.isoformat() if
msg.expiration_time else None),
'label': msg.label,
'partition_key': msg.partition_key,
'reply_to': msg.reply_to,
'reply_to_session_id': msg.reply_to_session_id,
'scheduled_enqueue_time': (msg.scheduled_enqueue_time.isoformat() if
msg.scheduled_enqueue_time else None),
'session_id': msg.session_id,
'time_to_live': msg.time_to_live,
'to': msg.to,
'user_properties': msg.user_properties,
})

return result
9 changes: 9 additions & 0 deletions tests/endtoend/test_http_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ class TestHttpFunctionsStein(TestHttpFunctions):
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'http_functions' /\
'http_functions_stein'


class TestHttpFunctionsSteinGeneric(TestHttpFunctions):

@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'http_functions' /\
'http_functions_stein' /\
'generic'
9 changes: 9 additions & 0 deletions tests/endtoend/test_servicebus_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ class TestServiceBusFunctionsStein(TestServiceBusFunctions):
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'servicebus_functions' / \
'servicebus_functions_stein'


class TestServiceBusFunctionsSteinGeneric(TestServiceBusFunctions):

@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'servicebus_functions' / \
'servicebus_functions_stein' / \
'generic'