Skip to content

Commit eb40d46

Browse files
committed
table & eh, unit, lint
1 parent f678917 commit eb40d46

File tree

14 files changed

+58
-97
lines changed

14 files changed

+58
-97
lines changed

.github/workflows/ci_ut_workflow.yml

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
# Retry a couple times to avoid certificate issue
6464
retry 5 python setup.py build
6565
retry 5 python setup.py webhost --branch-name=dev
66+
retry 5 python setup.py extension
6667
mkdir logs
6768
- name: Test with pytest
6869
env:

tests/endtoend/eventhub_batch_functions/eventhub_batch_functions_stein/function_app.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
connection="AzureWebJobsEventHubConnectionString",
1919
data_type="string",
2020
cardinality="many")
21-
@app.table_output(arg_name="$return",
22-
connection="AzureWebJobsStorage",
23-
table_name="EventHubBatchTest")
24-
def eventhub_multiple(events):
21+
@app.blob_output(arg_name="$return",
22+
path="python-worker-tests/test-eventhub-batch-triggered.txt",
23+
connection="AzureWebJobsStorage")
24+
def eventhub_multiple(events) -> str:
2525
table_entries = []
2626
for event in events:
2727
json_entry = event.get_body()
@@ -46,13 +46,12 @@ def eventhub_output_batch(req: func.HttpRequest, out: func.Out[str]) -> str:
4646

4747
# Retrieve the event data from storage blob and return it as Http response
4848
@app.function_name(name="get_eventhub_batch_triggered")
49-
@app.route(route="get_eventhub_batch_triggered/{id}")
50-
@app.table_input(arg_name="testEntities",
51-
connection="AzureWebJobsStorage",
52-
table_name="EventHubBatchTest",
53-
partition_key="{id}")
54-
def get_eventhub_batch_triggered(req: func.HttpRequest, testEntities):
55-
return func.HttpResponse(status_code=200, body=testEntities)
49+
@app.route(route="get_eventhub_batch_triggered")
50+
@app.blob_input(arg_name="testEntities",
51+
path="python-worker-tests/test-eventhub-batch-triggered.txt",
52+
connection="AzureWebJobsStorage")
53+
def get_eventhub_batch_triggered(req: func.HttpRequest, testEntities: func.InputStream):
54+
return func.HttpResponse(status_code=200, body=testEntities.read().decode('utf-8'))
5655

5756

5857
# Retrieve the event data from storage blob and return it as Http response

tests/endtoend/eventhub_batch_functions/eventhub_multiple/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# This is an actual EventHub trigger which handles Eventhub events in batches.
77
# It serializes multiple event data into a json and store it into a blob.
8-
def main(events):
8+
def main(events) -> str:
99
table_entries = []
1010
for event in events:
1111
json_entry = event.get_body()

tests/endtoend/eventhub_batch_functions/eventhub_multiple/function.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
},
1414
{
1515
"direction": "out",
16-
"type": "table",
16+
"type": "blob",
1717
"name": "$return",
18-
"tableName": "EventHubBatchTest",
18+
"path": "python-worker-tests/test-eventhub-batch-triggered.txt",
1919
"connection": "AzureWebJobsStorage"
2020
}
2121
]

tests/endtoend/eventhub_batch_functions/get_eventhub_batch_triggered/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55

66
# Retrieve the event data from storage blob and return it as Http response
7-
def main(req: func.HttpRequest, testEntities):
8-
return func.HttpResponse(status_code=200, body=testEntities)
7+
def main(req: func.HttpRequest, testEntities: func.InputStream):
8+
return func.HttpResponse(status_code=200, body=testEntities.read().decode('utf-8'))

tests/endtoend/eventhub_batch_functions/get_eventhub_batch_triggered/function.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
},
1313
{
1414
"direction": "in",
15-
"type": "table",
15+
"type": "blob",
1616
"name": "testEntities",
17-
"partitionKey": "WillBePopulated",
18-
"tableName": "EventHubBatchTest",
17+
"path": "python-worker-tests/test-eventhub-batch-triggered.txt",
1918
"connection": "AzureWebJobsStorage"
2019
},
2120
{

tests/endtoend/table_functions/table_functions_stein/function_app.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
row_key='{id}',
1616
partition_key="test")
1717
def table_in_binding(req: func.HttpRequest, testEntity):
18-
headers_dict = json.loads(testEntity)
19-
return func.HttpResponse(status_code=200, headers=headers_dict)
18+
return func.HttpResponse(status_code=200, body=testEntity)
2019

2120

2221
@app.function_name(name="table_out_binding")
@@ -28,6 +27,5 @@ def table_out_binding(req: func.HttpRequest, resp: func.Out[func.HttpResponse]):
2827
row_key_uuid = str(uuid.uuid4())
2928
table_dict = {'PartitionKey': 'test', 'RowKey': row_key_uuid}
3029
table_json = json.dumps(table_dict)
31-
http_resp = func.HttpResponse(status_code=200, headers=table_dict)
32-
resp.set(http_resp)
30+
resp.set(table_json)
3331
return table_json

