Exemplo n.º 1
0
        public async void PostProduct()
        {
            #region Arrange
            var options = new DbContextOptionsBuilder <AspnetAPIContext>()
                          .UseInMemoryDatabase(databaseName: "AspnetAPI")
                          .Options;

            Product product = new Product()
            {
                Name  = "Test",
                Price = 0
            };

            Product productCreated = null;
            #endregion

            #region Act
            using (var context = new AspnetAPIContext(options))
            {
                var controller = new ProductsController(context);
                await controller.PostProduct(product);

                productCreated = context.Product.Find(product.Id);
            }
            #endregion

            #region Assert
            Assert.NotNull(productCreated);
            #endregion
        }
Exemplo n.º 2
0
        public async void GetCartValue()
        {
            #region Arrange
            var options = new DbContextOptionsBuilder <AspnetAPIContext>()
                          .UseInMemoryDatabase(databaseName: "AspnetAPI4")
                          .Options;
            await using (var context = new AspnetAPIContext(options))
            {
                context.Add(new Cart()
                {
                    Quantity = 5,
                    Product  = new Product()
                    {
                        Name  = "Test1",
                        Price = 0
                    }
                });
                context.Add(new Cart()
                {
                    Quantity = 20,
                    Product  = new Product()
                    {
                        Name  = "Test2",
                        Price = 0
                    }
                });
                await context.SaveChangesAsync();
            }

            decimal cartTotalValue;
            #endregion

            #region Act

            await using (var context = new AspnetAPIContext(options))
            {
                var controller = new CartController(context);
                cartTotalValue = await controller.GetCartValue();
            }
            #endregion

            #region Assert
            Assert.Equal(225, cartTotalValue);
            #endregion
        }
Exemplo n.º 3
0
        public async void DeleteFromCart()
        {
            #region Arrange
            var options = new DbContextOptionsBuilder <AspnetAPIContext>()
                          .UseInMemoryDatabase(databaseName: "AspnetAPI2")
                          .Options;

            await using (var context = new AspnetAPIContext(options))
            {
                context.Add(new Cart()
                {
                    Quantity = 40,
                    Product  = new Product()
                    {
                        Name  = "Test",
                        Price = 0
                    }
                });
                await context.SaveChangesAsync();
            }

            Cart removedFromCart = null;
            int  cartSize        = 1;
            #endregion

            #region Act

            await using (var context = new AspnetAPIContext(options))
            {
                var controller = new CartController(context);
                removedFromCart = (await controller.DeleteFromCart(1)).Value;
                cartSize        = await context.Cart.CountAsync();
            }
            #endregion

            #region Assert
            Assert.Equal(0, cartSize);
            Assert.Equal(1, removedFromCart.ProductId);
            Assert.Equal(40, removedFromCart.Quantity);
            #endregion
        }
Exemplo n.º 4
0
        public async void GetProduct()
        {
            #region Arrange
            var options = new DbContextOptionsBuilder <AspnetAPIContext>()
                          .UseInMemoryDatabase(databaseName: "AspnetAPI")
                          .Options;
            using (var context = new AspnetAPIContext(options))
            {
                context.Add(new Product()
                {
                    Name  = "Test",
                    Price = 0
                });
                await context.SaveChangesAsync();
            }

            Product productExist             = null;
            Product productNotExist          = null;
            IEnumerable <Product> enumerable = null;
            #endregion

            #region Act
            using (var context = new AspnetAPIContext(options))
            {
                var controller = new ProductsController(context);
                productExist    = (await controller.GetProduct(1)).Value;
                productNotExist = (await controller.GetProduct(2)).Value;
                // In addition test GetProduct() method
                enumerable = (await controller.GetProduct()).Value;
            }
            #endregion

            #region Assert
            Assert.NotNull(productExist);
            Assert.Null(productNotExist);
            Assert.Equal("Test", productExist.Name);
            Assert.NotNull(enumerable);
            #endregion
        }
Exemplo n.º 5
0
        public async void GetCart()
        {
            #region Arrange
            var options = new DbContextOptionsBuilder <AspnetAPIContext>()
                          .UseInMemoryDatabase(databaseName: "AspnetAPI3")
                          .Options;
            await using (var context = new AspnetAPIContext(options))
            {
                context.Add(new Cart()
                {
                    Quantity = 5,
                    Product  = new Product()
                    {
                        Name  = "Test",
                        Price = 0
                    }
                });
                await context.SaveChangesAsync();
            }

            Dictionary <string, float> cart;
            #endregion

            #region Act

            await using (var context = new AspnetAPIContext(options))
            {
                var controller = new CartController(context);
                cart = await controller.GetCart();
            }
            #endregion

            #region Assert
            Assert.NotNull(cart);
            Assert.Single(cart);
            Assert.True(cart.ContainsKey("1"));
            Assert.True(cart.ContainsValue(5));
            #endregion
        }
Exemplo n.º 6
0
        public async void AddToCart()
        {
            #region Arrange
            var options = new DbContextOptionsBuilder <AspnetAPIContext>()
                          .UseInMemoryDatabase(databaseName: "AspnetAPI1")
                          .Options;

            await using (var context = new AspnetAPIContext(options))
            {
                context.Add(new Product()
                {
                    Name  = "Test",
                    Price = 0
                });
                await context.SaveChangesAsync();
            }

            Cart cartEntryCreated = null;
            #endregion

            #region Act

            await using (var context = new AspnetAPIContext(options))
            {
                var controller = new CartController(context);
                await controller.AddToCart(1, 20);

                cartEntryCreated = await context.Cart.FindAsync(1);
            }
            #endregion

            #region Assert
            Assert.NotNull(cartEntryCreated);
            Assert.Equal(1, cartEntryCreated.ProductId);
            Assert.Equal(20, cartEntryCreated.Quantity);
            #endregion
        }
Exemplo n.º 7
0
 public ProductsController(AspnetAPIContext context)
 {
     _context = context;
 }
Exemplo n.º 8
0
 public CartController(AspnetAPIContext context)
 {
     _context = context;
 }