public List <MonthCost> GetCosts(DateTime from, DateTime to, int organizationId) { List <MonthCost> monthCostList = new List <MonthCost>(); to = to.AddDays(1); //Need to add 1 day for correcly querying the database. //Acá se pide todo el año papa!!!!! PUEDE Y DEBE MEJORAR try { IEnumerable <OrganizationCost> ocList = this.orgCostRepo.GetByCondition(x => x.OrganizationId == organizationId && x.Year == from.Year); IEnumerable <CostHistory> chList = this.costHistRepo.GetByCondition(x => x.OrganizationId == organizationId); List <CostHistory> list = chList.ToList <CostHistory>(); foreach (var item in ocList) { CostHistory best = this.GetCostByClosetsMonthYear(list, item.Month, item.Year); PeriodCost pc = new PeriodCost() { month = item.Month, year = item.Year }; Cost bugsCost = new Cost() { cant = item.BugsAmount, unitCost = best.BugCost, }; Cost usersCost = new Cost() { cant = item.UsersAmount, unitCost = best.UserCost, }; MonthCost m = new MonthCost() { id = item.Id, bugs = bugsCost, users = usersCost, period = pc }; monthCostList.Add(m); } } catch (Exception e) { System.Console.WriteLine(e); } return(monthCostList); }
public async Task GetCostsTest() { DateTime from = new DateTime(2020, 1, 1); DateTime to = new DateTime(2020, 12, 1); List <MonthCost> returnedList = new List <MonthCost>(); PeriodCost periodMonth2 = new PeriodCost() { month = 2, year = 2020 }; Cost bugsCostMonth2 = new Cost() { cant = 100, unitCost = 2, }; Cost usersCostMonth2 = new Cost() { cant = 13, unitCost = 5, }; PeriodCost periodMonth5 = new PeriodCost() { month = 5, year = 2020 }; Cost bugsCostMonth5 = new Cost() { cant = 167, unitCost = 4, }; Cost usersCostMonth5 = new Cost() { cant = 7, unitCost = 6, }; MonthCost monthCost2 = new MonthCost() { id = 1, bugs = bugsCostMonth2, users = usersCostMonth2, period = periodMonth2 }; MonthCost monthCost5 = new MonthCost() { id = 2, bugs = bugsCostMonth5, users = usersCostMonth5, period = periodMonth5 }; returnedList.Add(monthCost2); returnedList.Add(monthCost5); var mockCostService = new Mock <ICostsService>(); mockCostService.Setup(m => m.GetCosts(from, to, 1)).Returns(returnedList); var controller = new CostsController( mockCostService.Object, (new Mock <ILoggerManager>()).Object, (new Mock <IPingService>()).Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); User usr = new User() { id = 1, organizationId = 1 }; TokenContent tokenCont = new TokenContent() { user = usr, organizationId = 1 }; Token token = new Token() { exp = 10000, iat = 10000, data = tokenCont }; controller.ControllerContext.HttpContext.Items["Token"] = token; var queryParam = new CostsFilterOptions() { From = from, To = to }; var result = await controller.Get(queryParam); var okResult = result as OkObjectResult; List <MonthCost> model = okResult.Value as List <MonthCost>; mockCostService.VerifyAll(); Assert.AreEqual(200, okResult.StatusCode); Assert.IsFalse(model == null); Assert.AreEqual(model.Count, 2); }