Exemplo n.º 1
0
        public ActionResult Edit(CompoundElementPartialDto compoundElement)
        {
            Log.Information("POST Compound/Edit triggered");

            ModelState.Clear();

            if (ModelState.IsValid)
            {
                bool isSuccess = _repo.UpdateCompoundElement(compoundElement);
                if (isSuccess)
                {
                    return(Redirect("/Compound/Index"));
                }
                ModelState.AddModelError(string.Empty, "Error saving compound.");
            }

            PopulateCombos();

            return(View(compoundElement));
        }
        public void UpdateCompoundElement_ElementQuantitiesSuccessfullyUpdated()
        {
            //Arrange
            var repo = new CompoundRepository();

            //Act
            var compound = repo.GetAllCompounds()
                           .First();
            var compoundElements   = repo.GetCompoundElementsByCompoundId(compound.Id);
            var compoundElementDto = new CompoundElementPartialDto()
            {
                CompoundId = compoundElements.First().Compound.Id,
                Name       = compoundElements.First().Compound.Name,
                TypeId     = compoundElements.First().Compound.TypeId,
                Elements   = Mapper.Map <List <ElementPartialDto> >(compoundElements)
            };
            var elementQuantities = compoundElementDto.Elements
                                    .Select(e => e.Quantity)
                                    .ToList();

            var random             = new Random();
            var expectedQuantities = new List <int>();

            foreach (var element in compoundElementDto.Elements)
            {
                var randomNumber = random.Next(100, 500);
                expectedQuantities.Add(randomNumber);
                element.Quantity = randomNumber;
            }

            repo.UpdateCompoundElement(compoundElementDto);
            var newCompoundElements = repo.GetCompoundElementsByCompoundId(compound.Id);
            var newElements         = Mapper.Map <List <ElementPartialDto> >(newCompoundElements);
            var newQuantities       = newElements
                                      .Select(e => e.Quantity)
                                      .ToList();

            //Assert
            Assert.AreEqual(expectedQuantities.Count, newQuantities.Count);
            Assert.IsTrue(expectedQuantities.All(newQuantities.Contains));
        }