public void ShouldEditClientStockAmount() { // Arrange ClientStockService clientStockService = new ClientStockService(clientStockTableRepository); clientStockTableRepository.ContainsByPK(1, 1).Returns(true); int clientId = 1; int stockId = 1; int amount = 5; ClientStock clientStock = new ClientStock() { ClientID = 1, StockID = 1, Quantity = 1000, }; // Act clientStockService.EditClientStocksAmount(clientId, stockId, amount); // Assert clientStockTableRepository.Received(1).FindByPK(clientId, stockId); clientStock.Quantity += amount; clientStockTableRepository.Received(1).SaveChanges(); }
public void RunTraiding() { int loopcount = 10; for (int i = 0; i < loopcount; i++) { int amountInLotForSale = 10; //Select random saler Client saler = GetRandomClient(); //Select random stock for saler ClientStock clstock = GetRandomClientStock(saler.ClientID); if (clstock == null || clstock.Quantity < 10) { continue; } //determine amount for sale int lotsAmount = clstock.Quantity / amountInLotForSale; Random random = new Random(); int amountForSale = random.Next(1, lotsAmount) * amountInLotForSale; orderService.AddOrder(new OrderInfo() { ClientId = clstock.ClientID, StockId = clstock.StockID, Quantity = amountForSale, OrderType = OrderInfo.OrdType.Sale }); this.logger.Info($"Order for sale stock {clstock.StockID} for client {clstock.ClientID} has been added to DB"); Order salerOrder = orderService.LastOrder(); Client customer; do { customer = GetRandomClient(); }while (customer.ClientID == saler.ClientID); orderService.AddOrder(new OrderInfo() { ClientId = customer.ClientID, StockId = clstock.StockID, Quantity = amountForSale, OrderType = OrderInfo.OrdType.Purchase }); this.logger.Info($"Order for purchasing stock {clstock.StockID} for client {customer.ClientID} has been added to DB"); Order customerOrder = orderService.LastOrder(); DateTime dealDateTime = DateTime.Now; decimal dealPrice = priceHistoryService.GetStockPriceByDateTime(new PriceArguments() { StockId = clstock.StockID, DateTimeLookUp = dealDateTime }); clientService.EditClientBalance(saler.ClientID, (dealPrice * amountForSale)); this.logger.Info($"Client {saler.ClientID} balance has been increased by {(dealPrice * amountForSale)}"); clientService.EditClientBalance(customer.ClientID, (-1 * dealPrice * amountForSale)); this.logger.Info($"Client {customer.ClientID} balance has been reduced by {(dealPrice * amountForSale)}"); clientStockService.EditClientStocksAmount(saler.ClientID, clstock.StockID, -amountForSale); this.logger.Info($"Client {saler.ClientID} stock {salerOrder.StockID} amount has been reduced on {amountForSale}"); clientStockService.EditClientStocksAmount(customer.ClientID, clstock.StockID, amountForSale); this.logger.Info($"Client {customer.ClientID} stock {salerOrder.StockID} amount has been increased on {amountForSale}"); transactionHistoryService.AddTransactionInfo(new TransactionInfo() { CustomerOrderId = customerOrder.OrderID, SalerOrderId = salerOrder.OrderID, TrDateTime = dealDateTime }); this.logger.Info($"Transaction has been added to DB"); orderService.SetIsExecuted(salerOrder); this.logger.Info($"Saler's order {salerOrder.OrderID} status has been set as 'IsExecuted'"); orderService.SetIsExecuted(customerOrder); this.logger.Info($"Customer's order {customerOrder.OrderID} status has been set as 'IsExecuted'"); this.logger.Info($"Deal is finished"); this.SimulatePriceChange(salerOrder.StockID, dealPrice, dealDateTime); this.logger.Info($"Stock {salerOrder.StockID} price has been changed'"); Thread.Sleep(10000); } }