Skip to content

Commit 3d69d36

Browse files
committed
add sql trigger e2e test
1 parent 82b988e commit 3d69d36

File tree

6 files changed

+108
-8
lines changed

6 files changed

+108
-8
lines changed

tests/endtoend/sql_functions/sql_input/function.json

Lines changed: 4 additions & 2 deletions
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
]
Lines changed: 15 additions & 0 deletions
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+
)
Lines changed: 29 additions & 0 deletions
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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)
Lines changed: 20 additions & 0 deletions
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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
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 function
33+
max_retries = 10
34+
35+
for try_no in range(max_retries):
36+
# Allow trigger to fire
37+
time.sleep(2)
38+
39+
try:
40+
# Check that the trigger has fired
41+
r = self.webhost.request('GET', 'sql_input2/' + id)
42+
self.assertEqual(r.status_code, 200)
43+
expectedText = "[{\"ProductId\": " + id + \
44+
", \"Name\": \"test\", \"Cost\": 100}]"
45+
self.assertEqual(r.text, expectedText)
46+
47+
except AssertionError:
48+
if try_no == max_retries - 1:
49+
raise
50+
else:
51+
break

0 commit comments

Comments
 (0)