tests/endtoend/table_functions/table_functions_stein/generic/function_app.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
row_key="{id}",
2020
partition_key="test")
2121
def table_in_binding(req: func.HttpRequest, testEntity):
22-
headers_dict = json.loads(testEntity)
23-
return func.HttpResponse(status_code=200, headers=headers_dict)
22+
return func.HttpResponse(status_code=200, body=testEntity)
2423

2524

2625
@app.function_name(name="table_out_binding")
@@ -36,6 +35,5 @@ def table_out_binding(req: func.HttpRequest, resp: func.Out[func.HttpResponse]):
3635
row_key_uuid = str(uuid.uuid4())
3736
table_dict = {'PartitionKey': 'test', 'RowKey': row_key_uuid}
3837
table_json = json.dumps(table_dict)
39-
http_resp = func.HttpResponse(status_code=200, headers=table_dict)
40-
resp.set(http_resp)
38+
resp.set(table_json)
4139
return table_json
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
import json
4-
53
import azure.functions as func
64

75

86
def main(req: func.HttpRequest, testEntity):
9-
headers_dict = json.loads(testEntity)
10-
return func.HttpResponse(status_code=200, headers=headers_dict)
7+
return func.HttpResponse(status_code=200, body=testEntity)

tests/endtoend/table_functions/table_out_binding/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ def main(req: func.HttpRequest, resp: func.Out[func.HttpResponse]):
1010
row_key_uuid = str(uuid.uuid4())
1111
table_dict = {'PartitionKey': 'test', 'RowKey': row_key_uuid}
1212
table_json = json.dumps(table_dict)
13-
http_resp = func.HttpResponse(status_code=200, headers=table_dict)
14-
resp.set(http_resp)
13+
resp.set(table_json)
1514
return table_json

tests/endtoend/test_eventhub_batch_functions.py

+16-55
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Licensed under the MIT License.
33
import json
44
import time
5-
import pathlib
65
from datetime import datetime
76
from unittest import skipIf
87

@@ -37,14 +36,9 @@ def get_libraries_to_install(cls):
3736

3837
def test_eventhub_multiple(self):
3938
NUM_EVENTS = 3
40-
all_row_keys_seen = dict([(str(i), True) for i in range(NUM_EVENTS)])
39+
all_row_keys_seen = dict([(i, True) for i in range(NUM_EVENTS)])
4140
partition_key = str(round(time.time()))
4241

43-
# Dynamically rewrite function.json to point to new partition key
44-
# for recording EventHub state
45-
old_partition_key = self._get_table_partition_key()
46-
self._set_table_partition_key(partition_key)
47-
4842
# wait for host to restart after change
4943
time.sleep(5)
5044

@@ -57,29 +51,26 @@ def test_eventhub_multiple(self):
5751
data=json.dumps(docs))
5852
self.assertEqual(r.status_code, 200)
5953

60-
row_keys = [str(i) for i in range(NUM_EVENTS)]
54+
row_keys = [i for i in range(NUM_EVENTS)]
6155
seen = [False] * NUM_EVENTS
6256
row_keys_seen = dict(zip(row_keys, seen))
6357

6458
# Allow trigger to fire.
6559
time.sleep(5)
6660

67-
try:
68-
r = self.webhost.request('GET', 'get_eventhub_batch_triggered')
61+
r = self.webhost.request('GET', 'get_eventhub_batch_triggered')
6962

70-
# Waiting for the blob get updated with the latest data from the
71-
# eventhub output binding
72-
time.sleep(2)
73-
self.assertEqual(r.status_code, 200)
74-
entries = r.json()
75-
for entry in entries:
76-
self.assertEqual(entry['PartitionKey'], partition_key)
77-
row_key = entry['RowKey']
78-
row_keys_seen[row_key] = True
63+
# Waiting for the blob get updated with the latest data from the
64+
# eventhub output binding
65+
time.sleep(2)
66+
self.assertEqual(r.status_code, 200)
67+
entries = r.json()
68+
for entry in entries:
69+
self.assertEqual(entry['PartitionKey'], partition_key)
70+
row_key = entry['RowKey']
71+
row_keys_seen[row_key] = True
7972

80-
self.assertDictEqual(all_row_keys_seen, row_keys_seen)
81-
finally:
82-
self._cleanup(old_partition_key)
73+
self.assertDictEqual(all_row_keys_seen, row_keys_seen)
8374

8475
def test_eventhub_multiple_with_metadata(self):
8576
# Generate a unique event body for EventHub event
@@ -142,36 +133,6 @@ def test_eventhub_multiple_with_metadata(self):
142133
self.assertGreaterEqual(sys_props['SequenceNumber'], 0)
143134
self.assertIsNotNone(sys_props['Offset'])
144135

