public void AddProductToBasket_GivenProductAndBasketExist_ShouldAddTheProductToTheBasket()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            // Add new product to database
            Product productToAdd = new Product()
            {
                Title = "New Product"
            };

            ctx.Products.Add(productToAdd);
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add product to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            basketRepository.AddProductToBasket(productToAdd.ProductId, basket.OwnerID);
            basket = basketRepository.GetBasketByOwnerId(ownerId);

            // Assert
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual(1, basket.BasketItems[0].ProductQuantity);
            Assert.AreEqual(productToAdd.ProductId, basket.BasketItems[0].ProductId);
            Assert.AreEqual(basket.BasketId, basket.BasketItems[0].BasketId);
        }
        public void RemoveProductFromBasket_GivenProductAndBasketExist_ShouldRemoveTheProductToTheBasket()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            // Add new product to database
            List <Product> productsToAdd = new List <Product>()
            {
                new Product()
                {
                    Title = "Product 1"
                },
                new Product()
                {
                    Title = "Product 2"
                },
                new Product()
                {
                    Title = "Product 3"
                }
            };

            foreach (var product in productsToAdd)
            {
                ctx.Products.Add(product);
            }
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add products to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            foreach (var product in productsToAdd)
            {
                basketRepository.AddProductToBasket(product.ProductId, basket.OwnerID);
            }

            // Delete product from basket
            basketRepository.RemoveProductFromBasket(productsToAdd[1].ProductId, ownerId);
            basket = basketRepository.GetBasketByOwnerId(ownerId);

            // Assert
            Assert.AreEqual(2, basket.BasketItems.Count);
            Assert.AreEqual(1, basket.BasketItems[0].ProductId);
            Assert.AreEqual(3, basket.BasketItems[1].ProductId);
        }
        public void DecrementProductQuantityInBasket_GivenProductExistsInBasket_ShouldDecrementProductQuantity()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            List <Product> productsToAdd = new List <Product>()
            {
                new Product()
                {
                    Title = "Product 1"
                },
                new Product()
                {
                    Title = "Product 2"
                }
            };

            foreach (var product in productsToAdd)
            {
                ctx.Products.Add(product);
            }
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add products to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            foreach (var product in productsToAdd)
            {
                basketRepository.AddProductToBasket(product.ProductId, basket.OwnerID);
            }

            int idOfProductToDecrement = 2;

            basketRepository.DecrementProductQuantityInBasket(idOfProductToDecrement, ownerId);
            basket = basketRepository.GetBasketByOwnerId(ownerId);

            // Assert
            Assert.AreEqual(1, basket.BasketItems[0].ProductId);
            Assert.AreEqual(1, basket.BasketItems[0].ProductQuantity);
            Assert.AreEqual(2, basket.BasketItems[1].ProductId);
            Assert.AreEqual(0, basket.BasketItems[1].ProductQuantity);
        }
        public void GetProductQuantityInBasket_GivenProductExistsInBasket_ShouldReturnCorrectQuantity()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            List <Product> productsToAdd = new List <Product>()
            {
                new Product()
                {
                    Title    = "Product 1",
                    Quantity = 2
                },
                new Product()
                {
                    Title    = "Product 2",
                    Quantity = 5
                }
            };

            foreach (var product in productsToAdd)
            {
                ctx.Products.Add(product);
            }
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add products to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            foreach (var product in productsToAdd)
            {
                basketRepository.AddProductToBasket(product.ProductId, basket.OwnerID);
            }
            int productId = 1;
            int quantityOfProductInBasket = basketRepository.GetProductQuantityInBasket(productId, ownerId);

            // Assert
            int expectedQuantity = 1;

            Assert.AreEqual(expectedQuantity, quantityOfProductInBasket);
        }
        public void IsProductInBasket_GivenProductIsNotInBasket_ShouldReturnFalse()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            List <Product> productsToAdd = new List <Product>()
            {
                new Product()
                {
                    Title = "Product 1"
                },
                new Product()
                {
                    Title = "Product 2"
                }
            };

            foreach (var product in productsToAdd)
            {
                ctx.Products.Add(product);
            }
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add products to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            foreach (var product in productsToAdd)
            {
                basketRepository.AddProductToBasket(product.ProductId, basket.OwnerID);
            }

            int  productIdToSearchFor = 3;
            bool isProductInBasket    = basketRepository.IsProductInBasket(productIdToSearchFor, ownerId);

            // Assert
            Assert.AreEqual(false, isProductInBasket);
        }
        public void GetProductQuantitiesInBasketAsAJsonString()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            List <Product> productsToAdd = new List <Product>()
            {
                new Product()
                {
                    Title    = "Product 1",
                    Quantity = 2
                }
            };

            foreach (var product in productsToAdd)
            {
                ctx.Products.Add(product);
            }
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add products to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            foreach (var product in productsToAdd)
            {
                basketRepository.AddProductToBasket(product.ProductId, basket.OwnerID);
            }
            string productQuantitiesString = basketRepository.GetProductQuantitiesInBasketAsAJsonString(ownerId);

            //Assert
            string expectedString = "[{\"productId\":1,\"quantity\":1,\"product\":{\"ProductId\":1,\"OwnerID\":null,\"Title\":\"Product 1\",\"Description\":null,\"Seller\":null,\"Price\":0,\"Quantity\":2,\"ImageUrl\":null}}]";

            Assert.AreEqual(expectedString, productQuantitiesString);
        }
        public void GetBasketByOwnerId_GivenThatBasketHasBasketItems_ShouldReturnBasketWithBasketItems()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            List <Product> productsToAdd = new List <Product>()
            {
                new Product()
                {
                    Title = "Product 1"
                }
            };

            foreach (var product in productsToAdd)
            {
                ctx.Products.Add(product);
            }
            ctx.SaveChanges();

            // Add new basket to database
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);

            // Add products to basket
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            foreach (var product in productsToAdd)
            {
                basketRepository.AddProductToBasket(product.ProductId, basket.OwnerID);
            }
            Basket retrievedBasket = basketRepository.GetBasketByOwnerId(ownerId);

            // Assert
            Assert.AreEqual(1, retrievedBasket.BasketItems.Count);
            Assert.AreEqual(1, retrievedBasket.BasketItems[0].ProductId);
        }