public SkuStock_added_tests()// Given_sku_created_and_stock_added_messages_When_projected()
        {
            _stockCreatedEvent = new SkuStockCreated(Guid.NewGuid(), Guid.NewGuid(), 100, TimeSpan.FromDays(2));
            _stockAddedEvent   = new StockAdded(_stockCreatedEvent.SourceId, 15, "test pack");

            ProjectionBuilder.Handle(_stockCreatedEvent).Wait();
            ProjectionBuilder.Handle(_stockAddedEvent).Wait();
        }
Beispiel #2
0
        public SkuStock_reserve_expired_tests()// Given_sku_created_and_stock_added_and_stock_reserved_messages_When_projected()
        {
            var stockId = Guid.NewGuid();

            _stockCreatedEvent    = new SkuStockCreated(stockId, Guid.NewGuid(), 1, TimeSpan.FromDays(2));
            _stockAddedEvent      = new StockAdded(stockId, 15, "test pack");
            _stockReservedEvent   = new StockReserved(stockId, Guid.NewGuid(), DateTime.Now.AddDays(1), 7);
            _reserveCanceledEvent = new ReserveExpired(stockId, _stockReservedEvent.ReserveId);

            ProjectionBuilder.Handle(_stockCreatedEvent).Wait();
            ProjectionBuilder.Handle(_stockAddedEvent).Wait();
            ProjectionBuilder.Handle(_stockReservedEvent).Wait();
            ProjectionBuilder.Handle(_reserveCanceledEvent).Wait();
        }
Beispiel #3
0
        public async Task When_adding_stock_Aggregate_quantity_should_be_increased_by_command_amount()
        {
            AddToStockCommand command;
            SkuStockCreated   created;
            StockAdded        added;
            var id       = Guid.NewGuid();
            var scenario = await AggregateScenario.New <SkuStock, SkuStockCommandsHandler>()
                           .Given(created = new SkuStockCreated(id, _skuId, 50, TimeSpan.FromMilliseconds(100)),
                                  added   = new StockAdded(id, 10, "test batch 2"))
                           .When(command  = new AddToStockCommand(id, _skuId, 10, "test batch"))
                           .Then(new StockAdded(id, command.Quantity, command.BatchArticle))
                           .Run();

            Assert.Equal(command.Quantity + created.Quantity + added.Quantity, scenario.Aggregate.Quantity);
        }
        public async Task Given_sku_created_message_double_projected_When_project_again_additioanal_transaction_occures()
        {
            var stockCreatedEvent = new Fixture().Create <SkuStockCreated>();
            var stockAddedEvent   = new StockAdded(stockCreatedEvent.SourceId, 15, "test pack");

            await ProjectionBuilder.Handle(stockCreatedEvent);

            await ProjectionBuilder.Handle(stockAddedEvent);

            await ProjectionBuilder.Handle(stockAddedEvent);

            using (var context = CreateContext())
            {
                Assert.Equal(3, context.StockHistory.Count());
            }
        }
        public async Task Given_sku_created_and_stock_added_and_stock_reserved_messages_When_projected_twice()
        {
            var stockId = Guid.NewGuid();

            var stockCreatedEvent    = new SkuStockCreated(stockId, Guid.NewGuid(), 1, TimeSpan.FromDays(2));
            var stockAddedEvent      = new StockAdded(stockId, 15, "test pack");
            var stockReservedEvent   = new StockReserved(stockId, Guid.NewGuid(), DateTime.Now.AddDays(1), 7);
            var reserveCanceledEvent = new ReserveCanceled(stockId, stockReservedEvent.ReserveId);

            await ProjectionBuilder.Handle(stockCreatedEvent);

            await ProjectionBuilder.Handle(stockAddedEvent);

            await ProjectionBuilder.Handle(stockReservedEvent);

            await ProjectionBuilder.Handle(reserveCanceledEvent);

            await ProjectionBuilder.Handle(reserveCanceledEvent)
            .ShouldThrow <ReserveEntryNotFoundException>();
        }
        public async Task Handle(StockAdded msg, IMessageMetadata metadata = null)
        {
            using (var context = _contextFactory())
            {
                var skuStock = await context.SkuStocks.FindAsync(msg.SourceId);

                if (skuStock == null)
                {
                    throw new SkuStockEntryNotFoundException(msg.SourceId);
                }

                var history = CreateHistory(skuStock, StockOperation.Added, msg.Quantity);

                skuStock.AvailableQuantity += msg.Quantity;
                skuStock.TotalQuantity     += msg.Quantity;
                skuStock.LastModified       = msg.CreatedTime;

                FillNewQuantities(history, skuStock);

                context.StockHistory.Add(history);
                await context.SaveChangesAsync();
            }
        }