示例#1
0
        public void TestCreateEntityViaDtoCtorDddKeyIsString()
        {
            //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 <DddCompositeIntStringCreateDto>();
                var service = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new DddCompositeIntStringCreateDto {
                    MyString = "Hello", MyInt = 1
                };
                service.CreateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully created a Ddd Composite Int String");
                context.DddCompositeIntStrings.Single().MyString.ShouldEqual("Hello");
                context.DddCompositeIntStrings.Single().MyInt.ShouldEqual(1);
            }
        }
        public void 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 CrudServices(context, utData.ConfigAndMapper);

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

                //VERIFY
                service.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }
示例#3
0
        public void TestNestedDtosV2Ok()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

                var utData = context.SetupSingleDtoAndEntities <BookListNestedV2Dto>();
                utData.AddSingleDto <AuthorNestedV2Dto>();
                var service = new CrudServices(context, utData.Wrapped);

                //ATTEMPT
                var dto = service.ReadSingle <BookListNestedV2Dto>(1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.AuthorsLink.Count.ShouldEqual(2);
                dto.AuthorsLink.First().AStringToHoldAuthorName.ShouldStartWith("Author");
                dto.AuthorsLink.First().Order.ShouldEqual((byte)0);
                dto.AuthorsLink.Last().AStringToHoldAuthorName.ShouldEqual("CommonAuthor");
                dto.AuthorsLink.Last().Order.ShouldEqual((byte)1);
            }
        }
        public void TestPutDifficultyValidationError()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

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

                var controller = new ToDoController();
                var utData     = context.SetupSingleDtoAndEntities <ChangeDifficultyDto>(_genericServiceConfig);
                var service    = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new ChangeDifficultyDto()
                {
                    Id         = 1,
                    Difficulty = 99,
                };
                var response = controller.Difficulty(dto, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(CreateResponse.ErrorsStatusCode);
                var rStatus = response.CopyToStatus();
                rStatus.IsValid.ShouldBeFalse();
                rStatus.GetAllErrors().ShouldEqual("The field Difficulty must be between 1 and 5.");
            }
        }
        public void 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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var ex = Assert.Throws <DbUpdateException>(() => service.DeleteAndSave <Book>(firstBook.BookId));

                //VERIFY
                ex.InnerException.Message.ShouldEqual("SQLite Error 19: 'FOREIGN KEY constraint failed'.");
            }
        }
示例#6
0
        public void TestCreateTestAbstractMainOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

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

                var utData  = context.SetupSingleDtoAndEntities <TestAbstractCreate>();
                var service = new CrudServices(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

                //ATTEMPT
                var dto = new TestAbstractCreate
                {
                    AbstractPropPrivate   = "Base Private",
                    AbstractPropPublic    = "Base Public",
                    NotAbstractPropPublic = "Main Public"
                };
                service.CreateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.Id.ShouldEqual(1);
                context.TestAbstractMains.Single().AbstractPropPrivate.ShouldEqual("Base Private");
                context.TestAbstractMains.Single().AbstractPropPublic.ShouldEqual("Base Public");
                context.TestAbstractMains.Single().NotAbstractPropPublic.ShouldEqual("Main Public");
            }
        }
        public void TestDtoAccessUpdateWithValidationTurnedOnViaGlobalConfig()
        {
            //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()
                {
                    DtoAccessValidateOnSave     = true,
                    SaveChangesExceptionHandler = CatchUniqueError19
                };
                var utData  = context.SetupSingleDtoAndEntities <UniqueNoConfigDto>(config);
                var service = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                service.UpdateAndSave(new UniqueNoConfigDto()
                {
                    Id = entity.Id, UniqueString = "Hello"
                });

                //VERIFY
                service.GetAllErrors().ShouldEqual("Unique Entity: Unique constraint failed");
            }
        }
示例#8
0
        public void TestUpdateAddPromotionWithMessageOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

                //ATTEMPT
                var utData  = context.SetupSingleDtoAndEntities <AddRemovePromotionDto>();
                var service = new CrudServices(context, utData.Wrapped);

                //ATTEMPT
                var dto = new AddRemovePromotionDto {
                    BookId = 1, ActualPrice = 1, PromotionalText = "Really Cheap!"
                };
                service.UpdateAndSave(dto, nameof(Book.AddPromotion));

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("The book's new price is $1.00.");
            }
        }
示例#9
0
        public void 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 CrudServices(context, utData.Wrapped);

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

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                var entity = context.Books.Find(4);
                entity.ActualPrice.ShouldEqual(220);
            }
        }
