public void TestDeleteEntityOk() { //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 CrudServices(context, utData.ConfigAndMapper); //ATTEMPT service.DeleteAndSave <Book>(1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully deleted a Book"); } using (var context = new EfCoreContext(options)) { context.Books.Count().ShouldEqual(3); } }
public void TestUpdateOnEntityKeyNotSetOk() { //SETUP var unique = Guid.NewGuid().ToString(); 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 CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var author = new Author { Name = "New Name", Email = unique }; var ex = Assert.Throws <InvalidOperationException>(() => service.UpdateAndSave(author)); //VERIFY ex.Message.ShouldStartWith("The primary key was not set on the entity class Author."); } }
public async Task TestUpdateOnEntityAlreadyTrackedOk() { //SETUP var unique = Guid.NewGuid().ToString(); 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 var author = await service.ReadSingleAsync <Author>(1); author.Email = unique; await service.UpdateAndSaveAsync(author); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); } using (var context = new EfCoreContext(options)) { context.Authors.Find(1).Email.ShouldEqual(unique); } }
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 TestUpdateJsonPatchTestFailsOk() { //SETUP var unique = Guid.NewGuid().ToString(); var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); context.ChangeTracker.Clear(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var patch = new JsonPatchDocument <Book>(); patch.Test(x => x.Title, "XXX"); patch.Replace(x => x.Title, unique); await service.UpdateAndSaveAsync(patch, 1); //VERIFY service.IsValid.ShouldBeFalse(); service.GetAllErrors().ShouldStartWith("The current value 'Refactoring' at path 'Title' is not equal to the test value 'XXX'."); } }
public async Task TestCreateEntityOk() { //SETUP var unique = Guid.NewGuid().ToString(); var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); } using (var context = new EfCoreContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var author = new Author { AuthorId = 1, Name = "New Name", Email = unique }; await service.CreateAndSaveAsync(author); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); } using (var context = new EfCoreContext(options)) { context.Authors.Count().ShouldEqual(1); context.Authors.Find(1).Email.ShouldEqual(unique); } }
public async Task TestUpdateJsonPatchNoUpdateBecauseSetterIsPrivateOk() { //SETUP var unique = Guid.NewGuid().ToString(); var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); context.ChangeTracker.Clear(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var patch = new JsonPatchDocument <Book>(); patch.Replace(x => x.Title, unique); await service.UpdateAndSaveAsync(patch, 1); //VERIFY service.IsValid.ShouldBeFalse(); service.GetAllErrors().ShouldStartWith("The property at path 'Title' could not be updated."); } }
public async Task TestUpdateJsonPatchWhereOk() { //SETUP var unique = Guid.NewGuid().ToString(); var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); context.ChangeTracker.Clear(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var patch = new JsonPatchDocument <Author>(); patch.Replace(x => x.Name, unique); await service.UpdateAndSaveAsync(patch, x => x.AuthorId == 1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully updated the Author"); context.Authors.Find(1).Name.ShouldEqual(unique); } }
public async Task TestDeleteEntityOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); context.ChangeTracker.Clear(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT await service.DeleteAndSaveAsync <Book>(1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); context.ChangeTracker.Clear(); context.Books.Count().ShouldEqual(3); } }
public void TestPerformanceGenericTypeCreate() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { var utData = context.SetupEntitiesDirect(); using (new TimeThings(_output, "Create Test<EfCoreContext>")) { var instance = new Test <EfCoreContext>(context); } using (new TimeThings(_output, "Create Test<EfCoreContext>", 1000)) { for (int i = 0; i < 1000; i++) { var instance = new Test <EfCoreContext>(context); } } using (new TimeThings(_output, "CrudServices<TestDbContext>")) { var service = new CrudServices <EfCoreContext>(context, utData.ConfigAndMapper); } using (new TimeThings(_output, "CrudServices<TestDbContext>", 1000)) { for (int i = 0; i < 1000; i++) { var service = new CrudServices <EfCoreContext>(context, utData.ConfigAndMapper); } } } }
public void TestUpdateOnEntityNotTrackedOk() { //SETUP var unique = Guid.NewGuid().ToString(); 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 CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var author = new Author { AuthorId = 1, Name = "New Name", Email = unique }; service.UpdateAndSave(author); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully updated the Author"); } using (var context = new EfCoreContext(options)) { context.Authors.Find(1).Email.ShouldEqual(unique); } }
public void TestUpdateJsonPatchKeysOk() { //SETUP var unique = Guid.NewGuid().ToString(); 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 CrudServices(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new EfCoreContext(options))); //ATTEMPT var patch = new JsonPatchDocument <Author>(); patch.Replace(x => x.Name, unique); service.UpdateAndSave(patch, 1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully updated the Author"); context.Authors.Find(1).Name.ShouldEqual(unique); } }
public void TestPerformanceReadMultipleToDto() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseDummyBooks(100); } using (var context = new EfCoreContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServices <EfCoreContext>(context, utData.ConfigAndMapper); using (new TimeThings(_output, "RunHandCoded ReadMany", 1)) { context.Books.Select(x => new BookTitleAndCount { BookId = x.BookId, Title = x.Title, ReviewsCount = x.Reviews.Count() }).ToList(); } using (new TimeThings(_output, "RunGenericService ReadManyNoTracked", 1)) { service.ReadManyNoTracked <BookTitleAndCount>().ToList(); } using (new TimeThings(_output, "RunGenericService ReadMany", 100)) { for (int i = 0; i < 100; i++) { context.Books.Select(x => new BookTitleAndCount { BookId = x.BookId, Title = x.Title, ReviewsCount = x.Reviews.Count() }).ToList(); } } using (new TimeThings(_output, "RunHandCoded Find", 100)) { for (int i = 0; i < 100; i++) { service.ReadManyNoTracked <BookTitleAndCount>().ToList(); } } } }
public void TestPerformanceReadSingleDirect() { //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 CrudServices <EfCoreContext>(context, utData.ConfigAndMapper); using (new TimeThings(_output, "RunHandCoded Find", 1)) { context.Find <Book>(1); } using (new TimeThings(_output, "RunGenericService Find", 1)) { service.ReadSingle <Book>(1); } using (new TimeThings(_output, "RunGenericService Find", 1000)) { for (int i = 0; i < 1000; i++) { service.ReadSingle <Book>(1); } } using (new TimeThings(_output, "RunHandCoded Find", 1000)) { for (int i = 0; i < 1000; i++) { context.Find <Book>(1); } } } }
public void TestDeleteWithActionEntityOk(bool stopDelete) { //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 CrudServices(context, utData.ConfigAndMapper); //ATTEMPT service.DeleteWithActionAndSave <Book>((c, e) => { var status = new StatusGenericHandler(); if (stopDelete) { status.AddError("Stop delete"); } return(status); }, 1); //VERIFY if (stopDelete) { service.IsValid.ShouldBeFalse(); } else { service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully deleted a Book"); } } using (var context = new EfCoreContext(options)) { context.Books.Count().ShouldEqual(stopDelete ? 4 : 3); } }
public void TestGetSingleOnEntityWhereException() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var ex = Assert.Throws <InvalidOperationException>(() => service.ReadSingle <Book>(x => true)); //VERIFY ex.Message.ShouldEqual("Sequence contains more than one element"); } }
public void TestNotEntityNoDtoOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var ex = Assert.Throws <InvalidOperationException>(() => service.ReadSingle <string>(1)); //VERIFY ex.Message.ShouldEqual("The class String is not registered as entity class in your DbContext EfCoreContext."); } }
public async Task TestGetSingleOnEntityNotFound() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect();; var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new EfCoreContext(options))); //ATTEMPT var book = await service.ReadSingleAsync <Book>(99); //VERIFY service.IsValid.ShouldBeFalse(); service.Errors.First().ToString().ShouldEqual("Sorry, I could not find the Book you were looking for."); book.ShouldBeNull(); } }
public void TestGetSingleOnEntityWhereBad() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var book = service.ReadSingle <Book>(x => x.BookId == 99); //VERIFY service.IsValid.ShouldBeFalse(); service.Errors.First().ToString().ShouldEqual("Sorry, I could not find the Book you were looking for."); book.ShouldBeNull(); } }
public void TestGetSingleOnEntityWhereOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var book = service.ReadSingle <Book>(x => x.BookId == 1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); book.BookId.ShouldEqual(1); context.Entry(book).State.ShouldEqual(EntityState.Unchanged); } }
public async Task TestGetSingleOnEntityOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new EfCoreContext(options))); //ATTEMPT var book = await service.ReadSingleAsync <Book>(1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); book.BookId.ShouldEqual(1); context.Entry(book).State.ShouldEqual(EntityState.Unchanged); } }
public void TestManyOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var books = service.ReadManyNoTracked <Book>(); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); books.Count().ShouldEqual(4); context.Entry(books.ToList().First()).State.ShouldEqual(EntityState.Detached); } }
public async Task TestGetSingleOnEntityWhereException() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.ReadSingleAsync <Book>(x => true)); //VERIFY #if NETCOREAPP2_1 ex.Message.ShouldEqual("Source sequence contains more than one element."); #elif NETCOREAPP3_0 ex.Message.ShouldEqual("Enumerator failed to MoveNextAsync."); #endif } }