Skip to content

Commit

Permalink
Fix MusicStore
Browse files Browse the repository at this point in the history
Specifically:
* Move to using SQLite in-memory testing (most of the work)
* Changed a couple of MusicStore queries--will file issues and update the source ASAP
  • Loading branch information
ajcvickers authored and dougbu committed Jun 1, 2019
1 parent b1e8bf2 commit 6c8d2bd
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public async Task<IActionResult> Browse(string genre)
{
// Retrieve Genre genre and its Associated associated Albums albums from database
var genreModel = await DbContext.Genres
.Include(g => g.Albums)
.Where(g => g.Name == genre)
.FirstOrDefaultAsync();

Expand All @@ -45,6 +44,8 @@ public async Task<IActionResult> Browse(string genre)
return NotFound();
}

await DbContext.Entry(genreModel).Collection(g => g.Albums).LoadAsync();

return View(genreModel);
}

Expand Down Expand Up @@ -83,4 +84,4 @@ public async Task<IActionResult> Details(
return View(album);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using MusicStore.Components;
using MusicStore.Mocks.Common;
Expand Down Expand Up @@ -47,16 +44,9 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

// Add EF services to the services container
if (_platform.UseInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase("Scratch"));
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
// Add EF services to the services container
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlite("Data Source=MusicStore.db"));

// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using System;
using System.Globalization;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Authentication.Twitter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MusicStore.Components;
using MusicStore.Mocks.Common;
using MusicStore.Mocks.Facebook;
Expand Down Expand Up @@ -50,16 +45,8 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

// Add EF services to the services container
if (_platform.UseInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase("Scratch"));
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlite("Data Source=MusicStore.db"));

// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
7 changes: 4 additions & 3 deletions src/MusicStore/samples/MusicStore/Models/ShoppingCart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,18 @@ public Task<int> GetCount()
.SumAsync();
}

public Task<decimal> GetTotal()
public async Task<decimal> GetTotal()
{
// Multiply album price by count of that album to get
// the current price for each of those albums in the cart
// sum all album price totals to get the cart total

return _dbContext
return (await _dbContext
.CartItems
.Where(c => c.CartId == _shoppingCartId)
.Select(c => c.Album.Price * c.Count)
.SumAsync();
.ToListAsync())
.Sum();
}

public async Task CreateOrder(Order order)
Expand Down
3 changes: 1 addition & 2 deletions src/MusicStore/samples/MusicStore/MusicStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
<Reference Include="Microsoft.AspNetCore.Session" />
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
<Reference Include="Microsoft.EntityFrameworkCore.InMemory" />
<Reference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<Reference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
</ItemGroup>

Expand Down
14 changes: 2 additions & 12 deletions src/MusicStore/samples/MusicStore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MusicStore.Components;
using MusicStore.Models;

Expand Down Expand Up @@ -40,16 +38,8 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

// Add EF services to the services container
if (_platform.UseInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase("Scratch"));
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")]));
}
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlite("Data Source=MusicStore.db"));

// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void ConfigureServices(IServiceCollection services)

// Add EF services to the services container
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
options.UseSqlite("Data Source=MusicStore.db"));

// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
14 changes: 2 additions & 12 deletions src/MusicStore/samples/MusicStore/StartupOpenIdConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using MusicStore.Components;
using MusicStore.Models;
Expand Down Expand Up @@ -57,16 +55,8 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

// Add EF services to the services container
if (_platform.UseInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase("Scratch"));
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlite("Data Source=MusicStore.db"));

// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
35 changes: 17 additions & 18 deletions src/MusicStore/test/MusicStore.Test/CartSummaryComponentTest.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MusicStore.Controllers;
using MusicStore.Models;
using Xunit;

namespace MusicStore.Components
{
public class CartSummaryComponentTest
public class CartSummaryComponentTest : IClassFixture<SqliteInMemoryFixture>
{
private readonly IServiceProvider _serviceProvider;
private readonly SqliteInMemoryFixture _fixture;

public CartSummaryComponentTest()
public CartSummaryComponentTest(SqliteInMemoryFixture fixture)
{
var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

var services = new ServiceCollection();

services
.AddMemoryCache()
.AddLogging()
.AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase("Scratch").UseInternalServiceProvider(efServiceProvider));

_serviceProvider = services.BuildServiceProvider();
_fixture = fixture;
_fixture.CreateDatabase();
}

[Fact]
Expand All @@ -45,7 +34,7 @@ public async Task CartSummaryComponent_Returns_CartedItems()
viewContext.HttpContext.Session.SetString("Session", cartId);

// DbContext initialization
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
var dbContext = _fixture.Context;
PopulateData(dbContext, cartId, albumTitle: "AlbumA", itemCount: 10);

// CartSummaryComponent initialization
Expand All @@ -68,10 +57,20 @@ public async Task CartSummaryComponent_Returns_CartedItems()

private static void PopulateData(MusicStoreContext context, string cartId, string albumTitle, int itemCount)
{
var album = new Album()
var album = new Album
{
AlbumId = 1,
Title = albumTitle,
Artist = new Artist
{
ArtistId = 1,
Name = "Kung Fu Kenny"
},
Genre = new Genre
{
GenreId = 1,
Name = "Rap"
}
};

var cartItems = Enumerable.Range(1, itemCount).Select(n =>
Expand Down
Loading

0 comments on commit 6c8d2bd

Please sign in to comment.