forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove basket items when basket is deleted (dotnet-architecture#170)
* Proving out removing the basket items at the same time the basket is deleted * Fixing existing unit tests * Adding a unit test for the Deletebasket method * Rename test and deleting basket items before basket * Added integration test for DeleteAsync method for BasketItems - Also added a BasketBuilder to create a Basket with no items, or one item.
- Loading branch information
1 parent
9528559
commit eb02750
Showing
6 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Basket> _basketRepository; | ||
private readonly EfRepository<BasketItem> _basketItemRepository; | ||
private BasketBuilder BasketBuilder { get; } = new BasketBuilder(); | ||
private readonly ITestOutputHelper _output; | ||
|
||
public DeleteAsync_Should(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
var dbOptions = new DbContextOptionsBuilder<CatalogContext>() | ||
.UseInMemoryDatabase(databaseName: "TestCatalog") | ||
.Options; | ||
_catalogContext = new CatalogContext(dbOptions); | ||
_basketRepository = new EfRepository<Basket>(_catalogContext); | ||
_basketItemRepository = new EfRepository<BasketItem>(_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); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IAsyncRepository<Basket>> _mockBasketRepo; | ||
private Mock<IAsyncRepository<BasketItem>> _mockBasketItemRepo; | ||
|
||
public DeleteBasket() | ||
{ | ||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>(); | ||
_mockBasketItemRepo = new Mock<IAsyncRepository<BasketItem>>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_InvokeBasketRepoOnceAndBasketItemRepoTwice_Given_TwoItemsInBasket() | ||
{ | ||
var basket = new Basket(); | ||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>()); | ||
basket.AddItem(2, It.IsAny<decimal>(), It.IsAny<int>()); | ||
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>())) | ||
.ReturnsAsync(basket); | ||
var basketService = new BasketService(_mockBasketRepo.Object, null, null, null, _mockBasketItemRepo.Object); | ||
|
||
await basketService.DeleteBasketAsync(It.IsAny<int>()); | ||
|
||
_mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny<Basket>()), Times.Once); | ||
_mockBasketItemRepo.Verify(x => x.DeleteAsync(It.IsAny<BasketItem>()), Times.Exactly(2)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |