Adding additional unit tests (#406)
* Creating new test class for RemoveEmptyItems * Adding tests for AddItemToBasket in BasketService * Removing unused GetBasketItemCountAsync * Adding tests for BasketWithItemsSpecification * Adding CustomerORdersWithItemsSpecification tests * Adding CatalogFilterPaginatedSpecifciation tests * Adding CatalogItemsSpecification tests
This commit is contained in:
@@ -22,8 +22,8 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate
|
|||||||
ShipToAddress = shipToAddress;
|
ShipToAddress = shipToAddress;
|
||||||
_orderItems = items;
|
_orderItems = items;
|
||||||
}
|
}
|
||||||
public string BuyerId { get; private set; }
|
|
||||||
|
|
||||||
|
public string BuyerId { get; private set; }
|
||||||
public DateTimeOffset OrderDate { get; private set; } = DateTimeOffset.Now;
|
public DateTimeOffset OrderDate { get; private set; } = DateTimeOffset.Now;
|
||||||
public Address ShipToAddress { get; private set; }
|
public Address ShipToAddress { get; private set; }
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate
|
namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate
|
||||||
{
|
{
|
||||||
|
|
||||||
public class OrderItem : BaseEntity
|
public class OrderItem : BaseEntity
|
||||||
{
|
{
|
||||||
public CatalogItemOrdered ItemOrdered { get; private set; }
|
public CatalogItemOrdered ItemOrdered { get; private set; }
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
|||||||
{
|
{
|
||||||
public interface IBasketService
|
public interface IBasketService
|
||||||
{
|
{
|
||||||
Task<int> GetBasketItemCountAsync(string userName);
|
|
||||||
Task TransferBasketAsync(string anonymousId, string userName);
|
Task TransferBasketAsync(string anonymousId, string userName);
|
||||||
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1);
|
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1);
|
||||||
Task SetQuantities(int basketId, Dictionary<string, int> quantities);
|
Task SetQuantities(int basketId, Dictionary<string, int> quantities);
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using System.Linq;
|
|
||||||
using Ardalis.GuardClauses;
|
using Ardalis.GuardClauses;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||||
|
|
||||||
@@ -37,21 +36,6 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
await _basketRepository.DeleteAsync(basket);
|
await _basketRepository.DeleteAsync(basket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> GetBasketItemCountAsync(string userName)
|
|
||||||
{
|
|
||||||
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
|
||||||
var basketSpec = new BasketWithItemsSpecification(userName);
|
|
||||||
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
|
|
||||||
if (basket == null)
|
|
||||||
{
|
|
||||||
_logger.LogInformation($"No basket found for {userName}");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int count = basket.Items.Sum(i => i.Quantity);
|
|
||||||
_logger.LogInformation($"Basket for {userName} has {count} items.");
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task SetQuantities(int basketId, Dictionary<string, int> quantities)
|
public async Task SetQuantities(int basketId, Dictionary<string, int> quantities)
|
||||||
{
|
{
|
||||||
Guard.Against.Null(quantities, nameof(quantities));
|
Guard.Against.Null(quantities, nameof(quantities));
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
|||||||
{
|
{
|
||||||
AddInclude(b => b.Items);
|
AddInclude(b => b.Items);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BasketWithItemsSpecification(string buyerId) : base(b => b.BuyerId == buyerId)
|
public BasketWithItemsSpecification(string buyerId) : base(b => b.BuyerId == buyerId)
|
||||||
{
|
{
|
||||||
AddInclude(b => b.Items);
|
AddInclude(b => b.Items);
|
||||||
|
|||||||
@@ -56,16 +56,6 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
|
|||||||
Assert.Equal(1, firstItem.Quantity);
|
Assert.Equal(1, firstItem.Quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void RemoveEmptyItems()
|
|
||||||
{
|
|
||||||
var basket = new Basket(_buyerId);
|
|
||||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
|
|
||||||
basket.RemoveEmptyItems();
|
|
||||||
|
|
||||||
Assert.Equal(0, basket.Items.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CantAddItemWithNegativeQuantity()
|
public void CantAddItemWithNegativeQuantity()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
|
||||||
|
{
|
||||||
|
public class BasketRemoveEmptyItems
|
||||||
|
{
|
||||||
|
private readonly int _testCatalogItemId = 123;
|
||||||
|
private readonly decimal _testUnitPrice = 1.23m;
|
||||||
|
private readonly string _buyerId = "Test buyerId";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RemovesEmptyBasketItems()
|
||||||
|
{
|
||||||
|
var basket = new Basket(_buyerId);
|
||||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
|
||||||
|
basket.RemoveEmptyItems();
|
||||||
|
|
||||||
|
Assert.Equal(0, basket.Items.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
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 AddItemToBasket
|
||||||
|
{
|
||||||
|
private readonly string _buyerId = "Test buyerId";
|
||||||
|
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||||
|
|
||||||
|
public AddItemToBasket()
|
||||||
|
{
|
||||||
|
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvokesBasketRepositoryGetByIdAsyncOnce()
|
||||||
|
{
|
||||||
|
var basket = new Basket(_buyerId);
|
||||||
|
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||||
|
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>())).ReturnsAsync(basket);
|
||||||
|
|
||||||
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
|
|
||||||
|
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
||||||
|
|
||||||
|
_mockBasketRepo.Verify(x => x.GetByIdAsync(It.IsAny<int>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvokesBasketRepositoryUpdateAsyncOnce()
|
||||||
|
{
|
||||||
|
var basket = new Basket(_buyerId);
|
||||||
|
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||||
|
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>())).ReturnsAsync(basket);
|
||||||
|
|
||||||
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
|
|
||||||
|
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
||||||
|
|
||||||
|
_mockBasketRepo.Verify(x => x.UpdateAsync(basket), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ namespace Microsoft.eShopWeb.UnitTests
|
|||||||
private readonly string _buyerId = "Test buyerId";
|
private readonly string _buyerId = "Test buyerId";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatchesBasketWithGivenId()
|
public void MatchesBasketWithGivenBasketId()
|
||||||
{
|
{
|
||||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||||
|
|
||||||
@@ -23,14 +23,37 @@ namespace Microsoft.eShopWeb.UnitTests
|
|||||||
|
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
Assert.Equal(_testBasketId, result.Id);
|
Assert.Equal(_testBasketId, result.Id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatchesNoBasketsIfIdNotPresent()
|
public void MatchesNoBasketsIfBasketIdNotPresent()
|
||||||
{
|
{
|
||||||
int badId = -1;
|
int badBasketId = -1;
|
||||||
var spec = new BasketWithItemsSpecification(badId);
|
var spec = new BasketWithItemsSpecification(badBasketId);
|
||||||
|
|
||||||
|
Assert.False(GetTestBasketCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.Any(spec.Criterias.FirstOrDefault()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatchesBasketWithGivenBuyerId()
|
||||||
|
{
|
||||||
|
var spec = new BasketWithItemsSpecification(_buyerId);
|
||||||
|
|
||||||
|
var result = GetTestBasketCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.FirstOrDefault(spec.Criterias.FirstOrDefault());
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal(_buyerId, result.BuyerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatchesNoBasketsIfBuyerIdNotPresent()
|
||||||
|
{
|
||||||
|
string badBuyerId = "badBuyerId";
|
||||||
|
var spec = new BasketWithItemsSpecification(badBuyerId);
|
||||||
|
|
||||||
Assert.False(GetTestBasketCollection()
|
Assert.False(GetTestBasketCollection()
|
||||||
.AsQueryable()
|
.AsQueryable()
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||||
|
{
|
||||||
|
public class CatalogFilterPaginatedSpecification
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ReturnsAllCatalogItems()
|
||||||
|
{
|
||||||
|
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, null, null);
|
||||||
|
|
||||||
|
var result = GetTestCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(spec.Criterias.FirstOrDefault());
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal(4, result.ToList().Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Returns2CatalogItemsWithSameBrandAndTypeId()
|
||||||
|
{
|
||||||
|
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, 1, 1);
|
||||||
|
|
||||||
|
var result = GetTestCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(spec.Criterias.FirstOrDefault());
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal(2, result.ToList().Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<CatalogItem> GetTestCollection()
|
||||||
|
{
|
||||||
|
var catalogItemList = new List<CatalogItem>();
|
||||||
|
|
||||||
|
catalogItemList.Add(new CatalogItem(1, 1, "Item 1", "Item 1", 1.00m, "TestUri1"));
|
||||||
|
catalogItemList.Add(new CatalogItem(1, 1, "Item 1.5", "Item 1.5", 1.50m, "TestUri1"));
|
||||||
|
catalogItemList.Add(new CatalogItem(2, 2, "Item 2", "Item 2", 2.00m, "TestUri2"));
|
||||||
|
catalogItemList.Add(new CatalogItem(3, 3, "Item 3", "Item 3", 3.00m, "TestUri3"));
|
||||||
|
|
||||||
|
return catalogItemList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.UnitTests
|
namespace Microsoft.eShopWeb.UnitTests
|
||||||
{
|
{
|
||||||
public class CatalogFilterSpecificationFilter
|
public class CatalogFilterSpecification
|
||||||
{
|
{
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(null, null, 5)]
|
[InlineData(null, null, 5)]
|
||||||
@@ -18,7 +17,7 @@ namespace Microsoft.eShopWeb.UnitTests
|
|||||||
[InlineData(2, 3, 0)]
|
[InlineData(2, 3, 0)]
|
||||||
public void MatchesExpectedNumberOfItems(int? brandId, int? typeId, int expectedCount)
|
public void MatchesExpectedNumberOfItems(int? brandId, int? typeId, int expectedCount)
|
||||||
{
|
{
|
||||||
var spec = new CatalogFilterSpecification(brandId, typeId);
|
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterSpecification(brandId, typeId);
|
||||||
|
|
||||||
var result = GetTestItemCollection()
|
var result = GetTestItemCollection()
|
||||||
.AsQueryable()
|
.AsQueryable()
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||||
|
using Moq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||||
|
{
|
||||||
|
public class CatalogItemsSpecification
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void MatchesSpecificCatalogItem()
|
||||||
|
{
|
||||||
|
var catalogItemIds = new int[] { 1 };
|
||||||
|
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||||
|
|
||||||
|
var result = GetTestCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(spec.Criterias.FirstOrDefault());
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Single(result.ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatchesAllCatalogItems()
|
||||||
|
{
|
||||||
|
var catalogItemIds = new int[] { 1, 3 };
|
||||||
|
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||||
|
|
||||||
|
var result = GetTestCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(spec.Criterias.FirstOrDefault());
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal(2, result.ToList().Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<CatalogItem> GetTestCollection()
|
||||||
|
{
|
||||||
|
var catalogItems = new List<CatalogItem>();
|
||||||
|
|
||||||
|
var mockCatalogItem1 = new Mock<CatalogItem>(1, 1, "Item 1 description", "Item 1", 1.5m, "Item1Uri");
|
||||||
|
mockCatalogItem1.SetupGet(x => x.Id).Returns(1);
|
||||||
|
|
||||||
|
var mockCatalogItem3 = new Mock<CatalogItem>(3, 3, "Item 3 description", "Item 3", 3.5m, "Item3Uri");
|
||||||
|
mockCatalogItem3.SetupGet(x => x.Id).Returns(3);
|
||||||
|
|
||||||
|
catalogItems.Add(mockCatalogItem1.Object);
|
||||||
|
catalogItems.Add(mockCatalogItem3.Object);
|
||||||
|
|
||||||
|
return catalogItems;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||||
|
{
|
||||||
|
public class CustomerOrdersWithItemsSpecification
|
||||||
|
{
|
||||||
|
private readonly string _buyerId = "TestBuyerId";
|
||||||
|
private Address _shipToAddress = new Address("Street", "City", "OH", "US", "11111");
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ReturnsOrderWithOrderedItem()
|
||||||
|
{
|
||||||
|
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||||
|
|
||||||
|
var result = GetTestCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.FirstOrDefault(spec.Criterias.FirstOrDefault());
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.NotNull(result.OrderItems);
|
||||||
|
Assert.Equal(1, result.OrderItems.Count);
|
||||||
|
Assert.NotNull(result.OrderItems.FirstOrDefault().ItemOrdered);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ReturnsAllOrderWithAllOrderedItem()
|
||||||
|
{
|
||||||
|
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||||
|
|
||||||
|
var result = GetTestCollection()
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(spec.Criterias.FirstOrDefault())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal(2, result.Count);
|
||||||
|
Assert.Equal(1, result[0].OrderItems.Count);
|
||||||
|
Assert.NotNull(result[0].OrderItems.FirstOrDefault().ItemOrdered);
|
||||||
|
Assert.Equal(2, result[1].OrderItems.Count);
|
||||||
|
Assert.NotNull(result[1].OrderItems.ToList()[0].ItemOrdered);
|
||||||
|
Assert.NotNull(result[1].OrderItems.ToList()[1].ItemOrdered);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Order> GetTestCollection()
|
||||||
|
{
|
||||||
|
var ordersList = new List<Order>();
|
||||||
|
|
||||||
|
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||||
|
new List<OrderItem>
|
||||||
|
{
|
||||||
|
new OrderItem(new CatalogItemOrdered(1, "Product1", "testurl"), 10.50m, 1)
|
||||||
|
}));
|
||||||
|
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||||
|
new List<OrderItem>
|
||||||
|
{
|
||||||
|
new OrderItem(new CatalogItemOrdered(2, "Product2", "testurl"), 15.50m, 2),
|
||||||
|
new OrderItem(new CatalogItemOrdered(2, "Product3", "testurl"), 20.50m, 1)
|
||||||
|
}));
|
||||||
|
|
||||||
|
return ordersList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user