Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
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));
}
15 changes: 15 additions & 0 deletions samples/samples-csharpscript/TriggerBindingSamples/host.json
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)"
}
}
13 changes: 0 additions & 13 deletions test/Integration/SqlTriggerBindingIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public SqlTriggerBindingIntegrationTests(ITestOutputHelper output = null) : base
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task SingleOperationTriggerTest(SupportedLanguages lang)
{
this.SetChangeTrackingForTable("Products");
Expand Down Expand Up @@ -80,7 +79,6 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task BatchSizeOverrideTriggerTest(SupportedLanguages lang)
{
// Use enough items to require 4 batches to be processed but then
Expand Down Expand Up @@ -124,7 +122,6 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MaxBatchSizeOverrideTriggerTest(SupportedLanguages lang)
{
// Use enough items to require 4 batches to be processed but then
Expand Down Expand Up @@ -168,7 +165,6 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task PollingIntervalOverrideTriggerTest(SupportedLanguages lang)
{
const int firstId = 1;
Expand Down Expand Up @@ -211,7 +207,6 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MultiOperationTriggerTest(SupportedLanguages lang)
{
int firstId = 1;
Expand Down Expand Up @@ -293,7 +288,6 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MultiFunctionTriggerTest(SupportedLanguages lang)
{
const string Trigger1Changes = "Trigger1 Changes: ";
Expand Down Expand Up @@ -420,7 +414,6 @@ public async Task MultiFunctionTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MultiHostTriggerTest(SupportedLanguages lang)
{
this.SetChangeTrackingForTable("Products");
Expand Down Expand Up @@ -472,7 +465,6 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void TableNotPresentTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -487,7 +479,6 @@ public void TableNotPresentTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void PrimaryKeyNotCreatedTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -503,7 +494,6 @@ public void PrimaryKeyNotCreatedTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void ReservedPrimaryKeyColumnNamesTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -519,7 +509,6 @@ public void ReservedPrimaryKeyColumnNamesTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void UnsupportedColumnTypesTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -535,7 +524,6 @@ public void UnsupportedColumnTypesTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void ChangeTrackingNotEnabledTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand Down Expand Up @@ -572,7 +560,6 @@ public async void GetMetricsTest()
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void UnsupportedDatabaseThrows(SupportedLanguages lang)
{
// Change database compat level to unsupported version
Expand Down
2 changes: 2 additions & 0 deletions test/Integration/test-csx/Common/Product.csx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Linq;
using System.IO;
Comment thread
MaddyDev marked this conversation as resolved.
Outdated
using System.Text;

public class Product
{
Expand Down
75 changes: 75 additions & 0 deletions test/Integration/test-csx/Common/utils.csx
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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)
Comment thread
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);
}
}
}
12 changes: 12 additions & 0 deletions test/Integration/test-csx/MultiFunctionTrigger1/function.json
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
}
15 changes: 15 additions & 0 deletions test/Integration/test-csx/MultiFunctionTrigger1/run.csx
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));
}
12 changes: 12 additions & 0 deletions test/Integration/test-csx/MultiFunctionTrigger2/function.json
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
}
15 changes: 15 additions & 0 deletions test/Integration/test-csx/MultiFunctionTrigger2/run.csx
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
}
14 changes: 14 additions & 0 deletions test/Integration/test-csx/PrimaryKeyNotPresentTrigger/run.csx
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
}
21 changes: 21 additions & 0 deletions test/Integration/test-csx/ProductsTriggerWithValidation/run.csx
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
}
Loading