示例#10
0
        public void TestCreateAuthorViaAutoMapperPrimaryKeyCopiedBackOk()
        {
            //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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new AuthorDto {
                    Name = "New Name", Email = unique
                };
                service.CreateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully created a Author");
                dto.AuthorId.ShouldNotEqual(0);
            }
        }
示例#11
0
        public void 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 CrudServices(context, utData.Wrapped);

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

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully updated the Book");
                context.Set <Review>().Count().ShouldEqual(3);
            }
        }
示例#12
0
        public void TestCreateAuthorViaAutoMapperOk()
        {
            //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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                using (new TimeThings(_output, "CreateAndSave"))
                {
                    var author = new AuthorDto {
                        Name = "New Name", Email = unique
                    };
                    service.CreateAndSave(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);
            }
        }
示例#13
0
        public void 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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new DtoStaticCreate {
                    MyInt = 1, MyString = null
                };
                service.CreateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeFalse();
                service.GetAllErrors().ShouldEqual("The string should not be null.");
                context.DddStaticFactEntities.Count().ShouldEqual(0);
            }
        }
示例#14
0
        public void TestCreateEntityUsingStaticCreatorOk()
        {
            //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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new DtoStaticCreate {
                    MyInt = 1, MyString = "Hello"
                };
                service.CreateAndSave(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);
            }
        }
示例#15
0
        public void TestCreateViaDtoWithAbstractBaseOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

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

                var utData  = context.SetupSingleDtoAndEntities <DddCtorEntityAbstractMainDto>();
                var service = new CrudServices(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

                //ATTEMPT
                var dto = new DddCtorEntityAbstractMainDto()
                {
                    MyString = "Test",
                    MyInt    = 123
                };
                service.CreateAndSave(dto, "ctor(2)");

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                context.DddCtorEntities.Single().Id.ShouldEqual(1);
                context.DddCtorEntities.Single().MyString.ShouldEqual("Test");
                context.DddCtorEntities.Single().MyInt.ShouldEqual(123);
            }
        }
示例#16
0
        public void 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 CrudServices(context, utData.Wrapped);

                //ATTEMPT
                var dto = new AuthorDto {
                    AuthorId = 1, Name = "New Name", Email = "*****@*****.**"
                };
                service.UpdateAndSave(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);
            }
        }
示例#17
0
        public void TestUpdateAbstractPropViaMethod_DOES_NOT_WORK()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                context.Add(new TestAbstractMain(null, null, null));
                context.SaveChanges();

                var utData  = context.SetupSingleDtoAndEntities <TestAbstractUpdateViaMethod>();
                var service = new CrudServices(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

                //ATTEMPT
                var dto = new TestAbstractUpdateViaMethod {
                    Id = 1, AbstractPropPrivate = "Test"
                };
                service.UpdateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                context.TestAbstractMains.Single().AbstractPropPrivate.ShouldEqual(null);
            }
        }
示例#18
0
        public void 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 CrudServices(context, utData.Wrapped);

                //ATTEMPT
                var dto = new Tests.Dtos.ChangePubDateDto {
                    BookId = 4, PublishedOn = new DateTime(2000, 1, 1)
                };
                service.UpdateAndSave(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));
            }
        }
        public void TestDirectAccessInvalidOperationExceptionOnSameKey()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                context.KeyIsStrings.Add(new KeyIsString {
                    Id = "Hello"
                });
                context.SaveChanges();
            }
            using (var context = new TestDbContext(options))
            {
                var utData  = context.SetupSingleDtoAndEntities <UniqueNoConfigDto>();
                var service = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var ex = Assert.Throws <DbUpdateException>(() =>
                                                           service.CreateAndSave(new KeyIsString {
                    Id = "Hello"
                }));

                //VERIFY
                ex.InnerException.Message.ShouldEqual(
                    "SQLite Error 19: 'UNIQUE constraint failed: KeyIsStrings.Id'.");
            }
        }
        public void TestJsonPatchOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

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

                var controller = new ToDoHybridController();
                var utData     = context.SetupEntitiesDirect(_genericServiceConfig);
                var service    = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var patch = new JsonPatchDocument <TodoItemHybrid>();
                patch.Replace(x => x.Difficulty, 5);
                var response = controller.Update(1, patch, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(CreateResponse.OkStatusCode);
                var rStatus = response.CopyToStatus();
                rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
                rStatus.Message.ShouldEqual("Successfully updated the Todo Item Hybrid");
                context.TodoItemHybrids.First().Difficulty.ShouldEqual(5);
            }
        }
        public void TestPutNameOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

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

                var controller = new ToDoController();
                var utData     = context.SetupSingleDtoAndEntities <ChangeNameDto>(_genericServiceConfig);
                var service    = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new ChangeNameDto()
                {
                    Id   = 2,
                    Name = "Test",
                };
                var response = controller.Name(dto, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(CreateResponse.OkStatusCode);
                var rStatus = response.CopyToStatus();
                rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
                rStatus.Message.ShouldEqual("Successfully updated the Todo Item");
            }
        }
        public void 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 CrudServices(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

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

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

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.Id.ShouldEqual(1);
            }
        }
        public void TestDeleteOK()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

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

                var controller = new ToDoController();
                var utData     = context.SetupSingleDtoAndEntities <ChangeDifficultyDto>(_genericServiceConfig);
                var service    = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var response = controller.Delete(2, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(CreateResponse.OkStatusCode);
                var rStatus = response.CopyToStatus();
                rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
                rStatus.Message.ShouldEqual("Successfully deleted a Todo Item");
                context.TodoItems.Count().ShouldEqual(5);
            }
        }
