diff --git a/src/ApplicationCore/Services/BasketService.cs b/src/ApplicationCore/Services/BasketService.cs index 713657e45..17c91e84b 100644 --- a/src/ApplicationCore/Services/BasketService.cs +++ b/src/ApplicationCore/Services/BasketService.cs @@ -12,6 +12,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services public class BasketService : IBasketService { private readonly IAsyncRepository _basketRepository; + private readonly IAsyncRepository _basketItemRepository; private readonly IUriComposer _uriComposer; private readonly IAppLogger _logger; private readonly IRepository _itemRepository; @@ -19,12 +20,14 @@ public class BasketService : IBasketService public BasketService(IAsyncRepository basketRepository, IRepository itemRepository, IUriComposer uriComposer, - IAppLogger logger) + IAppLogger logger, + IAsyncRepository basketItemRepository) { _basketRepository = basketRepository; _uriComposer = uriComposer; _logger = logger; _itemRepository = itemRepository; + _basketItemRepository = basketItemRepository; } public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity) @@ -40,6 +43,11 @@ public async Task DeleteBasketAsync(int basketId) { var basket = await _basketRepository.GetByIdAsync(basketId); + foreach (var item in basket.Items) + { + await _basketItemRepository.DeleteAsync(item); + } + await _basketRepository.DeleteAsync(basket); } diff --git a/tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs b/tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs new file mode 100644 index 000000000..c9014f1c1 --- /dev/null +++ b/tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs @@ -0,0 +1,46 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; +using Microsoft.eShopWeb.Infrastructure.Data; +using Microsoft.eShopWeb.UnitTests.Builders; +using System.Linq; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.eShopWeb.IntegrationTests.Repositories.BasketItemRepositoryTests +{ + public class DeleteAsync_Should + { + private readonly CatalogContext _catalogContext; + private readonly EfRepository _basketRepository; + private readonly EfRepository _basketItemRepository; + private BasketBuilder BasketBuilder { get; } = new BasketBuilder(); + private readonly ITestOutputHelper _output; + + public DeleteAsync_Should(ITestOutputHelper output) + { + _output = output; + var dbOptions = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestCatalog") + .Options; + _catalogContext = new CatalogContext(dbOptions); + _basketRepository = new EfRepository(_catalogContext); + _basketItemRepository = new EfRepository(_catalogContext); + } + + [Fact] + public async Task DeleteItemFromBasket() + { + var existingBasket = BasketBuilder.WithOneBasketItem(); + _catalogContext.Add(existingBasket); + _catalogContext.SaveChanges(); + + await _basketItemRepository.DeleteAsync(existingBasket.Items.FirstOrDefault()); + _catalogContext.SaveChanges(); + + var basketFromDB = _basketRepository.GetById(BasketBuilder.BasketId); + + Assert.Equal(0, basketFromDB.Items.Count); + } + } +} diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs new file mode 100644 index 000000000..dae2c64b2 --- /dev/null +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs @@ -0,0 +1,37 @@ +using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; +using Microsoft.eShopWeb.ApplicationCore.Interfaces; +using Microsoft.eShopWeb.ApplicationCore.Services; +using Moq; +using System.Threading.Tasks; +using Xunit; + +namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests +{ + public class DeleteBasket + { + private Mock> _mockBasketRepo; + private Mock> _mockBasketItemRepo; + + public DeleteBasket() + { + _mockBasketRepo = new Mock>(); + _mockBasketItemRepo = new Mock>(); + } + + [Fact] + public async Task Should_InvokeBasketRepoOnceAndBasketItemRepoTwice_Given_TwoItemsInBasket() + { + var basket = new Basket(); + basket.AddItem(1, It.IsAny(), It.IsAny()); + basket.AddItem(2, It.IsAny(), It.IsAny()); + _mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny())) + .ReturnsAsync(basket); + var basketService = new BasketService(_mockBasketRepo.Object, null, null, null, _mockBasketItemRepo.Object); + + await basketService.DeleteBasketAsync(It.IsAny()); + + _mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny()), Times.Once); + _mockBasketItemRepo.Verify(x => x.DeleteAsync(It.IsAny()), Times.Exactly(2)); + } + } +} diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs index 9c5f11dfa..7de18e384 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs @@ -21,7 +21,7 @@ public SetQuantities() [Fact] public async void ThrowsGivenInvalidBasketId() { - var basketService = new BasketService(_mockBasketRepo.Object, null, null, null); + var basketService = new BasketService(_mockBasketRepo.Object, null, null, null, null); await Assert.ThrowsAsync(async () => await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary())); @@ -30,7 +30,7 @@ await Assert.ThrowsAsync(async () => [Fact] public async void ThrowsGivenNullQuantities() { - var basketService = new BasketService(null, null, null, null); + var basketService = new BasketService(null, null, null, null, null); await Assert.ThrowsAsync(async () => await basketService.SetQuantities(123, null)); diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs index 38c0a3066..41e106990 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs @@ -9,7 +9,7 @@ public class TransferBasket [Fact] public async void ThrowsGivenNullAnonymousId() { - var basketService = new BasketService(null, null, null, null); + var basketService = new BasketService(null, null, null, null, null); await Assert.ThrowsAsync(async () => await basketService.TransferBasketAsync(null, "steve")); } @@ -17,7 +17,7 @@ public async void ThrowsGivenNullAnonymousId() [Fact] public async void ThrowsGivenNullUserId() { - var basketService = new BasketService(null, null, null, null); + var basketService = new BasketService(null, null, null, null, null); await Assert.ThrowsAsync(async () => await basketService.TransferBasketAsync("abcdefg", null)); } diff --git a/tests/UnitTests/Builders/BasketBuilder.cs b/tests/UnitTests/Builders/BasketBuilder.cs new file mode 100644 index 000000000..10d063292 --- /dev/null +++ b/tests/UnitTests/Builders/BasketBuilder.cs @@ -0,0 +1,34 @@ +using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; + +namespace Microsoft.eShopWeb.UnitTests.Builders +{ + public class BasketBuilder + { + private Basket _basket; + public string BasketBuyerId => "testbuyerId@test.com"; + public int BasketId => 1; + + public BasketBuilder() + { + _basket = WithNoItems(); + } + + public Basket Build() + { + return _basket; + } + + public Basket WithNoItems() + { + _basket = new Basket { BuyerId = BasketBuyerId, Id = BasketId }; + return _basket; + } + + public Basket WithOneBasketItem() + { + _basket = new Basket { BuyerId = BasketBuyerId, Id = BasketId }; + _basket.AddItem(2, 3.40m, 4); + return _basket; + } + } +}