Browse Source
* Moving Identity seeding to its own class and method. * Adding tests for AddItem * Added catalog api controller and functional tests Added and cleaned up some comments * Adding integration tests for OrderRepository * Getting integration test for order working with inmemory dbmain
committed by
GitHub
17 changed files with 306 additions and 94 deletions
@ -1,29 +0,0 @@ |
|||||
using System; |
|
||||
|
|
||||
namespace ApplicationCore.Exceptions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Note: No longer required.
|
|
||||
/// </summary>
|
|
||||
public class CatalogImageMissingException : Exception |
|
||||
{ |
|
||||
public CatalogImageMissingException(string message, |
|
||||
Exception innerException = null) |
|
||||
: base(message, innerException: innerException) |
|
||||
{ |
|
||||
} |
|
||||
public CatalogImageMissingException(Exception innerException) |
|
||||
: base("No catalog image found for the provided id.", |
|
||||
innerException: innerException) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public CatalogImageMissingException() : base() |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public CatalogImageMissingException(string message) : base(message) |
|
||||
{ |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,14 @@ |
|||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Infrastructure.Identity |
||||
|
{ |
||||
|
public class AppIdentityDbContextSeed |
||||
|
{ |
||||
|
public static async Task SeedAsync(UserManager<ApplicationUser> userManager) |
||||
|
{ |
||||
|
var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" }; |
||||
|
await userManager.CreateAsync(defaultUser, "Pass@word1"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using Microsoft.eShopWeb.Services; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Microsoft.eShopWeb.Controllers.Api |
||||
|
{ |
||||
|
[Route("api/[controller]/[action]")]
|
||||
|
public class BaseApiController : Controller |
||||
|
{ } |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
using Microsoft.eShopWeb.Services; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Microsoft.eShopWeb.Controllers.Api |
||||
|
{ |
||||
|
public class CatalogController : BaseApiController |
||||
|
{ |
||||
|
private readonly ICatalogService _catalogService; |
||||
|
|
||||
|
public CatalogController(ICatalogService catalogService) => _catalogService = catalogService; |
||||
|
|
||||
|
[HttpGet] |
||||
|
public async Task<IActionResult> List(int? brandFilterApplied, int? typesFilterApplied, int? page) |
||||
|
{ |
||||
|
var itemsPage = 10; |
||||
|
var catalogModel = await _catalogService.GetCatalogItems(page ?? 0, itemsPage, brandFilterApplied, typesFilterApplied); |
||||
|
return Ok(catalogModel); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.IO; |
||||
|
using Xunit; |
||||
|
using System.Threading.Tasks; |
||||
|
using Newtonsoft.Json; |
||||
|
using Microsoft.eShopWeb.ViewModels; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace FunctionalTests.Web.Controllers |
||||
|
{ |
||||
|
public class ApiCatalogControllerList : BaseWebTest |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task ReturnsFirst10CatalogItems() |
||||
|
{ |
||||
|
var response = await _client.GetAsync("/api/catalog/list"); |
||||
|
response.EnsureSuccessStatusCode(); |
||||
|
var stringResponse = await response.Content.ReadAsStringAsync(); |
||||
|
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse); |
||||
|
|
||||
|
Assert.Equal(10, model.CatalogItems.Count()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task ReturnsLast2CatalogItemsGivenPageIndex1() |
||||
|
{ |
||||
|
var response = await _client.GetAsync("/api/catalog/list?page=1"); |
||||
|
response.EnsureSuccessStatusCode(); |
||||
|
var stringResponse = await response.Content.ReadAsStringAsync(); |
||||
|
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse); |
||||
|
|
||||
|
Assert.Equal(2, model.CatalogItems.Count()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,29 +0,0 @@ |
|||||
using System.IO; |
|
||||
using Xunit; |
|
||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace FunctionalTests.Web.Controllers |
|
||||
{ |
|
||||
public class CatalogControllerGetImage : BaseWebTest |
|
||||
{ |
|
||||
//[Fact]
|
|
||||
// GetImage replaced by static file middleware
|
|
||||
public async Task ReturnsFileContentResultGivenValidId() |
|
||||
{ |
|
||||
var testFilePath = Path.Combine(_contentRoot, "pics//1.png"); |
|
||||
var expectedFileBytes = File.ReadAllBytes(testFilePath); |
|
||||
|
|
||||
var response = await _client.GetAsync("/catalog/pic/1"); |
|
||||
response.EnsureSuccessStatusCode(); |
|
||||
var streamResponse = await response.Content.ReadAsStreamAsync(); |
|
||||
byte[] byteResult; |
|
||||
using (var ms = new MemoryStream()) |
|
||||
{ |
|
||||
streamResponse.CopyTo(ms); |
|
||||
byteResult = ms.ToArray(); |
|
||||
} |
|
||||
|
|
||||
Assert.Equal(expectedFileBytes, byteResult); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,46 @@ |
|||||
|
using ApplicationCore.Entities.OrderAggregate; |
||||
|
using Infrastructure.Data; |
||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using UnitTests.Builders; |
||||
|
using Xunit; |
||||
|
using Xunit.Abstractions; |
||||
|
|
||||
|
namespace IntegrationTests.Repositories.OrderRepositoryTests |
||||
|
{ |
||||
|
public class GetById |
||||
|
{ |
||||
|
private readonly CatalogContext _catalogContext; |
||||
|
private readonly OrderRepository _orderRepository; |
||||
|
private OrderBuilder OrderBuilder { get; } = new OrderBuilder(); |
||||
|
private readonly ITestOutputHelper _output; |
||||
|
public GetById(ITestOutputHelper output) |
||||
|
{ |
||||
|
_output = output; |
||||
|
var dbOptions = new DbContextOptionsBuilder<CatalogContext>() |
||||
|
.UseInMemoryDatabase(databaseName: "TestCatalog") |
||||
|
.Options; |
||||
|
_catalogContext = new CatalogContext(dbOptions); |
||||
|
_orderRepository = new OrderRepository(_catalogContext); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void GetsExistingOrder() |
||||
|
{ |
||||
|
var existingOrder = OrderBuilder.WithDefaultValues(); |
||||
|
_catalogContext.Orders.Add(existingOrder); |
||||
|
_catalogContext.SaveChanges(); |
||||
|
int orderId = existingOrder.Id; |
||||
|
_output.WriteLine($"OrderId: {orderId}"); |
||||
|
|
||||
|
var orderFromRepo = _orderRepository.GetById(orderId); |
||||
|
Assert.Equal(OrderBuilder.TestBuyerId, orderFromRepo.BuyerId); |
||||
|
|
||||
|
// Note: Using InMemoryDatabase OrderItems is available. Will be null if using SQL DB.
|
||||
|
var firstItem = orderFromRepo.OrderItems.FirstOrDefault(); |
||||
|
Assert.Equal(OrderBuilder.TestUnits, firstItem.Units); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities; |
||||
|
using System.Linq; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace UnitTests.ApplicationCore.Entities.BasketTests |
||||
|
{ |
||||
|
public class AddItem |
||||
|
{ |
||||
|
private int _testCatalogItemId = 123; |
||||
|
private decimal _testUnitPrice = 1.23m; |
||||
|
private int _testQuantity = 2; |
||||
|
|
||||
|
[Fact] |
||||
|
public void AddsBasketItemIfNotPresent() |
||||
|
{ |
||||
|
var basket = new Basket(); |
||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity); |
||||
|
|
||||
|
var firstItem = basket.Items.Single(); |
||||
|
Assert.Equal(_testCatalogItemId, firstItem.CatalogItemId); |
||||
|
Assert.Equal(_testUnitPrice, firstItem.UnitPrice); |
||||
|
Assert.Equal(_testQuantity, firstItem.Quantity); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void IncrementsQuantityOfItemIfPresent() |
||||
|
{ |
||||
|
var basket = new Basket(); |
||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity); |
||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity); |
||||
|
|
||||
|
var firstItem = basket.Items.Single(); |
||||
|
Assert.Equal(_testQuantity * 2, firstItem.Quantity); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void KeepsOriginalUnitPriceIfMoreItemsAdded() |
||||
|
{ |
||||
|
var basket = new Basket(); |
||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity); |
||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice * 2, _testQuantity); |
||||
|
|
||||
|
var firstItem = basket.Items.Single(); |
||||
|
Assert.Equal(_testUnitPrice, firstItem.UnitPrice); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DefaultsToQuantityOfOne() |
||||
|
{ |
||||
|
var basket = new Basket(); |
||||
|
basket.AddItem(_testCatalogItemId, _testUnitPrice); |
||||
|
|
||||
|
var firstItem = basket.Items.Single(); |
||||
|
Assert.Equal(1, firstItem.Quantity); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using ApplicationCore.Entities.OrderAggregate; |
||||
|
|
||||
|
namespace UnitTests.Builders |
||||
|
{ |
||||
|
public class AddressBuilder |
||||
|
{ |
||||
|
private Address _address; |
||||
|
public string TestStreet => "123 Main St."; |
||||
|
public string TestCity => "Kent"; |
||||
|
public string TestState => "OH"; |
||||
|
public string TestCountry => "USA"; |
||||
|
public string TestZipCode => "44240"; |
||||
|
|
||||
|
public AddressBuilder() |
||||
|
{ |
||||
|
_address = WithDefaultValues(); |
||||
|
} |
||||
|
public Address Build() |
||||
|
{ |
||||
|
return _address; |
||||
|
} |
||||
|
public Address WithDefaultValues() |
||||
|
{ |
||||
|
_address = new Address(TestStreet, TestCity, TestState, TestCountry, TestZipCode); |
||||
|
return _address; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using ApplicationCore.Entities.OrderAggregate; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace UnitTests.Builders |
||||
|
{ |
||||
|
public class OrderBuilder |
||||
|
{ |
||||
|
private Order _order; |
||||
|
public string TestBuyerId => "12345"; |
||||
|
public int TestCatalogItemId => 234; |
||||
|
public string TestProductName => "Test Product Name"; |
||||
|
public string TestPictureUri => "http://test.com/image.jpg"; |
||||
|
public decimal TestUnitPrice = 1.23m; |
||||
|
public int TestUnits = 3; |
||||
|
public CatalogItemOrdered TestCatalogItemOrdered { get; } |
||||
|
|
||||
|
public OrderBuilder() |
||||
|
{ |
||||
|
TestCatalogItemOrdered = new CatalogItemOrdered(TestCatalogItemId, TestProductName, TestPictureUri); |
||||
|
_order = WithDefaultValues(); |
||||
|
} |
||||
|
|
||||
|
public Order Build() |
||||
|
{ |
||||
|
return _order; |
||||
|
} |
||||
|
|
||||
|
public Order WithDefaultValues() |
||||
|
{ |
||||
|
var orderItem = new OrderItem(TestCatalogItemOrdered, TestUnitPrice, TestUnits); |
||||
|
var itemList = new List<OrderItem>() { orderItem }; |
||||
|
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), itemList); |
||||
|
return _order; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue