コード例 #1
0
        public void TestRemoveShouldReduceAmountIfMoreThanOneExists()
        {
            //Arrange
            var uniqueId = Guid.NewGuid().ToString();

            var expectedShoppingCartItem = new ShoppingCartItem
            {
                Id             = 999,
                ShopItemId     = 999,
                ShoppingCartId = uniqueId,
                Amount         = 5
            };


            using (var context = new DatabaseContext(_options))
            {
                var shopItem = context.ShopItems.Find(999);
                context.ShoppingCartItems.Add(new ShoppingCartItem
                {
                    Id             = 999,
                    ShopItem       = shopItem,
                    ShoppingCartId = uniqueId,
                    Amount         = 6
                });

                context.SaveChanges();
            }

            using (var context = new DatabaseContext(_options))
            {
                var sut = new ShoppingCartItemRepository(context);

                //Act
                sut.Remove(new ShoppingCartItem
                {
                    ShopItemId     = 999,
                    ShoppingCartId = uniqueId
                });

                var result = sut.GetAll();


                //Assert
                Assert.Contains(expectedShoppingCartItem, result);
            }
        }
コード例 #2
0
        public void TestGetAllShouldReturnAllShoppingCartItemsIncludingShopItemProperty()
        {
            //Arrange
            var uniqueId = Guid.NewGuid().ToString();

            var expectedShoppingCartItem = new ShoppingCartItem
            {
                Id         = 999,
                Amount     = 1,
                ShopItemId = 999,
                ShopItem   = new ShopItem
                {
                    CategoryId = 999,
                    Id         = 999,
                    Name       = "TestShopItem"
                },
                ShoppingCartId = uniqueId
            };

            using (var context = new DatabaseContext(_options))
            {
                var shopItem = context.ShopItems.Find(999);

                context.ShoppingCartItems.Add(new ShoppingCartItem
                {
                    Id             = 999,
                    Amount         = 1,
                    ShopItem       = shopItem,
                    ShoppingCartId = uniqueId
                });

                context.SaveChanges();
            }

            using (var context = new DatabaseContext(_options))
            {
                var sut = new ShoppingCartItemRepository(context);

                //Act
                var result = sut.GetAll();

                //Assert
                Assert.Contains(expectedShoppingCartItem, result);
            }
        }
コード例 #3
0
        public void TestAddShouldIncreaseTheAmountIfEntityExists()
        {
            //Arrange
            const int expectedShoppingCartItemsCount  = 1;
            const int expectedShoppingCartItemsAmount = 2;
            var       uniqueId = Guid.NewGuid().ToString();

            var shoppingCartItem = new ShoppingCartItem
            {
                Id         = 999,
                ShopItemId = 999,
                ShopItem   = new ShopItem
                {
                    Id         = 999,
                    Name       = "TestItem",
                    CategoryId = 999
                },
                ShoppingCartId = uniqueId,
                Amount         = 1
            };


            using var context = new DatabaseContext(_options);

            var sut = new ShoppingCartItemRepository(context);

            //Act
            sut.Add(shoppingCartItem);
            sut.Add(shoppingCartItem);


            var result = sut.GetAll().Where(s => s.ShoppingCartId == uniqueId);

            //Assert
            Assert.Equal(expectedShoppingCartItemsCount, result.Count());

            Assert.Equal(expectedShoppingCartItemsAmount, result.First().Amount);
        }
コード例 #4
0
        public void TestRemoveShouldRemoveEntityIfOnlyOneExists()
        {
            //Arrange
            var uniqueId = Guid.NewGuid().ToString();

            using (var context = new DatabaseContext(_options))
            {
                var shopItem = context.ShopItems.Find(999);
                context.ShoppingCartItems.Add(new ShoppingCartItem
                {
                    Id             = 999,
                    ShopItem       = shopItem,
                    ShoppingCartId = uniqueId,
                    Amount         = 1
                });

                context.SaveChanges();
            }


            using (var context = new DatabaseContext(_options))
            {
                var sut = new ShoppingCartItemRepository(context);

                //Act
                sut.Remove(new ShoppingCartItem
                {
                    ShopItemId     = 999,
                    ShoppingCartId = uniqueId
                });

                var result = sut.GetAll();

                //Assert
                Assert.Empty(result);
            }
        }
コード例 #5
0
        public void TestClearShouldEmptyCart()
        {
            //Arrange
            var       uniqueId    = Guid.NewGuid().ToString();
            var       testClearId = Guid.NewGuid().ToString();
            const int expectedShoppingCartItems = 1;

            var fixture = new OmitRecursionFixture();

            using (var context = new DatabaseContext(_options))
            {
                var shopItems = fixture
                                .Build <ShopItem>()
                                .Without(p => p.Id)
                                .With(p => p.CategoryId, 999)
                                .CreateMany().ToList();
                context.ShopItems.AddRange(shopItems);

                foreach (var item in shopItems)
                {
                    context.Add(new ShoppingCartItem
                    {
                        Id             = item.Id,
                        ShopItem       = item,
                        ShoppingCartId = testClearId,
                        Amount         = 1
                    });
                }

                var shopItem = context.ShopItems.Find(999);
                context.ShoppingCartItems.Add(new ShoppingCartItem
                {
                    Id             = 999,
                    ShopItem       = shopItem,
                    ShoppingCartId = uniqueId,
                    Amount         = 1
                });

                context.SaveChanges();
            }


            using (var context = new DatabaseContext(_options))
            {
                var sut = new ShoppingCartItemRepository(context);

                //Act

                sut.Clear(testClearId);
                context.SaveChanges();
            }

            using (var context = new DatabaseContext(_options))
            {
                var sut    = new ShoppingCartItemRepository(context);
                var result = sut.GetAll();
                //Assert


                Assert.Equal(expectedShoppingCartItems, result.Count());

                Assert.NotNull(result.FirstOrDefault(i => i.ShoppingCartId == uniqueId));

                Assert.Null(result.FirstOrDefault(i => i.ShoppingCartId == testClearId));
            }
        }