Update Specification and other packages to latest version (#582)

* Updating repositories and specification version
Need to fix broken tests

* removing test that would just be testing mocked result now

* Refactored from IAsyncRepository and removed it.
Tests pass.

* Update packages
This commit is contained in:
Steve Smith
2021-10-25 15:13:02 -04:00
committed by GitHub
parent fee2bbce3d
commit 8a45a2c858
39 changed files with 281 additions and 289 deletions

View File

@@ -13,7 +13,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.BasketRepositoryTests
public class SetQuantities
{
private readonly CatalogContext _catalogContext;
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly EfRepository<Basket> _basketRepository;
private readonly BasketBuilder BasketBuilder = new BasketBuilder();
public SetQuantities()

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using System.Linq;
@@ -11,7 +12,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
public class GetById
{
private readonly CatalogContext _catalogContext;
private readonly OrderRepository _orderRepository;
private readonly EfRepository<Order> _orderRepository;
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
private readonly ITestOutputHelper _output;
public GetById(ITestOutputHelper output)
@@ -21,7 +22,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_orderRepository = new OrderRepository(_catalogContext);
_orderRepository = new EfRepository<Order>(_catalogContext);
}
[Fact]
@@ -37,6 +38,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
Assert.Equal(OrderBuilder.TestBuyerId, orderFromRepo.BuyerId);
// Note: Using InMemoryDatabase OrderItems is available. Will be null if using SQL DB.
// Use the OrderWithItemsByIdSpec instead of just GetById to get the full aggregate
var firstItem = orderFromRepo.OrderItems.FirstOrDefault();
Assert.Equal(OrderBuilder.TestUnits, firstItem.Units);
}

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using System.Collections.Generic;
@@ -12,7 +13,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
public class GetByIdWithItemsAsync
{
private readonly CatalogContext _catalogContext;
private readonly OrderRepository _orderRepository;
private readonly EfRepository<Order> _orderRepository;
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
public GetByIdWithItemsAsync()
@@ -21,7 +22,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_orderRepository = new OrderRepository(_catalogContext);
_orderRepository = new EfRepository<Order>(_catalogContext);
}
[Fact]
@@ -47,7 +48,8 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
_catalogContext.SaveChanges();
//Act
var orderFromRepo = await _orderRepository.GetByIdWithItemsAsync(secondOrderId);
var spec = new OrderWithItemsByIdSpec(secondOrderId);
var orderFromRepo = await _orderRepository.GetBySpecAsync(spec);
//Assert
Assert.Equal(secondOrderId, orderFromRepo.Id);

View File

@@ -11,25 +11,20 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class AddItemToBasket
{
private readonly string _buyerId = "Test buyerId";
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public AddItemToBasket()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnce()
{
var basket = new Basket(_buyerId);
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
}
[Fact]
@@ -37,7 +32,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var basket = new Basket(_buyerId);
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
var basketService = new BasketService(_mockBasketRepo.Object, null);

View File

@@ -10,12 +10,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class DeleteBasket
{
private readonly string _buyerId = "Test buyerId";
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public DeleteBasket()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()

View File

@@ -1,40 +1,35 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Moq;
using System;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Moq;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class SetQuantities
{
private readonly int _invalidId = -1;
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public SetQuantities()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
[Fact]
public async Task ThrowsGivenInvalidBasketId()
{
var basketService = new BasketService(_mockBasketRepo.Object, null);
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
}
[Fact]
public async Task ThrowsGivenNullQuantities()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await basketService.SetQuantities(123, null));
}
}
}
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class SetQuantities
{
private readonly int _invalidId = -1;
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task ThrowsGivenInvalidBasketId()
{
var basketService = new BasketService(_mockBasketRepo.Object, null);
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
}
[Fact]
public async Task ThrowsGivenNullQuantities()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await basketService.SetQuantities(123, null));
}
}
}

View File

