Skip to content

Commit f997fce

Browse files
committed
Improve Acrolinx score for SQL input, Python unit testing
1 parent dc5c989 commit f997fce

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

articles/azure-functions/functions-bindings-azure-sql-input.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ def main(req: func.HttpRequest, todoItem: func.SqlRowList) -> func.HttpResponse:
981981
<a id="http-trigger-delete-one-or-multiple-rows-python"></a>
982982
### HTTP trigger, delete rows
983983

984-
The following example shows a SQL input binding in a function.json file and a Python function that is [triggered by an HTTP](./functions-bindings-http-webhook-trigger.md) reques. It executes a stored procedure with input from the HTTP request query parameter.
984+
The following example shows a SQL input binding in a function.json file and a Python function that is [triggered by an HTTP](./functions-bindings-http-webhook-trigger.md) request. It executes a stored procedure with input from the HTTP request query parameter.
985985

986986
The stored procedure `dbo.DeleteToDo` must be created on the database. In this example, the stored procedure deletes a single record or all records depending on the value of the parameter.
987987

@@ -1078,7 +1078,7 @@ The [C# library](functions-dotnet-class-library.md) uses the [SqlAttribute](http
10781078
| **CommandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
10791079
| **ConnectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. |
10801080
| **CommandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is [Text](/dotnet/api/system.data.commandtype#fields) for a query and [StoredProcedure](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
1081-
| **Parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1081+
| **Parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. The parameter name and the parameter value cannot contain a comma (`,`) or an equals sign (`=`). |
10821082

10831083
::: zone-end
10841084

@@ -1093,7 +1093,7 @@ In the [Java functions runtime library](/java/api/overview/azure/functions/runti
10931093
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. |
10941094
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is ["Text"](/dotnet/api/system.data.commandtype#fields) for a query and ["StoredProcedure"](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
10951095
|**name** | Required. The unique name of the function binding. |
1096-
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1096+
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. The parameter name and the parameter value cannot contain a comma (`,`) or an equals sign (`=`). |
10971097

10981098
::: zone-end
10991099
::: zone pivot="programming-language-javascript,programming-language-typescript"
@@ -1109,7 +1109,7 @@ The following table explains the properties that you can set on the `options` ob
11091109
| **commandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
11101110
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
11111111
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is [Text](/dotnet/api/system.data.commandtype#fields) for a query and [StoredProcedure](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
1112-
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1112+
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. The parameter name and the parameter value cannot contain a comma (`,`) or an equals sign (`=`). |
11131113

11141114
# [Model v3](#tab/nodejs-v3)
11151115

@@ -1123,7 +1123,7 @@ The following table explains the binding configuration properties that you set i
11231123
| **commandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
11241124
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
11251125
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is [Text](/dotnet/api/system.data.commandtype#fields) for a query and [StoredProcedure](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
1126-
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1126+
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. The parameter name and the parameter value cannot contain a comma (`,`) or an equals sign (`=`). |
11271127

11281128
---
11291129

@@ -1141,7 +1141,7 @@ The following table explains the binding configuration properties that you set i
11411141
| **commandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
11421142
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
11431143
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is [Text](/dotnet/api/system.data.commandtype#fields) for a query and [StoredProcedure](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
1144-
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
1144+
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. The parameter name and the parameter value cannot contain a comma (`,`) or an equals sign (`=`). |
11451145
::: zone-end
11461146

11471147

@@ -1155,7 +1155,7 @@ The binding definition includes the SQL command text, the command type, paramete
11551155

11561156
Queries executed by the input binding are [parameterized](/dotnet/api/microsoft.data.sqlclient.sqlparameter) in Microsoft.Data.SqlClient to reduce the risk of [SQL injection](/sql/relational-databases/security/sql-injection) from the parameter values passed into the binding.
11571157

1158-
If an exception occurs when a SQL input binding is executed then the function code won't execute. This may result in an error code being returned, such as an HTTP trigger returning a 500 error code.
1158+
If an exception occurs when a SQL input binding is executed, then the function code does not execute. This behavior may result in an error code being returned, such as an HTTP trigger returning a 500 error code.
11591159

11601160
## Next steps
11611161

articles/azure-functions/functions-reference-python.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ func azure functionapp publish <APP_NAME> --no-build
11751175
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
11761176

11771177
## Unit testing
1178+
### Unit testing through pytest
11781179

11791180
Functions that are written in Python can be tested like other Python code by using standard testing frameworks. For most bindings, it's possible to create a mock input object by creating an instance of an appropriate class from the `azure.functions` package. Since the [`azure.functions`](https://pypi.org/project/azure-functions/) package isn't immediately available, be sure to install it via your *requirements.txt* file as described in the [package management](#package-management) section above.
11801181

@@ -1340,6 +1341,24 @@ class TestFunction(unittest.TestCase):
13401341

13411342
Inside your *.venv* Python virtual environment folder, install your favorite Python test framework, such as `pip install pytest`. Then run `pytest tests` to check the test result.
13421343

1344+
### Unit testing by invoking the function directly
1345+
With `azure-functions >= 1.21.0`, functions can also be called directly using the Python interpreter. This example shows how to unit test an HTTP trigger using the v2 programming model:
1346+
```python
1347+
# <project_root>/function_app.py
1348+
import azure.functions as func
1349+
import logging
1350+
1351+
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
1352+
1353+
@app.route(route="http_trigger")
1354+
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
1355+
return "Hello, World!"
1356+
1357+
print(http_trigger(None))
1358+
```
1359+
1360+
Note that with this approach, no additional package or setup is required. The function can be tested by calling `python function_app.py`, and it results in `Hello, World!` output in the terminal.
1361+
13431362
::: zone-end
13441363

13451364
## Temporary files

0 commit comments

Comments
 (0)