示例#1
0
        public async Task TestAddReviewDto()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <BookDbContext>();

            using var context = new BookDbContext(options);
            context.Database.EnsureCreated();
            var books = context.SeedDatabaseFourBooks();

            context.ChangeTracker.Clear();

            var utData  = context.SetupSingleDtoAndEntities <AddReviewDto>();
            var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

            //ATTEMPT
            var dto = new AddReviewDto
            {
                BookId = books[0].BookId, NumStars = 5, Comment = "Great Book", VoterName = "Test"
            };
            await service.UpdateAndSaveAsync(dto);

            //VERIFY
            var book = context.Books.Include(x => x.Reviews).Single(x => x.BookId == books[0].BookId);

            book.Reviews.Count.ShouldEqual(1);
            book.Reviews.Single().NumStars.ShouldEqual(5);
            book.Reviews.Single().Comment.ShouldEqual("Great Book");
            book.Reviews.Single().VoterName.ShouldEqual("Test");
        }
示例#2
0
        public async Task TestAddPromotionDto()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <BookDbContext>();

            using var context = new BookDbContext(options);
            context.Database.EnsureCreated();
            var books = context.SeedDatabaseFourBooks();

            context.ChangeTracker.Clear();

            var utData  = context.SetupSingleDtoAndEntities <AddPromotionDto>();
            var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

            //ATTEMPT
            var dto = new AddPromotionDto {
                BookId = books[1].BookId, ActualPrice = 1.23m, PromotionalText = "Save!"
            };
            await service.UpdateAndSaveAsync(dto);

            //VERIFY
            var book = context.Books.Single(x => x.BookId == books[1].BookId);

            book.ActualPrice.ShouldEqual(1.23m);
            book.OrgPrice.ShouldNotEqual(book.ActualPrice);
            book.PromotionalText.ShouldEqual("Save!");
        }
示例#3
0
        public async Task TestUpdateNoSqlErrorHandler()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                context.UniqueEntities.Add(new UniqueEntity {
                    UniqueString = "Hello"
                });
                var entity = new UniqueEntity {
                    UniqueString = "Goodbye"
                };
                context.UniqueEntities.Add(entity);
                context.SaveChanges();

                var utData  = context.SetupSingleDtoAndEntities <UniqueWithConfigDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                entity.UniqueString = "Hello";
                var ex = await Assert.ThrowsAsync <DbUpdateException>(() => service.UpdateAndSaveAsync(entity));

                //VERIFY
                ex.InnerException.Message.ShouldEqual(
                    "SQLite Error 19: 'UNIQUE constraint failed: UniqueEntities.UniqueString'.");
            }
        }
示例#4
0
        public async Task TestUpdateCatchSqlErrorOn()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                context.UniqueEntities.Add(new UniqueEntity {
                    UniqueString = "Hello"
                });
                var entity = new UniqueEntity {
                    UniqueString = "Goodbye"
                };
                context.UniqueEntities.Add(entity);
                context.SaveChanges();

                var config = new GenericServicesConfig()
                {
                    SaveChangesExceptionHandler = CatchUniqueError19
                };
                var utData  = context.SetupSingleDtoAndEntities <UniqueWithConfigDto>(config);
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                entity.UniqueString = "Hello";
                await service.UpdateAndSaveAsync(entity);

                //VERIFY
                service.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }
        public async Task 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 CrudServicesAsync(context, utData.ConfigAndMapper);;

                //ATTEMPT
                var author = new Author {
                    Name = "New Name", Email = unique
                };
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.UpdateAndSaveAsync(author));

                //VERIFY
                ex.Message.ShouldStartWith("The primary key was not set on the entity class Author.");
            }
        }
        public async Task 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 CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var author = new Author {
                    AuthorId = 1, Name = "New Name", 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 TestDbQueryUpdateFail()
        {
            //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.UpdateAndSaveAsync(new ChildReadOnly()));

                //VERIFY
                ex.Message.ShouldEqual("The class ChildReadOnly of style DbQuery cannot be used in Update.");
            }
        }
示例#8
0
        public async Task Root_method_call_fails()
        {
            // arrange
            var options = SqliteInMemory.CreateOptions <RootContext>();

            using (var context = new RootContext(options))
            {
                context.Database.EnsureCreated();
                var utData  = context.SetupSingleDtoAndEntities <SetItemDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                // act
                var dto = new SetItemDto
                {
                    Id   = 1,
                    Item = "A item!"
                };
                await service.UpdateAndSaveAsync(dto);
            }
        }
        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" });
            }
        }
示例#10
0
        public async Task TestUpdateViaStatedMethodBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <Tests.Dtos.ChangePubDateDto>();
                var service = new CrudServicesAsync(context, utData.Wrapped);

                //ATTEMPT
                var dto = new Tests.Dtos.ChangePubDateDto {
                    BookId = 4, PublishedOn = new DateTime(2000, 1, 1)
                };
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.UpdateAndSaveAsync(dto, nameof(Book.AddReview)));

                //VERIFY
                ex.Message.ShouldStartWith("Could not find a method of name AddReview. The method that fit the properties in the DTO/VM are:");
            }
        }