@@ -1,41 +1,36 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Moq;
using System;
using System;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class TransferBasket
{
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public TransferBasket()
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class TransferBasket
{
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task ThrowsGivenNullAnonymousId()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
[Fact]
public async Task ThrowsGivenNullAnonymousId()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
}
[Fact]
public async Task ThrowsGivenNullUserId()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
}
[Fact]
public async Task ThrowsGivenNullUserId()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
}
[Fact]
@@ -43,12 +38,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var anonymousBasket = null as Basket;
var userBasket = new Basket(_existentUserBasketBuyerId);
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
}
[Fact]
@@ -60,7 +55,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
var userBasket = new Basket(_existentUserBasketBuyerId);
userBasket.AddItem(1, 10, 4);
userBasket.AddItem(2, 99, 3);
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
@@ -77,7 +72,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
var userBasket = new Basket(_existentUserBasketBuyerId);
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
@@ -91,12 +86,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
var userBasket = null as Basket;
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
}
}
}
}
}

View File

@@ -1,5 +1,4 @@
using Ardalis.Specification.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Moq;
using System.Collections.Generic;
@@ -13,16 +12,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
private readonly int _testBasketId = 123;
private readonly string _buyerId = "Test buyerId";
// tests with specifications can use an evaluator or just WhereExpressions.FirstOrDefault if only one
private readonly SpecificationEvaluator<Basket> _evaluator = new SpecificationEvaluator<Basket>();
[Fact]
public void MatchesBasketWithGivenBasketId()
{
var spec = new BasketWithItemsSpecification(_testBasketId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.FirstOrDefault();
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
Assert.NotNull(result);
Assert.Equal(_testBasketId, result.Id);
@@ -34,8 +29,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
int badBasketId = -1;
var spec = new BasketWithItemsSpecification(badBasketId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.Any();
var result = spec.Evaluate(GetTestBasketCollection()).Any();
Assert.False(result);
}
@@ -45,8 +39,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
{
var spec = new BasketWithItemsSpecification(_buyerId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.FirstOrDefault();
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
Assert.NotNull(result);
Assert.Equal(_buyerId, result.BuyerId);
@@ -58,8 +51,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
string badBuyerId = "badBuyerId";
var spec = new BasketWithItemsSpecification(badBuyerId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.Any();
var result = spec.Evaluate(GetTestBasketCollection()).Any();
Assert.False(result);
}

View File

@@ -12,7 +12,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
{
public class GetMyOrders
{
private readonly Mock<IOrderRepository> _mockOrderRepository;
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
public GetMyOrders()
{
@@ -20,12 +20,12 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
Order order = new Order("buyerId", address, new List<OrderItem> { item });
_mockOrderRepository = new Mock<IOrderRepository>();
_mockOrderRepository = new Mock<IReadRepository<Order>>();
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
}
[Fact]
public async Task NotReturnNullIfOrdersArePresent()
public async Task NotReturnNullIfOrdersArePresIent()
{
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");

View File

@@ -1,6 +1,7 @@
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Web.Features.OrderDetails;
using Moq;
using System.Collections.Generic;
@@ -12,7 +13,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
{
public class GetOrderDetails
{
private readonly Mock<IOrderRepository> _mockOrderRepository;
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
public GetOrderDetails()
{
@@ -20,8 +21,9 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
Order order = new Order("buyerId", address, new List<OrderItem> { item });
_mockOrderRepository = new Mock<IOrderRepository>();
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
_mockOrderRepository = new Mock<IReadRepository<Order>>();
_mockOrderRepository.Setup(x => x.GetBySpecAsync(It.IsAny<OrderWithItemsByIdSpec>(),default))
.ReturnsAsync(order);
}
[Fact]
@@ -35,17 +37,5 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
Assert.NotNull(result);
}
[Fact]
public async Task BeNullIfOrderNotFound()
{
var request = new eShopWeb.Web.Features.OrderDetails.GetOrderDetails("SomeUserName", 100);
var handler = new GetOrderDetailsHandler(_mockOrderRepository.Object);
var result = await handler.Handle(request, CancellationToken.None);
Assert.Null(result);
}
}
}