コード例 #1
0
 public UserToProduct Save(UserToProduct entity)
 {
     if (entity.Id == Guid.Empty)
     {
         var dao = Mapper.Map <UserToProductDAO>(entity);
         dao.InitializeEntity();
         DataContext.UserToProducts.Add(dao);
         return(Mapper.Map <UserToProduct>(dao));
     }
     else
     {
         var oldEntity = DataContext.UserToProducts.FirstOrDefault(m => m.Id == entity.Id);
         oldEntity.ChangeTo(entity);
         return(entity);
     }
 }
コード例 #2
0
        public void UserToProduct_Crud_Test()
        {
            //Arrange
            var userToProduct = new UserToProduct
            {
                 Amount=125,
                 CreatedDate=DateTime.Now,
                 UserId=Guid.NewGuid(),
                 ProductId=Guid.NewGuid()
            };

            //Act
            var addedItem= _userToProductRepository.Save(userToProduct);

            //Assert
            Assert.AreEqual(userToProduct.Amount, addedItem.Amount);
            Assert.AreEqual(userToProduct.CreatedDate, userToProduct.CreatedDate);
            Assert.AreEqual(userToProduct.UserId, userToProduct.UserId);
            Assert.AreEqual(userToProduct.ProductId, userToProduct.ProductId);

            //Act
            var item = _userToProductRepository.FindById(addedItem.Id);

            //Assert
            Assert.IsNotNull(item);
            Assert.AreEqual(userToProduct.UserId, item.UserId);
            Assert.AreEqual(userToProduct.ProductId, item.ProductId);
            Assert.AreEqual(addedItem.Id, item.Id);

            //Arrange
            item.Amount = 250;

            //Act
            var updatedItem = _userToProductRepository.Save(item);

            //Assert
            Assert.AreEqual(item.Id, updatedItem.Id);
            Assert.AreEqual(item.Amount, updatedItem.Amount);

            //Act
            _userToProductRepository.Remove(item.Id);
            var removedItem = _userToProductRepository.FindById(item.Id);

            //Assert
            Assert.IsNull(removedItem);
            Assert.AreNotEqual(item.Id, removedItem?.Id);
        }