Skip to content

Commit

Permalink
NotifyAfter changes and unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ttossavainen committed Sep 10, 2024
1 parent 2900c43 commit bd2308f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,84 @@ public async Task TestBulkInsert_All()
}
}

[TestMethod]
public async Task TestBulkInsert_NotifyAfterZero()
{
var transactionLevels = new List<SqlTransactionIsolationLevel>() {
SqlTransactionIsolationLevel.Unspecified,
SqlTransactionIsolationLevel.Serializable,
SqlTransactionIsolationLevel.None,
SqlTransactionIsolationLevel.ReadUncommitted,
SqlTransactionIsolationLevel.ReadCommitted
};

foreach (var transactionLevel in transactionLevels)
{
Init();

var options = new Options()
{
SqlTransactionIsolationLevel = transactionLevel,
CommandTimeoutSeconds = 60,
FireTriggers = false,
KeepIdentity = false,
NotifyAfter = 0,
ConvertEmptyPropertyValuesToNull = false,
KeepNulls = true,
TableLock = false,
};

var result = await MicrosoftSQL.BulkInsert(_input, options, default);
Assert.IsTrue(result.Success);
Assert.AreEqual(0, result.Count);
Assert.AreEqual(3, GetRowCount());

await MicrosoftSQL.BulkInsert(_input, options, default);
Assert.AreEqual(6, GetRowCount());

CleanUp();
}
}

[TestMethod]
public async Task TestBulkInsert_NotifyAfterOne()
{
var transactionLevels = new List<SqlTransactionIsolationLevel>() {
SqlTransactionIsolationLevel.Unspecified,
SqlTransactionIsolationLevel.Serializable,
SqlTransactionIsolationLevel.None,
SqlTransactionIsolationLevel.ReadUncommitted,
SqlTransactionIsolationLevel.ReadCommitted
};

foreach (var transactionLevel in transactionLevels)
{
Init();

var options = new Options()
{
SqlTransactionIsolationLevel = transactionLevel,
CommandTimeoutSeconds = 60,
FireTriggers = false,
KeepIdentity = false,
NotifyAfter = 1,
ConvertEmptyPropertyValuesToNull = false,
KeepNulls = true,
TableLock = false,
};

var result = await MicrosoftSQL.BulkInsert(_input, options, default);
Assert.IsTrue(result.Success);
Assert.AreEqual(3, result.Count);
Assert.AreEqual(3, GetRowCount());

await MicrosoftSQL.BulkInsert(_input, options, default);
Assert.AreEqual(6, GetRowCount());

CleanUp();
}
}

private static int GetRowCount()
{
using var connection = new SqlConnection(_connString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ private static async Task<long> ExecuteHandler(Options options, string tableName
sqlBulkCopy.DestinationTableName = tableName;
sqlBulkCopy.SqlRowsCopied += (s, e) => rowsCopied = e.RowsCopied;

// Calculate the number of rows and set value for NotifyAfter
var rowCount = dataSet.Tables[0].Rows.Count;
sqlBulkCopy.NotifyAfter = rowCount > 0 ? Math.Max(1, rowCount / 10) : 1;
if (options.NotifyAfter == 0)
{
// Calculate the number of rows and set value for NotifyAfter
var rowCount = dataSet.Tables[0].Rows.Count;
sqlBulkCopy.NotifyAfter = rowCount > 0 ? Math.Max(1, rowCount / 10) : 1;
}
if (options.NotifyAfter > 0)
sqlBulkCopy.NotifyAfter = options.NotifyAfter;
else
sqlBulkCopy.NotifyAfter = 0;

await sqlBulkCopy.WriteToServerAsync(dataSet.Tables[0], cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class Options

/// <summary>
/// Defines the number of rows to be processed before generating a notification event.
/// If the number of rows is unknown, NotifyAfter is dynamically set to 10% of the total row count, with a minimum value of 1.
/// Notification events can be used for error handling to see approximately which row the error occurred.
/// Default value 0 = There won't be any notifications until the task is completed and Result.Count will be 0.
/// The default value of 0 will set NotifyAfter dynamically to 10% of the total row count, with a minimum value of 1.
/// A value of -1 means there won't be any notifications until the task is completed, and Result.Count will be 0.
/// Setting a value greater than the total number of rows can cause Result.Count to be 0.
/// Notification events can be used for error handling to see approximately which row the error occurred at.
/// </summary>
/// <example>0</example>
public int NotifyAfter { get; set; }
Expand Down

0 comments on commit bd2308f

Please sign in to comment.