-
Notifications
You must be signed in to change notification settings - Fork 67
add csx trigger samples #806
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
Changes from 6 commits
164aef7
9c8ab5f
ca758fe
ce46d27
5b806b7
ae312e5
2f509ea
c752af4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "recommendations": [ | ||
| "ms-azuretools.vscode-azurefunctions" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "dbo.Products", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #load "../../Common/product.csx" | ||
| #r "Newtonsoft.Json" | ||
| #r "Microsoft.Azure.WebJobs.Extensions.Sql" | ||
|
|
||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
|
||
| public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log) | ||
| { | ||
| log.LogInformation("SQL Changes: " + JsonConvert.SerializeObject(changes)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "version": "2.0", | ||
| "logging": { | ||
| "applicationInsights": { | ||
| "samplingSettings": { | ||
| "isEnabled": true, | ||
| "excludedTypes": "Request" | ||
| } | ||
| } | ||
| }, | ||
| "extensionBundle": { | ||
| "id": "Microsoft.Azure.Functions.ExtensionBundle.Preview", | ||
| "version": "[4.*, 5.0.0)" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.IO; | ||
| using System.Text; | ||
|
|
||
| public class Product | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this file materially different from utils.cs elsewhere in test code? Can it be reused?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's testutils and the functions in use aren't there, so added a new one here - but I finally figured out on how to use the utils from extension itself. |
||
|
|
||
| #r "Newtonsoft.Json" | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.IO; | ||
| using System.Text; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Serialization; | ||
|
|
||
| public static class Utils | ||
| { | ||
| /// <summary> | ||
| /// Default JSON serializer settings to use | ||
| /// </summary> | ||
| private static readonly JsonSerializerSettings _defaultJsonSerializationSettings; | ||
|
|
||
| static Utils() | ||
| { | ||
| _defaultJsonSerializationSettings = new JsonSerializerSettings | ||
| { | ||
| ContractResolver = new DefaultContractResolver() | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serializes the specified object into a JSON string. | ||
| /// </summary> | ||
| /// <param name="obj">The object to serialize</param> | ||
| /// <param name="settings">The specific settings to use, uses a simple set of default settings if not specified</param> | ||
| /// <returns>The serialized JSON string</returns> | ||
| /// <remarks>This will NOT use any global settings to avoid picking up changes that may have been made by other code running in the host (such as user functions)</remarks> | ||
| public static string JsonSerializeObject(object obj, JsonSerializerSettings settings = null) | ||
|
Charles-Gagnon marked this conversation as resolved.
Outdated
|
||
| { | ||
| settings = settings ?? _defaultJsonSerializationSettings; | ||
| // Following the Newtonsoft implementation in JsonConvert of creating a new JsonSerializer each time. | ||
| // https://github.com/JamesNK/Newtonsoft.Json/blob/57025815e564d36821acf778e2c00d02225aab35/Src/Newtonsoft.Json/JsonConvert.cs#L612 | ||
| // If performance ends up being an issue could look into creating a single instance of the serializer for each setting. | ||
| var serializer = JsonSerializer.Create(settings); | ||
| // 256 is value used by Newtonsoft by default - helps avoid having to expand it too many times for larger strings | ||
| // https://github.com/JamesNK/Newtonsoft.Json/blob/57025815e564d36821acf778e2c00d02225aab35/Src/Newtonsoft.Json/JsonConvert.cs#L659 | ||
| var sb = new StringBuilder(256); | ||
| var sw = new StringWriter(sb); | ||
| using (JsonWriter writer = new JsonTextWriter(sw)) | ||
| { | ||
| serializer.Serialize(writer, obj); | ||
| return sb.ToString(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Deserializes the JSON string into an instance of the specified type | ||
| /// </summary> | ||
| /// <typeparam name="T">The type to deserialize into</typeparam> | ||
| /// <param name="json">The string containing the JSON</param> | ||
| /// <param name="settings">The specific settings to use, uses a simple set of default settings if not specified</param> | ||
| /// <returns>The instance of T being deserialized</returns> | ||
| /// <remarks>This will NOT use any global settings to avoid picking up changes that may have been made by other code running in the host (such as user functions)</remarks> | ||
| public static T JsonDeserializeObject<T>(string json, JsonSerializerSettings settings = null) | ||
| { | ||
| settings = settings ?? _defaultJsonSerializationSettings; | ||
| // Following the Newtonsoft implementation in JsonConvert of creating a new JsonSerializer each time. | ||
| // https://github.com/JamesNK/Newtonsoft.Json/blob/57025815e564d36821acf778e2c00d02225aab35/Src/Newtonsoft.Json/JsonConvert.cs#L821 | ||
| // If performance ends up being an issue could look into creating a single instance of the serializer for each setting. | ||
| var serializer = JsonSerializer.Create(settings); | ||
| using (JsonReader reader = new JsonTextReader(new StringReader(json))) | ||
| { | ||
| return serializer.Deserialize<T>(reader); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "dbo.Products", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #load "../Common/Product.csx" | ||
| #load "../Common/utils.csx" | ||
| #r "Newtonsoft.Json" | ||
| #r "Microsoft.Azure.WebJobs.Extensions.Sql" | ||
|
|
||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
|
||
| public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log) | ||
| { | ||
| log.LogInformation("Trigger1 Changes: " + Utils.JsonSerializeObject(changes)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "dbo.Products", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #load "../Common/Product.csx" | ||
| #load "../Common/utils.csx" | ||
| #r "Newtonsoft.Json" | ||
| #r "Microsoft.Azure.WebJobs.Extensions.Sql" | ||
|
|
||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
|
||
| public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log) | ||
| { | ||
| log.LogInformation("Trigger2 Changes: " + Utils.JsonSerializeObject(changes)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "dbo.ProductsWithoutPrimaryKey", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #load "../Common/Product.csx" | ||
| #r "Newtonsoft.Json" | ||
| #r "Microsoft.Azure.WebJobs.Extensions.Sql" | ||
|
|
||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
|
||
| public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log) | ||
| { | ||
| throw new NotImplementedException("Associated test case should fail before the function is invoked."); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "dbo.Products", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #load "../Common/Product.csx" | ||
| #load "../Common/utils.csx" | ||
| #r "Newtonsoft.Json" | ||
| #r "Microsoft.Azure.WebJobs.Extensions.Sql" | ||
|
|
||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
|
||
| public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log) | ||
| { | ||
| string expectedMaxBatchSize = Environment.GetEnvironmentVariable("TEST_EXPECTED_MAX_BATCH_SIZE"); | ||
| if (!string.IsNullOrEmpty(expectedMaxBatchSize) && int.Parse(expectedMaxBatchSize) != changes.Count) | ||
| { | ||
| throw new Exception($"Invalid max batch size, got {changes.Count} changes but expected {expectedMaxBatchSize}"); | ||
| } | ||
| // The output is used to inspect the trigger binding parameter in test methods. | ||
| log.LogInformation("SQL Changes: " + Utils.JsonSerializeObject(changes)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "dbo.ProductsWithReservedPrimaryKeyColumnNames", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.