Skip to content

Commit 33e982c

Browse files
author
Evan Roman
committed
Test
1 parent c0235e9 commit 33e982c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import json
4+
5+
import azure.functions as func
6+
import azurefunctions.extensions.bindings.eventhub as eh
7+
8+
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
9+
10+
# An HttpTrigger to generating EventHub event from EventHub Output Binding
11+
@app.function_name(name="eventhub_output")
12+
@app.route(route="eventhub_output")
13+
@app.event_hub_output(arg_name="event",
14+
event_hub_name="python-worker-ci-eventhub-one",
15+
connection="AzureWebJobsEventHubConnectionString")
16+
def eventhub_output(req: func.HttpRequest, event: func.Out[str]) -> str:
17+
event.set(req.get_body().decode('utf-8'))
18+
return 'OK'
19+
20+
# This is an actual EventHub trigger which will convert the event data
21+
# into a storage blob.
22+
@app.function_name(name="eventhub_trigger")
23+
@app.event_hub_message_trigger(arg_name="event",
24+
event_hub_name="python-worker-ci-eventhub-one",
25+
connection="AzureWebJobsEventHubConnectionString"
26+
)
27+
@app.blob_output(arg_name="$return",
28+
path="python-worker-tests/test-eventhub-triggered.txt",
29+
connection="AzureWebJobsStorage")
30+
def eventhub_trigger(event: eh.EventData) -> bytes:
31+
return bytes(event.body_as_str())
32+
33+
# Retrieve the event data from storage blob and return it as Http response
34+
@app.function_name(name="get_eventhub_triggered")
35+
@app.route(route="get_eventhub_triggered")
36+
@app.blob_input(arg_name="file",
37+
path="python-worker-tests/test-eventhub-triggered.txt",
38+
connection="AzureWebJobsStorage")
39+
def get_eventhub_triggered(req: func.HttpRequest,
40+
file: func.InputStream) -> str:
41+
return file.read().decode('utf-8')
42+
43+
@app.event_hub_message_trigger(
44+
arg_name="event",
45+
event_hub_name="python-worker-ci-eventhub-one",
46+
connection="AzureWebJobsEventHubConnectionString"
47+
)
48+
def eventhub_trigger(event: eh.EventHubData) -> str:
49+
return event.body_as_str()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import sys
4+
import time
5+
import unittest
6+
7+
from tests.utils import testutils
8+
9+
10+
@unittest.skipIf(sys.version_info.minor <= 8, "The base extension"
11+
"is only supported for 3.9+.")
12+
class TestDeferredBindingsEventHubFunctions(testutils.WebHostTestCase):
13+
14+
@classmethod
15+
def get_script_dir(cls):
16+
return testutils.EXTENSION_TESTS_FOLDER / 'deferred_bindings_tests' / \
17+
'deferred_bindings_eventhub_functions'
18+
19+
@classmethod
20+
def get_libraries_to_install(cls):
21+
return ['azurefunctions-extensions-bindings-eventhub']
22+
23+
def test_ed_eventhub_trigger(self):
24+
data = "DummyData"
25+
26+
r = self.webhost.request('POST', 'eventhub_output',
27+
data=data.encode('utf-8'))
28+
self.assertEqual(r.status_code, 200)
29+
self.assertEqual(r.text, 'OK')
30+
31+
# # Once the event get generated, allow function host to poll from
32+
# # EventHub and wait for eventhub_trigger to execute,
33+
# # converting the event metadata into a blob.
34+
# time.sleep(5)
35+
36+
# # Call get_eventhub_triggered to retrieve event metadata from blob.
37+
# r = self.webhost.request('GET', 'get_eventhub_triggered')
38+
39+
# # Waiting for the blob get updated with the latest data from the
40+
# # eventhub output binding
41+
# time.sleep(5)
42+
# self.assertEqual(r.status_code, 200)
43+
# response = r.json()

0 commit comments

Comments
 (0)