public void TestStopOnFirstBeforeHandlerThatHasAnError(bool stopOnFirst)
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var logs    = new List <LogOutput>();
            var config  = new GenericEventRunnerConfig
            {
                StopOnFirstBeforeHandlerThatHasAnError = stopOnFirst
            };
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>(logs, config);
            {
                var tax = new TaxRate(DateTime.Now, 6);
                context.Add(tax);

                //ATTEMPT
                tax.AddEvent(new EventTestBeforeReturnError());
                tax.AddEvent(new EventTestBeforeReturnError());
                var ex = Assert.Throws <GenericEventRunnerStatusException>(() => context.SaveChanges());

                //VERIFY
                context.StatusFromLastSaveChanges.IsValid.ShouldBeFalse();
                context.StatusFromLastSaveChanges.Errors.Count.ShouldEqual(stopOnFirst ? 1 : 2);
                logs.Count.ShouldEqual(stopOnFirst ? 1 : 4);
            }
        }
Beispiel #2
0
        public void TestDeDupBeforeEvents()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var logs    = new List <LogOutput>();
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>(logs);
            {
                var tax = new TaxRate(DateTime.Now, 123);
                context.Add(tax);
                context.SaveChanges();
                logs.Clear();

                //ATTEMPT
                var normalCount = 0;
                var deDepCount  = 0;
                tax.AddEvent(new TaxRateChangedEvent(123, () => normalCount++));
                tax.AddEvent(new TaxRateChangedEvent(123, () => normalCount++));
                tax.AddEvent(new DeDupEvent(() => deDepCount++));
                tax.AddEvent(new DeDupEvent(() => deDepCount++));

                context.SaveChanges();

                //VERIFY
                normalCount.ShouldEqual(2);
                deDepCount.ShouldEqual(1);
                foreach (var logOutput in logs)
                {
                    _output.WriteLine(logOutput.Message);
                }
            }
        }
Beispiel #3
0
        public async Task TestStopOnFirstBeforeHandlerThatHasAnError(bool stopOnFirst)
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var config  = new GenericEventRunnerConfig
            {
                StopOnFirstBeforeHandlerThatHasAnError = stopOnFirst
            };
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>(config: config);
            {
                var tax = new TaxRate(DateTime.Now, 6);
                context.Add(tax);

                //ATTEMPT
                tax.AddEvent(new EventTestBeforeReturnError());
                tax.AddEvent(new EventTestBeforeReturnError());
                var status = await context.SaveChangesWithStatusAsync();

                //VERIFY
                status.IsValid.ShouldBeFalse();
                status.Errors.Count.ShouldEqual(stopOnFirst ? 1 : 2);
                context.StatusFromLastSaveChanges.Errors.Count.ShouldEqual(stopOnFirst ? 1 : 2);
            }
        }
Beispiel #4
0
        public async Task TestCircularEventException()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>();
            {
                var tax = new TaxRate(DateTime.Now, 6);
                context.Add(tax);

                //ATTEMPT
                tax.AddEvent(new EventCircularEvent());
                var ex = await Assert.ThrowsAsync <GenericEventRunnerException>(async() => await context.SaveChangesAsync());

                //VERIFY
                ex.Message.ShouldStartWith("The BeforeSave event loop exceeded the config's MaxTimesToLookForBeforeEvents value of 6.");
            }
        }
Beispiel #5
0
        public async Task TestBeforeHandlerThrowsExceptionWithAttribute()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>();
            {
                var tax = new TaxRate(DateTime.Now, 6);
                context.Add(tax);

                //ATTEMPT
                tax.AddEvent(new EventTestExceptionHandlerWithAttribute());
                var ex = await Assert.ThrowsAsync <ApplicationException>(async() => await context.SaveChangesAsync());

                //VERIFY
                ex.Message.ShouldEqual(nameof(BeforeHandlerThrowsExceptionWithAttribute));
            }
        }
        public void TestBeforeHandlerThrowsException()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>();
            {
                var tax = new TaxRate(DateTime.Now, 6);
                context.Add(tax);

                //ATTEMPT
                tax.AddEvent(new EventTestBeforeExceptionHandler());
                var ex = Assert.Throws <ApplicationException>(() => context.SaveChanges());

                //VERIFY
                ex.Message.ShouldEqual(nameof(BeforeHandlerThrowsException));
            }
        }
Beispiel #7
0
        public async Task TestMissingHandlerThrowsException(EventToSend beforeAfter)
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();
            var context = options.CreateAndSeedDbWithDiForHandlers <OrderCreatedHandler>();
            {
                var tax = new TaxRate(DateTime.Now, 6);
                context.Add(tax);

                //ATTEMPT
                tax.AddEvent(new EventWithNoHandler(), beforeAfter);
                var ex = await Assert.ThrowsAsync <GenericEventRunnerException>(async() => await context.SaveChangesAsync());

                //VERIFY
                ex.Message.ShouldEqual(beforeAfter == EventToSend.BeforeSave
                    ? $"Could not find a BeforeSave event handler for the event {typeof(EventWithNoHandler).Name}."
                    : $"Could not find a AfterSave event handler for the event {typeof(EventWithNoHandler).Name}.");
            }
        }