public async Task TestDbQueryReadManyViaDtoOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                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);
            }
        }
예제 #2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            try
            {
                serviceDeferral        = taskInstance.GetDeferral();
                taskInstance.Canceled += OnTaskCanceled;

                ExecutingPlatform.Current = AppPlatform.UWP;
                var context = new EfCoreContext();
                var utData  = context.SetupSingleDtoAndEntities <AccountViewModel>();
                utData.AddSingleDto <CategoryViewModel>();
                utData.AddSingleDto <PaymentViewModel>();
                utData.AddSingleDto <RecurringPaymentViewModel>();

                var crudService = new CrudServicesAsync(context, utData.ConfigAndMapper);

                var liveTileManager = new LiveTileManager(crudService);
                await liveTileManager.UpdatePrimaryLiveTile();

                await liveTileManager.UpdateSecondaryLiveTiles();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Debug.WriteLine("LiveTile update failed.");
            }
            finally
            {
                serviceDeferral?.Complete();
            }
        }
예제 #3
0
        public async Task TestProjectFromEntityToDtoIgnoreQueryFiltersOk()
        {
            //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.SetupSingleDtoAndEntities <SoftDelEntityDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                context.SoftDelEntities.Count().ShouldEqual(0);
                context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(1);

                //ATTEMPT
                var dto = await service.ProjectFromEntityToDto <SoftDelEntity, SoftDelEntityDto>(x => x.IgnoreQueryFilters().Where(y => y.Id == 1))
                          .SingleAsync();

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.Id.ShouldEqual(1);
            }
        }
예제 #4
0
        public async Task TestDeleteNoSqlErrorHandler()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();
                var firstBook = context.Books.First();
                var status    = Order.CreateOrder("J", DateTime.Today,
                                                  new List <OrderBooksDto> {
                    new OrderBooksDto(firstBook.BookId, firstBook, 1)
                });
                context.Add(status.Result);

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

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <DbUpdateException>(() => service.DeleteAndSaveAsync <Book>(firstBook.BookId));

                //VERIFY
                ex.InnerException.Message.ShouldEqual("SQLite Error 19: 'FOREIGN KEY constraint failed'.");
            }
        }
예제 #5
0
        public async Task TestCreateCatchSqlErrorOn()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

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

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

                //ATTEMPT
                await service.CreateAndSaveAsync(new UniqueEntity { UniqueString = "Hello" });

                //VERIFY
                service.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }
        public async Task TestCreateEntityUsingStaticCreateOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
            }
            using (var context = new TestDbContext(options))
            {
                var utData  = context.SetupSingleDtoAndEntities <DtoStaticCreate>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

                //ATTEMPT
                var dto = new DtoStaticCreate {
                    MyInt = 1, MyString = "Hello"
                };
                await service.CreateAndSaveAsync(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully created a Ddd Static Create Entity");
            }
            using (var context = new TestDbContext(options))
            {
                context.DddStaticFactEntities.Count().ShouldEqual(1);
                context.DddStaticFactEntities.Single().MyString.ShouldEqual("Hello");
                context.DddStaticFactEntities.Single().MyInt.ShouldEqual(1);
            }
        }
        public async Task TestCreateAuthorViaAutoMapperMappingViaTestSetupOk()
        {
            //SETUP
            var unique  = Guid.NewGuid().ToString();
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

                //ATTEMPT
                var author = new AuthorDto {
                    Name = "New Name", Email = unique
                };
                await service.CreateAndSaveAsync(author);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully created a Author");
            }
            using (var context = new EfCoreContext(options))
            {
                context.Authors.Count().ShouldEqual(1);
                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 TestUpdateViaStatedMethodOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

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

                //ATTEMPT
                var dto = new Dtos.ChangePubDateDto {
                    BookId = 4, PublishedOn = new DateTime(2000, 1, 1)
                };
                await service.UpdateAndSaveAsync(dto, nameof(Book.RemovePromotion));

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                var entity = context.Books.Find(4);
                entity.ActualPrice.ShouldEqual(220);
            }
        }
        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 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 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 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 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.");
            }
        }
예제 #15
0
        public async Task TestRemoveReviewDto()
        {
            //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 <RemoveReviewDto>();
            var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

            //ATTEMPT
            var dto = new RemoveReviewDto {
                BookId = books[3].BookId, ReviewId = books[3].Reviews.Last().ReviewId
            };
            await service.UpdateAndSaveAsync(dto);

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

            book.Reviews.Count.ShouldEqual(1);
        }
예제 #16
0
        public async Task TestDeleteAsyncWithQueryFilterFailsOk()
        {
            //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.DeleteAndSaveAsync <SoftDelEntity>(1);

                //VERIFY
                service.IsValid.ShouldBeFalse();
                service.GetAllErrors().ShouldEqual("Sorry, I could not find the Soft Del Entity you wanted to delete.");
            }
            using (var context = new TestDbContext(options))
            {
                context.SoftDelEntities.IgnoreQueryFilters().Count().ShouldEqual(1);
            }
        }
