Skip to content

Commit

Permalink
Port bulk copy tests from devdiv (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Pham authored Aug 4, 2021
1 parent ba2dbfe commit df9990c
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,25 @@ public void TestKeysFromCertificatesCreatedWithMultipleCryptoProviders(string co
sqlSetupStrategyCsp = new SQLSetupStrategyCspExt(cspPath);
string tableName = sqlSetupStrategyCsp.CspProviderTable.Name;

using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
sqlConn.Open();
using SqlConnection sqlConn = new(connectionString);
sqlConn.Open();

Table.DeleteData(tableName, sqlConn);
Table.DeleteData(tableName, sqlConn);

// insert 1 row data
Customer customer = new Customer(45, "Microsoft", "Corporation");
// insert 1 row data
Customer customer = new Customer(45, "Microsoft", "Corporation");

DatabaseHelper.InsertCustomerData(sqlConn, tableName, customer);
DatabaseHelper.InsertCustomerData(sqlConn, tableName, customer);

// Test INPUT parameter on an encrypted parameter
using (SqlCommand sqlCommand = new SqlCommand(@"SELECT CustomerId, FirstName, LastName FROM [@tableName] WHERE FirstName = @firstName",
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled))
{
sqlCommand.Parameters.AddWithValue(@"tableName", tableName);
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
// Test INPUT parameter on an encrypted parameter
using SqlCommand sqlCommand = new(@$"SELECT CustomerId, FirstName, LastName FROM [{tableName}] WHERE FirstName = @firstName",
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled);
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
customerFirstParam.Direction = System.Data.ParameterDirection.Input;

using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
ValidateResultSet(sqlDataReader);
Console.WriteLine(@"INFO: Successfully validated using a certificate using provider:{0}", providerName);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(@"INFO: Failed to validate using a certificate using provider:{0}", providerName);
Console.WriteLine(@"Exception: {0}", e.Message);
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
ValidateResultSet(sqlDataReader);
Console.WriteLine(@"INFO: Successfully validated using a certificate using provider:{0}", providerName);
}
finally
{
Expand Down Expand Up @@ -168,35 +156,25 @@ public void TestEncryptDecryptWithCSP(string connectionString)

try
{
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
sqlConn.Open();
using SqlConnection sqlConn = new(connectionString);
sqlConn.Open();

Table.DeleteData(tableName, sqlConn);
Table.DeleteData(tableName, sqlConn);

// insert 1 row data
Customer customer = new Customer(45, "Microsoft", "Corporation");
// insert 1 row data
Customer customer = new Customer(45, "Microsoft", "Corporation");

DatabaseHelper.InsertCustomerData(sqlConn, tableName, customer);
DatabaseHelper.InsertCustomerData(sqlConn, tableName, customer);

// Test INPUT parameter on an encrypted parameter
using (SqlCommand sqlCommand = new SqlCommand(@"SELECT CustomerId, FirstName, LastName FROM [@tableName] WHERE FirstName = @firstName",
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled))
{
sqlCommand.Parameters.AddWithValue(@"tableName", tableName);
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
// Test INPUT parameter on an encrypted parameter
using SqlCommand sqlCommand = new(@$"SELECT CustomerId, FirstName, LastName FROM [{tableName}] WHERE FirstName = @firstName",
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled);
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
Console.WriteLine(@"Exception: {0}");
customerFirstParam.Direction = System.Data.ParameterDirection.Input;

using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
ValidateResultSet(sqlDataReader);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(@"Exception: {0}", e.Message);
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
ValidateResultSet(sqlDataReader);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@
<Compile Include="SQL\RetryLogic\SqlCommandReliabilityTest.cs" />
<Compile Include="SQL\RetryLogic\SqlConnectionReliabilityTest.cs" />
<Compile Include="SQL\RetryLogic\SqlConfigurationManagerReliabilityTest.cs" />
<Compile Include="SQL\SqlBulkCopyTest\KeepNulls.cs" />
<Compile Include="SQL\SqlBulkCopyTest\OrderHint.cs" />
<Compile Include="SQL\SqlBulkCopyTest\OrderHintAsync.cs" />
<Compile Include="SQL\SqlBulkCopyTest\OrderHintDuplicateColumn.cs" />
<Compile Include="SQL\SqlBulkCopyTest\OrderHintIdentityColumn.cs" />
<Compile Include="SQL\SqlBulkCopyTest\OrderHintMissingTargetColumn.cs" />
<Compile Include="SQL\SqlBulkCopyTest\OrderHintTransaction.cs" />
<Compile Include="SQL\DataClassificationTest\DataClassificationTest.cs" />
<Compile Include="SQL\SqlBulkCopyTest\TableLock.cs" />
<Compile Include="TracingTests\EventSourceTest.cs" />
<Compile Include="SQL\AdapterTest\AdapterTest.cs" />
<Compile Include="SQL\AsyncTest\AsyncTimeoutTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ public static void ExecuteTest()
{
SqlCommand command = new SqlCommand(GenerateCommandText(), connection);
connection.Open();

IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
System.Threading.Thread.Sleep(100);
}

Assert.True(command.EndExecuteNonQuery(result) > 0, "FAILED: BeginExecuteNonQuery did not complete successfully.");
}
catch (SqlException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,6 @@ public static void ConnectionKilledTest()
// Execute SELECT statement.
DataTestUtility.RunNonQuery(s_dbConnectionString, s_selectTableCmd);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Assert.Null(ex);
}
finally
{
// Kill all the connections, set Database to SINGLE_USER Mode and drop Database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,82 @@ public static void TypeVersionKnobTest()
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[InlineData(true)]
[InlineData(false)]
public static void BulkCopyTest(bool useReader)
{
string tempTableSrc = "t" + Guid.NewGuid().ToString().Replace('-', '_');
string tempTableDst = "#t_" + Guid.NewGuid().ToString().Replace('-', '_');
string prepTableSrc1 = "CREATE TABLE " + tempTableSrc + " (ci int, c0 dateTime, c1 date, c2 time(7), c3 datetime2(3), c4 datetimeoffset)";
string prepTableSrc2 = "INSERT INTO " + tempTableSrc + " VALUES (0, " +
"'1753-01-01 12:00AM', " +
"'1753-01-01', " +
"'20:12:13.36', " +
"'2000-12-31 23:59:59.997', " +
"'9999-12-31 15:59:59.997 -08:00')";
string prepTableDst3 = "CREATE TABLE " + tempTableDst + " (ci int, c0 dateTime, c1 date, c2 time(7), c3 datetime2(3), c4 datetimeoffset)";

using SqlConnection conn = new(DataTestUtility.TCPConnectionString);
conn.Open();
using SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = prepTableSrc1;
cmd.ExecuteNonQuery();
cmd.CommandText = prepTableSrc2;
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT * FROM " + tempTableSrc;

try
{
using SqlConnection connDst = new(DataTestUtility.TCPConnectionString);
connDst.Open();
using SqlCommand cmd2 = connDst.CreateCommand();
cmd2.CommandText = prepTableDst3;
_ = cmd2.ExecuteNonQuery();

using (SqlBulkCopy bcp = new(connDst))
{
bcp.DestinationTableName = tempTableDst;
if (useReader)
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
bcp.WriteToServer(reader);
}
}
else
{
SqlDataAdapter adapter = new(cmd);
DataTable sourceTbl = new();
adapter.Fill(sourceTbl);
bcp.WriteToServer(sourceTbl);
}
}

cmd2.CommandText = "SELECT * FROM " + tempTableDst;
using (SqlDataReader reader = cmd.ExecuteReader())
{
int numberOfRows = 0;
Assert.Equal(6, reader.VisibleFieldCount);
while (reader.Read())
{
Assert.Equal(typeof(int), reader[0].GetType());
Assert.Equal(typeof(DateTime), reader[1].GetType());
Assert.Equal(typeof(DateTime), reader[2].GetType());
Assert.Equal(typeof(TimeSpan), reader[3].GetType());
Assert.Equal(typeof(DateTime), reader[4].GetType());
Assert.Equal(typeof(DateTimeOffset), reader[5].GetType());
numberOfRows++;
}
Assert.Equal(1, numberOfRows);
}
}
finally
{
cmd.CommandText = "DROP TABLE " + tempTableSrc;
cmd.ExecuteNonQuery();
}
}

private static bool IsValidParam(SqlDbType dbType, string col, object value, SqlConnection conn, string tempTable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public static void Test()
txScope.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Assert.Null(ex);
}
finally
{
#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Data;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
class KeepNulls
{
public static void Test(string srcconstr, string dstconstr, string srctable, string dsttable)
{
using SqlConnection destConn = new(dstconstr);
destConn.Open();

using SqlCommand dstcmd = destConn.CreateCommand();
Helpers.TryExecute(dstcmd, "create table " + srctable + " (col1 int, col2 text, col3 text)");
Helpers.TryExecute(dstcmd, "insert into " + srctable + "(col1, col3) values (1, 'Michael')");
Helpers.TryExecute(dstcmd, "insert into " + srctable + "(col1, col2, col3) values (2, 'Quark', 'Astrid')");
Helpers.TryExecute(dstcmd, "insert into " + srctable + "(col1, col2) values (66, 'K�se');");

Helpers.TryExecute(dstcmd, "create table " + dsttable + " (col1 int identity(1,1), col2 text default 'Jogurt', col3 text)");

using SqlConnection sourceConn = new(srcconstr);
sourceConn.Open();

using SqlCommand srccmd = new("select * from " + srctable, sourceConn);
using IDataReader reader = srccmd.ExecuteReader();

try
{
using SqlBulkCopy bulkcopy = new(destConn, SqlBulkCopyOptions.KeepNulls, null);
bulkcopy.DestinationTableName = dsttable;
SqlBulkCopyColumnMappingCollection ColumnMappings = bulkcopy.ColumnMappings;
ColumnMappings.Add("col1", "col1");
ColumnMappings.Add("col2", "col2");
ColumnMappings.Add("col3", "col3");

bulkcopy.WriteToServer(reader);
Helpers.VerifyResults(destConn, dsttable, 3, 3);
}
finally
{
Helpers.TryDropTable(dstconstr, srctable);
Helpers.TryDropTable(dstconstr, dsttable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ public void CheckConstraintsTest()
CheckConstraints.Test(_connStr, AddGuid("SqlBulkCopyTest_Extensionsrc"), AddGuid("SqlBulkCopyTest_Extensiondst"));
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public void TableLockTest()
{
TableLock.Test(_connStr, _connStr, AddGuid("SqlBulkCopyTest_TableLock0"), AddGuid("SqlBulkCopyTest_TableLock1"));
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public void KeepNullsTest()
{
KeepNulls.Test(_connStr, _connStr, AddGuid("SqlBulkCopyTest_KeepNulls0"), AddGuid("SqlBulkCopyTest_KeepNulls1"));
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public void TransactionTest()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Data;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
class TableLock
{
public static void Test(string srcconstr, string dstconstr, string srctable, string dsttable)
{
using SqlConnection destConn = new(dstconstr);
destConn.Open();

using SqlCommand dstcmd = destConn.CreateCommand();
Helpers.TryExecute(dstcmd, "create table " + srctable + " (col1 int, col2 text, col3 text)");
Helpers.TryExecute(dstcmd, "insert into " + srctable + "(col1, col3) values (1, 'Michael')");
Helpers.TryExecute(dstcmd, "insert into " + srctable + "(col1, col2, col3) values (2, 'Quark', 'Astrid')");
Helpers.TryExecute(dstcmd, "insert into " + srctable + "(col1, col2) values (66, 'K�se');");

Helpers.TryExecute(dstcmd, "create table " + dsttable + " (col1 int identity(1,1), col2 text default 'Jogurt', col3 text)");

using SqlConnection sourceConn = new(srcconstr);
sourceConn.Open();

using SqlCommand srccmd = new SqlCommand("select * from " + srctable, sourceConn);
using IDataReader reader = srccmd.ExecuteReader();
try
{
using SqlBulkCopy bulkcopy = new(destConn, SqlBulkCopyOptions.TableLock, null);
bulkcopy.DestinationTableName = dsttable;
SqlBulkCopyColumnMappingCollection ColumnMappings = bulkcopy.ColumnMappings;
ColumnMappings.Add("col1", "col1");
ColumnMappings.Add("col2", "col2");
ColumnMappings.Add("col3", "col3");

bulkcopy.WriteToServer(reader);
Helpers.VerifyResults(destConn, dsttable, 3, 3);
}
finally
{
Helpers.TryDropTable(dstconstr, srctable);
Helpers.TryDropTable(dstconstr, dsttable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,7 @@ public void ExecuteNonQueryErrorTest()
conn.Open();
Console.WriteLine("SqlClient.DiagnosticTest.ExecuteNonQueryErrorTest Connection Open Successful");

try
{
var output = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("SqlClient.DiagnosticTest.ExecuteNonQueryErrorTest " + e.Message);
}
SqlException ex = Assert.Throws<SqlException>(() => cmd.ExecuteNonQuery());
Console.WriteLine("SqlClient.DiagnosticTest.ExecuteNonQueryErrorTest Command Executed");
}
Console.WriteLine("SqlClient.DiagnosticTest.ExecuteNonQueryErrorTest Command Disposed");
Expand Down

0 comments on commit df9990c

Please sign in to comment.