public void MapItemList_GivenItemDTOAsList_ShouldReturnAnItemList()
 {
     //---------------Set up test pack-------------------
     var itemDto = new ItemDto()
     {
         Id = 2,
         Description = "Description two",
         ItemName = "ItemDTO name2"
     };
     var itemDto1 = new ItemDto()
     {
         Id = 3,
         Description = "Description three",
         ItemName = "ItemDTO name3"
     };
     var itemDTOList = new List<ItemDto>()
     {
         itemDto,
         itemDto1
     };
     var mapper = new ItemMapper();
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     List<ItemViewModel> itemList = mapper.MapItemList(itemDTOList);
     //---------------Test Result -----------------------
     
     Assert.AreEqual(itemDTOList.Count, itemList.Count);
 }
 public void Add(ItemDto newItemDto)
 {
     if (newItemDto == null)
     {
         throw new ApplicationException("Null item cannot be added");
     }
     if (IsItemExist(newItemDto))
     {
         throw new ApplicationException("Item already exists");
     }
     _context.Items.Add(newItemDto);
     _context.SaveChanges();
 }
Esempio n. 3
0
 public ItemViewModel MapItem(ItemDto itemDto)
 {
     if (itemDto == null)
     {
         return null;
     }
     var item = new ItemViewModel()
     {
         Id = itemDto.Id,
         Description = itemDto.Description,
         Name = itemDto.ItemName
     };
     return item;
 }
Esempio n. 4
0
        public ItemDto MapItemDto(ItemViewModel item)
        {
            if (item == null)
            {
                return null;
            }
            var itemDto = new ItemDto()
            {
                Id = item.Id,
                ItemName = item.Name,
                Description = item.Description

            };
            return itemDto;
        }
 public void MapItem_GivenItemDTOByName_ShouldReturnAnItem()
 {
     //---------------Set up test pack-------------------
     var itemDto = new ItemDto()
     {
         Id = 2,
         Description = "Description two",
         ItemName = "ItemDTO name2"
     };
     var mapper = new ItemMapper();
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     ItemViewModel item = mapper.MapItem(itemDto);
     //---------------Test Result -----------------------
     Assert.AreEqual(itemDto.ItemName, item.Name);
 }
 public void MapItem_GivenItemDTO_ShouldReturnAnItem()
 {
     //---------------Set up test pack-------------------
     var itemDTO = new ItemDto()
     {
         Id = 1,
         Description = "First item DTO",
         ItemName = "ItemDTO name1"
     };
     var mapper = new ItemMapper();
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     ItemViewModel item = mapper.MapItem(itemDTO);
     //---------------Test Result -----------------------
     Assert.AreEqual(itemDTO.Id, item.Id);
 }
        public void GetItemList_GivenOneItem_ShouldReturnThatItem()
        {
            var testItem = new ItemDto()
            {
                Id = 1,
                Description = "Description one",
                ItemName = "Ball"
            };
            var testItemList = new List<ItemDto>() { testItem };

            var context = Substitute.For<ILendingLibraryContext>();
            SetContextWithDtos(context, testItemList);
            //---------------Set up test pack-------------------
            var repo = new ItemRepository(context);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = repo.GetItemList();
            //---------------Test Result -----------------------
            Assert.AreEqual(testItemList, result);
        }
        public void Details_GivenItemId_ShouldReturnViewWithItemDetails()
        {
            //---------------Set up test pack-------------------
            var repo = Substitute.For<IItemRepository>();
            var itemDto = new ItemDto {Id = 2, ItemName = "FakeName"};
            
            repo.GetItemById(itemDto.Id).Returns(itemDto);
           
            var controller = new ItemController(repo);
            //---------------Execute Test ----------------------
            const int expectedId = 2;
            var viewResult = controller.Details(expectedId) as ViewResult;
            //---------------Test Result -----------------------
            var resultItem = (ItemViewModel) viewResult?.ViewData.Model;

            Assert.IsNotNull(resultItem);

            var map = new ItemMapper();
            var expectedItem = map.MapItem(itemDto);

            Assert.AreSame(expectedItem.Name, resultItem.Name);

        }
        public void Delete_GivenItemId_ShouldDeleteTheItem()
        {
            //---------------Set up test pack-------------------
            int expectedId = 1;
            var itemDto = new ItemDto();
            itemDto.Id = expectedId;
            var repo = Substitute.For<IItemRepository>();
            var controller = new ItemController(repo);
            repo.GetItemById(expectedId).Returns(itemDto);
            
            //---------------Execute Test ----------------------

            var viewResult = controller.Delete(expectedId) as ViewResult;
            //---------------Test Result -----------------------
            var item = (ItemViewModel) viewResult.ViewData.Model;

            Assert.IsTrue(item.Id.Equals(1));
        }
 public void Add_GivenNewItem_ShouldAddToList()
 {
     //---------------Set up test pack-------------------
     var newItem = new ItemDto()
     {
         Id = 4,
         Description = "Description four",
         ItemName = "AnotherItemName"
     };
     var itemsList = new List<ItemDto>();
     var context = Substitute.For<ILendingLibraryContext>();
     SetContextWithDtos(context,itemsList);
     var repo = new ItemRepository(context);
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     repo.Add(newItem);
     //var result = repo.GetItemList();
     var result = context.Items.Where(x => x.Id == newItem.Id);
     Assert.IsNotNull(result);
 }
        public void Update_GivenItemIdThatDoesNotExist_ShouldAddToItemList()
        {
            var items = SetUpItemDataContext();
            //---------------Set up test pack-------------------
            var itemToUpdate = new ItemDto()
            {
                Id = 7,
                Description = "ItemDTO I'm going to update",
                ItemName = "ItemToUpdate"
            };
            var context = Substitute.For<ILendingLibraryContext>();
            SetContextWithDtos(context, items);
            var repo = new ItemRepository(context);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            repo.Update(itemToUpdate);
            var addedItem = repo.GetItemById(itemToUpdate.Id);
            
            //---------------Test Result -----------------------
            Assert.AreEqual(itemToUpdate, addedItem);
            Assert.IsNotNull(addedItem);

        }
        public void Add_GivenAlreadyExistingItem_ShouldNotAddItemToListAndThrow()
        {

            //---------------Set up test pack-------------------
            var newItem = new ItemDto()
            {
                Id = 10,
                Description = "Description one",
                ItemName = "ItemName"
            };
            var itemsList = new List<ItemDto>
            {
                newItem
            };
            var context = Substitute.For<ILendingLibraryContext>();
            SetContextWithDtos(context, itemsList);
            var repo = CreateItemRepository(context);
            var message = "Item already exists";
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var  result = Assert.Throws<ApplicationException>(() => repo.Add(newItem));
            //---------------Test Result -----------------------
            
            Assert.AreEqual(message, result.Message);
            

        }
Esempio n. 13
0
        public void Update(ItemDto itemDto)
        {
            var item = GetItemById(itemDto.Id);
            if (item == null)
            {
                _context.Items.Add(itemDto);
                _context.SaveChanges();
            }
            else
            {

                item.Description = itemDto.Description;
                item.ItemName = itemDto.ItemName;
                
                _context.SaveChanges();
            }
        }
Esempio n. 14
0
 private bool IsItemExist(ItemDto itemDto)
 {
     return _context.Items.Any(t => t.Id == itemDto.Id);
 }