Skip to content

Commit

Permalink
#941 Added the CancellableTraceLogTest class for Unit Testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Sep 15, 2022
1 parent a7286d8 commit 69aee0d
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 25 deletions.
Expand Up @@ -31,31 +31,6 @@ private class TraceEntity
public string Name { get; set; }
}

private class RejectorTrace : ITrace
{
public void AfterExecution<TResult>(ResultTraceLog<TResult> log)
{
}

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 Average
Expand Down
@@ -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
}
}

0 comments on commit 69aee0d

Please sign in to comment.