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
@@ -0,0 +1,173 @@
using Simpleverse.Repository.Db.Extensions;
using Simpleverse.Repository.Db.SqlServer;
using StackExchange.Profiling.Data;
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Simpleverse.Repository.Db.Test.SqlServer.Entity
{
[Collection("SqlServerCollection")]
public class CancellationTokenTests : DatabaseTestFixture
{
private readonly SqlRepository _sqlRepository;

public CancellationTokenTests(DatabaseFixture fixture, ITestOutputHelper output)
: base(fixture, output)
{
_sqlRepository = new SqlRepository(() => (ProfiledDbConnection)fixture.GetProfiledConnection());
}

[Fact]
public async Task ListAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<Identity>();
var entity = new IdentityEntity(_sqlRepository);
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => entity.ListAsync(cancellationToken: cancelledToken)
);
}
}

[Fact]
public async Task GetAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<Identity>();
var entity = new IdentityEntity(_sqlRepository);
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => entity.GetAsync(cancellationToken: cancelledToken)
);
}
}

[Fact]
public async Task AddAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<Identity>();
var entity = new IdentityEntity(_sqlRepository);
var records = TestData.IdentityWithoutIdData(1);
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => entity.AddAsync(records, cancellationToken: cancelledToken)
);
}
}

[Fact]
public async Task UpdateAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<Identity>();
var entity = new IdentityEntity(_sqlRepository);
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => entity.UpdateAsync(
update => update.Name = "test",
cancellationToken: cancelledToken
)
);
}
}

[Fact]
public async Task DeleteAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<Identity>();
var entity = new IdentityEntity(_sqlRepository);
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => entity.DeleteAsync(cancellationToken: cancelledToken)
);
}
}

[Fact]
public async Task ExistsAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<Identity>();
var entity = new IdentityEntity(_sqlRepository);
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => entity.ExistsAsync(cancellationToken: cancelledToken)
);
}
}

[Fact]
public async Task DbRepository_QueryAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
{
// arrange
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => _sqlRepository.QueryAsync<Identity>("SELECT 1", null, cancelledToken)
);
}
}

[Fact]
public async Task DbRepository_ExecuteAsync_WhenCancellationTokenCancelled_ThrowsOperationCanceledException()
{
using (var profiler = Profile())
{
// arrange
var builder = new QueryBuilder<Identity>();
var query = builder.AddTemplate("SELECT 1");
var cancelledToken = new CancellationToken(canceled: true);

// act & assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => _sqlRepository.ExecuteAsync(query, cancelledToken)
);
}
}
}
}
63 changes: 32 additions & 31 deletions src/Simpleverse.Repository.Db/DbConnectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
using Dapper;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;

namespace Simpleverse.Repository.Db
{
public static class DbConnectionExtensions
{
public static Task<IEnumerable<R>> QueryAsync<R>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync<R>(query.RawSql, param: query.Parameters, transaction: tran);
public static Task<IEnumerable<R>> QueryAsync<R>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<R>(new CommandDefinition(query.RawSql, query.Parameters, transaction: tran, cancellationToken: cancellationToken));

public static Task<IEnumerable<dynamic>> QueryAsync(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync(query.RawSql, param: query.Parameters, transaction: tran);
public static Task<IEnumerable<dynamic>> QueryAsync(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync(new CommandDefinition(query.RawSql, query.Parameters, transaction: tran, cancellationToken: cancellationToken));

public static Task<IEnumerable<(TFirst, TSecond)>> QueryAsync<TFirst, TSecond>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran)
=> conn.QueryAsync<TFirst, TSecond>(query.RawSql, param: query.Parameters, tran: tran);
public static Task<IEnumerable<(TFirst, TSecond)>> QueryAsync<TFirst, TSecond>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond>(query.RawSql, param: query.Parameters, tran: tran, cancellationToken: cancellationToken);

public static Task<IEnumerable<(TFirst, TSecond, TThrid)>> QueryAsync<TFirst, TSecond, TThrid>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThrid>(query.RawSql, param: query.Parameters, tran: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThrid)>> QueryAsync<TFirst, TSecond, TThrid>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThrid>(query.RawSql, param: query.Parameters, tran: tran, cancellationToken: cancellationToken);

public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth)>> QueryAsync<TFirst, TSecond, TThrid, TFourth>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth>(query.RawSql, param: query.Parameters, tran: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth)>> QueryAsync<TFirst, TSecond, TThrid, TFourth>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth>(query.RawSql, param: query.Parameters, tran: tran, cancellationToken: cancellationToken);

public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth, TFifth)>> QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth>(query.RawSql, param: query.Parameters, tran: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth, TFifth)>> QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth>(query.RawSql, param: query.Parameters, tran: tran, cancellationToken: cancellationToken);

public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth, TFifth, TSixth)>> QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth>(query.RawSql, param: query.Parameters, tran: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth, TFifth, TSixth)>> QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth>(query.RawSql, param: query.Parameters, tran: tran, cancellationToken: cancellationToken);

