Exemple #1
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");
            }
        }
Exemple #2
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'.");
            }
        }
        public async Task TestDeleteEntityOk()
        {
            //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.DeleteAndSaveAsync <Book>(1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
            }
            using (var context = new EfCoreContext(options))
            {
                context.Books.Count().ShouldEqual(3);
            }
        }
Exemple #4
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);
            }
        }
        public async Task TestDbQueryDeleteFail()
        {
            //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.DeleteAndSaveAsync <ChildReadOnly>(1));

                //VERIFY
                ex.Message.ShouldEqual("The class ChildReadOnly of style DbQuery cannot be used in Delete.");
            }
        }