Refactoring and Adding Tests (#58)

* 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 db
This commit is contained in:
Steve Smith
2017-10-20 12:52:42 -04:00
committed by GitHub
parent 32950aa175
commit 0eb4d72b89
17 changed files with 306 additions and 94 deletions

View File

@@ -1,5 +1,7 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
// This can easily be modified to be BaseEntity<T> and public T Id to support different key types.
// Using non-generic integer types for simplicity and to ease caching logic
public class BaseEntity
{
public int Id { get; set; }

View File

@@ -5,6 +5,5 @@
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public int CatalogItemId { get; set; }
// public CatalogItem Item { get; set; }
}
}

View File

@@ -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)
{
}
}
}

View File

@@ -12,7 +12,6 @@ namespace ApplicationCore.Services
public string ComposePicUri(string uriTemplate)
{
return uriTemplate.Replace("http://catalogbaseurltobereplaced", _catalogSettings.CatalogBaseUrl);
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -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
{ }
}

View File

@@ -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);
}
}
}

View File

@@ -18,25 +18,20 @@ namespace Microsoft.eShopWeb
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
try
{
var catalogContext = services.GetRequiredService<CatalogContext>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
CatalogContextSeed.SeedAsync(catalogContext, loggerFactory)
.Wait();
// move to IdentitySeed method
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" };
userManager.CreateAsync(defaultUser, "Pass@word1").Wait();
AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
//var logger = services.GetRequiredService<ILogger<Program>>();
//logger.LogError(ex, "An error occurred seeding the DB.");
var logger = loggerFactory.CreateLogger<Program>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}

View File

@@ -30,29 +30,54 @@ namespace Microsoft.eShopWeb
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
public void ConfigureDevelopmentServices(IServiceCollection services)
{
// use in-memory database
ConfigureTestingServices(services);
// use real database
// ConfigureProductionServices(services);
}
public void ConfigureTestingServices(IServiceCollection services)
{
// use in-memory database
services.AddDbContext<CatalogContext>(c =>
c.UseInMemoryDatabase("Catalog"));
// Add Identity DbContext
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseInMemoryDatabase("Identity"));
ConfigureServices(services);
}
public void ConfigureProductionServices(IServiceCollection services)
{
// use real database
services.AddDbContext<CatalogContext>(c =>
{
try
{
//c.UseInMemoryDatabase("Catalog");
// Requires LocalDB which can be installed with SQL Server Express 2016
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
}
catch (System.Exception ex )
catch (System.Exception ex)
{
var message = ex.Message;
}
}
});
// Add Identity DbContext
services.AddDbContext<AppIdentityDbContext>(options =>
//options.UseInMemoryDatabase("Identity"));
options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
ConfigureServices(services);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
@@ -129,22 +154,5 @@ namespace Microsoft.eShopWeb
await context.Response.WriteAsync(sb.ToString());
}));
}
// moved to Program.cs
//public void ConfigureDevelopment(IApplicationBuilder app,
// IHostingEnvironment env,
// ILoggerFactory loggerFactory,
// UserManager<ApplicationUser> userManager,
// CatalogContext catalogContext)
//{
// Configure(app, env);
// //Seed Data
// CatalogContextSeed.SeedAsync(app, catalogContext, loggerFactory)
// .Wait();
// var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" };
// userManager.CreateAsync(defaultUser, "Pass@word1").Wait();
//}
}
}