Skip to content

Commit dbca168

Browse files
authored
Update tests for azure-functions EventHub triggers (#592)
* Update tests for azure-functions 1.0.7 * Add failure cases for test * Fix linting
1 parent 4332695 commit dbca168

File tree

10 files changed

+191
-1
lines changed

10 files changed

+191
-1
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def run(self):
249249
],
250250
extras_require={
251251
'dev': [
252-
'azure-functions==1.0.5',
252+
'azure-functions==1.0.7',
253253
'flake8~=3.5.0',
254254
'mypy',
255255
'pytest',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import List
2+
import azure.functions as func
3+
4+
5+
# This is testing the function load feature for the multiple events annotation
6+
def main(events: List[func.EventHubEvent]) -> str:
7+
return 'OK_MANY'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
4+
"bindings": [
5+
{
6+
"type": "eventHubTrigger",
7+
"name": "events",
8+
"direction": "in",
9+
"eventHubName": "python-worker-iot-ci",
10+
"connection": "AzureWebJobsEventHubConnectionString",
11+
"cardinality": "many"
12+
},
13+
{
14+
"type": "blob",
15+
"direction": "out",
16+
"name": "$return",
17+
"connection": "AzureWebJobsStorage",
18+
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
19+
}
20+
]
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import List
2+
3+
4+
# This is testing the function load feature for the multiple events annotation
5+
# The event shouldn't be List[str]
6+
def main(events: List[str]) -> str:
7+
return 'BAD'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
4+
"bindings": [
5+
{
6+
"type": "eventHubTrigger",
7+
"name": "events",
8+
"direction": "in",
9+
"eventHubName": "python-worker-iot-ci",
10+
"connection": "AzureWebJobsEventHubConnectionString",
11+
"cardinality": "many"
12+
},
13+
{
14+
"type": "blob",
15+
"direction": "out",
16+
"name": "$return",
17+
"connection": "AzureWebJobsStorage",
18+
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
19+
}
20+
]
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import azure.functions as func
2+
3+
4+
# This is testing the function load feature for the single event annotation
5+
def main(event: func.EventHubEvent) -> str:
6+
return 'OK_ONE'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
4+
"bindings": [
5+
{
6+
"type": "eventHubTrigger",
7+
"name": "event",
8+
"direction": "in",
9+
"eventHubName": "python-worker-iot-ci",
10+
"connection": "AzureWebJobsEventHubConnectionString",
11+
"cardinality": "one"
12+
},
13+
{
14+
"type": "blob",
15+
"direction": "out",
16+
"name": "$return",
17+
"connection": "AzureWebJobsStorage",
18+
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
19+
}
20+
]
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
# This is testing the function load feature for the single event annotation
4+
# The event shouldn't be int
5+
def main(event: int) -> str:
6+
return 'BAD'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
4+
"bindings": [
5+
{
6+
"type": "eventHubTrigger",
7+
"name": "event",
8+
"direction": "in",
9+
"eventHubName": "python-worker-iot-ci",
10+
"connection": "AzureWebJobsEventHubConnectionString",
11+
"cardinality": "one"
12+
},
13+
{
14+
"type": "blob",
15+
"direction": "out",
16+
"name": "$return",
17+
"connection": "AzureWebJobsStorage",
18+
"path": "python-worker-tests/test-eventhub-iot-triggered.txt"
19+
}
20+
]
21+
}

tests/unittests/test_mock_eventhub_functions.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,83 @@ async def call_and_check():
4848
)
4949

5050
await call_and_check()
51+
52+
async def test_mock_eventhub_cardinality_one(self):
53+
async with testutils.start_mockhost(
54+
script_root=self.mock_funcs_dir) as host:
55+
56+
func_id, r = await host.load_function('eventhub_cardinality_one')
57+
self.assertEqual(r.response.function_id, func_id)
58+
self.assertEqual(r.response.result.status,
59+
protos.StatusResult.Success)
60+
61+
_, r = await host.invoke_function(
62+
'eventhub_cardinality_one',
63+
[
64+
protos.ParameterBinding(
65+
name='event',
66+
data=protos.TypedData(
67+
json=json.dumps({
68+
'id': 'cardinality_one'
69+
})
70+
),
71+
),
72+
],
73+
metadata={}
74+
)
75+
76+
self.assertEqual(r.response.result.status,
77+
protos.StatusResult.Success)
78+
self.assertEqual(r.response.return_value.string, 'OK_ONE')
79+
80+
async def test_mock_eventhub_cardinality_one_bad_annotation(self):
81+
async with testutils.start_mockhost(
82+
script_root=self.mock_funcs_dir) as host:
83+
84+
# This suppose to fail since the event should not be int
85+
func_id, r = await host.load_function(
86+
'eventhub_cardinality_one_bad_anno'
87+
)
88+
self.assertEqual(r.response.function_id, func_id)
89+
self.assertEqual(r.response.result.status,
90+
protos.StatusResult.Failure)
91+
92+
async def test_mock_eventhub_cardinality_many(self):
93+
async with testutils.start_mockhost(
94+
script_root=self.mock_funcs_dir) as host:
95+
96+
func_id, r = await host.load_function('eventhub_cardinality_many')
97+
self.assertEqual(r.response.function_id, func_id)
98+
self.assertEqual(r.response.result.status,
99+
protos.StatusResult.Success)
100+
101+
_, r = await host.invoke_function(
102+
'eventhub_cardinality_many',
103+
[
104+
protos.ParameterBinding(
105+
name='events',
106+
data=protos.TypedData(
107+
json=json.dumps({
108+
'id': 'cardinality_many'
109+
})
110+
),
111+
),
112+
],
113+
metadata={}
114+
)
115+
116+
self.assertEqual(r.response.result.status,
117+
protos.StatusResult.Success)
118+
self.assertEqual(r.response.return_value.string, 'OK_MANY')
119+
120+
async def test_mock_eventhub_cardinality_many_bad_annotation(self):
121+
async with testutils.start_mockhost(
122+
script_root=self.mock_funcs_dir) as host:
123+
124+
# This suppose to fail since the event should not be List[str]
125+
func_id, r = await host.load_function(
126+
'eventhub_cardinality_many_bad_anno'
127+
)
128+
self.assertEqual(r.response.function_id, func_id)
129+
self.assertEqual(r.response.result.status,
130+
protos.StatusResult.Failure)

0 commit comments

Comments
 (0)