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 GetBasketByOwnerId_GivenThatTheBasketExists_ShouldReturnBasket()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);
            Basket basket = basketRepository.GetBasketByOwnerId(ownerId);

            // Assert
            Assert.AreEqual(ownerId, basket.OwnerID);
        }
        public void DoesBasketExist_GivenThatBasketExists_ShouldReturnTrue()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);
            bool doesBasketExist = basketRepository.DoesBasketExist(ownerId);

            // Assert
            Assert.AreEqual(true, doesBasketExist);
        }
        public void CreateBasket_ShouldCreateBasket()
        {
            // Arrange
            InMemoryProductDbContext ctx = CreateTestDatabase();
            BasketRepository         basketRepository = new BasketRepository(ctx);

            // Act
            string ownerId = "george";

            basketRepository.CreateBasket(ownerId);
            var    query           = from basket in ctx.Baskets where basket.OwnerID == ownerId select basket;
            Basket retreivedBasket = query.Single();

            // Assert
            Assert.AreEqual(ownerId, retreivedBasket.OwnerID);
        }
        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);
        }
        public void AddBasketToUser_AddsUserForeignKeyToBasketTable_WhenBasketObjectIsPassed()
        {
            //Arrange
            List <User> usersInDatabase = new List <User>();
            int         userId          = 1;
            Mock <User> mockUser        = new Mock <User>();

            mockUser.Object.UserId = userId;
            usersInDatabase.Add(mockUser.Object);
            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(usersInDatabase)
                            .WithFind(usersInDatabase, "UserId");
            var mockDatabasePlatform          = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, User>(mockDbSet);
            BasketRepository basketRepository = new BasketRepository(mockDatabasePlatform.Object);
            Mock <Basket>    mockBasket       = new Mock <Basket>();

            //Act
            basketRepository.CreateBasket(userId, mockBasket.Object);

            //Assert
            CollectionAssert.Contains(mockDatabasePlatform.Object.Users.ToList()[0].Baskets, mockBasket.Object);
        }
Exemple #12
0
 public void CreateBasket(int userId, Basket basket)
 {
     repository.CreateBasket(userId, basket);
 }