Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -92,14 +92,6 @@ public static class DataTestUtility
//SQL Server EngineEdition
private static string s_sqlServerEngineEdition;

// Currently, only Azure SQL supports vectors and JSON.
// Our CI images with specific SQL Server versions lag
// behind with vector and JSON support.
// JSON Column type
public static readonly bool IsJsonSupported = !IsNotAzureServer();
// VECTOR column type
public static readonly bool IsVectorSupported = !IsNotAzureServer();

// Azure Synapse EngineEditionId == 6
// More could be read at https://learn.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver16#propertyname
public static bool IsAzureSynapse
Expand Down Expand Up @@ -181,7 +173,6 @@ static DataTestUtility()
ManagedIdentitySupported = c.ManagedIdentitySupported;
IsManagedInstance = c.IsManagedInstance;
AliasName = c.AliasName;
IsJsonSupported = c.IsJsonSupported;

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

public static bool IsAzureServer()
{
return AreConnStringsSetup() && Utils.IsAzureSqlServer(new SqlConnectionStringBuilder(TCPConnectionString).DataSource);
}

public static bool IsNotAzureServer()
{
return !AreConnStringsSetup() || !Utils.IsAzureSqlServer(new SqlConnectionStringBuilder(TCPConnectionString).DataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@
using Newtonsoft.Json;
using Xunit.Abstractions;
using Xunit;
using System.Collections;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SQL.JsonTest
{
public class JsonBulkCopyTest
{
private readonly ITestOutputHelper _output;
private static readonly string _generatedJsonFile = DataTestUtility.GenerateRandomCharacters("randomRecords");
private static readonly string _outputFile = DataTestUtility.GenerateRandomCharacters("serverResults");
private static readonly string _sourceTableName = DataTestUtility.GenerateObjectName();
private static readonly string _destinationTableName = DataTestUtility.GenerateObjectName();
private static readonly string _generatedJsonFile = DataTestUtility.GetUniqueName("randomRecords");
private static readonly string _outputFile = DataTestUtility.GetUniqueName("serverResults");
private static readonly string _sourceTableName = DataTestUtility.GetUniqueName("jsonBulkCopySrcTable", true);
private static readonly string _destinationTableName = DataTestUtility.GetUniqueName("jsonBulkCopyDestTable", true);

public JsonBulkCopyTest(ITestOutputHelper output)
{
_output = output;
}

public static IEnumerable<object[]> JsonBulkCopyTestData()
{
yield return new object[] { CommandBehavior.Default, false, 300, 100 };
yield return new object[] { CommandBehavior.Default, true, 300, 100 };
yield return new object[] { CommandBehavior.SequentialAccess, false, 300, 100 };
yield return new object[] { CommandBehavior.SequentialAccess, true, 300, 100 };
yield return new object[] { CommandBehavior.Default, false, 30, 10 };
yield return new object[] { CommandBehavior.Default, true, 30, 10 };
yield return new object[] { CommandBehavior.SequentialAccess, false, 30, 10 };
yield return new object[] { CommandBehavior.SequentialAccess, true, 30, 10 };
}

private void PopulateData(int noOfRecords, int rows)
Expand Down Expand Up @@ -87,7 +86,7 @@ private void PrintJsonDataToFileAndCompare(SqlConnection connection)
try
{
DeleteFile(_outputFile);
using (SqlCommand command = new SqlCommand("SELECT [data] FROM [" + _destinationTableName + "]", connection))
using (SqlCommand command = new SqlCommand("SELECT [data] FROM " + _destinationTableName, connection))
{
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
Expand Down Expand Up @@ -125,7 +124,7 @@ private async Task PrintJsonDataToFileAndCompareAsync(SqlConnection connection)
try
{
DeleteFile(_outputFile);
using (SqlCommand command = new SqlCommand("SELECT [data] FROM [" + _destinationTableName + "]", connection))
using (SqlCommand command = new SqlCommand("SELECT [data] FROM " + _destinationTableName, connection))
{
using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
Expand Down Expand Up @@ -159,7 +158,7 @@ private async Task PrintJsonDataToFileAndCompareAsync(SqlConnection connection)

private void StreamJsonFileToServer(SqlConnection connection)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO [" + _sourceTableName + "] (data) VALUES (@jsondata)", connection))
using (SqlCommand cmd = new SqlCommand("INSERT INTO " + _sourceTableName + " (data) VALUES (@jsondata)", connection))
{
using (StreamReader jsonFile = File.OpenText(_generatedJsonFile))
{
Expand All @@ -171,7 +170,7 @@ private void StreamJsonFileToServer(SqlConnection connection)

private async Task StreamJsonFileToServerAsync(SqlConnection connection)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO [" + _sourceTableName + "] (data) VALUES (@jsondata)", connection))
using (SqlCommand cmd = new SqlCommand("INSERT INTO " + _sourceTableName + " (data) VALUES (@jsondata)", connection))
{
using (StreamReader jsonFile = File.OpenText(_generatedJsonFile))
{
Expand Down Expand Up @@ -265,7 +264,7 @@ private async Task BulkCopyDataAsync(CommandBehavior cb, bool enableStraming, in
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[MemberData(
nameof(JsonBulkCopyTestData)
#if NETFRAMEWORK
Expand All @@ -289,7 +288,7 @@ public void TestJsonBulkCopy(CommandBehavior cb, bool enableStraming, int jsonAr
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[MemberData(
nameof(JsonBulkCopyTestData)
#if NETFRAMEWORK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class JsonRecord
public string Name { get; set; }
}

public class JsonStreamTest
public class JsonStreamTest
{
private readonly ITestOutputHelper _output;
private static readonly string _jsonFile = "randomRecords.json";
private static readonly string _outputFile = "serverRecords.json";
private static readonly string _jsonFile = DataTestUtility.GetUniqueName("randomRecords") + ".json";
private static readonly string _outputFile = DataTestUtility.GetUniqueName("serverRecords") + ".json";

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

private void CompareJsonFiles()
Expand Down Expand Up @@ -157,10 +157,10 @@ private void DeleteFile(string filename)
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestJsonStreaming()
{
GenerateJsonFile(10000, _jsonFile);
GenerateJsonFile(1000, _jsonFile);
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
connection.Open();
Expand All @@ -173,10 +173,10 @@ public void TestJsonStreaming()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public async Task TestJsonStreamingAsync()
{
GenerateJsonFile(10000, _jsonFile);
GenerateJsonFile(1000, _jsonFile);
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
await connection.OpenAsync();
Expand All @@ -190,4 +190,3 @@ public async Task TestJsonStreamingAsync()
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public JsonTest(ITestOutputHelper output)
{
_output = output;
}

private static readonly string JsonDataString = "[{\"name\":\"Dave\",\"skills\":[\"Python\"]},{\"name\":\"Ron\",\"surname\":\"Peter\"}]";

private void ValidateRowsAffected(int rowsAffected)
Expand Down Expand Up @@ -73,7 +73,7 @@ private void ValidateNullJson(SqlDataReader reader)
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestJsonWrite()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -137,7 +137,7 @@ public void TestJsonWrite()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public async Task TestJsonWriteAsync()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -201,7 +201,7 @@ public async Task TestJsonWriteAsync()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestJsonRead()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -260,7 +260,7 @@ public void TestJsonRead()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public async Task TestJsonReadAsync()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -319,7 +319,7 @@ public async Task TestJsonReadAsync()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestNullJson()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -350,7 +350,7 @@ public void TestNullJson()
DataTestUtility.DropTable(connection, tableName);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestJsonAPIs()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -398,7 +398,7 @@ public void TestJsonAPIs()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestJsonWithMARS()
{
string table1Name = DataTestUtility.GenerateObjectName();
Expand Down Expand Up @@ -454,7 +454,7 @@ public void TestJsonWithMARS()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestJsonSPParams()
{
string tableName = DataTestUtility.GenerateObjectName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class VectorFloat32TestData
public static float[] testData = new float[] { 1.1f, 2.2f, 3.3f };
public static int vectorColumnLength = testData.Length;
// Incorrect size for SqlParameter.Size
public static int IncorrectParamSize = 3234;
public static int IncorrectParamSize = 3234;
public static IEnumerable<object[]> GetVectorFloat32TestData()
{
// Pattern 1-4 with SqlVector<float>(values: testData)
Expand All @@ -43,11 +43,11 @@ public static IEnumerable<object[]> GetVectorFloat32TestData()

// Pattern 1-4 with SqlVector<float>.Null
yield return new object[] { 1, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };

// Following scenario is not supported in SqlClient.
// This can only be fixed with a behavior change that SqlParameter.Value is internally set to DBNull.Value if it is set to null.
//yield return new object[] { 2, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };

yield return new object[] { 3, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };
yield return new object[] { 4, SqlVector<float>.Null, Array.Empty<float>(), vectorColumnLength };
}
Expand Down Expand Up @@ -128,7 +128,7 @@ private void ValidateInsertedData(SqlConnection connection, float[] expectedData
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader.GetSqlValue(0), expectedData, expectedLength);

if (!reader.IsDBNull(0))
{
{
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader.GetValue(0), expectedData, expectedLength);
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader[0], expectedData, expectedLength);
ValidateSqlVectorFloat32Object(reader.IsDBNull(0), (SqlVector<float>)reader["VectorData"], expectedData, expectedLength);
Expand All @@ -147,7 +147,7 @@ private void ValidateInsertedData(SqlConnection connection, float[] expectedData
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
public void TestSqlVectorFloat32ParameterInsertionAndReads(
int pattern,
Expand Down Expand Up @@ -213,7 +213,7 @@ private async Task ValidateInsertedDataAsync(SqlConnection connection, float[] e
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
public async Task TestSqlVectorFloat32ParameterInsertionAndReadsAsync(
int pattern,
Expand Down Expand Up @@ -247,7 +247,7 @@ public async Task TestSqlVectorFloat32ParameterInsertionAndReadsAsync(
await ValidateInsertedDataAsync(conn, expectedValues, expectedLength);
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[MemberData(nameof(VectorFloat32TestData.GetVectorFloat32TestData), MemberType = typeof(VectorFloat32TestData), DisableDiscoveryEnumeration = true)]
public void TestStoredProcParamsForVectorFloat32(
int pattern,
Expand Down Expand Up @@ -304,7 +304,7 @@ public void TestStoredProcParamsForVectorFloat32(
Assert.Throws<InvalidOperationException>(() => command.ExecuteNonQuery());
}

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

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[InlineData(1)]
[InlineData(2)]
public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
Expand All @@ -374,8 +374,8 @@ public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
DataTable table = null;
switch (bulkCopySourceMode)
{
case 1:

case 1:
// Use SqlServer table as source
var insertCmd = new SqlCommand($"insert into {s_bulkCopySrcTableName} values (@VectorData)", sourceConnection);
var vectorParam = new SqlParameter(s_vectorParamName, new SqlVector<float>(VectorFloat32TestData.testData));
Expand All @@ -400,8 +400,8 @@ public void TestBulkCopyFromSqlTable(int bulkCopySourceMode)
throw new ArgumentOutOfRangeException(nameof(bulkCopySourceMode), $"Unsupported bulk copy source mode: {bulkCopySourceMode}");
}



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

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
[InlineData(1)]
[InlineData(2)]
public async Task TestBulkCopyFromSqlTableAsync(int bulkCopySourceMode)
Expand Down Expand Up @@ -560,7 +560,7 @@ public async Task TestBulkCopyFromSqlTableAsync(int bulkCopySourceMode)
Assert.Equal(VectorFloat32TestData.testData.Length, vector.Length);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsVectorSupported))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAzureServer))]
public void TestInsertVectorsFloat32WithPrepare()
{
SqlConnection conn = new SqlConnection(s_connectionString);
Expand Down
Loading
Loading