Esempio n. 1
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 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);
            }
        }
Esempio n. 3
0
        public async Task TestCreateNoSqlErrorHandler()
        {
            //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 utData  = context.SetupSingleDtoAndEntities <UniqueWithConfigDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <DbUpdateException>(() =>
                                                                      service.CreateAndSaveAsync(new UniqueEntity {
                    UniqueString = "Hello"
                }));

                //VERIFY
                ex.InnerException.Message.ShouldEqual(
                    "SQLite Error 19: 'UNIQUE constraint failed: UniqueEntities.UniqueString'.");
            }
        }
        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();
            }
        }
        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 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);
            }
        }
Esempio n. 7
0
        public async Task TestBeforeSaveChangesMethodProvidedWithError()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

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

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

                //ATTEMPT
                await service.CreateAndSaveAsync(new NormalEntity { MyString = "bad word" });

                //VERIFY
                service.IsValid.ShouldBeFalse();
                service.GetAllErrors().ShouldEqual("The NormalEntity class contained a bad word.");
            }
            using (var context = new TestDbContext(options))
            {
                context.NormalEntities.Count().ShouldEqual(0);
            }
        }
        public async Task TestCreateEntityOk()
        {
            //SETUP
            var unique  = Guid.NewGuid().ToString();
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
            }
            using (var context = new EfCoreContext(options))
            {
                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

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

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
            }
            using (var context = new EfCoreContext(options))
            {
                context.Authors.Count().ShouldEqual(1);
                context.Authors.Find(1).Email.ShouldEqual(unique);
            }
        }
Esempio n. 9
0
        public async Task TestSqlErrorHandlerWorksOkAsync(int sqlErrorCode, bool shouldThrowException)
        {
            IStatusGeneric CatchUniqueError(Exception e, DbContext context)
            {
                var dbUpdateEx  = e as DbUpdateException;
                var sqliteError = dbUpdateEx?.InnerException as SqliteException;

                return(sqliteError?.SqliteErrorCode == sqlErrorCode
                    ? new StatusGenericHandler().AddError("Unique constraint failed")
                    : null);
            }

            //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 = CatchUniqueError
                };
                var utData  = context.SetupSingleDtoAndEntities <UniqueWithConfigDto>(config);
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                try
                {
                    await service.CreateAndSaveAsync(new UniqueWithConfigDto { UniqueString = "Hello" });
                }
                //VERIFY
                catch (Exception)
                {
                    shouldThrowException.ShouldBeTrue();
                    return;
                }

                shouldThrowException.ShouldBeFalse();
                service.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }
        public async Task TestDbQueryCreateFail()
        {
            //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.CreateAndSaveAsync(new ChildReadOnly()));

                //VERIFY
                ex.Message.ShouldEqual("The class ChildReadOnly of style DbQuery cannot be used in Create.");
            }
        }
        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);
            }
        }
Esempio n. 12
0
        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 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());
            }
        }
Esempio n. 14
0
        public async Task TestCreateBookDto()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <BookDbContext>();

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

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

            //ATTEMPT
            var dto = new CreateBookDto
            {
                Title         = "New book", ImageUrl = "link", Price = 123, PublishedOn = new DateTime(2020, 1, 1),
                EstimatedDate = false, Publisher = "Test", Authors = new [] { new Author("Author1", null) }
            };
            await service.CreateAndSaveAsync(dto);

            //VERIFY
            var book = context.Books.Single();

            book.ToString().ShouldEqual($"New book: by Author1. Price 123, 0 reviews. Published {new DateTime(2020, 1, 1):d}");
        }
        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.");
            }
        }
        public async Task TestCreateAuthorNameNullNoValidationOk()
        {
            //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 = null, Email = unique
                };
                var ex = await Assert.ThrowsAsync <Microsoft.EntityFrameworkCore.DbUpdateException> (() => service.CreateAndSaveAsync(author));

                //VERIFY
                ex.InnerException?.Message.ShouldEqual("SQLite Error 19: 'NOT NULL constraint failed: Authors.Name'.");
            }
        }