public async Task TestDeleteWithActionEntityOk(int bookId) { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); } using (var context = new EfCoreContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT await service.DeleteWithActionAndSaveAsync <Book>(DeleteCheck, bookId); //VERIFY if (bookId == 1) { service.IsValid.ShouldBeFalse(); } else { service.IsValid.ShouldBeTrue(service.GetAllErrors()); } } using (var context = new EfCoreContext(options)) { context.Books.Count().ShouldEqual(bookId == 1 ? 4 : 3); } }
public async Task TestDeleteWithActionAsyncWithQueryFilterOk() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var author = new SoftDelEntity { SoftDeleted = true }; context.Add(author); context.SaveChanges(); context.ChangeTracker.Clear(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); context.SoftDelEntities.Count().ShouldEqual(0); context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(1); //ATTEMPT await service.DeleteWithActionAndSaveAsync <SoftDelEntity>(DelHandlerAsync, 1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully deleted a Soft Del Entity"); context.ChangeTracker.Clear(); context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(0); } }
public async Task TestDeleteWithActionAsyncWithQueryFilterError() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var author = new SoftDelEntity { SoftDeleted = false }; context.Add(author); context.SaveChanges(); } using (var context = new TestDbContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); context.SoftDelEntities.Count().ShouldEqual(1); context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(1); //ATTEMPT await service.DeleteWithActionAndSaveAsync <SoftDelEntity>(DelHandlerAsync, 1); //VERIFY service.IsValid.ShouldBeFalse(); service.GetAllErrors().ShouldEqual("Can't delete if not already soft deleted."); } using (var context = new TestDbContext(options)) { context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(1); } }
public async Task TestDbQueryDeleteWithRulesFail() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var ex = await Assert.ThrowsAsync <InvalidOperationException> (() => service.DeleteWithActionAndSaveAsync <ChildReadOnly>((c, e) => null, 1)); //VERIFY ex.Message.ShouldEqual("The class ChildReadOnly of style DbQuery cannot be used in Delete."); } }
public async Task TestDeleteWithActionAsyncWithQueryFilterOk() { async Task <IStatusGeneric> DelHandlerAsync(DbContext context, SoftDelEntity entity) { var status = new StatusGenericHandler(); if (!entity.SoftDeleted) { status.AddError("Can't delete if not already soft deleted."); } return(status); } //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var author = new SoftDelEntity { SoftDeleted = true }; context.Add(author); context.SaveChanges(); } using (var context = new TestDbContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); context.SoftDelEntities.Count().ShouldEqual(0); context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(1); //ATTEMPT await service.DeleteWithActionAndSaveAsync <SoftDelEntity>(DelHandlerAsync, 1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully deleted a Soft Del Entity"); } using (var context = new TestDbContext(options)) { context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(0); } }