public void TryToSubstractStockWhenProductHasStock() { var context = ApplicationDbContextInMemory.Get(); var productInStockId = 1; var productId = 1; context.Stocks.Add(new ProductInStock { ProductInStockId = productInStockId, ProductId = productId, Stock = 1 }); context.SaveChanges(); var handler = new ProductInStockUpdateStockEventHandler(context, GetLogger); handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = 1, Action = ProductInStockAction.Substract } } }, new CancellationToken()).Wait(); }
public void TryToAddStockWhenProductExists() { var context = CatalogDbContextInMemory.Get(); var productInStockId = 3; var productId = 3; // Add product context.Stocks.Add(new ProductInStock { ProductInStockId = productInStockId, ProductId = productId, Stock = 1 }); context.SaveChanges(); var command = new ProductInStockUpdateStockEventHandler(context, GetLogger); command.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem> { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = ProductInStockAction.Add } } }, new CancellationToken()).Wait(); Assert.AreEqual(context.Stocks.First(x => x.ProductInStockId == productInStockId).Stock, 3); }
public void TryToAddStockWhenProductExists() { var context = ApplicationDbContextInMemory.Get(); var productInStockId = 3; var productId = 3; context.Stocks.Add(new ProductInStock { ProductInStockId = productInStockId, ProductId = productId, Stock = 1 }); context.SaveChanges(); var handler = new ProductInStockUpdateStockEventHandler(context, GetLogger); handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = ProductInStockAction.Add } } }, new CancellationToken()).Wait(); var stockInDb = context.Stocks.Single(x => x.ProductId == productId).Stock; Assert.AreEqual(stockInDb, 3); }
public async Task TryToSubstractStockWhenProductHasStock() { var dbContext = ApplicationDbContextInMemory.Get(); var productInStockId = 1; var productId = 1; dbContext.Stocks.Add(new ProductInStock { ProductId = productId, ProductInStockId = productInStockId, Stock = 1 }); dbContext.SaveChanges(); var handler = new ProductInStockUpdateStockEventHandler(dbContext, logger); await handler.Handle(new ProductInStockUpdateStockCommand() { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem() { ProductId = productId, Stock = 1, Action = ProductInStockAction.Substract } } }, CancellationToken.None); var p = dbContext.Stocks.Find(productId); Assert.Equals(p.Stock, 0); }
public void TryToAddStockWhenProductNotExists() { ApplicationDBContext context = ApplicationDbContextInMemory.Get(); int productId = 4; ProductInStockUpdateStockEventHandler handler = new ProductInStockUpdateStockEventHandler(context, GetLogger); handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = Common.ProductInStockAction.Add } } }, new CancellationToken()).Wait(); var stockInDb = context.Stocks.Single(item => item.ProductId == productId).Stock; Assert.AreEqual(stockInDb, 2); }
public void TryToSubstractStockWhenProductHasStock() { var _context = ApplicationDbContextInMemory.Get(); int productId = 1; int productInStockId = 1; double stock = 1; _context.Stocks.Add(new Domain.ProductInStock { ProductId = productId, ProductInStockId = productInStockId, Stock = stock }); _context.SaveChanges(); var handler = new ProductInStockUpdateStockEventHandler(_context, GetLogger); handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = stock, Action = Common.Enums.ProductInStockAction.Substract } } }, new System.Threading.CancellationToken()).Wait(); }
public void TryToAddStockWhenProductExists() { var _context = ApplicationDbContextInMemory.Get(); int productId = 5; int productInStockId = 5; double stock = 1; _context.Stocks.Add(new Domain.ProductInStock { ProductId = productId, ProductInStockId = productInStockId, Stock = stock }); _context.SaveChanges(); var handler = new ProductInStockUpdateStockEventHandler(_context, GetLogger); handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = Common.Enums.ProductInStockAction.Add } } }, new System.Threading.CancellationToken()).Wait(); var stockInDb = _context.Stocks.Single(x => x.ProductId == productId).Stock; Assert.AreEqual(stockInDb, 3); }
public async Task TryToSubstractStockWhenProductHasStock() { var context = ApplicationDbContextInMemory.Get(); var productInStockId = 1; var productId = 1; // Add product context.Stocks.Add(new ProductInStock { ProductInStockId = productInStockId, ProductId = productId, Stock = 1 }); context.SaveChanges(); var command = new ProductInStockUpdateStockEventHandler(context, GetIlogger); await command.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem> { new ProductInStockUpdateItem { ProductId = 1, Stock = 1, Action = Common.Enums.ProductInStockAction.Substract } } }, new System.Threading.CancellationToken()); }
public void TryToSubstractStockWhenProductHasntStock() { var context = ApplicationDbContextInMemory.Get(); var productInStockId = 2; var productId = 2; context.Stocks.Add(new ProductInStock { ProductInStockId = productInStockId, ProductId = productId, Stock = 1 }); context.SaveChanges(); var handler = new ProductInStockUpdateStockEventHandler(context, GetLogger); try { handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = ProductInStockAction.Substract } } }, new CancellationToken()).Wait(); } catch (AggregateException ae) { var exception = ae.GetBaseException(); if (exception is ProducInStockUpdateStockCommandException) { throw new ProducInStockUpdateStockCommandException(exception?.InnerException?.Message); } } }
public void TryToAddStockWhenProductNotExists() { var context = ApplicationDbContextInMemory.Get(); var command = new ProductInStockUpdateStockEventHandler(context, GetIlogger); var productId = 4; command.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem> { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = Common.Enums.ProductInStockAction.Add } } }, new System.Threading.CancellationToken()).Wait(); Assert.AreEqual(context.Stocks.First(x => x.ProductId == productId).Stock, 2); }
public void TryToSubstractStockWhenProductHasntStock() { var _context = ApplicationDbContextInMemory.Get(); int productId = 6; int productInStockId = 6; double stock = 1; _context.Stocks.Add(new Domain.ProductInStock { ProductId = productId, ProductInStockId = productInStockId, Stock = stock }); _context.SaveChanges(); try { var handler = new ProductInStockUpdateStockEventHandler(_context, GetLogger); handler.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Stock = 3, Action = Common.Enums.ProductInStockAction.Substract } } }, new System.Threading.CancellationToken()).Wait(); } catch (AggregateException ae) { var exception = ae.GetBaseException(); if (exception is ProductInStockUpdateStockCommandException) { throw new ProductInStockUpdateStockCommandException(exception?.InnerException?.Message); } } }
public void TryToSubstractStockWhenProductHasntStock() { var context = ApplicationDbContextInMemory.Get(); var productInStockId = 2; var productId = 2; // Add product context.Stocks.Add(new ProductInStock { ProductInStockId = productInStockId, ProductId = productId, Stock = 1 }); context.SaveChanges(); var command = new ProductInStockUpdateStockEventHandler(context, GetIlogger); try { command.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem> { new ProductInStockUpdateItem { ProductId = productId, Stock = 2, Action = Common.Enums.ProductInStockAction.Substract } } }, new System.Threading.CancellationToken()).Wait(); } catch (AggregateException ae) { if (ae.GetBaseException() is ProductInStockUpdateStockCommandException) { throw new ProductInStockUpdateStockCommandException(ae.InnerException?.Message); } } }
public void TryToAddStockWhenProductNotExist() { var context = ApplicationDbContextInMemory.Get(); var productId = 4; var handle = new ProductInStockUpdateStockEventHandler(context, GetLogger); handle.Handle(new ProductInStockUpdateStockCommand { Items = new List <ProductInStockUpdateItem>() { new ProductInStockUpdateItem { ProductId = productId, Action = Common.Enums.ProductInStockAction.Add, Stock = 2 } } }, new CancellationToken()).Wait(); var stockInDb = context.Stocks.Single(x => x.ProductId == productId).Stock; Assert.AreEqual(stockInDb, 2); }