public void SetExceptionResult_ShouldSetExpectedProperties() { //---------------Set up test pack------------------- var exceptionMessage = RandomValueGenerator.CreateRandomString(20, 40); var exception = new Exception(exceptionMessage); var actionResult = new DbContextActionResult <object>(); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- actionResult.SetExceptionResult(exception); //---------------Test Result ----------------------- actionResult.ActionResult.Should().Be(DbContextActionResult.Exception); actionResult.Results.Should().BeNull(); actionResult.ActionMessage.Trim().Should().Be($"Exception: {exception}"); actionResult.Exception.Should().Be(exception); }
/// <inheritdoc /> public override async Task <IDbContextActionResult <T> > ExecuteActionAsync <T>(DbContextAction dbContextAction, T dataModel = default(T), string sqlCommandText = null, IEnumerable <IDataAccessParameter> dataParameters = null) { var actionResult = new DbContextActionResult <T>(); var mapData = true; var wasConnectionOpen = DbConnection?.State == ConnectionState.Open; try { var dbCommand = DbConnection.CreateCommand(); dbCommand.CommandTimeout = CommandTimeout; dbCommand.CommandType = CommandType.Text; switch (dbContextAction) { case DbContextAction.Retrieve: dbCommand.CommandText = _sqlStatementBuildProvider.BuildSelectStatement(dataModel); break; case DbContextAction.Create: dbCommand.CommandText = _sqlStatementBuildProvider.BuildInsertStatement(dataModel); mapData = false; break; case DbContextAction.StoredProcedure: dbCommand.CommandType = CommandType.StoredProcedure; break; default: throw new InvalidEnumArgumentException(nameof(dbContextAction), (int)dbContextAction, typeof(DbContextActionResult)); } await OpenAsync(); var resultData = new List <T>(); if (mapData) { var dataReader = dbCommand.ExecuteReader(); while (dataReader.Read()) { var rowData = Enumerable.Range(0, dataReader.FieldCount) .ToDictionary(i => dataReader.GetName(i), i => dataReader.GetValue(i)); resultData.Add((T)await _dataModelPopulateProvider.PopulateAsync(typeof(T), rowData, dbContextAction)); } dataReader.Close(); } else { dbCommand.ExecuteNonQuery(); } if (!wasConnectionOpen) { await CloseAsync(); } actionResult.SetSuccessResult(resultData); } catch (Exception runtimeException) { actionResult.SetExceptionResult(runtimeException); } return(actionResult); }