public void CheickDbObjectCommand_Generic_Should_Throw_ArgumentNull_Exception() { // Arrange this.fluentDbCommand = new FluentDbCommandBase(); DbObjectCommand <string> nullCommand = null; DbObjectCommand <string> emptyCommand = new DbObjectCommand <string>(); emptyCommand.ObjectParameter = null; emptyCommand.Operation = DbOperation.Delete; emptyCommand.ScriptSql = string.Empty; DbObjectCommand <string> emptyCommand_2 = new DbObjectCommand <string>(); emptyCommand_2.ObjectParameter = "fake param"; emptyCommand_2.Operation = DbOperation.ExecuteSql; emptyCommand_2.ScriptSql = string.Empty; // Assert Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(nullCommand)); Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(emptyCommand)); Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(emptyCommand_2)); Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(new List <DbObjectCommand <string> >() { emptyCommand })); }
/// <summary> /// Execute safely the func, either we are in a retry policy or in a multiple command context. /// </summary> /// <param name="execution">Function to execute.</param> /// <param name="throwException">Define if we should throw the exception that occured.</param> private async Task <DbCommandResult> SafeExecute(Func <Task <DbCommandResult> > execution, bool throwException) { if (!this.MultipleCommandExecution) { // Execute this in the Safe Execution (with retry policy) return(await FluentDbCommandBase.SafeExecuteDbCommandAction(execution, this.RetryPolicy, throwException)); } else { // it will be executed inside the SafeExecuteDbCommandAction return(await execution()); } }
public async Task SafeExecuteDbCommandAction_Should_Execute_MultipleTimes_Defined_By_RetryPolicy_And_But_Should_Throw_Exception() { // Arrange var mockFakeFuncInterface = new Mock <IFakeFuncInterface>(); mockFakeFuncInterface.Setup(x => x.DoSomething()).Throws <Exception>(); RetryPolicyOption retry = new RetryPolicyOption(); retry.MaxRetries = 10; retry.ShouldRetryOn <Exception>(); Func <Task <DbCommandResult> > execution = () => FluentDbCommandBase.SafeExecuteDbCommandAction(() => mockFakeFuncInterface.Object.DoSomething(), retry, true); // Assert await Assert.ThrowsAnyAsync <RetryReachedException>(execution); mockFakeFuncInterface.Verify(x => x.DoSomething(), Times.Exactly(10)); }
public async Task SafeExecuteDbCommandAction_Should_Execute_Once_With_RetryPolicyNull() { // Arrange var mockFakeFuncInterface = new Mock <IFakeFuncInterface>(); mockFakeFuncInterface.Setup(x => x.DoSomething()).Returns(Task.FromResult <DbCommandResult>(new DbCommandResult() { Exception = null, IsSuccess = true, Result = 1 })); // Act var result = await FluentDbCommandBase.SafeExecuteDbCommandAction(() => mockFakeFuncInterface.Object.DoSomething(), null, false); // Assert Assert.True(result.IsSuccess); Assert.Null(result.Exception); Assert.Equal(1, result.Result); mockFakeFuncInterface.Verify(x => x.DoSomething(), Times.Once); }
/// <inheritdoc /> public async Task <TReturn> ExecuteAsync <TReturn>(string sql, Func <TReturn> customValueProviderToExecute, Action?dispatcherPostExecution = null, bool throwException = false) { DbCommandResult result = new DbCommandResult(); if (string.IsNullOrWhiteSpace(sql)) { throw new ArgumentNullException(nameof(sql)); } this.EnsureDbConnection(); Func <Task <DbCommandResult> > execution = async() => { try { this.EnsureDbConnectionIsOpened(); result.Result = await this.dbConnection.ExecuteAsync(sql, transaction : this.Transaction); result.IsSuccess = true; dispatcherPostExecution?.Invoke(); } catch (Exception ex) { result.Exception = ex; result.Result = -1; result.IsSuccess = false; dispatcherPostExecution?.Invoke(); if (throwException) { throw; } } return(result); }; await FluentDbCommandBase.SafeExecuteDbCommandAction(execution, this.RetryPolicy, throwException); return(customValueProviderToExecute()); }
public async Task SafeExecuteDbCommandAction_Should_Execute_MultipleTimes_Defined_By_RetryPolicy_And_But_ShouldNot_Throw_Exception() { // Arrange var mockFakeFuncInterface = new Mock <IFakeFuncInterface>(); mockFakeFuncInterface.Setup(x => x.DoSomething()).Throws <Exception>(); RetryPolicyOption retry = new RetryPolicyOption(); retry.MaxRetries = 10; retry.ShouldRetryOn <Exception>(); // Act var result = await FluentDbCommandBase.SafeExecuteDbCommandAction(() => mockFakeFuncInterface.Object.DoSomething(), retry, false); // Assert Assert.False(result.IsSuccess); Assert.NotNull(result.Exception); Assert.IsType <RetryReachedException>(result.Exception); mockFakeFuncInterface.Verify(x => x.DoSomething(), Times.Exactly(10)); }
/// <inheritdoc /> public async Task <DbCommandResult> ExecuteAsync(Action?dispatcherPostExecution = null, bool throwException = false) { this.EnsureDbConnection(); this.EnsureDbConnectionIsOpened(); Func <Task <DbCommandResult> > execution = async() => { bool commitException = false; DbCommandResultCollection results = new DbCommandResultCollection(); // wait all tasks in the queue while (this.ActionsToExecute.Any()) { Func <Task <DbCommandResult> > dbTask = this.ActionsToExecute.Dequeue(); results.Add(await dbTask()); } DbCommandResult mergeResult = results.MergeResults(); // We should not commit if there is any exception that occured var commit = this.CommitOrRollBackIfNecessary(mergeResult); commitException = !commit.Item1; dispatcherPostExecution?.Invoke(); // Get the eventuals commit exception mergeResult.Exception = commit.Item2; if (throwException && (!mergeResult.IsSuccess || commitException)) { // throw the inner exception (Exception of the merge result or the commit exception) throw commit.Item2 !; } return(mergeResult); }; return(await FluentDbCommandBase.SafeExecuteDbCommandAction(execution, this.RetryPolicy, throwException)); }
/// <inheritdoc /> public async Task <DbCommandResult> ExecuteAsync(string sql, Action?dispatcherPostExecution = null, bool throwException = false) { if (string.IsNullOrWhiteSpace(sql)) { throw new ArgumentNullException(nameof(sql)); } this.EnsureDbConnection(); Func <Task <DbCommandResult> > toExecute = async() => { DbCommandResult result = new DbCommandResult(); try { this.EnsureDbConnectionIsOpened(); result.Result = await this.dbConnection.ExecuteAsync(sql, transaction : this.Transaction); result.IsSuccess = result.Result > 0; dispatcherPostExecution?.Invoke(); } catch (Exception ex) { result.Exception = ex; result.Result = -1; result.IsSuccess = false; dispatcherPostExecution?.Invoke(); this.ThrowExceptionIfNecessary(throwException, ex); } return(result); }; // Execute the command return(await FluentDbCommandBase.SafeExecuteDbCommandAction(toExecute, this.RetryPolicy, throwException)); }
public void CheickDbObjectCommand_Should_Throw_ArgumentNull_Exception() { // Arrange this.fluentDbCommand = new FluentDbCommandBase(); DbObjectCommand nullCommand = null; DbObjectCommand emptyCommand = new DbObjectCommand(); emptyCommand.Parameters = Enumerable.Empty <object>(); emptyCommand.ScriptSql = "SELECT 1"; DbObjectCommand emptyCommand_2 = new DbObjectCommand(); emptyCommand.Parameters = new object[] { 10 }; emptyCommand.ScriptSql = string.Empty; // Assert Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(nullCommand)); Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(emptyCommand_2)); Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(emptyCommand)); Assert.Throws <ArgumentNullException>(() => this.fluentDbCommand.CheickDbObjectCommand(new List <DbObjectCommand>() { emptyCommand })); }