Skip to content

Commit 29aefda

Browse files
authored
Merge branch 'dev' into hallvictoria/sdk-bindings
2 parents f9e6952 + 4c8fb7b commit 29aefda

File tree

7 files changed

+111
-9
lines changed

7 files changed

+111
-9
lines changed

tests/endtoend/sql_functions/sql_input/function.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"direction": "in",
99
"methods": [
1010
"get"
11-
]
11+
],
12+
"route": "sql_input/{productid}"
1213
},
1314
{
1415
"name": "$return",
@@ -19,8 +20,9 @@
1920
"name": "products",
2021
"type": "sql",
2122
"direction": "in",
22-
"commandText": "SELECT * FROM Products",
23+
"commandText": "SELECT * FROM Products WHERE ProductId = @ProductId",
2324
"commandType": "Text",
25+
"parameters": "@ProductId={productid}",
2426
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
2527
}
2628
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
import azure.functions as func
6+
7+
8+
def main(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse:
9+
rows = list(map(lambda r: json.loads(r.to_json()), products))
10+
11+
return func.HttpResponse(
12+
json.dumps(rows),
13+
status_code=200,
14+
mimetype="application/json"
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
"bindings": [
4+
{
5+
"authLevel": "anonymous",
6+
"name": "req",
7+
"type": "httpTrigger",
8+
"direction": "in",
9+
"methods": [
10+
"get"
11+
],
12+
"route": "sql_input2/{productid}"
13+
},
14+
{
15+
"name": "$return",
16+
"type": "http",
17+
"direction": "out"
18+
},
19+
{
20+
"name": "products",
21+
"type": "sql",
22+
"direction": "in",
23+
"commandText": "SELECT * FROM Products2 WHERE ProductId = @ProductId",
24+
"commandType": "Text",
25+
"parameters": "@ProductId={productid}",
26+
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
27+
}
28+
]
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import json
4+
import azure.functions as func
5+
6+
7+
def main(changes, r: func.Out[func.SqlRow]) -> str:
8+
row = func.SqlRow.from_dict(json.loads(changes)[0]["Item"])
9+
r.set(row)
10+
return "OK"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
"disabled": false,
4+
"bindings": [
5+
{
6+
"name": "changes",
7+
"type": "sqlTrigger",
8+
"direction": "in",
9+
"tableName": "dbo.Products",
10+
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
11+
},
12+
{
13+
"name": "r",
14+
"type": "sql",
15+
"direction": "out",
16+
"commandText": "[dbo].[Products2]",
17+
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
18+
}
19+
]
20+
}

tests/endtoend/test_sql_functions.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
import json
4-
from unittest import skip
4+
import time
55

66
from tests.utils import testutils
77

88

9-
@skip("Unskip when azure functions with SQL is released.")
109
class TestSqlFunctions(testutils.WebHostTestCase):
1110

1211
@classmethod
1312
def get_script_dir(cls):
1413
return testutils.E2E_TESTS_FOLDER / 'sql_functions'
1514

1615
@testutils.retryable_test(3, 5)
17-
def test_sql_output_and_input(self):
18-
row = {"ProductId": 0, "Name": "test", "Cost": 100}
16+
def test_sql_binding_trigger(self):
17+
id = str(round(time.time()))
18+
row = {"ProductId": id, "Name": "test", "Cost": 100}
19+
# Insert a row into Products table using sql_output function
1920
r = self.webhost.request('POST', 'sql_output',
2021
data=json.dumps(row))
2122
self.assertEqual(r.status_code, 201)
2223

23-
r = self.webhost.request('GET', 'sql_input')
24+
# Check that the row was successfully inserted using sql_input function
25+
r = self.webhost.request('GET', 'sql_input/' + id)
2426
self.assertEqual(r.status_code, 200)
25-
expectedText = "[{\"ProductId\": 0, \"Name\": \"test\", \"Cost\": 100}]"
27+
expectedText = "[{\"ProductId\": " + id + \
28+
", \"Name\": \"test\", \"Cost\": 100}]"
2629
self.assertEqual(r.text, expectedText)
30+
31+
# Check that the sql_trigger function has been triggered and
32+
# the row has been inserted into Products2 table using sql_input2
33+
# function
34+
max_retries = 10
35+
36+
for try_no in range(max_retries):
37+
# Allow trigger to fire
38+
time.sleep(2)
39+
40+
try:
41+
# Check that the trigger has fired
42+
r = self.webhost.request('GET', 'sql_input2/' + id)
43+
self.assertEqual(r.status_code, 200)
44+
expectedText = "[{\"ProductId\": " + id + \
45+
", \"Name\": \"test\", \"Cost\": 100}]"
46+
self.assertEqual(r.text, expectedText)
47+
48+
except AssertionError:
49+
if try_no == max_retries - 1:
50+
raise
51+
else:
52+
break

tests/utils/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus"
7575
Version="4.2.1" />
7676
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Sql"
77-
Version="0.1.346-preview" />
77+
Version="3.0.534" />
7878
<PackageReference
7979
Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator"
8080
Version="1.1.3" />

0 commit comments

Comments
 (0)