Example #1
0
        public void TestSqlErrorHandlerNotInPlace()
        {
            //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 GenericBizRunnerConfig {
                    TurnOffCaching = true
                };
                var bizInstance = new BizActionCheckSqlErrorHandlerWriteDb(context);
                var runner      = new ActionService <IBizActionCheckSqlErrorHandlerWriteDb>(context, bizInstance, _mapper, config);

                //ATTEMPT
                var ex = Assert.Throws <DbUpdateException>(() => runner.RunBizAction("Hello"));

                //VERIFY
                ex.InnerException.Message.ShouldEqual("SQLite Error 19: 'UNIQUE constraint failed: UniqueEntities.UniqueString'.");
            }
        }
Example #2
0
        public void TestSqlErrorHandlerWorksOk(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 GenericBizRunnerConfig
                {
                    TurnOffCaching = true,
                    SaveChangesExceptionHandler = CatchUniqueError
                };
                var utData = NonDiBizSetup.SetupDtoMapping <ServiceLayerBizInDto>(config);
                utData.AddDtoMapping <ServiceLayerBizOutDto>();
                var bizInstance = new BizActionCheckSqlErrorHandlerWriteDb(context);
                var runner      = new ActionService <IBizActionCheckSqlErrorHandlerWriteDb>(context, bizInstance, utData.WrappedConfig);

                //ATTEMPT
                try
                {
                    runner.RunBizAction("Hello");
                }
                //VERIFY
                catch (Exception e)
                {
                    shouldThrowException.ShouldBeTrue();
                    return;
                }

                shouldThrowException.ShouldBeFalse();
                runner.Status.GetAllErrors().ShouldEqual("Unique constraint failed");
            }
        }