Skip to content

Commit 6cf65d7

Browse files
committed
Fix MusicStore
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
1 parent eb48c0f commit 6cf65d7

17 files changed

+278
-288
lines changed

src/MusicStore/samples/MusicStore/Controllers/StoreController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public async Task<IActionResult> Browse(string genre)
3636
{
3737
// Retrieve Genre genre and its Associated associated Albums albums from database
3838
var genreModel = await DbContext.Genres
39-
.Include(g => g.Albums)
4039
.Where(g => g.Name == genre)
4140
.FirstOrDefaultAsync();
4241

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

47+
await DbContext.Entry(genreModel).Collection(g => g.Albums).LoadAsync();
48+
4849
return View(genreModel);
4950
}
5051

@@ -83,4 +84,4 @@ public async Task<IActionResult> Details(
8384
return View(album);
8485
}
8586
}
86-
}
87+
}

src/MusicStore/samples/MusicStore/ForTesting/Mocks/StartupOpenIdConnectTesting.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Builder;
6-
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
76
using Microsoft.AspNetCore.Hosting;
87
using Microsoft.AspNetCore.Identity;
9-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
108
using Microsoft.AspNetCore.Localization;
119
using Microsoft.EntityFrameworkCore;
1210
using Microsoft.Extensions.Caching.Memory;
1311
using Microsoft.Extensions.Configuration;
1412
using Microsoft.Extensions.DependencyInjection;
15-
using Microsoft.Extensions.Logging;
1613
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
1714
using MusicStore.Components;
1815
using MusicStore.Mocks.Common;
@@ -47,16 +44,9 @@ public void ConfigureServices(IServiceCollection services)
4744
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
4845

4946
// Add EF services to the services container
50-
if (_platform.UseInMemoryStore)
51-
{
52-
services.AddDbContext<MusicStoreContext>(options =>
53-
options.UseInMemoryDatabase("Scratch"));
54-
}
55-
else
56-
{
57-
services.AddDbContext<MusicStoreContext>(options =>
58-
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
59-
}
47+
// Add EF services to the services container
48+
services.AddDbContext<MusicStoreContext>(options =>
49+
options.UseSqlite("Data Source=MusicStore.db"));
6050

6151
// Add Identity services to the services container
6252
services.AddIdentity<ApplicationUser, IdentityRole>()

src/MusicStore/samples/MusicStore/ForTesting/Mocks/StartupSocialTesting.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
using System;
21
using System.Globalization;
32
using Microsoft.AspNetCore.Authentication.OAuth;
43
using Microsoft.AspNetCore.Authentication.Twitter;
54
using Microsoft.AspNetCore.Authorization;
65
using Microsoft.AspNetCore.Builder;
7-
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
86
using Microsoft.AspNetCore.Hosting;
9-
using Microsoft.AspNetCore.Http;
107
using Microsoft.AspNetCore.Identity;
11-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
128
using Microsoft.AspNetCore.Localization;
139
using Microsoft.EntityFrameworkCore;
1410
using Microsoft.Extensions.Caching.Memory;
1511
using Microsoft.Extensions.Configuration;
1612
using Microsoft.Extensions.DependencyInjection;
17-
using Microsoft.Extensions.Logging;
1813
using MusicStore.Components;
1914
using MusicStore.Mocks.Common;
2015
using MusicStore.Mocks.Facebook;
@@ -50,16 +45,8 @@ public void ConfigureServices(IServiceCollection services)
5045
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
5146

5247
// Add EF services to the services container
53-
if (_platform.UseInMemoryStore)
54-
{
55-
services.AddDbContext<MusicStoreContext>(options =>
56-
options.UseInMemoryDatabase("Scratch"));
57-
}
58-
else
59-
{
60-
services.AddDbContext<MusicStoreContext>(options =>
61-
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
62-
}
48+
services.AddDbContext<MusicStoreContext>(options =>
49+
options.UseSqlite("Data Source=MusicStore.db"));
6350

6451
// Add Identity services to the services container
6552
services.AddIdentity<ApplicationUser, IdentityRole>()

src/MusicStore/samples/MusicStore/Models/ShoppingCart.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ public Task<int> GetCount()
115115
.SumAsync();
116116
}
117117

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

124-
return _dbContext
124+
return (await _dbContext
125125
.CartItems
126126
.Where(c => c.CartId == _shoppingCartId)
127127
.Select(c => c.Album.Price * c.Count)
128-
.SumAsync();
128+
.ToListAsync())
129+
.Sum();
129130
}
130131

131132
public async Task CreateOrder(Order order)

src/MusicStore/samples/MusicStore/MusicStore.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
3232
<Reference Include="Microsoft.AspNetCore.Session" />
3333
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
34-
<Reference Include="Microsoft.EntityFrameworkCore.InMemory" />
35-
<Reference Include="Microsoft.EntityFrameworkCore.SqlServer" />
34+
<Reference Include="Microsoft.EntityFrameworkCore.Sqlite" />
3635
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
3736
</ItemGroup>
3837

src/MusicStore/samples/MusicStore/Startup.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Hosting;
55
using Microsoft.AspNetCore.Identity;
6-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
76
using Microsoft.AspNetCore.Localization;
87
using Microsoft.EntityFrameworkCore;
98
using Microsoft.Extensions.Configuration;
109
using Microsoft.Extensions.DependencyInjection;
11-
using Microsoft.Extensions.Logging;
1210
using MusicStore.Components;
1311
using MusicStore.Models;
1412

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

4240
// Add EF services to the services container
43-
if (_platform.UseInMemoryStore)
44-
{
45-
services.AddDbContext<MusicStoreContext>(options =>
46-
options.UseInMemoryDatabase("Scratch"));
47-
}
48-
else
49-
{
50-
services.AddDbContext<MusicStoreContext>(options =>
51-
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")]));
52-
}
41+
services.AddDbContext<MusicStoreContext>(options =>
42+
options.UseSqlite("Data Source=MusicStore.db"));
5343

5444
// Add Identity services to the services container
5545
services.AddIdentity<ApplicationUser, IdentityRole>()

src/MusicStore/samples/MusicStore/StartupNtlmAuthentication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void ConfigureServices(IServiceCollection services)
5757

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

6262
// Add Identity services to the services container
6363
services.AddIdentity<ApplicationUser, IdentityRole>()

src/MusicStore/samples/MusicStore/StartupOpenIdConnect.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.AspNetCore.Identity;
5-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
65
using Microsoft.AspNetCore.Localization;
76
using Microsoft.EntityFrameworkCore;
87
using Microsoft.Extensions.Configuration;
98
using Microsoft.Extensions.DependencyInjection;
10-
using Microsoft.Extensions.Logging;
119
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
1210
using MusicStore.Components;
1311
using MusicStore.Models;
@@ -57,16 +55,8 @@ public void ConfigureServices(IServiceCollection services)
5755
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
5856

5957
// Add EF services to the services container
60-
if (_platform.UseInMemoryStore)
61-
{
62-
services.AddDbContext<MusicStoreContext>(options =>
63-
options.UseInMemoryDatabase("Scratch"));
64-
}
65-
else
66-
{
67-
services.AddDbContext<MusicStoreContext>(options =>
68-
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
69-
}
58+
services.AddDbContext<MusicStoreContext>(options =>
59+
options.UseSqlite("Data Source=MusicStore.db"));
7060

7161
// Add Identity services to the services container
7262
services.AddIdentity<ApplicationUser, IdentityRole>()

src/MusicStore/test/MusicStore.Test/CartSummaryComponentTest.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
1-
using System;
21
using System.Linq;
32
using System.Threading.Tasks;
43
using Microsoft.AspNetCore.Http;
54
using Microsoft.AspNetCore.Mvc.Rendering;
65
using Microsoft.AspNetCore.Mvc.ViewComponents;
7-
using Microsoft.EntityFrameworkCore;
8-
using Microsoft.Extensions.DependencyInjection;
96
using MusicStore.Controllers;
107
using MusicStore.Models;
118
using Xunit;
129

1310
namespace MusicStore.Components
1411
{
15-
public class CartSummaryComponentTest
12+
public class CartSummaryComponentTest : IClassFixture<SqliteInMemoryFixture>
1613
{
17-
private readonly IServiceProvider _serviceProvider;
14+
private readonly SqliteInMemoryFixture _fixture;
1815

19-
public CartSummaryComponentTest()
16+
public CartSummaryComponentTest(SqliteInMemoryFixture fixture)
2017
{
21-
var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();
22-
23-
var services = new ServiceCollection();
24-
25-
services
26-
.AddMemoryCache()
27-
.AddLogging()
28-
.AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase("Scratch").UseInternalServiceProvider(efServiceProvider));
29-
30-
_serviceProvider = services.BuildServiceProvider();
18+
_fixture = fixture;
19+
_fixture.CreateDatabase();
3120
}
3221

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

4736
// DbContext initialization
48-
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
37+
var dbContext = _fixture.Context;
4938
PopulateData(dbContext, cartId, albumTitle: "AlbumA", itemCount: 10);
5039

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

6958
private static void PopulateData(MusicStoreContext context, string cartId, string albumTitle, int itemCount)
7059
{
71-
var album = new Album()
60+
var album = new Album
7261
{
7362
AlbumId = 1,
7463
Title = albumTitle,
64+
Artist = new Artist
65+
{
66+
ArtistId = 1,
67+
Name = "Kung Fu Kenny"
68+
},
69+
Genre = new Genre
70+
{
71+
GenreId = 1,
72+
Name = "Rap"
73+
}
7574
};
7675

7776
var cartItems = Enumerable.Range(1, itemCount).Select(n =>

0 commit comments

Comments
 (0)