Fix 147 include order id in details query (#169)
* Adding Id to query so the correct order details are always pulled back * Removing unused usings - also removed the sync method as it is not used anywhere in the solution. The Async should be the preferred one. * Adding integration test for GetByIdWithItemAsync * Rename test
This commit is contained in:
committed by
Steve Smith
parent
c7c16c4265
commit
0d44a514ab
@@ -4,9 +4,8 @@ using System.Threading.Tasks;
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
||||
{
|
||||
|
||||
public interface IOrderRepository : IRepository<Order>, IAsyncRepository<Order>
|
||||
public interface IOrderRepository : IAsyncRepository<Order>
|
||||
{
|
||||
Order GetByIdWithItems(int id);
|
||||
Task<Order> GetByIdWithItemsAsync(int id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
@@ -11,21 +10,13 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
public OrderRepository(CatalogContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public Order GetByIdWithItems(int id)
|
||||
{
|
||||
return _dbContext.Orders
|
||||
.Include(o => o.OrderItems)
|
||||
.Include($"{nameof(Order.OrderItems)}.{nameof(OrderItem.ItemOrdered)}")
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
|
||||
public Task<Order> GetByIdWithItemsAsync(int id)
|
||||
{
|
||||
return _dbContext.Orders
|
||||
.Include(o => o.OrderItems)
|
||||
.Include($"{nameof(Order.OrderItems)}.{nameof(OrderItem.ItemOrdered)}")
|
||||
.FirstOrDefaultAsync();
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using Microsoft.eShopWeb.Infrastructure.Data;
|
||||
using Microsoft.eShopWeb.UnitTests.Builders;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
||||
{
|
||||
public class GetByIdWithItemsAsync_Should
|
||||
{
|
||||
private readonly CatalogContext _catalogContext;
|
||||
private readonly OrderRepository _orderRepository;
|
||||
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
|
||||
private readonly ITestOutputHelper _output;
|
||||
public GetByIdWithItemsAsync_Should(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
var dbOptions = new DbContextOptionsBuilder<CatalogContext>()
|
||||
.UseInMemoryDatabase(databaseName: "TestCatalog")
|
||||
.Options;
|
||||
_catalogContext = new CatalogContext(dbOptions);
|
||||
_orderRepository = new OrderRepository(_catalogContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOrderAndItemsByOrderId_When_MultipleOrdersPresent()
|
||||
{
|
||||
//Arrange
|
||||
var itemOneUnitPrice = 5.50m;
|
||||
var itemOneUnits = 2;
|
||||
var itemTwoUnitPrice = 7.50m;
|
||||
var itemTwoUnits = 5;
|
||||
|
||||
var firstOrder = OrderBuilder.WithDefaultValues();
|
||||
_catalogContext.Orders.Add(firstOrder);
|
||||
int firstOrderId = firstOrder.Id;
|
||||
|
||||
var secondOrderItems = new List<OrderItem>();
|
||||
secondOrderItems.Add(new OrderItem(OrderBuilder.TestCatalogItemOrdered, itemOneUnitPrice, itemOneUnits));
|
||||
secondOrderItems.Add(new OrderItem(OrderBuilder.TestCatalogItemOrdered, itemTwoUnitPrice, itemTwoUnits));
|
||||
var secondOrder = OrderBuilder.WithItems(secondOrderItems);
|
||||
_catalogContext.Orders.Add(secondOrder);
|
||||
int secondOrderId = secondOrder.Id;
|
||||
|
||||
_catalogContext.SaveChanges();
|
||||
|
||||
//Act
|
||||
var orderFromRepo = await _orderRepository.GetByIdWithItemsAsync(secondOrderId);
|
||||
|
||||
//Assert
|
||||
Assert.Equal(secondOrderId, orderFromRepo.Id);
|
||||
Assert.Equal(secondOrder.OrderItems.Count, orderFromRepo.OrderItems.Count);
|
||||
Assert.Equal(1, orderFromRepo.OrderItems.Count(x => x.UnitPrice == itemOneUnitPrice));
|
||||
Assert.Equal(1, orderFromRepo.OrderItems.Count(x => x.UnitPrice == itemTwoUnitPrice));
|
||||
Assert.Equal(itemOneUnits, orderFromRepo.OrderItems.SingleOrDefault(x => x.UnitPrice == itemOneUnitPrice).Units);
|
||||
Assert.Equal(itemTwoUnits, orderFromRepo.OrderItems.SingleOrDefault(x => x.UnitPrice == itemTwoUnitPrice).Units);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user