예제 #1
0
        public async Task GetBasketsWithDifferentPage()
        {
            // arrange
            var basket1 = new BasketOfItems(1);

            basket1.AddUpdateOrRemoveItem(1, 3);
            await _sut.CreateBasketAsync(basket1);

            var basket2 = new BasketOfItems(1);

            basket2.AddUpdateOrRemoveItem(2, 3);
            await _sut.CreateBasketAsync(basket2);

            var basket3 = new BasketOfItems(1);

            basket3.AddUpdateOrRemoveItem(3, 3);
            await _sut.CreateBasketAsync(basket3);

            // act
            var actual = await _sut.GetBaskets(1, 2);

            // assert
            Assert.That(actual.Count(), Is.EqualTo(1));
            Assert.That(actual.Select(x => x.Id).ToArray(), Does.Contain(basket3.Id));
        }
예제 #2
0
        private static void AssertItemAdded(BasketOfItems basket, long productId, int quantity)
        {
            var item = basket.BasketItems.FirstOrDefault(x => x.ProductId.Equals(productId));

            Assert.That(item, Is.Not.Null);
            Assert.That(item.Id, Is.GreaterThan(0));
            Assert.That(item.Quantity, Is.EqualTo(quantity));
        }
예제 #3
0
 private async Task <BasketOfItems> AddItemToBasket(BasketOfItems basket, long productId = 10328L, int quantity = 5)
 {
     return(await _sut.AddItemToBasket(new UpdateBasketModel()
     {
         BasketId = basket.Id,
         ProductId = productId,
         Quantity = quantity
     }));
 }
예제 #4
0
        public void CreateBasket()
        {
            // arrange
            // act
            var sut = new BasketOfItems(1);

            //assert
            Assert.That(sut.Id, Is.GreaterThan(0));
            Assert.That(sut.CustomerId, Is.EqualTo(1));
            Assert.That(sut.BasketItems, Is.Empty);
        }
예제 #5
0
        public async Task <ActionResult <BasketOfItems> > Post([FromBody] CreateBasketModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var basket = new BasketOfItems(model.CustomerId);
            await BasketRepository.CreateBasketAsync(basket);

            return(CreatedAtRoute("GetBasket", new { id = basket.Id }, basket));
        }
예제 #6
0
        public async Task UpdateBasket(BasketOfItems basket)
        {
            var dbBasket = await Context.Baskets.FirstOrDefaultAsync(x => x.Id.Equals(basket.Id));

            if (dbBasket == null)
            {
                throw new ArgumentException("Couldnt find basket");
            }

            //dbBasket = basket;
            Context.Baskets.Update(dbBasket);
            await Context.SaveChangesAsync();
        }
예제 #7
0
        public async Task CreateBasket()
        {
            // arrange
            var basket = new BasketOfItems(1);

            // act
            await _sut.CreateBasketAsync(basket);

            // assert
            var dbBasket = await _context.Baskets.FirstOrDefaultAsync(x => x.Id.Equals(basket.Id));

            Assert.That(dbBasket, Is.Not.Null);
        }
예제 #8
0
        public void AddItemThenRemove()
        {
            // arrange
            var sut = new BasketOfItems(1);

            // act
            sut.AddUpdateOrRemoveItem(1, 1);
            sut.AddUpdateOrRemoveItem(1, 0);

            //assert
            Assert.That(sut.Id, Is.GreaterThan(0));
            Assert.That(sut.CustomerId, Is.EqualTo(1));
            Assert.That(sut.BasketItems, Is.Empty);
        }
예제 #9
0
        public void AddSingleItem()
        {
            // arrange
            var sut = new BasketOfItems(1);

            // act
            sut.AddUpdateOrRemoveItem(1, 1);

            //assert
            Assert.That(sut.Id, Is.GreaterThan(0));
            Assert.That(sut.CustomerId, Is.EqualTo(1));
            Assert.That(sut.BasketItems, Has.Count.EqualTo(1));
            Assert.That(sut.BasketItems.All(x => x.Quantity.Equals(1)));
        }
예제 #10
0
        public async Task GetBasketById()
        {
            // arrange
            var basket = new BasketOfItems(1);

            basket.AddUpdateOrRemoveItem(1, 3);
            await _sut.CreateBasketAsync(basket);

            // act
            var actual = await _sut.GetBasketById(basket.Id);

            // assert
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.Id, Is.GreaterThan(0));
            Assert.That(actual.BasketItems, Has.Count.EqualTo(1));
        }
예제 #11
0
        public async Task DeleteBasket()
        {
            // arrange
            var basket = new BasketOfItems(1);

            basket.AddUpdateOrRemoveItem(1, 3);
            await _sut.CreateBasketAsync(basket);

            // act
            await _sut.DeleteBasket(basket.Id);

            // assert
            var dbBasket = await _context.Baskets.FirstOrDefaultAsync(x => x.Id.Equals(basket.Id));

            Assert.That(dbBasket, Is.Null);
        }
예제 #12
0
        public async Task CreateMultipleBasketsShouldntHaveSameId()
        {
            // arrange
            var basket1 = new BasketOfItems(1);
            var basket2 = new BasketOfItems(1);

            // act
            await _sut.CreateBasketAsync(basket1);

            await _sut.CreateBasketAsync(basket2);

            // assert
            var dbBasketsCount = await _context.Baskets.CountAsync();

            Assert.That(dbBasketsCount, Is.EqualTo(2));
            Assert.That(basket1.Id, Is.Not.EqualTo(basket2.Id));
        }
예제 #13
0
        public async Task UpdateBasket()
        {
            // arrange
            var basket = new BasketOfItems(1);
            await _sut.CreateBasketAsync(basket);

            basket.AddUpdateOrRemoveItem(1, 4);

            // act
            await _sut.UpdateBasket(basket);

            // assert
            var dbBasket = await _context.Baskets.FirstOrDefaultAsync(x => x.Id.Equals(basket.Id));

            Assert.That(dbBasket, Is.Not.Null);
            Assert.That(dbBasket.BasketItems, Has.Count.EqualTo(1));
            Assert.That(dbBasket.BasketItems.All(x => x.Quantity.Equals(4)));
        }
예제 #14
0
 public async Task CreateBasketAsync(BasketOfItems basket)
 {
     Context.Baskets.Add(basket);
     await Context.SaveChangesAsync();
 }