Skip to content

Commit fe9f5b7

Browse files
Port #3559 to release/6.1
1 parent d2f2f26 commit fe9f5b7

File tree

9 files changed

+120
-82
lines changed

9 files changed

+120
-82
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,6 @@ public static class DataTestUtility
9292
//SQL Server EngineEdition
9393
private static string s_sqlServerEngineEdition;
9494

95-
// Currently, only Azure SQL supports vectors and JSON.
96-
// Our CI images with specific SQL Server versions lag
97-
// behind with vector and JSON support.
98-
// JSON Column type
99-
public static readonly bool IsJsonSupported = !IsNotAzureServer();
100-
// VECTOR column type
101-
public static readonly bool IsVectorSupported = !IsNotAzureServer();
102-
10395
// Azure Synapse EngineEditionId == 6
10496
// More could be read at https://learn.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver16#propertyname
10597
public static bool IsAzureSynapse
@@ -181,7 +173,6 @@ static DataTestUtility()
181173
ManagedIdentitySupported = c.ManagedIdentitySupported;
182174
IsManagedInstance = c.IsManagedInstance;
183175
AliasName = c.AliasName;
184-
IsJsonSupported = c.IsJsonSupported;
185176

186177
#if NETFRAMEWORK
187178
System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
@@ -453,6 +444,11 @@ public static bool IsAADAuthorityURLSetup()
453444
return !string.IsNullOrEmpty(AADAuthorityURL);
454445
}
455446

