public async Task 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 CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var book = await service.ReadSingleAsync <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 TestNoBeforeSaveChangesMethodProvided() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var utData = context.SetupSingleDtoAndEntities <UniqueWithConfigDto>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT await service.CreateAndSaveAsync(new NormalEntity { MyString = "bad word" }); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); context.ChangeTracker.Clear(); context.NormalEntities.Count().ShouldEqual(1); } }
public async Task TestCreateEntityNotCopyKeyBackBecauseDtoPropertyHasPrivateSetterOk() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var utData = context.SetupSingleDtoAndEntities <NormalEntityKeyPrivateSetDto>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options))); //ATTEMPT var dto = new NormalEntityKeyPrivateSetDto(); await service.CreateAndSaveAsync(dto, CrudValues.UseAutoMapper); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); context.NormalEntities.Count().ShouldEqual(1); dto.Id.ShouldEqual(0); } }
public async Task TestProjectFromEntityToDtoOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <BookTitle>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new EfCoreContext(options))); //ATTEMPT var dto = await service.ProjectFromEntityToDto <Book, BookTitle>(x => x.Where(y => y.BookId == 1)).SingleAsync(); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); dto.BookId.ShouldEqual(1); dto.Title.ShouldEqual("Refactoring"); } }
public async Task TestProjectBookTitleManyOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <BookTitle>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var list = await service.ReadManyNoTracked <BookTitle>().ToListAsync(); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); list.Count.ShouldEqual(4); list.Select(x => x.Title).ShouldEqual(new [] { "Refactoring", "Patterns of Enterprise Application Architecture", "Domain-Driven Design", "Quantum Networking" }); } }
public async Task TestReadSingleCollection() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <BookWithAuthors>(); var service = new CrudServicesAsync(context, utData.Wrapped); //ATTEMPT var dto = await service.ReadSingleAsync <BookWithAuthors>(1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); dto.BookId.ShouldEqual(1); dto.Authors.Count.ShouldEqual(1); } }
public async Task TestCallUpdateBookWithExistingAuthorWithTwoIncludesAsync() { //SETUP int bookId; int authorId; var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); var bookSeeded = context.SeedDatabaseFourBooks(); bookId = bookSeeded.First().BookId; authorId = bookSeeded.Last().AuthorsLink.First().AuthorId; } using (var context = new EfCoreContext(options)) { var utData = context.SetupSingleDtoAndEntities <AddNewAuthorToBookUsingIncludesDto>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var dto = new AddNewAuthorToBookUsingIncludesDto { BookId = bookId, AddThisAuthor = context.Authors.SingleOrDefault(x => x.AuthorId == authorId), Order = 2 }; await service.UpdateAndSaveAsync(dto); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); service.Message.ShouldEqual("Successfully updated the Book"); var bookAuthorsName = context.Books .Where(x => x.BookId == bookId) .SelectMany(x => x.AuthorsLink.Select(y => y.Author.Name)) .ToArray(); bookAuthorsName.ShouldEqual(new String[] { "Martin Fowler", "Future Person" }); } }
public async Task 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 CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var books = await service.ReadManyNoTracked <Book>().ToListAsync(); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); books.Count.ShouldEqual(4); context.Entry(books.ToList().First()).State.ShouldEqual(EntityState.Detached); } }
public async Task TestProjectSingleOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <BookTitleAndCount>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var dto = await service.ReadSingleAsync <BookTitleAndCount>(1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); dto.BookId.ShouldEqual(1); dto.Title.ShouldEqual("Refactoring"); } }
public async Task EmailSentForNewAlbum() { //Setup DbContext.SeedDatabaseSubscriberToTwoArtists(); //Attempt and Verify var subscriptionDto = await CrudServicesAsync.ReadSingleAsync <SubscriptionDto>(1L); CrudServicesAsync.IsValid.ShouldBeTrue(CrudServicesAsync.GetAllErrors()); subscriptionDto.Id.ShouldEqual(1); var artistDto = await CrudServicesAsync.ReadSingleAsync <ArtistDto>(1L); var output = await _subscriberAppService.NotifySubscribers(new NotifySubscribersInput { Subscriptions = new List <SubscriptionDto> { subscriptionDto }, Artist = artistDto, Album = new AlbumDto { CreatedDate = DateTime.UtcNow, Name = "Testing < 123 <br /> with HTML", ReleaseDate = DateTime.UtcNow.AddDays(-1).ToString("yyyy-MM-dd"), SpotifyId = "1iBbBHhftbF1lJmOPs4D18", Image = new SpotifyImageDto { Width = 300, Height = 300, Url = "https://i.scdn.co/image/ab67616d00001e023587a048cf3e56f0efc56990" } } }); output.HasError.ShouldBeFalse(output.ErrorMessage); //TODO: verify that the email exists on disk? }
public async Task TestProjectBookTitleManyOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <BookTitle>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var list = await service.ReadManyWithPreQueryNoTracked <Book, BookTitle>(books => books.Where(x => x.AuthorsLink.Select(y => y.Author.Name).Contains("Martin Fowler"))).ToListAsync(); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); list.Count.ShouldEqual(2); list.Select(x => x.Title).ShouldEqual(new [] { "Refactoring", "Patterns of Enterprise Application Architecture" }); } }
public async Task TestCreateAuthorNameGoodNoValidationOk() { //SETUP var unique = Guid.NewGuid().ToString(); var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); var utData = context.SetupSingleDtoAndEntities <LocalAuthorDto>(); var service = new CrudServicesAsync(context, utData.Wrapped); //ATTEMPT var author = new LocalAuthorDto { Name = "Name", Email = unique }; await service.CreateAndSaveAsync(author); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); } }
public async Task TestDbQueryReadManyViaDtoOk() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); #if NETCOREAPP3_0 context.ExecuteScriptFileInTransaction(TestData.GetFilePath("ReplaceTableWithView.sql")); #endif context.Add(new Parent { Children = new List <Child> { new Child { MyString = "Hello" }, new Child { MyString = "Goodbye" } } }); context.SaveChanges(); } using (var context = new TestDbContext(options)) { var utData = context.SetupSingleDtoAndEntities <ChildDbQueryDto>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var entities = await service.ReadManyNoTracked <ChildDbQueryDto>().ToListAsync(); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); entities.Count.ShouldEqual(2); } }
public async Task TestDbQueryReadSingleWhereDirectOk() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); #if NETCOREAPP3_0 context.ExecuteScriptFileInTransaction(TestData.GetFilePath("ReplaceTableWithView.sql")); #endif context.Add(new Parent { Children = new List <Child> { new Child { MyString = "Hello" }, new Child { MyString = "Goodbye" } } }); context.SaveChanges(); } using (var context = new TestDbContext(options)) { var utData = context.SetupEntitiesDirect(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var entity = await service.ReadSingleAsync <ChildReadOnly>(x => x.ChildId == 1); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); entity.ShouldNotBeNull(); } }
public async Task TestCreateEntityUsingStaticCreateWithBadStatusOk() { //SETUP var options = SqliteInMemory.CreateOptions <TestDbContext>(); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); var utData = context.SetupSingleDtoAndEntities <DtoStaticCreate>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var dto = new DtoStaticCreate { MyInt = 1, MyString = null }; await service.CreateAndSaveAsync(dto); //VERIFY service.IsValid.ShouldBeFalse(); service.GetAllErrors().ShouldEqual("The string should not be null."); context.DddStaticFactEntities.Count().ShouldEqual(0); } }
public async Task TestReadSingleWhereNullIsErrorAsync(int bookId, bool shouldBeValid) { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <BookTitle>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var book = await service.ReadSingleAsync <Book>(x => x.BookId == bookId); //VERIFY service.IsValid.ShouldEqual(shouldBeValid); if (!service.IsValid) { service.GetAllErrors().ShouldEqual("Sorry, I could not find the Book you were looking for."); } } }
public async Task TestCreateAuthorNameNullPerDtoConfigValidationOk() { //SETUP var unique = Guid.NewGuid().ToString(); var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); var utData = context.SetupSingleDtoAndEntities <LocalAuthorDtoWithConfig>(); var service = new CrudServicesAsync(context, utData.Wrapped); //ATTEMPT var author = new LocalAuthorDtoWithConfig { Name = null, Email = unique }; await service.CreateAndSaveAsync(author); //VERIFY service.IsValid.ShouldBeFalse(); service.GetAllErrors().ShouldEqual("Author: The Name field is required."); } }