public void GetInventoryPartFromId_WhenIdExists() { InventoryPartDto expectedObject = MockObjectsUtil.GetMockInventoryPart(1); Mock <InventoryContext> mockDbContext = new Mock <InventoryContext>(); List <InventoryPartDto> data = new List <InventoryPartDto>() { expectedObject }; Mock <DbSet <InventoryPartDto> > mockSet = new Mock <DbSet <InventoryPartDto> >(); mockSet.As <IQueryable <InventoryPartDto> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); mockSet.As <IQueryable <InventoryPartDto> >().Setup(m => m.Provider) .Returns(data.AsQueryable().Provider); mockSet.As <IQueryable <InventoryPartDto> >().Setup(m => m.Expression) .Returns(data.AsQueryable().Expression); mockSet.As <IQueryable <InventoryPartDto> >().Setup(m => m.ElementType) .Returns(data.AsQueryable().ElementType); mockSet.Setup(m => m.Find(It.IsAny <object[]>())) .Returns <object[]>(ids => data.FirstOrDefault(d => d.ID == (int)ids[0])); mockDbContext.Setup(x => x.Set <InventoryPartDto>()).Returns(mockSet.Object); mockDbContext.Setup(c => c.InventoryParts).Returns(mockSet.Object); InventoryPartManager manager = new InventoryPartManager(mockDbContext.Object); InventoryPartView result = manager.GetInventoryPartFromId(1); Assert.AreEqual(expectedObject.ID, result.ID); }
public virtual void DeleteInventoryPart(InventoryPartView inventoryPart) { try { this.unitOfWork.InventoryParts.Delete(inventoryPart.ID); this.unitOfWork.Commit(); } catch (Exception e) { Log.Error(e.Message); throw new DatabaseAccessException(e.Message); } }
public virtual InventoryPartView GetInventoryPartFromId(int?id) { try { InventoryPartDto inventoryPart = this.unitOfWork.InventoryParts.GetByID(id); InventoryPartView inventoryPartView = iMapper.Map <InventoryPartDto, InventoryPartView>(inventoryPart); return(inventoryPartView); } catch (Exception e) { Log.Error(e.Message); throw new DatabaseAccessException(e.Message); } }
public virtual void UpdateInventoryPart(InventoryPartView inventoryPart) { try { InventoryPartDto inventoryPartDto = iMapper.Map <InventoryPartView, InventoryPartDto>(inventoryPart); this.unitOfWork.InventoryParts.Update(inventoryPartDto); this.unitOfWork.Commit(); } catch (Exception e) { Log.Error(e.Message); throw new DatabaseAccessException(e.Message); } }
public void TestDetails_WhenIdIsNull() { InventoryPartView expectedObject = MockObjectsUtil.GetMockInventoryPartView(1); List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.GetInventoryPartFromId(It.IsAny <int?>())).Returns(expectedObject); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); HttpStatusCodeResult result = controller.Details(null) as HttpStatusCodeResult; Assert.AreEqual(400, result.StatusCode); }
public void TestDetails_WhenItemNotExists() { InventoryPartView expectedObject = null; List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.GetInventoryPartFromId(It.IsAny <int?>())).Returns(expectedObject); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); ViewResult result = controller.Details(2) as ViewResult; Assert.IsNull(result); }
public void TestEdit_WhenIdIsNull() { InventoryPartView expectedObject = MockObjectsUtil.GetMockInventoryPartView(1); List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.UpdateInventoryPart(It.IsAny <InventoryPartView>())); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); HttpStatusCodeResult result = controller.Edit((int?)null) as HttpStatusCodeResult; Assert.AreEqual(400, result.StatusCode); }
public void TestEdit_WhenItemNotExists() { InventoryPartView expectedObject = MockObjectsUtil.GetMockInventoryPartView(1); List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.UpdateInventoryPart(It.IsAny <InventoryPartView>())); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); ViewResult result = controller.Edit(2) as ViewResult; Assert.IsNull(result); }
public ActionResult Edit([Bind(Include = "ID,Name,AvailabeNoOfUnits,ReorderLevel,UnitPrice")] InventoryPartView inventoryPart) { if (ModelState.IsValid) { try { this.InventoryPartManager.UpdateInventoryPart(inventoryPart); return(RedirectToAction("Index")); } catch (DatabaseAccessException e) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } } return(View(inventoryPart)); }
public void TestCreate_EmptyParameter() { InventoryPartView expectedObject = MockObjectsUtil.GetMockInventoryPartView(1); List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.InsertInventoryPart(It.IsAny <InventoryPartView>())); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); ViewResult result = controller.Create() as ViewResult; Assert.IsNotNull(result); Assert.IsNull(result.Model); }
public void TestDetails_WhenItemExists() { InventoryPartView expectedObject = MockObjectsUtil.GetMockInventoryPartView(1); List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.GetInventoryPartFromId(It.IsAny <int?>())).Returns(expectedObject); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); ViewResult result = controller.Details(1) as ViewResult; Assert.IsNotNull(result); InventoryPartView actualObject = (InventoryPartView)result.Model; Assert.AreEqual(expectedObject.ID, actualObject.ID); }
public ActionResult DeleteConfirmed(int id) { InventoryPartView inventoryPart = this.InventoryPartManager.GetInventoryPartFromId(id); if (inventoryPart == null) { Log.Error($"Record for id :{id} not found."); return(HttpNotFound()); } else { try { this.InventoryPartManager.DeleteInventoryPart(inventoryPart); return(RedirectToAction("Index")); } catch (DatabaseAccessException e) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } } }
public void TestCreate_WithParameters() { InventoryPartView inputObject = new InventoryPartView() { ID = 2, Name = "test 2", ReorderLevel = 50, AvailabeNoOfUnits = 100, UnitPrice = 10 }; InventoryPartView expectedObject = MockObjectsUtil.GetMockInventoryPartView(1); List <InventoryPartView> data = new List <InventoryPartView>() { MockObjectsUtil.GetMockInventoryPartView(1) }; Mock <InventoryPartManager> mockManager = new Mock <InventoryPartManager>(); mockManager.Setup(x => x.InsertInventoryPart(It.IsAny <InventoryPartView>())); InventoryPartsController controller = new InventoryPartsController(mockManager.Object); ViewResult result = controller.Create(inputObject) as ViewResult; Assert.IsNull(result); }
// GET: InventoryParts/Details/5 /// <summary> /// Returns the inventory item specified by the id. /// </summary> /// <param name="id">The identifier.</param> /// <returns></returns> public ActionResult Details(int?id) { if (id == null) { Log.Error("Input is empty"); return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } InventoryPartView inventoryPart = null; try { inventoryPart = this.InventoryPartManager.GetInventoryPartFromId(id); } catch (DatabaseAccessException e) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } if (inventoryPart == null) { Log.Error($"Record for id :{id} not found."); return(HttpNotFound()); } return(View(inventoryPart)); }