예제 #17
0
        public async Task TestChangePubDateDto()
        {
            //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 <ChangePubDateDto>();
            var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

            //ATTEMPT
            var dto = new ChangePubDateDto {
                BookId = books[3].BookId, PublishedOn = new DateTime(2020, 1, 1)
            };
            await service.UpdateAndSaveAsync(dto);

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

            book.PublishedOn.ShouldEqual(new DateTime(2020, 1, 1));
        }
예제 #18
0
        public async Task TestUpdateAddReviewOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

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

                //ATTEMPT
                var dto = new Tests.Dtos.AddReviewDto {
                    BookId = 1, Comment = "comment", NumStars = 3, VoterName = "user"
                };
                await service.UpdateAndSaveAsync(dto, nameof(Book.AddReview));

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                context.Set <Review>().Count().ShouldEqual(3);
            }
        }
        public async Task TestCreateEntityUsingStaticCreateWithBadStatusOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
            }
            using (var context = new TestDbContext(options))
            {
                var utData  = context.SetupSingleDtoAndEntities <DtoStaticCreate>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

                //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);
            }
        }
예제 #20
0
        public async Task TestUpdateViaStatedMethodInPerDtoConfigOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

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

                //ATTEMPT
                var dto = new DtoWithConfig {
                    BookId = 4
                };
                await service.UpdateAndSaveAsync(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                var entity = context.Books.Find(4);
                entity.ActualPrice.ShouldEqual(220);
            }
        }
        public async Task TestCreateEntityUsingDefaults_AutoMapperOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
            }
            using (var context = new EfCoreContext(options))
            {
                var utData = context.SetupSingleDtoAndEntities <AuthorNameDto>();
                context.SetupSingleDtoAndEntities <AuthorNameDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new EfCoreContext(options)));

                //ATTEMPT
                var dto = new AuthorNameDto {
                    Name = "New Name"
                };
                await service.CreateAndSaveAsync(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully created a Author");
            }
            using (var context = new EfCoreContext(options))
            {
                context.Authors.Count().ShouldEqual(1);
                context.Authors.Single().Name.ShouldEqual("New Name");
                context.Authors.Single().Email.ShouldBeNull();
            }
        }
예제 #22
0
        public async Task TestUpdateViaAutoMapperOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.Add(new Author {
                    Name = "Start Name", Email = "*****@*****.**"
                });
                context.SaveChanges();

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

                //ATTEMPT
                var dto = new AuthorDto {
                    AuthorId = 1, Name = "New Name", Email = "*****@*****.**"
                };
                await service.UpdateAndSaveAsync(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Author");
                var entity = context.Authors.Find(1);
                entity.Name.ShouldEqual("Start Name");
                entity.Email.ShouldEqual(dto.Email);
            }
        }
예제 #23
0
        public async Task TestDeleteCatchSqlErrorTurnedOn()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();
                var firstBook = context.Books.First();
                var status    = Order.CreateOrder("J", DateTime.Today,
                                                  new List <OrderBooksDto> {
                    new OrderBooksDto(firstBook.BookId, firstBook, 1)
                });
                context.Add(status.Result);

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

                //ATTEMPT
                await service.DeleteAndSaveAsync <Book>(firstBook.BookId);

                //VERIFY
                service.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }
예제 #24
0
        public async Task TestUpdatePublicationDateViaAutoMapperOk()
        {
            //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)
                };
                await service.UpdateAndSaveAsync(dto, CrudValues.UseAutoMapper);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                var entity = context.Books.Find(4);
                entity.PublishedOn.ShouldEqual(new DateTime(2000, 1, 1));
            }
        }
예제 #25
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'.");
            }
        }
예제 #26
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!");
        }
        public async Task TestCreateTodoOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

            using (var context = new ExampleDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabase();

                var controller = new ToDoHybridController();
                var utData     = context.SetupSingleDtoAndEntities <CreateTodoHybridDto>(_genericServiceConfig);
                var service    = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new CreateTodoHybridDto()
                {
                    Name       = "Test",
                    Difficulty = 3,
                };
                var response = await controller.PostAsync(dto, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(201);
                var rStatus = response.CheckCreateResponse("GetSingleHybridTodo", new { id = 7 }, dto);
                rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
                rStatus.Message.ShouldEqual("Success");
            }
        }
예제 #28
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");
        }
        public async Task TestCallAddReviewWithIncludeWithIncludedReviewsAsync()
        {
            //SETUP
            int bookId;
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                var bookSeeded = context.SeedDatabaseFourBooks();
                bookId = bookSeeded.Last().BookId;
            }

            using (var context = new EfCoreContext(options))
            {
                var utData  = context.SetupSingleDtoAndEntities <AddReviewWithIncludeDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new AddReviewWithIncludeDto
                {
                    BookId = bookId, Comment = "bad", NumStars = 1, VoterName = "user"
                };
                await service.UpdateAndSaveAsync(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                var entity = context.Books.Include(x => x.Reviews).Single(x => x.BookId == bookId);
                entity.Reviews.Count().ShouldEqual(3);
                entity.Reviews.Single(x => x.Comment == "bad").NumStars.ShouldEqual(1);
            }
        }
        public async Task TestDbQueryReadSingleWhereDirectOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                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();
            }
        }