public BiasTradeEventResult UpdateTradeRow(BiasTradeEvent tradeEvent)
 {
     var biasReport = _biasReportRepository.GetBiasReport(tradeEvent.BiasSessionId);
     var biasRow = biasReport.BiasRows.First(br => br.Ticker == tradeEvent.Ticker);
     biasRow.TradeCount = tradeEvent.TradeAmount;
     biasRow.BenchmarkWeight = tradeEvent.TradeAmount * .02m;
     _biasReportRepository.SaveBiasRow(biasRow);
     var sectorList = biasReport.BiasRows
         .GroupBy(br => br.Sector)
         .Select(g => new Sector { Name = g.Key, Weight = g.Sum(br => br.BenchmarkWeight) });
     return new BiasTradeEventResult { BiasRow = biasRow, Sectors = sectorList.ToList() };
 }
        public void WhenTradeEventIsReceivedBiasServiceIsNotifiedAndResultingBiasRowIsReturned()
        {
            var service = Substitute.For<IBiasReportService>();
            var tradeEvent = new BiasTradeEvent {Ticker = "A", BiasSessionId = 3, TradeAmount = 5};
            var biasTradeEventResult = new BiasTradeEventResult();
            service.UpdateTradeRow(tradeEvent).Returns(biasTradeEventResult);

            var controller = new BiasTradeController(service);
            var row = controller.PostBiasTrade(tradeEvent);

            service.Received().UpdateTradeRow(tradeEvent);
            Assert.That(row,Is.SameAs(biasTradeEventResult));
        }
        public void WhenTradeEventIsReceivedBiasRowIsRetrieivedAndBenchmarkWeightIsCalculatedAndBiasRowIsSaved()
        {
            int biasReportSessionId = 100;
            string ticker = "A";
            int tradeCount = 5;

            var repository = Substitute.For<IBiasReportRepository>();
            var existingBiasRow = new BiasRow {BiasSessionId = biasReportSessionId,TradeCount = 0, Ticker = "1"};
            repository.GetBiasRow(biasReportSessionId, ticker).Returns(existingBiasRow);

            var tradeEvent = new BiasTradeEvent { BiasSessionId = biasReportSessionId, Ticker = ticker, TradeAmount = tradeCount };

            var service = new BiasReportService(repository);
            service.UpdateTradeRow(tradeEvent);

            repository.Received().SaveBiasRow(existingBiasRow);
            Assert.That(existingBiasRow.TradeCount,Is.EqualTo(5));
            Assert.That(existingBiasRow.BenchmarkWeight, Is.EqualTo(tradeCount * .02m));
        }
 public BiasTradeEventResult PostBiasTrade(BiasTradeEvent tradeEvent)
 {
     return _biasReportService.UpdateTradeRow(tradeEvent);
 }