public void ForceValidateOn() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var config = new GenericBizRunnerConfig { TurnOffCaching = true, DoNotValidateSaveChanges = true }; var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionInOnlyWriteDbForceValidateOn(context); var runner = new ActionService <IBizActionInOnlyWriteDbForceValidateOn>(context, bizInstance, utData.WrappedConfig); var input = new BizDataIn { Num = 1 }; //ATTEMPT runner.RunBizAction(input); //VERIFY runner.Status.HasErrors.ShouldBeTrue(); context.LogEntries.Any().ShouldBeFalse(); } }
public void TestSqlErrorHandlerNotInPlace() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); context.UniqueEntities.Add(new UniqueEntity { UniqueString = "Hello" }); context.SaveChanges(); var config = new GenericBizRunnerConfig { TurnOffCaching = true }; var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionCheckSqlErrorHandlerWriteDb(context); var runner = new ActionService <IBizActionCheckSqlErrorHandlerWriteDb>(context, bizInstance, utData.WrappedConfig); //ATTEMPT var ex = Assert.Throws <DbUpdateException>(() => runner.RunBizAction("Hello")); //VERIFY ex.InnerException.Message.ShouldEqual("SQLite Error 19: 'UNIQUE constraint failed: UniqueEntities.UniqueString'."); } }
public async Task TestValidation(int num, bool hasErrors, bool doNotValidateSaveChanges) { //SETUP var config = new GenericBizRunnerConfig { TurnOffCaching = true, DoNotValidateSaveChanges = doNotValidateSaveChanges }; var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionInOutWriteDbAsync(context); var runner = new ActionServiceInOutAsync <IBizActionInOutWriteDbAsync, BizDataIn, BizDataOut>(true, utData.WrappedConfig); var inDto = new ServiceLayerBizInDto { Num = num }; //ATTEMPT var data = await runner.RunBizActionDbAndInstanceAsync <ServiceLayerBizOutDto>(context, bizInstance, inDto); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); if (hasErrors) { context.LogEntries.Any().ShouldBeFalse(); } else { context.LogEntries.Single().LogText.ShouldEqual(num.ToString()); } } }
public async Task TestActionServiceInOnlyMappingDatabaseOk(int num, bool hasErrors) { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(_noCachingConfig); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionInOnlyWriteDbAsync(context); var runner = new ActionServiceInOnlyAsync <IBizActionInOnlyWriteDbAsync, BizDataIn>(true, utData.WrappedConfig); var inDto = new ServiceLayerBizInDto { Num = num }; //ATTEMPT await runner.RunBizActionDbAndInstanceAsync(context, bizInstance, inDto); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); if (hasErrors) { context.LogEntries.Any().ShouldBeFalse(); } else { context.LogEntries.Single().LogText.ShouldEqual(num.ToString()); } } }
public void TestCreateTodoOk() { //SETUP var options = SqliteInMemory.CreateOptions <ExampleDbContext>(); using (var context = new ExampleDbContext(options)) { context.Database.EnsureCreated(); context.SeedDatabase(); var controller = new ToDoController(); var noCachingConfig = new GenericBizRunnerConfig { TurnOffCaching = true }; var utData = new NonDiBizSetup(noCachingConfig); var bizInstance = new CreateTodoBizLogic(context); var service = new ActionService <ICreateTodoBizLogic>(context, bizInstance, utData.WrappedConfig); //ATTEMPT var dto = new CreateTodoDto() { Name = "Test", Difficulty = 3, }; var response = controller.Post(dto, service); //VERIFY response.GetStatusCode().ShouldEqual(201); var rStatus = response.CheckCreateResponse("GetSingleTodo", new { id = 7 }, context.TodoItems.Find(7)); rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors()); rStatus.Message.ShouldEqual("Success"); } }
public void TestNormalValidateActionsBasedOnConfig(bool doNotValidate, bool hasErrors) { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var config = new GenericBizRunnerConfig { TurnOffCaching = true, DoNotValidateSaveChanges = doNotValidate }; var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionInOnlyWriteDb(context); var runner = new ActionService <IBizActionInOnlyWriteDb>(context, bizInstance, utData.WrappedConfig); var input = new BizDataIn { Num = 1 }; //ATTEMPT runner.RunBizAction(input); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); if (hasErrors) { context.LogEntries.Any().ShouldBeFalse(); } else { context.LogEntries.Single().LogText.ShouldEqual("1"); } } }
public async Task TestActionServiceInOutMappingOk(int num, bool hasErrors) { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(_noCachingConfig); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionInOutAsync(); var runner = new ActionServiceInOutAsync <IBizActionInOutAsync, BizDataIn, BizDataOut>(false, utData.WrappedConfig); var inDto = new ServiceLayerBizInDto { Num = num }; //ATTEMPT var data = await runner.RunBizActionDbAndInstanceAsync <ServiceLayerBizOutDto>(_dbContext, bizInstance, inDto); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); if (hasErrors) { data.ShouldBeNull(); } else { data.Output.ShouldEqual(inDto.Num.ToString()); } }
public TestBizDecode() { var utData = new NonDiBizSetup(new GenericBizRunnerConfig { TurnOffCaching = true }); _wrappedConfig = utData.WrappedConfig; }
public TestGetDtoAndResetDtoAsync() { var config = new GenericBizRunnerConfig { TurnOffCaching = true }; var utData = new NonDiBizSetup(config); _wrappedConfig = utData.WrappedConfig; }
public TestActionServiceInOut() { var config = new GenericBizRunnerConfig { TurnOffCaching = true }; var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); _wrappedConfig = utData.WrappedConfig; }
public void TestNotValidDto() { //SETUP //ATTEMPT var ex = Assert.Throws <InvalidOperationException>(() => NonDiBizSetup.SetupDtoMapping <string>()); //VERIFY ex.Message.ShouldEqual("The class String doesn't inherit from one of the Biz Runner Dto classes."); }
public async Task TestInputIsBad() { //SETUP var utData = new NonDiBizSetup(_noCachingConfig); var bizInstance = new BizActionOutOnlyAsync(); var runner = new ActionServiceAsync <IBizActionOutOnlyAsync>(_emptyDbContext, bizInstance, utData.WrappedConfig); var input = "string"; //ATTEMPT var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await runner.RunBizActionAsync <string>(input)); //VERIFY ex.Message.ShouldEqual("Your call of IBizActionOutOnlyAsync needed 'InOut, Async' but the Business class had a different setup of 'Out, Async'"); }
public void TestDtoWithOverrideOfAutoMapperSetup() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizOutWithMappingDto>(); //ATTEMPT var input = new BizDataOut { Output = "Hello" }; var data = utData.WrappedConfig.FromBizIMapper.Map <ServiceLayerBizOutWithMappingDto>(input); //VERIFY data.MappedOutput.ShouldEqual("Hello with suffix."); }
public void TestViaAddAutoMapper() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizOutDto>(); //ATTEMPT var input = new BizDataOut { Output = "hello" }; var data = utData.WrappedConfig.FromBizIMapper.Map <ServiceLayerBizOutDto>(input); //VERIFY data.Output.ShouldEqual("hello"); }
public void TestBizInMappingDto() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(); //ATTEMPT var input = new ServiceLayerBizInDto { Num = 234 }; var data = utData.WrappedConfig.ToBizIMapper.Map <BizDataIn>(input); //VERIFY data.Num.ShouldEqual(234); }
public async Task TestActionServiceOutOnlyNoDtoOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizOutDto>(_noCachingConfig); var bizInstance = new BizActionOutOnlyAsync(); var runner = new ActionServiceOutOnlyAsync <IBizActionOutOnlyAsync, BizDataOut>(false, utData.WrappedConfig); //ATTEMPT var data = await runner.RunBizActionDbAndInstanceAsync <BizDataOut>(_dbContext, bizInstance); //VERIFY bizInstance.HasErrors.ShouldBeFalse(); data.Output.ShouldEqual("Result"); }
public async Task TestActionServiceOutOnlyDirectOk() { //SETUP var utData = new NonDiBizSetup(_noCachingConfig); var bizInstance = new BizActionOutOnlyAsync(); var runner = new ActionServiceAsync <IBizActionOutOnlyAsync>(_emptyDbContext, bizInstance, utData.WrappedConfig); //ATTEMPT var data = await runner.RunBizActionAsync <BizDataOut>(); //VERIFY bizInstance.HasErrors.ShouldBeFalse(); data.Output.ShouldEqual("Result"); }
public async Task TestSqlErrorHandlerWorksOkAsync(int sqlErrorCode, bool shouldThrowException) { IStatusGeneric CatchUniqueError(Exception e, DbContext context) { var dbUpdateEx = e as DbUpdateException; var sqliteError = dbUpdateEx?.InnerException as SqliteException; return(sqliteError?.SqliteErrorCode == sqlErrorCode ? new StatusGenericHandler().AddError("Unique constraint failed") : null); } //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); context.UniqueEntities.Add(new UniqueEntity { UniqueString = "Hello" }); context.SaveChanges(); var config = new GenericBizRunnerConfig { TurnOffCaching = true, SaveChangesExceptionHandler = CatchUniqueError }; var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionCheckSqlErrorHandlerWriteDbAsync(context); var runner = new ActionServiceAsync <IBizActionCheckSqlErrorHandlerWriteDbAsync>(context, bizInstance, utData.WrappedConfig); //ATTEMPT try { await runner.RunBizActionAsync("Hello"); } //VERIFY catch (Exception e) { shouldThrowException.ShouldBeTrue(); return; } shouldThrowException.ShouldBeFalse(); runner.Status.GetAllErrors().ShouldEqual("Unique constraint failed"); } }
public async Task TestBizInCopyAsyncServiceInstanceOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDtoAsync>(); var copier = new CopyToBizDataAsync <BizDataIn, ServiceLayerBizInDtoAsync>(); var input = new ServiceLayerBizInDtoAsync { Num = 234 }; //ATTEMPT var data = await copier.CopyToBizAsync(null, utData.WrappedConfig.ToBizIMapper, input); //VERIFY data.Num.ShouldEqual(234); }
public async Task TestBizInCopyAsyncSyncDtoOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(); var copier = DtoAccessGenerator.BuildCopier(typeof(ServiceLayerBizInDto), typeof(BizDataIn), true, true, true); var input = new ServiceLayerBizInDto { Num = 234 }; //ATTEMPT var data = await copier.DoCopyToBizAsync <BizDataIn>(null, utData.WrappedConfig.ToBizIMapper, input); //VERIFY data.Num.ShouldEqual(234); }
public async Task TestActionServiceInOnlyNoDtoOk(int num, bool hasErrors) { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(_noCachingConfig); var bizInstance = new BizActionInOnlyAsync(); var runner = new ActionServiceInOnlyAsync <IBizActionInOnlyAsync, BizDataIn>(false, utData.WrappedConfig); var input = new BizDataIn { Num = num }; //ATTEMPT await runner.RunBizActionDbAndInstanceAsync(_dbContext, bizInstance, input); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); }
public async Task TestActionServiceOutOnlyDtosAsyncOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDtoAsync>(_noCachingConfig); utData.AddDtoMapping <ServiceLayerBizOutDtoAsync>(); var bizInstance = new BizActionOutOnlyAsync(); var runner = new ActionServiceAsync <IBizActionOutOnlyAsync>(_emptyDbContext, bizInstance, utData.WrappedConfig); //ATTEMPT var data = await runner.RunBizActionAsync <ServiceLayerBizOutDtoAsync>(); //VERIFY bizInstance.HasErrors.ShouldBeFalse(); data.Output.ShouldEqual("Result"); }
public async Task TestBizOutCopyAsyncAsyncDtoOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizOutDtoAsync>(); var copier = DtoAccessGenerator.BuildCopier(typeof(BizDataOut), typeof(ServiceLayerBizOutDtoAsync), false, true, true); var input = new BizDataOut { Output = "test copy" }; //ATTEMPT var data = await copier.DoCopyFromBizAsync <ServiceLayerBizOutDtoAsync>(null, utData.WrappedConfig.FromBizIMapper, input); //VERIFY data.Output.ShouldEqual("test copy"); data.CopyFromBizDataCalled.ShouldBeTrue(); data.SetupSecondaryOutputDataCalled.ShouldBeTrue(); }
public void TestBizInCopyOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(); var copier = DtoAccessGenerator.BuildCopier(typeof(ServiceLayerBizInDto), typeof(BizDataIn), true, false, true); var input = new ServiceLayerBizInDto { Num = 234 }; //ATTEMPT var data = copier.DoCopyToBiz <BizDataIn>(null, utData.WrappedConfig.ToBizIMapper, input); //VERIFY data.Num.ShouldEqual(234); input.CopyToBizDataCalled.ShouldBeTrue(); input.SetupSecondaryDataCalled.ShouldBeFalse(); }
public void TestActionServiceInOnlyMappingOk(int num, bool hasErrors) { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(_noCachingConfig); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionInOnly(); var runner = new ActionServiceInOnly <IBizActionInOnly, BizDataIn>(false, utData.WrappedConfig); var inDto = new ServiceLayerBizInDto { Num = num }; //ATTEMPT runner.RunBizActionDbAndInstance(_dbContext, bizInstance, inDto); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); }
public void TestActionServiceNestedOutUsingGenericActionFromBizDtoOk() { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerNestedOutDto>(_config); utData.AddDtoMapping <ServiceLayerNestedOutChildDto>(); var wrappedConfig = utData.WrappedConfig; var bizInstance = new BizActionNestedOutOnly(); var runner = new ActionService <IBizActionNestedOutOnly>(_emptyDbContext, bizInstance, wrappedConfig); //ATTEMPT var data = runner.RunBizAction <ServiceLayerNestedOutDto>(); //VERIFY bizInstance.HasErrors.ShouldBeFalse(); data.Output.ShouldEqual("Test"); data.ChildData.ChildInt.ShouldEqual(123); data.ChildData.ChildString.ShouldEqual("Nested"); }
public void TestSqlErrorHandlerWorksEvenIfValidationIsTurnedOff() { IStatusGeneric CatchUniqueError(Exception e, DbContext context) { var dbUpdateEx = e as DbUpdateException; var sqliteError = dbUpdateEx?.InnerException as SqliteException; return(sqliteError?.SqliteErrorCode == 19 ? new StatusGenericHandler().AddError("Unique constraint failed") : null); } //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); context.UniqueEntities.Add(new UniqueEntity { UniqueString = "Hello" }); context.SaveChanges(); var config = new GenericBizRunnerConfig { TurnOffCaching = true, DoNotValidateSaveChanges = true, SaveChangesExceptionHandler = CatchUniqueError }; var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config); utData.AddDtoMapping <ServiceLayerBizOutDto>(); var bizInstance = new BizActionCheckSqlErrorHandlerWriteDb(context); var runner = new ActionService <IBizActionCheckSqlErrorHandlerWriteDb>(context, bizInstance, utData.WrappedConfig); //ATTEMPT runner.RunBizAction("Hello"); //VERIFY runner.Status.GetAllErrors().ShouldEqual("Unique constraint failed"); } }
public void TestActionServiceInOutValuesOk(int input, bool hasErrors) { //SETUP var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(_noCachingConfig); var bizInstance = new BizActionValueInOut(); var runner = new ActionServiceInOut <IBizActionValueInOut, int, string>(false, utData.WrappedConfig); //ATTEMPT var data = runner.RunBizActionDbAndInstance <string>(_dbContext, bizInstance, input); //VERIFY bizInstance.HasErrors.ShouldEqual(hasErrors); if (hasErrors) { data.ShouldBeNull(); } else { data.ShouldEqual(input.ToString()); } }
public async Task TestActionServiceOutOnlyMappingDatabaseOk() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizOutDto>(_noCachingConfig); var bizInstance = new BizActionOutOnlyWriteDbAsync(context); var runner = new ActionServiceOutOnlyAsync <IBizActionOutOnlyWriteDbAsync, BizDataOut>(true, utData.WrappedConfig); //ATTEMPT var data = await runner.RunBizActionDbAndInstanceAsync <ServiceLayerBizOutDto>(context, bizInstance); //VERIFY bizInstance.HasErrors.ShouldBeFalse(); context.LogEntries.Single().LogText.ShouldEqual("BizActionOutOnlyWriteDbAsync"); } }