示例#24
0
        public void TestInDtosNestedOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

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

                var utData = context.SetupSingleDtoAndEntities <InContactAddressDto>();
                utData.AddSingleDto <InAddressDto>();
                var service = new CrudServices(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new TestDbContext(options)));

                //ATTEMPT
                var dto = new InContactAddressDto
                {
                    Name   = "test",
                    Addess = new InAddressDto
                    {
                        Address1 = "some street"
                    }
                };
                service.CreateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                var contact = context.ContactAddresses.SingleOrDefault();
                contact.Name.ShouldEqual("test");
                contact.Address.Address1.ShouldEqual("some street");
            }
        }
        public void 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 CrudServices(context, utData.ConfigAndMapper);

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

                //VERIFY
                service.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }
        public void TestQueryFilterWorksOnShopStock()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <CompanyDbContext>();

            using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("accessKey*")))
            {
                context.Database.EnsureCreated();
                var company = Company.AddTenantToDatabaseWithSaveChanges("TestCompany", PaidForModules.None, context);
                var shop    = RetailOutlet.AddTenantToDatabaseWithSaveChanges("TestShop", company, context);
                var stock   = new ShopStock {
                    Name = "dress", NumInStock = 5, Shop = shop
                };
                context.Add(stock);
                context.SaveChanges();

                var utData  = context.SetupSingleDtoAndEntities <SellItemDto>();
                var service = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new SellItemDto
                {
                    TenantItemId = shop.TenantItemId,
                    ShopStockId  = stock.ShopStockId,
                    NumBought    = 1
                };
                var shopSale = service.CreateAndSave(dto);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                context.ShopSales.Count().ShouldEqual(1);
                context.ShopStocks.Single().NumInStock.ShouldEqual(4);
            }
        }
        public void TestUpdateNoSqlErrorHandlerOn()
        {
            //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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                entity.UniqueString = "Hello";
                var ex = Assert.Throws <DbUpdateException>(() => service.UpdateAndSave(entity));

                //VERIFY
                ex.InnerException.Message.ShouldEqual(
                    "SQLite Error 19: 'UNIQUE constraint failed: UniqueEntities.UniqueString'.");
            }
        }
示例#28
0
        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 TestCallAddReviewWithIncludeWithIncludedReviews()
        {
            //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 CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = new AddReviewWithIncludeDto
                {
                    BookId = bookId, Comment = "bad", NumStars = 1, VoterName = "user"
                };
                service.UpdateAndSave(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);
            }
        }
示例#30
0
        public void TestCreateEntityViaDtoCtorCreateCtor2Ok()
        {
            //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 <DtoCtorCreate>();
                var service = new CrudServices(context, utData.ConfigAndMapper);

                //ATTEMPT
                using (new TimeThings(_output, "CreateAndSave"))
                {
                    var dto = new DtoCtorCreate {
                        MyInt = 123, MyString = "Hello"
                    };
                    service.CreateAndSave(dto, "ctor(2)");
                }

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual("Successfully created a Ddd Ctor Entity");
            }
            using (var context = new TestDbContext(options))
            {
                context.DddCtorEntities.Count().ShouldEqual(1);
                context.DddCtorEntities.Find(1).MyInt.ShouldEqual(123);
                context.DddCtorEntities.Find(1).MyString.ShouldEqual("Hello");
            }
        }