public void Get_InvestmentDetails_Should_Return_NotFound_For_An_Invalid_InvestmentId() { // Mock the investment detail not being found InvestmentPerformance investmentPerformance = null; var mock = new Mock <IInvestmentRepository>(); mock.Setup(repo => repo.GetInvestmentDetail(0)).Returns(investmentPerformance); _investmentController = new InvestmentController(mock.Object); var result = _investmentController.GetInvestmentDetail(0); Assert.IsInstanceOf <NotFoundResult>(result); }
public InvestmentPerformance GetInvestmentDetail(int investmentId) { const string sql = @"SELECT sp.[Id], s.[Name], sp.Shares, sp.PurchaseCostPerShare, sp.Shares * s.Price As CurrentValue, s.Price As CurrentPrice, sp.PurchaseDate, (sp.Shares * s.Price) - (sp.Shares * sp.PurchaseCostPerShare) As NetGain FROM [Investment].[dbo].[StockPurchase] sp INNER JOIN [Investment].[dbo].[Stock] s on sp.StockId = s.Id WHERE sp.[Id] = @InvestmentId"; StockPurchase stockPurchase; using (var connection = new SqlConnection(_configuration["ConnectionStrings:DefaultConnection"])) { stockPurchase = connection.QuerySingleOrDefault <StockPurchase>(sql, new { InvestmentId = investmentId }); } InvestmentPerformance investmentPerformance = null; if (stockPurchase != null) { investmentPerformance = new InvestmentPerformance { Id = stockPurchase.Id, Name = stockPurchase.Name, Shares = stockPurchase.Shares, CostBasisPerShare = stockPurchase.PurchaseCostPerShare, CurrentValue = stockPurchase.CurrentValue, CurrentPrice = stockPurchase.CurrentPrice, Term = PerformanceCalculationHelper.GetTerm(stockPurchase.PurchaseDate), NetGain = PerformanceCalculationHelper.CalculateNetGain(stockPurchase.Shares, stockPurchase.CurrentPrice, stockPurchase.PurchaseCostPerShare), }; } return(investmentPerformance); }
public void Get_InvestmentDetails_Should_Return_Ok_For_A_Valid_InvestmentId() { // Mock the investment being found InvestmentPerformance investmentPerformance = new InvestmentPerformance { Id = 1, Name = "Test1", Shares = 100, CostBasisPerShare = 0.1M, CurrentValue = 20, CurrentPrice = 0.2M, Term = Term.Long, NetGain = 10, }; var mock = new Mock <IInvestmentRepository>(); mock.Setup(repo => repo.GetInvestmentDetail(1)).Returns(investmentPerformance); _investmentController = new InvestmentController(mock.Object); var result = _investmentController.GetInvestmentDetail(1); Assert.IsInstanceOf <OkObjectResult>(result); }