-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSqlOutputBindingIntegrationTests.cs
472 lines (417 loc) · 22.1 KB
/
SqlOutputBindingIntegrationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Azure.WebJobs.Extensions.Sql.Samples.OutputBindingSamples;
using Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common;
using Xunit;
using Xunit.Abstractions;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Common;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Integration
{
[Collection(IntegrationTestsCollection.Name)]
public class SqlOutputBindingIntegrationTests : IntegrationTestBase
{
public SqlOutputBindingIntegrationTests(ITestOutputHelper output) : base(output)
{
}
[Theory]
[SqlInlineData(1, "Test", 5)]
[SqlInlineData(0, "", 0)]
[SqlInlineData(-500, "ABCD", 580)]
public void AddProductTest(int id, string name, int cost, SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProduct), lang);
var query = new Dictionary<string, object>()
{
{ "productId", id },
{ "name", name },
{ "cost", cost }
};
this.SendOutputPostRequest("addproduct", JsonConvert.SerializeObject(query)).Wait();
// Verify result
Assert.Equal(name, this.ExecuteScalar($"select Name from Products where ProductId={id}"));
Assert.Equal(cost, this.ExecuteScalar($"select cost from Products where ProductId={id}"));
}
[Theory]
[SqlInlineData(1, "Test", 5)]
[SqlInlineData(0, "", 0)]
[SqlInlineData(-500, "ABCD", 580)]
// Currently Java functions return null when the parameter for name is an empty string
// Issue link: https://github.com/Azure/azure-functions-sql-extension/issues/517
[UnsupportedLanguages(SupportedLanguages.PowerShell, SupportedLanguages.Java)]
public void AddProductParamsTest(int id, string name, int cost, SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductParams), lang);
var query = new Dictionary<string, string>()
{
{ "productId", id.ToString() },
{ "name", name },
{ "cost", cost.ToString() }
};
this.SendOutputGetRequest("addproduct-params", query).Wait();
// Verify result
Assert.Equal(name, this.ExecuteScalar($"select Name from Products where ProductId={id}"));
Assert.Equal(cost, this.ExecuteScalar($"select cost from Products where ProductId={id}"));
}
[Theory]
[SqlInlineData()]
// ProductID (POCO field) does not match ProductId (table column) and JSON
// serialization is case sensitive in Java
// TODO: https://github.com/Azure/azure-functions-sql-extension/issues/411
[UnsupportedLanguages(SupportedLanguages.Java)]
public void AddProductArrayTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductsArray), lang);
// First insert some test data
this.ExecuteNonQuery("INSERT INTO Products VALUES (1, 'test', 100)");
this.ExecuteNonQuery("INSERT INTO Products VALUES (2, 'test', 100)");
this.ExecuteNonQuery("INSERT INTO Products VALUES (3, 'test', 100)");
Product[] prods = new[]
{
new Product()
{
ProductID = 1,
Name = "Cup",
Cost = 2
},
new Product
{
ProductID = 2,
Name = "Glasses",
Cost = 12
}
};
this.SendOutputPostRequest("addproducts-array", JsonConvert.SerializeObject(prods)).Wait();
// Function call changes first 2 rows to (1, 'Cup', 2) and (2, 'Glasses', 12)
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(1) FROM Products WHERE Cost = 100"));
Assert.Equal(2, this.ExecuteScalar("SELECT Cost FROM Products WHERE ProductId = 1"));
Assert.Equal(2, this.ExecuteScalar("SELECT ProductId FROM Products WHERE Cost = 12"));
}
/// <summary>
/// Test compatibility with converting various data types to their respective
/// SQL server types.
/// </summary>
/// <param name="lang">The language to run the test against</param>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.PowerShell, SupportedLanguages.OutOfProc)]
public void AddProductColumnTypesTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductColumnTypes), lang, true);
var queryParameters = new Dictionary<string, string>()
{
{ "productId", "999" }
};
this.SendOutputGetRequest("addproduct-columntypes", queryParameters).Wait();
// If we get here then the test is successful - an exception will be thrown if there were any problems
}
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.JavaScript, SupportedLanguages.PowerShell, SupportedLanguages.Java, SupportedLanguages.OutOfProc)] // Collectors are only available in C#
public void AddProductsCollectorTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductsCollector), lang);
// Function should add 5000 rows to the table
this.SendOutputGetRequest("addproducts-collector").Wait();
Assert.Equal(5000, this.ExecuteScalar("SELECT COUNT(1) FROM Products"));
}
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.OutOfProc)]
public void QueueTriggerProductsTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(QueueTriggerProducts), lang);
string uri = $"http://localhost:{this.Port}/admin/functions/QueueTriggerProducts";
string json = "{ 'input': 'Test Data' }";
this.SendPostRequest(uri, json).Wait();
Thread.Sleep(5000);
// Function should add 100 rows
Assert.Equal(100, this.ExecuteScalar("SELECT COUNT(1) FROM Products"));
}
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.OutOfProc)]
public void TimerTriggerProductsTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(TimerTriggerProducts), lang);
// Since this function runs on a schedule (every 5 seconds), we don't need to invoke it.
// We will wait 6 seconds to guarantee that it has been fired at least once, and check that at least 1000 rows of data has been added.
Thread.Sleep(6000);
int rowsAdded = (int)this.ExecuteScalar("SELECT COUNT(1) FROM Products");
Assert.True(rowsAdded >= 1000);
}
[Theory]
[SqlInlineData()]
public void AddProductExtraColumnsTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductExtraColumns), lang, true);
// Since ProductExtraColumns has columns that does not exist in the table,
// no rows should be added to the table.
Assert.Throws<AggregateException>(() => this.SendOutputGetRequest("addproduct-extracolumns").Wait());
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM Products"));
}
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.OutOfProc)]
public void AddProductMissingColumnsTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductMissingColumns), lang, true);
// Even though the ProductMissingColumns object is missing the Cost column,
// the row should still be added successfully since Cost can be null.
this.SendOutputPostRequest("addproduct-missingcolumns", string.Empty).Wait();
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM Products"));
}
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.OutOfProc)]
public void AddProductMissingColumnsNotNullTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductMissingColumnsExceptionFunction), lang, true);
// Since the Sql table does not allow null for the Cost column,
// inserting a row without a Cost value should throw an Exception.
Assert.Throws<AggregateException>(() => this.SendOutputPostRequest("addproduct-missingcolumnsexception", string.Empty).Wait());
}
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.OutOfProc)]
public void AddProductNoPartialUpsertTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductsNoPartialUpsert), lang, true);
Assert.Throws<AggregateException>(() => this.SendOutputPostRequest("addproducts-nopartialupsert", string.Empty).Wait());
// No rows should be upserted since there was a row with an invalid value
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsNameNotNull"));
}
/// <summary>
/// Tests that for tables with an identity column we are able to insert items.
/// </summary>
[Theory]
[SqlInlineData()]
// Currently PowerShell gives an error due to the deserialization of the object
// Issue link: https://github.com/Azure/azure-functions-sql-extension/issues/448
[UnsupportedLanguages(SupportedLanguages.PowerShell)]
public void AddProductWithIdentity(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductWithIdentityColumn), lang);
// Identity column (ProductID) is left out for new items
var query = new Dictionary<string, string>()
{
{ "name", "MyProduct" },
{ "cost", "1" }
};
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
this.SendOutputGetRequest(nameof(AddProductWithIdentityColumn), query).Wait();
// Product should have been inserted correctly even without an ID when there's an identity column present
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
}
/// <summary>
/// Tests that for tables with an identity column we are able to insert multiple items at once
/// </summary>
[Theory]
[SqlInlineData()]
// Currently PowerShell gives an error due to the deserialization of the object
// Issue link: https://github.com/Azure/azure-functions-sql-extension/issues/448
[UnsupportedLanguages(SupportedLanguages.PowerShell)]
public void AddProductsWithIdentityColumnArray(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductsWithIdentityColumnArray), lang);
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
this.SendOutputGetRequest(nameof(AddProductsWithIdentityColumnArray)).Wait();
// Multiple items should have been inserted
Assert.Equal(2, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
}
/// <summary>
/// Tests that for tables with multiple primary columns (including an identity column) we are able to
/// insert items.
/// </summary>
[Theory]
[SqlInlineData()]
// Currently PowerShell gives an error due to the deserialization of the object
// Issue link: https://github.com/Azure/azure-functions-sql-extension/issues/448
[UnsupportedLanguages(SupportedLanguages.PowerShell)]
public void AddProductWithIdentity_MultiplePrimaryColumns(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductWithMultiplePrimaryColumnsAndIdentity), lang);
var query = new Dictionary<string, string>()
{
{ "externalId", "101" },
{ "name", "MyProduct" },
{ "cost", "1" }
};
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithMultiplePrimaryColumnsAndIdentity"));
this.SendOutputGetRequest(nameof(AddProductWithMultiplePrimaryColumnsAndIdentity), query).Wait();
// Product should have been inserted correctly even without an ID when there's an identity column present
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithMultiplePrimaryColumnsAndIdentity"));
}
/// <summary>
/// Tests that when using a table with an identity column that if the identity column is specified
/// by the function we handle inserting/updating that correctly.
/// </summary>
[Theory]
[SqlInlineData()]
public void AddProductWithIdentity_SpecifyIdentityColumn(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductWithIdentityColumnIncluded), lang);
var query = new Dictionary<string, string>()
{
{ "productId", "1" },
{ "name", "MyProduct" },
{ "cost", "1" }
};
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
this.SendOutputGetRequest(nameof(AddProductWithIdentityColumnIncluded), query).Wait();
// New row should have been inserted
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
query = new Dictionary<string, string>()
{
{ "productId", "1" },
{ "name", "MyProduct2" },
{ "cost", "1" }
};
this.SendOutputGetRequest(nameof(AddProductWithIdentityColumnIncluded), query).Wait();
// Existing row should have been updated
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity WHERE Name='MyProduct2'"));
}
/// <summary>
/// Tests that when using a table with an identity column we can handle a null (missing) identity column
/// </summary>
[Theory]
[SqlInlineData()]
public void AddProductWithIdentity_NoIdentityColumn(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductWithIdentityColumnIncluded), lang);
var query = new Dictionary<string, string>()
{
{ "name", "MyProduct" },
{ "cost", "1" }
};
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
this.SendOutputGetRequest(nameof(AddProductWithIdentityColumnIncluded), query).Wait();
// New row should have been inserted
Assert.Equal(1, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
query = new Dictionary<string, string>()
{
{ "name", "MyProduct2" },
{ "cost", "1" }
};
this.SendOutputGetRequest(nameof(AddProductWithIdentityColumnIncluded), query).Wait();
// Another new row should have been inserted
Assert.Equal(2, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithIdentity"));
}
/// <summary>
/// Tests that when using a table with an identity column along with other primary
/// keys an error is thrown if at least one of the primary keys is missing.
/// </summary>
[Theory]
[SqlInlineData()]
// Currently PowerShell gives an error due to the deserialization of the object
// Issue link: https://github.com/Azure/azure-functions-sql-extension/issues/448
[UnsupportedLanguages(SupportedLanguages.PowerShell)]
public void AddProductWithIdentity_MissingPrimaryColumn(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductWithMultiplePrimaryColumnsAndIdentity), lang);
var query = new Dictionary<string, string>()
{
// Missing externalId
{ "name", "MyProduct" },
{ "cost", "1" }
};
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithMultiplePrimaryColumnsAndIdentity"));
Assert.Throws<AggregateException>(() => this.SendOutputGetRequest(nameof(AddProductWithMultiplePrimaryColumnsAndIdentity), query).Wait());
// Nothing should have been inserted
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithMultiplePrimaryColumnsAndIdentity"));
}
/// <summary>
/// Tests that when using a case sensitive database, an error is thrown if
/// the POCO fields case and column names case do not match.
/// </summary>
[Theory]
[SqlInlineData()]
// JSON serialization is case sensitive in Java
// TODO: https://github.com/Azure/azure-functions-sql-extension/issues/411
[UnsupportedLanguages(SupportedLanguages.Java)]
public void AddProductCaseSensitiveTest(SupportedLanguages lang)
{
// Set table info cache timeout to 0 minutes so that new collation gets picked up
var environmentVariables = new Dictionary<string, string>()
{
{ "AZ_FUNC_TABLE_INFO_CACHE_TIMEOUT_MINUTES", "0" }
};
this.StartFunctionHost(nameof(AddProductParams), lang, false, null, environmentVariables);
// Change database collation to case sensitive
this.ExecuteNonQuery($"ALTER DATABASE {this.DatabaseName} SET Single_User WITH ROLLBACK IMMEDIATE; ALTER DATABASE {this.DatabaseName} COLLATE Latin1_General_CS_AS; ALTER DATABASE {this.DatabaseName} SET Multi_User;");
var query = new Dictionary<string, string>()
{
{ "productId", "1" },
{ "name", "test" },
{ "cost", "100" }
};
// The upsert should fail since the database is case sensitive and the column name "ProductId"
// does not match the POCO field "ProductID"
Assert.Throws<AggregateException>(() => this.SendOutputGetRequest("addproduct-params", query).Wait());
// Change database collation back to case insensitive
this.ExecuteNonQuery($"ALTER DATABASE {this.DatabaseName} SET Single_User WITH ROLLBACK IMMEDIATE; ALTER DATABASE {this.DatabaseName} COLLATE Latin1_General_CI_AS; ALTER DATABASE {this.DatabaseName} SET Multi_User;");
this.SendOutputGetRequest("addproduct-params", query).Wait();
// Verify result
Assert.Equal("test", this.ExecuteScalar($"select Name from Products where ProductId={1}"));
Assert.Equal(100, this.ExecuteScalar($"select cost from Products where ProductId={1}"));
}
/// <summary>
/// Tests that a row is inserted successfully when the object is missing
/// the primary key column with a default value.
/// </summary>
[Theory]
[SqlInlineData()]
// Currently PowerShell gives an unknown error (when testing locally we get a missing primary key error)
// Issue link: https://github.com/Azure/azure-functions-sql-extension/issues/448
[UnsupportedLanguages(SupportedLanguages.PowerShell)]
public void AddProductWithDefaultPKTest(SupportedLanguages lang)
{
this.StartFunctionHost(nameof(AddProductWithDefaultPK), lang);
var product = new Dictionary<string, object>()
{
{ "name", "MyProduct" },
{ "cost", 1 }
};
Assert.Equal(0, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithDefaultPK"));
this.SendOutputPostRequest("addproductwithdefaultpk", JsonConvert.SerializeObject(product)).Wait();
this.SendOutputPostRequest("addproductwithdefaultpk", JsonConvert.SerializeObject(product)).Wait();
Assert.Equal(2, this.ExecuteScalar("SELECT COUNT(*) FROM dbo.ProductsWithDefaultPK"));
}
/// <summary>
/// Tests that when using an unsupported database the expected error is thrown
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.OutOfProc)]
public async Task UnsupportedDatabaseThrows(SupportedLanguages lang)
{
// Change database compat level to unsupported version
this.ExecuteNonQuery($"ALTER DATABASE {this.DatabaseName} SET COMPATIBILITY_LEVEL = 120;");
var foundExpectedMessageSource = new TaskCompletionSource<bool>();
this.StartFunctionHost(nameof(AddProductParams), lang, false, (object sender, DataReceivedEventArgs e) =>
{
if (e.Data.Contains("SQL bindings require a database compatibility level of 130 or higher to function. Current compatibility level = 120"))
{
foundExpectedMessageSource.SetResult(true);
}
});
var query = new Dictionary<string, string>()
{
{ "productId", "1" },
{ "name", "test" },
{ "cost", "100" }
};
// The upsert should fail since the database compat level is not supported
Exception exception = Assert.Throws<AggregateException>(() => this.SendOutputGetRequest("addproduct-params", query).Wait());
// Verify the message contains the expected error so that other errors don't mistakenly make this test pass
// Wait 2sec for message to get processed to account for delays reading output
await foundExpectedMessageSource.Task.TimeoutAfter(TimeSpan.FromMilliseconds(2000), $"Timed out waiting for expected error message");
}
}
}