Skip to content

Update tests for azure-functions EventHub triggers #592

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 3 commits into from
Dec 12, 2019
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def run(self):
],
extras_require={
'dev': [
'azure-functions==1.0.5',
'azure-functions==1.0.7',
'flake8~=3.5.0',
'mypy',
'pytest',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import List
import azure.functions as func


# This is testing the function load feature for the multiple events annotation
def main(events: List[func.EventHubEvent]) -> str:
return 'OK_MANY'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"scriptFile": "__init__.py",

"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "in",
"eventHubName": "python-worker-iot-ci",
"connection": "AzureWebJobsEventHubConnectionString",
"cardinality": "many"
},
{
"type": "blob",
"direction": "out",
"name": "$return",
"connection": "AzureWebJobsStorage",
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import List


# This is testing the function load feature for the multiple events annotation
# The event shouldn't be List[str]
def main(events: List[str]) -> str:
return 'BAD'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"scriptFile": "__init__.py",

"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "in",
"eventHubName": "python-worker-iot-ci",
"connection": "AzureWebJobsEventHubConnectionString",
"cardinality": "many"
},
{
"type": "blob",
"direction": "out",
"name": "$return",
"connection": "AzureWebJobsStorage",
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import azure.functions as func


# This is testing the function load feature for the single event annotation
def main(event: func.EventHubEvent) -> str:
return 'OK_ONE'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"scriptFile": "__init__.py",

"bindings": [
{
"type": "eventHubTrigger",
"name": "event",
"direction": "in",
"eventHubName": "python-worker-iot-ci",
"connection": "AzureWebJobsEventHubConnectionString",
"cardinality": "one"
},
{
"type": "blob",
"direction": "out",
"name": "$return",
"connection": "AzureWebJobsStorage",
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


# This is testing the function load feature for the single event annotation
# The event shouldn't be int
def main(event: int) -> str:
return 'BAD'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"scriptFile": "__init__.py",

"bindings": [
{
"type": "eventHubTrigger",
"name": "event",
"direction": "in",
"eventHubName": "python-worker-iot-ci",
"connection": "AzureWebJobsEventHubConnectionString",
"cardinality": "one"
},
{
"type": "blob",
"direction": "out",
"name": "$return",
"connection": "AzureWebJobsStorage",
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
}
]
}
80 changes: 80 additions & 0 deletions tests/unittests/test_mock_eventhub_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,83 @@ async def call_and_check():
)

await call_and_check()

async def test_mock_eventhub_cardinality_one(self):
async with testutils.start_mockhost(
script_root=self.mock_funcs_dir) as host:

func_id, r = await host.load_function('eventhub_cardinality_one')
self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'eventhub_cardinality_one',
[
protos.ParameterBinding(
name='event',
data=protos.TypedData(
json=json.dumps({
'id': 'cardinality_one'
})
),
),
],
metadata={}
)

self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(r.response.return_value.string, 'OK_ONE')

async def test_mock_eventhub_cardinality_one_bad_annotation(self):
async with testutils.start_mockhost(
script_root=self.mock_funcs_dir) as host:

# This suppose to fail since the event should not be int
func_id, r = await host.load_function(
'eventhub_cardinality_one_bad_anno'
)
self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Failure)

async def test_mock_eventhub_cardinality_many(self):
async with testutils.start_mockhost(
script_root=self.mock_funcs_dir) as host:

func_id, r = await host.load_function('eventhub_cardinality_many')
self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'eventhub_cardinality_many',
[
protos.ParameterBinding(
name='events',
data=protos.TypedData(
json=json.dumps({
'id': 'cardinality_many'
})
),
),
],
metadata={}
)

self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(r.response.return_value.string, 'OK_MANY')

async def test_mock_eventhub_cardinality_many_bad_annotation(self):
async with testutils.start_mockhost(
script_root=self.mock_funcs_dir) as host:

# This suppose to fail since the event should not be List[str]
func_id, r = await host.load_function(
'eventhub_cardinality_many_bad_anno'
)
self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Failure)