Skip to content

Commit

Permalink
Adding additional unit tests (dotnet-architecture#406)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
efleming18 authored Jun 21, 2020
1 parent d8848a9 commit a0ba412
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/ApplicationCore/Entities/OrderAggregate/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public Order(string buyerId, Address shipToAddress, List<OrderItem> items)
ShipToAddress = shipToAddress;
_orderItems = items;
}
public string BuyerId { get; private set; }

public string BuyerId { get; private set; }
public DateTimeOffset OrderDate { get; private set; } = DateTimeOffset.Now;
public Address ShipToAddress { get; private set; }

Expand Down
1 change: 0 additions & 1 deletion src/ApplicationCore/Entities/OrderAggregate/OrderItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate
{

public class OrderItem : BaseEntity
{
public CatalogItemOrdered ItemOrdered { get; private set; }
Expand Down
1 change: 0 additions & 1 deletion src/ApplicationCore/Interfaces/IBasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IBasketService
{
Task<int> GetBasketItemCountAsync(string userName);
Task TransferBasketAsync(string anonymousId, string userName);
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1);
Task SetQuantities(int basketId, Dictionary<string, int> quantities);
Expand Down
16 changes: 0 additions & 16 deletions src/ApplicationCore/Services/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using System.Linq;
using Ardalis.GuardClauses;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;

Expand Down Expand Up @@ -37,21 +36,6 @@ public async Task DeleteBasketAsync(int basketId)
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)
{
Guard.Against.Null(quantities, nameof(quantities));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public BasketWithItemsSpecification(int basketId) : base(b => b.Id == basketId)
{
AddInclude(b => b.Items);
}

public BasketWithItemsSpecification(string buyerId) : base(b => b.BuyerId == buyerId)
{
AddInclude(b => b.Items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ public void DefaultsToQuantityOfOne()
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]
public void CantAddItemWithNegativeQuantity()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BasketWithItems
private readonly string _buyerId = "Test buyerId";

[Fact]
public void MatchesBasketWithGivenId()
public void MatchesBasketWithGivenBasketId()
{
var spec = new BasketWithItemsSpecification(_testBasketId);

Expand All @@ -23,14 +23,37 @@ public void MatchesBasketWithGivenId()

Assert.NotNull(result);
Assert.Equal(_testBasketId, result.Id);
}

[Fact]
public void MatchesNoBasketsIfBasketIdNotPresent()
{
int badBasketId = -1;
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 MatchesNoBasketsIfIdNotPresent()
public void MatchesNoBasketsIfBuyerIdNotPresent()
{
int badId = -1;
var spec = new BasketWithItemsSpecification(badId);
string badBuyerId = "badBuyerId";
var spec = new BasketWithItemsSpecification(badBuyerId);

Assert.False(GetTestBasketCollection()
.AsQueryable()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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.Linq;
using Xunit;

namespace Microsoft.eShopWeb.UnitTests
{
public class CatalogFilterSpecificationFilter
public class CatalogFilterSpecification
{
[Theory]
[InlineData(null, null, 5)]
Expand All @@ -18,7 +17,7 @@ public class CatalogFilterSpecificationFilter
[InlineData(2, 3, 0)]
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()
.AsQueryable()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit a0ba412

Please sign in to comment.