145-
def _cleanup(self, old_partition_key):
146-
self._set_table_partition_key(old_partition_key)
147-
148-
def _get_table_partition_key(self):
149-
func_dict = self._get_table_function_json_dict()
150-
partition_key = func_dict['bindings'][1]['partitionKey']
151-
return partition_key
152-
153-
def _set_table_partition_key(self, partition_key):
154-
full_json_path = self._get_table_function_json_path()
155-
156-
func_dict = self._get_table_function_json_dict()
157-
func_dict['bindings'][1]['partitionKey'] = partition_key
158-
159-
with open(full_json_path, 'w') as f:
160-
json.dump(func_dict, f, indent=2)
161-
162-
def _get_table_function_json_dict(self):
163-
full_json_path = self._get_table_function_json_path()
164-
165-
with open(full_json_path, 'r') as f:
166-
func_dict = json.load(f)
167-
168-
return func_dict
169-
170-
def _get_table_function_json_path(self):
171-
script_dir = pathlib.Path(self.get_script_dir())
172-
json_path = pathlib.Path('get_eventhub_batch_triggered/function.json')
173-
return testutils.TESTS_ROOT / script_dir / json_path
174-
175136

176137
@skipIf(is_envvar_true(DEDICATED_DOCKER_TEST)
177138
or is_envvar_true(CONSUMPTION_DOCKER_TEST),
@@ -191,7 +152,7 @@ def get_libraries_to_install(cls):
191152

192153
def test_eventhub_multiple(self):
193154
NUM_EVENTS = 3
194-
all_row_keys_seen = dict([(str(i), True) for i in range(NUM_EVENTS)])
155+
all_row_keys_seen = dict([(i, True) for i in range(NUM_EVENTS)])
195156
partition_key = str(round(time.time()))
196157

197158
docs = []
@@ -203,7 +164,7 @@ def test_eventhub_multiple(self):
203164
data=json.dumps(docs))
204165
self.assertEqual(r.status_code, 200)
205166

206-
row_keys = [str(i) for i in range(NUM_EVENTS)]
167+
row_keys = [i for i in range(NUM_EVENTS)]
207168
seen = [False] * NUM_EVENTS
208169
row_keys_seen = dict(zip(row_keys, seen))
209170

@@ -212,7 +173,7 @@ def test_eventhub_multiple(self):
212173

213174
r = self.webhost.request(
214175
'GET',
215-
f'get_eventhub_batch_triggered/{partition_key}')
176+
'get_eventhub_batch_triggered')
216177
self.assertEqual(r.status_code, 200)
217178
entries = r.json()
218179
for entry in entries:

tests/endtoend/test_table_functions.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_script_dir(cls):
2424
def test_table_bindings(self):
2525
out_resp = self.webhost.request('POST', 'table_out_binding')
2626
self.assertEqual(out_resp.status_code, 200)
27-
row_key = out_resp.headers['rowKey']
27+
row_key = json.loads(out_resp.text)['RowKey']
2828

2929
script_dir = pathlib.Path(self.get_script_dir())
3030
json_path = pathlib.Path('table_in_binding/function.json')
@@ -42,8 +42,12 @@ def test_table_bindings(self):
4242

4343
in_resp = self.webhost.request('GET', 'table_in_binding')
4444
self.assertEqual(in_resp.status_code, 200)
45-
in_row_key = in_resp.headers['rowKey']
46-
self.assertEqual(in_row_key, row_key)
45+
row_key_present = False
46+
for row in json.loads(in_resp.text):
47+
if row["RowKey"] == row_key:
48+
row_key_present = True
49+
break
50+
self.assertTrue(row_key_present)
4751

4852

4953
@skipIf(is_envvar_true(DEDICATED_DOCKER_TEST)
@@ -60,12 +64,16 @@ def get_script_dir(cls):
6064
def test_table_bindings(self):
6165
out_resp = self.webhost.request('POST', 'table_out_binding')
6266
self.assertEqual(out_resp.status_code, 200)
63-
row_key = out_resp.headers['rowKey']
67+
row_key = json.loads(out_resp.text)['RowKey']
6468

6569
in_resp = self.webhost.request('GET', f'table_in_binding/{row_key}')
6670
self.assertEqual(in_resp.status_code, 200)
67-
in_row_key = in_resp.headers['rowKey']
68-
self.assertEqual(in_row_key, row_key)
71+
row_key_present = False
72+
for row in json.loads(in_resp.text):
73+
if row["RowKey"] == row_key:
74+
row_key_present = True
75+
break
76+
self.assertTrue(row_key_present)
6977

7078

7179
@skipIf(is_envvar_true(DEDICATED_DOCKER_TEST)

tests/utils/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Version="4.0.1" />
3636
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask"
3737
Version="2.13.2" />
38-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables"
38+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables"
3939
Version="1.2.1" />
4040
<PackageReference Include="System.Drawing.Common"
4141
Version="4.7.3" />

tests/utils/testutils_lc.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ def _encrypt_context(cls, encryption_key: str, plain_text: str) -> str:
278278
return f'{iv_base64}.{encrypted_base64}.{key_sha256_base64}'
279279

280280
def __enter__(self):
281-
mesh_image = (os.environ.get(_CUSTOM_IMAGE) or
282-
self._find_latest_mesh_image(self._host_version, self._py_version))
281+
mesh_image = (os.environ.get(_CUSTOM_IMAGE)
282+
or self._find_latest_mesh_image(self._host_version,
283+
self._py_version))
283284
self.spawn_container(image=mesh_image)
284285
return self
285286

0 commit comments

Comments
 (0)