447+
public static bool IsAzureServer()
448+
{
449+
return AreConnStringsSetup() && Utils.IsAzureSqlServer(new SqlConnectionStringBuilder(TCPConnectionString).DataSource);
450+
}
451+
456452
public static bool IsNotAzureServer()
457453
{
458454
return !AreConnStringsSetup() || !Utils.IsAzureSqlServer(new SqlConnectionStringBuilder(TCPConnectionString).DataSource);

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonBulkCopyTest.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@
77
using Newtonsoft.Json;
88
using Xunit.Abstractions;
99
using Xunit;
10-
using System.Collections;
1110

1211
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SQL.JsonTest
1312
{
1413
public class JsonBulkCopyTest
1514
{
1615
private readonly ITestOutputHelper _output;
17-
private static readonly string _generatedJsonFile = DataTestUtility.GenerateRandomCharacters("randomRecords");
18-
private static readonly string _outputFile = DataTestUtility.GenerateRandomCharacters("serverResults");
19-
private static readonly string _sourceTableName = DataTestUtility.GenerateObjectName();
20-
private static readonly string _destinationTableName = DataTestUtility.GenerateObjectName();
21-
16+
private static readonly string _generatedJsonFile = DataTestUtility.GetUniqueName("randomRecords");
17+
private static readonly string _outputFile = DataTestUtility.GetUniqueName("serverResults");
18+
private static readonly string _sourceTableName = DataTestUtility.GetUniqueName("jsonBulkCopySrcTable", true);
19+
private static readonly string _destinationTableName = DataTestUtility.GetUniqueName("jsonBulkCopyDestTable", true);
20+
2221
public JsonBulkCopyTest(ITestOutputHelper output)
2322
{
2423
_output = output;
2524
}
2625

2726
public static IEnumerable<object[]> JsonBulkCopyTestData()
2827
{
29-
yield return new object[] { CommandBehavior.Default, false, 300, 100 };
30-
yield return new object[] { CommandBehavior.Default, true, 300, 100 };
31-
yield return new object[] { CommandBehavior.SequentialAccess, false, 300, 100 };
32-
yield return new object[] { CommandBehavior.SequentialAccess, true, 300, 100 };
28+
yield return new object[] { CommandBehavior.Default, false, 30, 10 };
29+
yield return new object[] { CommandBehavior.Default, true, 30, 10 };
30+
yield return new object[] { CommandBehavior.SequentialAccess, false, 30, 10 };
31+
yield return new object[] { CommandBehavior.SequentialAccess, true, 30, 10 };
3332
}
3433

3534
private void PopulateData(int noOfRecords, int rows)
@@ -87,7 +86,7 @@ private void PrintJsonDataToFileAndCompare(SqlConnection connection)
8786
try
8887
{
8988
DeleteFile(_outputFile);
90-
using (SqlCommand command = new SqlCommand("SELECT [data] FROM [" + _destinationTableName + "]", connection))
89+
using (SqlCommand command = new SqlCommand("SELECT [data] FROM " + _destinationTableName, connection))
9190
{
9291
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
9392
{
@@ -125,7 +124,7 @@ private async Task PrintJsonDataToFileAndCompareAsync(SqlConnection connection)
125124
try
126125
{
127126
DeleteFile(_outputFile);
128-
using (SqlCommand command = new SqlCommand("SELECT [data] FROM [" + _destinationTableName + "]", connection))
127+
using (SqlCommand command = new SqlCommand("SELECT [data] FROM " + _destinationTableName, connection))
129128
{
130129
using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
131130
{
@@ -159,7 +158,7 @@ private async Task PrintJsonDataToFileAndCompareAsync(SqlConnection connection)
159158

160159
private void StreamJsonFileToServer(SqlConnection connection)
161160
{
162-
using (SqlCommand cmd = new SqlCommand("INSERT INTO [" + _sourceTableName + "] (data) VALUES (@jsondata)", connection))
161+
using (SqlCommand cmd = new SqlCommand("INSERT INTO " + _sourceTableName + " (data) VALUES (@jsondata)", connection))
163162
{
164163
using (StreamReader jsonFile = File.OpenText(_generatedJsonFile))
165164
{
@@ -171,7 +170,7 @@ private void StreamJsonFileToServer(SqlConnection connection)
171170

172171
private async Task StreamJsonFileToServerAsync(SqlConnection connection)
173172
{
174-
using (SqlCommand cmd = new SqlCommand("INSERT INTO [" + _sourceTableName + "] (data) VALUES (@jsondata)", connection))
173+
using (SqlCommand cmd = new SqlCommand("INSERT INTO " + _sourceTableName + " (data) VALUES (@jsondata)", connection))
175174
{
176175
using (StreamReader jsonFile = File.OpenText(_generatedJsonFile))
177176
{
@@ -265,7 +264,7 @@ private async Task BulkCopyDataAsync(CommandBehavior cb, bool enableStraming, in
265264
}
266265
}
267266

268-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
267+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
269268
[MemberData(
270269
nameof(JsonBulkCopyTestData)
271270
#if NETFRAMEWORK
@@ -289,7 +288,7 @@ public void TestJsonBulkCopy(CommandBehavior cb, bool enableStraming, int jsonAr
289288
}
290289
}
291290

292-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
291+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
293292
[MemberData(
294293
nameof(JsonBulkCopyTestData)
295294
#if NETFRAMEWORK

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonStreamTest.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public class JsonRecord
1919
public string Name { get; set; }
2020
}
2121

22-
public class JsonStreamTest
22+
public class JsonStreamTest
2323
{
2424
private readonly ITestOutputHelper _output;
25-
private static readonly string _jsonFile = "randomRecords.json";
26-
private static readonly string _outputFile = "serverRecords.json";
25+
private static readonly string _jsonFile = DataTestUtility.GetUniqueName("randomRecords") + ".json";
26+
private static readonly string _outputFile = DataTestUtility.GetUniqueName("serverRecords") + ".json";
2727

2828
public JsonStreamTest(ITestOutputHelper output)
2929
{
@@ -49,7 +49,7 @@ private void GenerateJsonFile(int noOfRecords, string filename)
4949
string json = JsonConvert.SerializeObject(records, Formatting.Indented);
5050
File.WriteAllText(filename, json);
5151
Assert.True(File.Exists(filename));
52-
_output.WriteLine("Generated JSON file "+filename);
52+
_output.WriteLine("Generated JSON file " + filename);
5353
}
5454

5555
private void CompareJsonFiles()
@@ -157,10 +157,10 @@ private void DeleteFile(string filename)
157157
}
158158
}
159159

160-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
160+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
161161
public void TestJsonStreaming()
162162
{
163-
GenerateJsonFile(10000, _jsonFile);
163+
GenerateJsonFile(1000, _jsonFile);
164164
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
165165
{
166166
connection.Open();
@@ -173,10 +173,10 @@ public void TestJsonStreaming()
173173
}
174174
}
175175

176-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
176+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
177177
public async Task TestJsonStreamingAsync()
178178
{
179-
GenerateJsonFile(10000, _jsonFile);
179+
GenerateJsonFile(1000, _jsonFile);
180180
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
181181
{
182182
await connection.OpenAsync();
@@ -190,4 +190,3 @@ public async Task TestJsonStreamingAsync()
190190
}
191191
}
192192
}
193-

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public JsonTest(ITestOutputHelper output)
2222
{
2323
_output = output;
2424
}
25-
25+
2626
private static readonly string JsonDataString = "[{\"name\":\"Dave\",\"skills\":[\"Python\"]},{\"name\":\"Ron\",\"surname\":\"Peter\"}]";
2727

2828
private void ValidateRowsAffected(int rowsAffected)
@@ -73,7 +73,7 @@ private void ValidateNullJson(SqlDataReader reader)
7373
}
7474
}
7575

76-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
76+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
7777
public void TestJsonWrite()
7878
{
7979
string tableName = DataTestUtility.GenerateObjectName();
@@ -137,7 +137,7 @@ public void TestJsonWrite()
137137
}
138138
}
139139

140-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
140+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
141141
public async Task TestJsonWriteAsync()
142142
{
143143
string tableName = DataTestUtility.GenerateObjectName();
@@ -201,7 +201,7 @@ public async Task TestJsonWriteAsync()
201201
}
202202
}
203203

204-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
204+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
205205
public void TestJsonRead()
206206
{
207207
string tableName = DataTestUtility.GenerateObjectName();
@@ -260,7 +260,7 @@ public void TestJsonRead()
260260
}
261261
}
262262

263-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
263+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
264264
public async Task TestJsonReadAsync()
265265
{
266266
string tableName = DataTestUtility.GenerateObjectName();
@@ -319,7 +319,7 @@ public async Task TestJsonReadAsync()
319319
}
320320
}
321321

322-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
322+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
323323
public void TestNullJson()
324324
{
325325
string tableName = DataTestUtility.GenerateObjectName();
@@ -350,7 +350,7 @@ public void TestNullJson()
350350
DataTestUtility.DropTable(connection, tableName);
351351
}
352352

353-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
353+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
354354
public void TestJsonAPIs()
355355
{
356356
string tableName = DataTestUtility.GenerateObjectName();
@@ -398,7 +398,7 @@ public void TestJsonAPIs()
398398
}
399399
}
400400

401-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
401+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
402402
public void TestJsonWithMARS()
403403
{
404404
string table1Name = DataTestUtility.GenerateObjectName();
@@ -454,7 +454,7 @@ public void TestJsonWithMARS()
454454
}
455455
}
456456

457-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
457+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
458458
public void TestJsonSPParams()
459459
{
460460
string tableName = DataTestUtility.GenerateObjectName();

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/VectorTest/NativeVectorFloat32Tests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class VectorFloat32TestData
2020
public static float[] testData = new float[] { 1.1f, 2.2f, 3.3f };
2121
public static int vectorColumnLength = testData.Length;
2222
// Incorrect size for SqlParameter.Size
23-
public static int IncorrectParamSize = 3234;
23+
public static int IncorrectParamSize = 3234;
2424
public static IEnumerable<object[]> GetVectorFloat32TestData()
2525
{
2626
// Pattern 1-4 with SqlVector<float>(values: testData)
@@ -43,11 +43,11 @@ public static IEnumerable<object[]> GetVectorFloat32TestData()
4343

4444
// Pattern 1-4 with SqlVector<float>.Null
4545
yield return new object[] { 1, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };
46-
46+
4747
// Following scenario is not supported in SqlClient.
4848
// This can only be fixed with a behavior change that SqlParameter.Value is internally set to DBNull.Value if it is set to null.
4949
//yield return new object[] { 2, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };
50-
50+
5151
yield return new object[] { 3, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };
5252
yield return new object[] { 4, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };
5353
}
@@ -128,7 +128,7 @@ private void ValidateInsertedData(SqlConnection connection, float[] expectedData
128128
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader.GetSqlValue(0), expectedData, expectedLength);
129129

130130
if (!reader.IsDBNull(0))
131-
{
131+
{
132132
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader.GetValue(0), expectedData, expectedLength);
133133
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader[0], expectedData, expectedLength);
134134
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader["VectorData"], expectedData, expectedLength);
@@ -147,7 +147,7 @@ private void ValidateInsertedData(SqlConnection connection, float[] expectedData
147147
}
148148
}
149149

150-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
150+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
151151
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
152152
public void TestSqlVectorFloat32ParameterInsertionAndReads(
153153
int pattern,
@@ -213,7 +213,7 @@ private async Task ValidateInsertedDataAsync(SqlConnection connection, float[] e
213213
}
214214
}
215215

216-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
216+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
217217
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
218218
public async Task TestSqlVectorFloat32ParameterInsertionAndReadsAsync(
219219
int pattern,
@@ -247,7 +247,7 @@ public async Task TestSqlVectorFloat32ParameterInsertionAndReadsAsync(
247247
await ValidateInsertedDataAsync(conn, expectedValues, expectedLength);
248248
}
249249

250-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
250+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
251251
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
252252
public void TestStoredProcParamsForVectorFloat32(
253253
int pattern,
@@ -304,7 +304,7 @@ public void TestStoredProcParamsForVectorFloat32(
304304
Assert.Throws<InvalidOperationException>(() => command.ExecuteNonQuery());
305305
}
306306

307-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
307+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
308308
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
309309
public async Task TestStoredProcParamsForVectorFloat32Async(
310310
int pattern,
@@ -361,7 +361,7 @@ public async Task TestStoredProcParamsForVectorFloat32Async(
361361
await Assert.ThrowsAsync<InvalidOperationException>(async () => await command.ExecuteNonQueryAsync());
362362
}
363363

364-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
364+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
365365
[InlineData(1)]
366366
[InlineData(2)]
367367
public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
@@ -374,8 +374,8 @@ public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
374374
DataTable table = null;
375375
switch (bulkCopySourceMode)
376376
{
377-
378-
case 1:
377+
378+
case 1:
379379
// Use SqlServer table as source
380380
var insertCmd = new SqlCommand($"insert into {s_bulkCopySrcTableName} values (@VectorData)", sourceConnection);
381381
var vectorParam = new SqlParameter(s_vectorParamName, new SqlVector<float>(VectorFloat32TestData.testData));
@@ -400,8 +400,8 @@ public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
400400
throw new ArgumentOutOfRangeException(nameof(bulkCopySourceMode), $"Unsupported bulk copy source mode: {bulkCopySourceMode}");
401401
}
402402

403-
404-
403+
404+
405405
//Bulkcopy from sql server table to destination table
406406
using SqlCommand sourceDataCommand = new SqlCommand($"SELECT Id, VectorData FROM {s_bulkCopySrcTableName}", sourceConnection);
407407
using SqlDataReader reader = sourceDataCommand.ExecuteReader();
@@ -460,7 +460,7 @@ public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
460460
Assert.Equal(VectorFloat32TestData.testData.Length, ((SqlVector<float>)verifyReader.GetSqlVector<float>(0)).Length);
461461
}
462462

463-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
463+
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
464464
[InlineData(1)]
465465
[InlineData(2)]
466466
public async Task TestBulkCopyFromSqlTableAsync(int bulkCopySourceMode)
@@ -560,7 +560,7 @@ public async Task TestBulkCopyFromSqlTableAsync(int bulkCopySourceMode)
560560
Assert.Equal(VectorFloat32TestData.testData.Length, vector.Length);
561561
}
562562

563-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
563+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
564564
public void TestInsertVectorsFloat32WithPrepare()
565565
{
566566
SqlConnection conn = new SqlConnection(s_connectionString);

0 commit comments

Comments
 (0)