public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth, TFifth, TSixth, TSeventh)>> QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth, TSeventh>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth, TSeventh>(query.RawSql, param: query.Parameters, tran: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThrid, TFourth, TFifth, TSixth, TSeventh)>> QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth, TSeventh>(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThrid, TFourth, TFifth, TSixth, TSeventh>(query.RawSql, param: query.Parameters, tran: tran, cancellationToken: cancellationToken);

public static Task<IEnumerable<(TFirst, TSecond)>> QueryAsync<TFirst, TSecond>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, (TFirst, TSecond)>(rawSql, (first, second) => (first, second), param: param, transaction: tran);
public static Task<IEnumerable<(TFirst, TSecond)>> QueryAsync<TFirst, TSecond>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, (TFirst, TSecond)>(new CommandDefinition(rawSql, param, transaction: tran, cancellationToken: cancellationToken), (first, second) => (first, second));

public static Task<IEnumerable<(TFirst, TSecond, TThird)>> QueryAsync<TFirst, TSecond, TThird>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThird, (TFirst, TSecond, TThird)>(rawSql, (first, second, third) => (first, second, third), param: param, transaction: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThird)>> QueryAsync<TFirst, TSecond, TThird>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThird, (TFirst, TSecond, TThird)>(new CommandDefinition(rawSql, param, transaction: tran, cancellationToken: cancellationToken), (first, second, third) => (first, second, third));

public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth)>> QueryAsync<TFirst, TSecond, TThird, TFourth>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, (TFirst, TSecond, TThird, TFourth)>(rawSql, (first, second, third, fourth) => (first, second, third, fourth), param: param, transaction: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth)>> QueryAsync<TFirst, TSecond, TThird, TFourth>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, (TFirst, TSecond, TThird, TFourth)>(new CommandDefinition(rawSql, param, transaction: tran, cancellationToken: cancellationToken), (first, second, third, fourth) => (first, second, third, fourth));

public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth, TFifth)>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth>(this IDbConnection conn, string rawSql, object param, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, (TFirst, TSecond, TThird, TFourth, TFifth)>(rawSql, (first, second, third, fourth, fifth) => (first, second, third, fourth, fifth), param: param, transaction: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth, TFifth)>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth>(this IDbConnection conn, string rawSql, object param, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, (TFirst, TSecond, TThird, TFourth, TFifth)>(new CommandDefinition(rawSql, param, transaction: tran, cancellationToken: cancellationToken), (first, second, third, fourth, fifth) => (first, second, third, fourth, fifth));

public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth, TFifth, TSixth)>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, (TFirst, TSecond, TThird, TFourth, TFifth, TSixth)>(rawSql, (first, second, third, fourth, fifth, sixth) => (first, second, third, fourth, fifth, sixth), param: param, transaction: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth, TFifth, TSixth)>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, (TFirst, TSecond, TThird, TFourth, TFifth, TSixth)>(new CommandDefinition(rawSql, param, transaction: tran, cancellationToken: cancellationToken), (first, second, third, fourth, fifth, sixth) => (first, second, third, fourth, fifth, sixth));

public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh)>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh)>(rawSql, (first, second, third, fourth, fifth, sixth, seventh) => (first, second, third, fourth, fifth, sixth, seventh), param: param, transaction: tran);
public static Task<IEnumerable<(TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh)>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>(this IDbConnection conn, string rawSql, object param = null, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, (TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh)>(new CommandDefinition(rawSql, param, transaction: tran, cancellationToken: cancellationToken), (first, second, third, fourth, fifth, sixth, seventh) => (first, second, third, fourth, fifth, sixth, seventh));

public static Task<int> ExecuteAsync(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null)
=> conn.ExecuteAsync(query.RawSql, param: query.Parameters, transaction: tran);
public static Task<int> ExecuteAsync(this IDbConnection conn, SqlBuilder.Template query, IDbTransaction tran = null, CancellationToken cancellationToken = default)
=> conn.ExecuteAsync(new CommandDefinition(query.RawSql, query.Parameters, transaction: tran, cancellationToken: cancellationToken));

public static async Task<R> ExecuteAsync<R>(this IDbConnection conn, Func<IDbConnection, Task<R>> function)
{
Expand Down
Loading
Loading