Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#941 Added the CancellableTraceLogTest class for Unit Testing.
- Loading branch information
1 parent
a7286d8
commit 69aee0d
Showing
2 changed files
with
220 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Trace/CancellableTraceLogTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using RepoDb.Exceptions; | ||
using RepoDb.Interfaces; | ||
using RepoDb.UnitTests.CustomObjects; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace RepoDb.UnitTests.Trace | ||
{ | ||
[TestClass] | ||
public class CancellableTraceLogTest | ||
{ | ||
[ClassInitialize] | ||
public static void ClassInitialize(TestContext context) | ||
{ | ||
DbSettingMapper.Add<TraceDbConnection>(new CustomDbSetting(), true); | ||
DbHelperMapper.Add<TraceDbConnection>(new CustomDbHelper(), true); | ||
StatementBuilderMapper.Add<TraceDbConnection>(new CustomStatementBuilder(), true); | ||
} | ||
|
||
#region SubClasses | ||
|
||
private class TraceDbConnection : CustomDbConnection { } | ||
|
||
private class CancelTrace : ITrace | ||
{ | ||
public void AfterExecution<TResult>(ResultTraceLog<TResult> log) | ||
{ | ||
// Do nothing | ||
} | ||
|
||
public Task AfterExecutionAsync<TResult>(ResultTraceLog<TResult> log, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void BeforeExecution(CancellableTraceLog log) | ||
{ | ||
log.Cancel(true); | ||
} | ||
|
||
public Task BeforeExecutionAsync(CancellableTraceLog log, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
log.Cancel(true); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
#region ExecuteNonQuery | ||
|
||
[TestMethod, ExpectedException(typeof(CancelledExecutionException))] | ||
public void ThrowExceptionOnExecuteNonQueryCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteNonQuery("", trace: new CancelTrace()); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AggregateException))] | ||
public void ThrowExceptionOnExecuteNonQueryAsyncCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteNonQueryAsync("", trace: new CancelTrace()) | ||
.Wait(); | ||
} | ||
|
||
#endregion | ||
|
||
#region ExecuteQuery | ||
|
||
[TestMethod, ExpectedException(typeof(CancelledExecutionException))] | ||
public void ThrowExceptionOnExecuteQueryCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteQuery("", trace: new CancelTrace()); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AggregateException))] | ||
public void ThrowExceptionOnExecuteQueryAsyncCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteQueryAsync("", trace: new CancelTrace()) | ||
.Wait(); | ||
} | ||
|
||
#endregion | ||
|
||
#region ExecuteScalar | ||
|
||
[TestMethod, ExpectedException(typeof(CancelledExecutionException))] | ||
public void ThrowExceptionOnExecuteScalarCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteScalar("", trace: new CancelTrace()); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AggregateException))] | ||
public void ThrowExceptionOnExecuteScalarAsyncCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteScalarAsync("", trace: new CancelTrace()) | ||
.Wait(); | ||
} | ||
|
||
#endregion | ||
|
||
#region ExecuteQueryMultiple | ||
|
||
[TestMethod, ExpectedException(typeof(CancelledExecutionException))] | ||
public void ThrowExceptionOnExecuteQueryMultipleCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteQueryMultiple("", trace: new CancelTrace()); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AggregateException))] | ||
public void ThrowExceptionExecuteQueryMultipleAsyncCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.ExecuteQueryMultipleAsync("", trace: new CancelTrace()) | ||
.Wait(); | ||
} | ||
|
||
#endregion | ||
|
||
#region Average | ||
|
||
[TestMethod, ExpectedException(typeof(CancelledExecutionException))] | ||
public void ThrowExceptionOnAverageCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.Average("", (Field)null, (object)null, trace: new CancelTrace()); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AggregateException))] | ||
public void ThrowExceptionOnAverageAsyncCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.AverageAsync("", (Field)null, (object)null, trace: new CancelTrace()) | ||
.Wait(); | ||
} | ||
|
||
#endregion | ||
|
||
#region AverageAll | ||
|
||
[TestMethod, ExpectedException(typeof(CancelledExecutionException))] | ||
public void ThrowExceptionOnAverageAllCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.AverageAll("", (Field)null, trace: new CancelTrace()); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AggregateException))] | ||
public void ThrowExceptionOnAverageAllAsyncCancelledOperation() | ||
{ | ||
// Prepare | ||
var connection = new TraceDbConnection(); | ||
|
||
// Act | ||
connection | ||
.AverageAllAsync("", (Field)null, trace: new CancelTrace()) | ||
.Wait(); | ||
} | ||
|
||
#endregion | ||
|
||
#endregion | ||
} | ||
} |