Skip to content

Add sql trigger e2e test #1375

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 8 commits into from
Mar 28, 2024
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
6 changes: 4 additions & 2 deletions tests/endtoend/sql_functions/sql_input/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"direction": "in",
"methods": [
"get"
]
],
"route": "sql_input/{productid}"
},
{
"name": "$return",
Expand All @@ -19,8 +20,9 @@
"name": "products",
"type": "sql",
"direction": "in",
"commandText": "SELECT * FROM Products",
"commandText": "SELECT * FROM Products WHERE ProductId = @ProductId",
"commandType": "Text",
"parameters": "@ProductId={productid}",
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
}
]
Expand Down
15 changes: 15 additions & 0 deletions tests/endtoend/sql_functions/sql_input2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import azure.functions as func


def main(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse:
rows = list(map(lambda r: json.loads(r.to_json()), products))

return func.HttpResponse(
json.dumps(rows),
status_code=200,
mimetype="application/json"
)
29 changes: 29 additions & 0 deletions tests/endtoend/sql_functions/sql_input2/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get"
],
"route": "sql_input2/{productid}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"name": "products",
"type": "sql",
"direction": "in",
"commandText": "SELECT * FROM Products2 WHERE ProductId = @ProductId",
"commandType": "Text",
"parameters": "@ProductId={productid}",
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
}
]
}
10 changes: 10 additions & 0 deletions tests/endtoend/sql_functions/sql_trigger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
import azure.functions as func


def main(changes, r: func.Out[func.SqlRow]) -> str:
row = func.SqlRow.from_dict(json.loads(changes)[0]["Item"])
r.set(row)
return "OK"
20 changes: 20 additions & 0 deletions tests/endtoend/sql_functions/sql_trigger/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"scriptFile": "__init__.py",
"disabled": false,
"bindings": [
{
"name": "changes",
"type": "sqlTrigger",
"direction": "in",
"tableName": "dbo.Products",
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
},
{
"name": "r",
"type": "sql",
"direction": "out",
"commandText": "[dbo].[Products2]",
"connectionStringSetting": "AzureWebJobsSqlConnectionString"
}
]
}
38 changes: 32 additions & 6 deletions tests/endtoend/test_sql_functions.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
from unittest import skip
import time

from tests.utils import testutils


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

@classmethod
def get_script_dir(cls):
return testutils.E2E_TESTS_FOLDER / 'sql_functions'

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

r = self.webhost.request('GET', 'sql_input')
# Check that the row was successfully inserted using sql_input function
r = self.webhost.request('GET', 'sql_input/' + id)
self.assertEqual(r.status_code, 200)
expectedText = "[{\"ProductId\": 0, \"Name\": \"test\", \"Cost\": 100}]"
expectedText = "[{\"ProductId\": " + id + \
", \"Name\": \"test\", \"Cost\": 100}]"
self.assertEqual(r.text, expectedText)

# Check that the sql_trigger function has been triggered and
# the row has been inserted into Products2 table using sql_input2
# function
max_retries = 10

for try_no in range(max_retries):
# Allow trigger to fire
time.sleep(2)

try:
# Check that the trigger has fired
r = self.webhost.request('GET', 'sql_input2/' + id)
self.assertEqual(r.status_code, 200)
expectedText = "[{\"ProductId\": " + id + \
", \"Name\": \"test\", \"Cost\": 100}]"
self.assertEqual(r.text, expectedText)

except AssertionError:
if try_no == max_retries - 1:
raise
else:
break
2 changes: 1 addition & 1 deletion tests/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus"
Version="4.2.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Sql"
Version="0.1.346-preview" />
Version="3.0.534" />
<PackageReference
Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator"
Version="1.1.3" />
Expand Down
Loading