Exemplo n.º 1
0
        public async Task AddAsync_AddOne_UpdateIfExists()
        {
            // Arrange
            Product product1 = new Product
            {
                Id          = 1,
                ProductName = "Racing Car",
                CategoryId  = 1,
                Description = "Nice Racing Car",
                ImagePath   = "abc.png",
                UnitPrice   = 23.1
            };
            var      cartId   = Guid.NewGuid().ToString();
            CartItem cartItem = new CartItem
            {
                Id        = Guid.NewGuid().ToString(),
                CartId    = cartId,
                ProductId = product1.Id,
                Product   = product1,
                Created   = DateTime.Now,
                Quantity  = 1
            };

            var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase("WingtipToysDB").Options);

            context.Products.Add(product1);
            context.CartItems.Add(cartItem);
            context.SaveChanges();

            ICartService service = new CartService(context, _mapper);

            CartItemDto cartDto = new CartItemDto
            {
                CartId    = cartId,
                ProductId = product1.Id,
                Quantity  = 3
            };

            // Act
            await service.AddAsync(cartDto);

            // Assert
            var tempList = context.CartItems.ToList();

            Assert.AreEqual(1, tempList.Count);
            Assert.AreEqual(cartItem.Id, tempList[0].Id);
            Assert.AreEqual(cartItem.CartId, tempList[0].CartId);
            Assert.AreEqual(cartItem.ProductId, tempList[0].ProductId);
            Assert.AreEqual(3, tempList[0].Quantity);

            // Clean data
            context.Products.RemoveRange(context.Products);
            context.CartItems.RemoveRange(context.CartItems);
            context.SaveChanges();
            context.Dispose();
        }
Exemplo n.º 2
0
        public async Task PatchAsync_Quantity_UpdateOne()
        {
            string dbName = "PatchAsync_Quantity_UpdateOne";
            // Arrange
            Product product1 = new Product
            {
                Id          = 1,
                ProductName = "Racing Car",
                CategoryId  = 1,
                Description = "Nice Racing Car",
                ImagePath   = "abc.png",
                UnitPrice   = 23.1
            };
            var      cartId   = Guid.NewGuid().ToString();
            CartItem cartItem = new CartItem
            {
                Id        = Guid.NewGuid().ToString(),
                CartId    = cartId,
                ProductId = product1.Id,
                Product   = product1,
                Created   = DateTime.Now,
                Quantity  = 1
            };

            var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase(dbName).Options);

            context.Products.Add(product1);
            context.CartItems.Add(cartItem);
            context.SaveChanges();

            ICartService service = new CartService(context, _mapper);

            // Act
            await service.PatchAsync(cartItem.CartId, cartItem.Id, 3);

            // Assert
            CartItem item = context.CartItems.FirstOrDefault(c => c.Id == cartItem.Id);

            Assert.IsNotNull(item);
            Assert.AreEqual(3, item.Quantity);

            // Clean data
            context.Products.RemoveRange(context.Products);
            context.CartItems.RemoveRange(context.CartItems);
            context.SaveChanges();
            context.Dispose();
        }
Exemplo n.º 3
0
        public async Task DeleteAsync_CartId_ItemId_DeleteOne()
        {
            string dbName = "DeleteAsync_CartId_ItemId_DeleteOne";
            // Arrange
            Product product1 = new Product
            {
                Id          = 1,
                ProductName = "Racing Car",
                CategoryId  = 1,
                Description = "Nice Racing Car",
                ImagePath   = "abc.png",
                UnitPrice   = 23.1
            };
            Product product2 = new Product
            {
                Id          = 2,
                ProductName = "Luxery Car",
                CategoryId  = 1,
                Description = "Very expencive CAr",
                ImagePath   = "abcd.png",
                UnitPrice   = 150.1
            };
            var      cartId   = Guid.NewGuid().ToString();
            CartItem cartItem = new CartItem
            {
                Id        = Guid.NewGuid().ToString(),
                CartId    = cartId,
                ProductId = product1.Id,
                Created   = DateTime.Now,
                Quantity  = 1
            };
            CartItem cartItem2 = new CartItem
            {
                Id        = Guid.NewGuid().ToString(),
                CartId    = cartId,
                ProductId = product2.Id,
                Created   = DateTime.Now,
                Quantity  = 5
            };

            var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase(dbName).Options);

            context.Products.Add(product1);
            context.CartItems.Add(cartItem);
            context.Products.Add(product2);
            context.CartItems.Add(cartItem2);
            context.SaveChanges();

            ICartService service = new CartService(context, _mapper);

            // Act
            await service.DeleteAsync(cartItem.CartId, cartItem.Id);

            // Assert
            var tempList = context.CartItems.ToList();

            Assert.AreEqual(1, tempList.Count);

            // Clean data
            context.Products.RemoveRange(context.Products);
            context.CartItems.RemoveRange(context.CartItems);
            context.SaveChanges();
            context.Dispose();
        }