Skip to content

Commit abe38e5

Browse files
authored
add [AutoResolve] for reading from app setting (#208)
* add [AutoResolve] for reading from app setting * add sample * add test * update test * fix test
1 parent 9fa9008 commit abe38e5

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

samples/Common/ProductUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static List<Product> GetNewProducts(int num)
1919
var product = new Product
2020
{
2121
ProductID = i,
22-
Cost = 100,
22+
Cost = 100 * i,
2323
Name = "test"
2424
};
2525
products.Add(product);

samples/GlobalSuppressions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Unused parameter is required by functions binding", Scope = "member", Target = "~M:Microsoft.Azure.WebJobs.Extensions.Sql.Samples.OutputBindingSamples.AddProductsCollector.Run(Microsoft.AspNetCore.Http.HttpRequest,Microsoft.Azure.WebJobs.ICollector{Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common.Product})~Microsoft.AspNetCore.Mvc.IActionResult")]
2121
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Unused parameter is required by functions binding", Scope = "member", Target = "~M:Microsoft.Azure.WebJobs.Extensions.Sql.Samples.OutputBindingSamples.TimerTriggerProducts.Run(Microsoft.Azure.WebJobs.TimerInfo,Microsoft.Extensions.Logging.ILogger,Microsoft.Azure.WebJobs.ICollector{Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common.Product})")]
2222
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Unused parameter is required by functions binding", Scope = "member", Target = "~M:Microsoft.Azure.WebJobs.Extensions.Sql.Samples.InputBindingSamples.GetProducts.Run(Microsoft.AspNetCore.Http.HttpRequest,System.Collections.Generic.IEnumerable{Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common.Product})~Microsoft.AspNetCore.Mvc.IActionResult")]
23-
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Unused parameter is required by functions binding", Scope = "member", Target = "~M:Microsoft.Azure.WebJobs.Extensions.Sql.Samples.InputBindingSamples.GetProductNamesView.Run(Microsoft.AspNetCore.Http.HttpRequest,System.Collections.Generic.IEnumerable{Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common.ProductName})~Microsoft.AspNetCore.Mvc.IActionResult")]
23+
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Unused parameter is required by functions binding", Scope = "member", Target = "~M:Microsoft.Azure.WebJobs.Extensions.Sql.Samples.InputBindingSamples.GetProductNamesView.Run(Microsoft.AspNetCore.Http.HttpRequest,System.Collections.Generic.IEnumerable{Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common.ProductName})~Microsoft.AspNetCore.Mvc.IActionResult")]
24+
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Unused parameter is required by functions binding", Scope = "member", Target = "~M:Microsoft.Azure.WebJobs.Extensions.Sql.Samples.InputBindingSamples.GetProductsStoredProcedureFromAppSetting.Run(Microsoft.AspNetCore.Http.HttpRequest,System.Collections.Generic.IEnumerable{Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common.Product})~Microsoft.AspNetCore.Mvc.IActionResult")]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Azure.WebJobs.Extensions.Http;
8+
using Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common;
9+
10+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Samples.InputBindingSamples
11+
{
12+
/// <summary>
13+
/// This shows an example of a SQL Input binding that uses a stored procedure
14+
/// from an app setting value to query for Products with a specific cost that is also defined as an app setting value.
15+
/// </summary>
16+
public static class GetProductsStoredProcedureFromAppSetting
17+
{
18+
[FunctionName("GetProductsStoredProcedureFromAppSetting")]
19+
public static IActionResult Run(
20+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproductsbycost")]
21+
HttpRequest req,
22+
[Sql("%Sp_SelectCost%",
23+
CommandType = System.Data.CommandType.StoredProcedure,
24+
Parameters = "@Cost=%ProductCost%",
25+
ConnectionStringSetting = "SqlConnectionString")]
26+
IEnumerable<Product> products)
27+
{
28+
return new OkObjectResult(products);
29+
}
30+
}
31+
}

samples/local.settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"Values": {
44
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
55
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
6-
"SqlConnectionString": ""
6+
"SqlConnectionString": "",
7+
"Sp_SelectCost": "SelectProductsCost",
8+
"ProductCost": 100
79
}
810
}

src/SqlAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public SqlAttribute(string commandText)
4040
/// For an input binding, either a SQL query or stored procedure that will be run in the database referred to in the ConnectionString.
4141
/// For an output binding, the table name.
4242
/// </summary>
43+
[AutoResolve]
4344
public string CommandText { get; }
4445

4546
/// <summary>

test/Integration/SqlInputBindingIntegrationTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ public async void GetProductsNameEmptyTest(int n, int cost)
9898
Assert.Equal(expectedResponse, actualResponse, StringComparer.OrdinalIgnoreCase);
9999
}
100100

101+
[Fact]
102+
public async void GetProductsByCostTest()
103+
{
104+
this.StartFunctionHost(nameof(GetProductsStoredProcedureFromAppSetting));
105+
106+
// Generate T-SQL to insert n rows of data with cost
107+
Product[] products = GetProducts(3, 100);
108+
this.InsertProducts(products);
109+
Product[] productsWithCost100 = GetProducts(1, 100);
110+
111+
// Run the function
112+
HttpResponseMessage response = await this.SendInputRequest("getproductsbycost");
113+
114+
// Verify result
115+
string expectedResponse = JsonConvert.SerializeObject(productsWithCost100);
116+
string actualResponse = await response.Content.ReadAsStringAsync();
117+
118+
Assert.Equal(expectedResponse, actualResponse, StringComparer.OrdinalIgnoreCase);
119+
}
120+
101121
[Fact]
102122
public async void GetProductNamesViewTest()
103123
{
@@ -132,6 +152,21 @@ private static Product[] GetProductsWithSameCost(int n, int cost)
132152
return result;
133153
}
134154

155+
private static Product[] GetProducts(int n, int cost)
156+
{
157+
var result = new Product[n];
158+
for (int i = 1; i <= n; i++)
159+
{
160+
result[i - 1] = new Product
161+
{
162+
ProductID = i,
163+
Name = "test",
164+
Cost = cost * i
165+
};
166+
}
167+
return result;
168+
}
169+
135170
private static Product[] GetProductsWithSameCostAndName(int n, int cost, string name, int offset = 0)
136171
{
137172
var result = new Product[n];

0 commit comments

Comments
 (0)