public void TestSoftDeleteServiceGetSoftDeletedEntriesOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); var book1 = context.AddBookWithReviewToDb("test1"); var book2 = context.AddBookWithReviewToDb("test2"); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config); var status = service.SetSoftDelete(book1); status.IsValid.ShouldBeTrue(status.GetAllErrors()); } using (var context = new SingleSoftDelDbContext(options)) { var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config); //ATTEMPT var softDelBooks = service.GetSoftDeletedEntries <Book>().ToList(); //VERIFY softDelBooks.Count.ShouldEqual(1); softDelBooks.Single().Title.ShouldEqual("test1"); context.Books.Count().ShouldEqual(1); context.Books.IgnoreQueryFilters().Count().ShouldEqual(2); } }
public async Task TestSoftDeleteServiceSetSoftDeleteOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); var book = context.AddBookWithReviewToDb(); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config); //ATTEMPT var status = await service.SetSoftDeleteAsync(book); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1); } using (var context = new SingleSoftDelDbContext(options)) { context.Books.Count().ShouldEqual(0); context.Books.IgnoreQueryFilters().Count().ShouldEqual(1); } }
public void TestHardDeleteSoftDeletedEntryNoCallSaveChangesOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); var book = context.AddBookWithReviewToDb(); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config); var status = service.SetSoftDelete(book); status.IsValid.ShouldBeTrue(status.GetAllErrors()); } using (var context = new SingleSoftDelDbContext(options)) { var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config); //ATTEMPT var status = service.HardDeleteSoftDeletedEntry(context.Books.IgnoreQueryFilters().Single(), false); context.Books.IgnoreQueryFilters().Count().ShouldEqual(1); context.SaveChanges(); context.Books.IgnoreQueryFilters().Count().ShouldEqual(0); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1); } }
public void TestSoftDeleteServiceResetSoftDeleteViaKeysOk() { //SETUP int bookId; var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); bookId = context.AddBookWithReviewToDb().Id; var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config); var status1 = service.SetSoftDeleteViaKeys <Book>(bookId); status1.IsValid.ShouldBeTrue(status1.GetAllErrors()); //ATTEMPT var status2 = service.ResetSoftDeleteViaKeys <Book>(bookId); //VERIFY status2.IsValid.ShouldBeTrue(status2.GetAllErrors()); status2.Result.ShouldEqual(1); } using (var context = new SingleSoftDelDbContext(options)) { context.Books.Count().ShouldEqual(1); context.Books.IgnoreQueryFilters().Count().ShouldEqual(1); } }
public void TestAddBookWithReviewOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); //ATTEMPT context.AddBookWithReviewToDb(); context.ChangeTracker.Clear(); //VERIFY var book = context.Books .Include(x => x.Reviews) .Include(x => x.OneToOneRelationship) .Single(); book.Title.ShouldEqual("test"); book.OneToOneRelationship.ShouldNotBeNull(); book.Reviews.ShouldNotBeNull(); book.Reviews.Single().NumStars.ShouldEqual(1); } }
public async Task TestSoftDeleteServiceSetSoftDeleteViaKeysBadNumberOfKeys() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); var book = context.AddBookWithReviewToDb(); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config); //ATTEMPT var ex = await Assert.ThrowsAsync <ArgumentException>(async() => await service.SetSoftDeleteViaKeysAsync <Book>(1, 2)); //VERIFY ex.Message.ShouldEqual("Mismatch in keys: your provided 2 key(s) and the entity has 1 key(s) (Parameter 'keyValues')"); } }
public async Task TestSoftDeleteServiceSetSoftDeleteViaKeysBadKeyType() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); var book = context.AddBookWithReviewToDb(); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config); //ATTEMPT var ex = await Assert.ThrowsAsync <ArgumentException>(async() => await service.SetSoftDeleteViaKeysAsync <Book>(book)); //VERIFY ex.Message.ShouldEqual("Mismatch in keys: your provided key 1 (of 1) is of type Book but entity key's type is System.Int32 (Parameter 'keyValues')"); } }
public async Task TestSoftDeleteServiceSetSoftDeleteOneToOneBad() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); context.AddBookWithReviewToDb(); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config); //ATTEMPT var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.SetSoftDeleteViaKeysAsync <OneToOne>(1)); //VERIFY ex.Message.ShouldEqual("You cannot soft delete a one-to-one relationship. It causes problems if you try to create a new version."); } }
public async Task TestQueryBookWithReviewsOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); context.AddBookWithReviewToDb(); //ATTEMPT var query = context.Books.Include(x => x.Reviews); var book = await query.SingleAsync(); //VERIFY _output.WriteLine(query.ToQueryString()); book.Title.ShouldEqual("test"); book.Reviews.ShouldNotBeNull(); book.Reviews.Single().NumStars.ShouldEqual(1); } }
public async Task TestAddBookWithReviewOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); //ATTEMPT context.AddBookWithReviewToDb(); } using (var context = new SingleSoftDelDbContext(options)) { //VERIFY var book = await context.Books.Include(x => x.Reviews).SingleAsync(); book.Title.ShouldEqual("test"); book.Reviews.ShouldNotBeNull(); book.Reviews.Single().NumStars.ShouldEqual(1); } }
public void TestHardDeleteViaKeysNotFoundOk() { //SETUP var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); using (var context = new SingleSoftDelDbContext(options)) { context.Database.EnsureCreated(); var book = context.AddBookWithReviewToDb(); var config = new ConfigSoftDeleteWithUserId(context); var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config); var status1 = service.SetSoftDelete(book); status1.IsValid.ShouldBeTrue(status1.GetAllErrors()); //ATTEMPT var status = service.HardDeleteViaKeys <Book>(234); //VERIFY status.IsValid.ShouldBeFalse(status.GetAllErrors()); status.GetAllErrors().ShouldEqual("Could not find the entry you ask for."); } }