parent
e450dd707e
commit
e3180baea5
|
|
@ -76,16 +76,19 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Existing_database_not_using_migrations_exception_passes_thru()
|
public async Task Existing_database_not_using_migrations_exception_passes_thru()
|
||||||
{
|
{
|
||||||
TestServer server = SetupTestServer<BloggingContext, DatabaseErrorButNoMigrationsMiddleware>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
var ex = await Assert.ThrowsAsync<DbUpdateException>(async () =>
|
{
|
||||||
await server.CreateClient().GetAsync("http://localhost/"));
|
TestServer server = SetupTestServer<BloggingContext, DatabaseErrorButNoMigrationsMiddleware>(database);
|
||||||
|
var ex = await Assert.ThrowsAsync<DbUpdateException>(async () =>
|
||||||
|
await server.CreateClient().GetAsync("http://localhost/"));
|
||||||
|
|
||||||
Assert.Equal("Invalid column name 'Name'.", ex.InnerException.Message);
|
Assert.Equal("Invalid column name 'Name'.", ex.InnerException.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DatabaseErrorButNoMigrationsMiddleware
|
class DatabaseErrorButNoMigrationsMiddleware
|
||||||
|
|
@ -97,7 +100,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
{
|
{
|
||||||
var db = context.RequestServices.GetService<BloggingContext>();
|
var db = context.RequestServices.GetService<BloggingContext>();
|
||||||
db.Database.EnsureCreated();
|
db.Database.EnsureCreated();
|
||||||
db.Database.ExecuteSqlCommand("ALTER TABLE dbo.Blog DROP COLUMN Name");
|
db.Database.ExecuteSqlCommand("ALTER TABLE dbo.Blogs DROP COLUMN Name");
|
||||||
|
|
||||||
db.Blogs.Add(new Blog());
|
db.Blogs.Add(new Blog());
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
|
@ -105,19 +108,22 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Error_page_displayed_no_migrations()
|
public async Task Error_page_displayed_no_migrations()
|
||||||
{
|
{
|
||||||
TestServer server = SetupTestServer<BloggingContext, NoMigrationsMiddleware>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
{
|
||||||
|
TestServer server = SetupTestServer<BloggingContext, NoMigrationsMiddleware>(database);
|
||||||
|
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", ">"), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", ">"), content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NoMigrationsMiddleware
|
class NoMigrationsMiddleware
|
||||||
|
|
@ -134,23 +140,26 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Error_page_displayed_pending_migrations()
|
public async Task Error_page_displayed_pending_migrations()
|
||||||
{
|
{
|
||||||
TestServer server = SetupTestServer<BloggingContextWithMigrations, PendingMigrationsMiddleware>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
{
|
||||||
|
TestServer server = SetupTestServer<BloggingContextWithMigrations, PendingMigrationsMiddleware>(database);
|
||||||
|
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||||
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingMigrationsTitle", typeof(BloggingContextWithMigrations).Name), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingMigrationsTitle", typeof(BloggingContextWithMigrations).Name), content);
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", ">"), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", ">"), content);
|
||||||
Assert.Contains("<li>111111111111111_MigrationOne</li>", content);
|
Assert.Contains("<li>111111111111111_MigrationOne</li>", content);
|
||||||
Assert.Contains("<li>222222222222222_MigrationTwo</li>", content);
|
Assert.Contains("<li>222222222222222_MigrationTwo</li>", content);
|
||||||
|
|
||||||
Assert.DoesNotContain(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
Assert.DoesNotContain(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PendingMigrationsMiddleware
|
class PendingMigrationsMiddleware
|
||||||
|
|
@ -167,20 +176,25 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Error_page_displayed_pending_model_changes()
|
public async Task Error_page_displayed_pending_model_changes()
|
||||||
{
|
{
|
||||||
TestServer server = SetupTestServer<BloggingContextWithPendingModelChanges, PendingModelChangesMiddleware>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
{
|
||||||
|
TestServer server = SetupTestServer<BloggingContextWithPendingModelChanges, PendingModelChangesMiddleware>(database);
|
||||||
|
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||||
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingChangesTitle", typeof(BloggingContextWithPendingModelChanges).Name), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingChangesTitle", typeof(BloggingContextWithPendingModelChanges).Name), content);
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommand").Replace(">", ">"), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandCLI").Replace(">", ">"), content);
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommand").Replace(">", ">"), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
||||||
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandCLI").Replace(">", ">"), content);
|
||||||
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", ">"), content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PendingModelChangesMiddleware
|
class PendingModelChangesMiddleware
|
||||||
|
|
@ -191,50 +205,53 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
public virtual Task Invoke(HttpContext context)
|
public virtual Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var db = context.RequestServices.GetService<BloggingContextWithPendingModelChanges>();
|
var db = context.RequestServices.GetService<BloggingContextWithPendingModelChanges>();
|
||||||
db.Database.Migrate();
|
db.Database.Migrate();
|
||||||
|
|
||||||
db.Blogs.Add(new Blog());
|
db.Blogs.Add(new Blog());
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
throw new Exception("SaveChanges should have thrown");
|
throw new Exception("SaveChanges should have thrown");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Error_page_then_apply_migrations()
|
public async Task Error_page_then_apply_migrations()
|
||||||
{
|
{
|
||||||
TestServer server = SetupTestServer<BloggingContextWithMigrations, ApplyMigrationsMiddleware>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
var client = server.CreateClient();
|
|
||||||
|
|
||||||
var expectedMigrationsEndpoint = "/ApplyDatabaseMigrations";
|
|
||||||
var expectedContextType = typeof(BloggingContextWithMigrations).AssemblyQualifiedName;
|
|
||||||
|
|
||||||
// Step One: Initial request with database failure
|
|
||||||
HttpResponseMessage response = await client.GetAsync("http://localhost/");
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
// Ensure the url we're going to test is what the page is using in it's JavaScript
|
|
||||||
var javaScriptEncoder = JavaScriptEncoder.Default;
|
|
||||||
Assert.Contains("req.open(\"POST\", \"" + JavaScriptEncode(expectedMigrationsEndpoint) + "\", true);", content);
|
|
||||||
Assert.Contains("var formBody = \"context=" + JavaScriptEncode(UrlEncode(expectedContextType)) + "\";", content);
|
|
||||||
|
|
||||||
// Step Two: Request to migrations endpoint
|
|
||||||
var formData = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
|
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>("context", expectedContextType)
|
TestServer server = SetupTestServer<BloggingContextWithMigrations, ApplyMigrationsMiddleware>(database);
|
||||||
});
|
var client = server.CreateClient();
|
||||||
|
|
||||||
response = await client.PostAsync("http://localhost" + expectedMigrationsEndpoint, formData);
|
var expectedMigrationsEndpoint = "/ApplyDatabaseMigrations";
|
||||||
content = await response.Content.ReadAsStringAsync();
|
var expectedContextType = typeof(BloggingContextWithMigrations).AssemblyQualifiedName;
|
||||||
Console.WriteLine(content);
|
|
||||||
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
|
||||||
|
|
||||||
// Step Three: Successful request after migrations applied
|
// Step One: Initial request with database failure
|
||||||
response = await client.GetAsync("http://localhost/");
|
HttpResponseMessage response = await client.GetAsync("http://localhost/");
|
||||||
content = await response.Content.ReadAsStringAsync();
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||||
Assert.Equal("Saved a Blog", content);
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
// Ensure the url we're going to test is what the page is using in it's JavaScript
|
||||||
|
var javaScriptEncoder = JavaScriptEncoder.Default;
|
||||||
|
Assert.Contains("req.open(\"POST\", \"" + JavaScriptEncode(expectedMigrationsEndpoint) + "\", true);", content);
|
||||||
|
Assert.Contains("var formBody = \"context=" + JavaScriptEncode(UrlEncode(expectedContextType)) + "\";", content);
|
||||||
|
|
||||||
|
// Step Two: Request to migrations endpoint
|
||||||
|
var formData = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>("context", expectedContextType)
|
||||||
|
});
|
||||||
|
|
||||||
|
response = await client.PostAsync("http://localhost" + expectedMigrationsEndpoint, formData);
|
||||||
|
content = await response.Content.ReadAsStringAsync();
|
||||||
|
Console.WriteLine(content);
|
||||||
|
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
||||||
|
|
||||||
|
// Step Three: Successful request after migrations applied
|
||||||
|
response = await client.GetAsync("http://localhost/");
|
||||||
|
content = await response.Content.ReadAsStringAsync();
|
||||||
|
Assert.Equal("Saved a Blog", content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApplyMigrationsMiddleware
|
class ApplyMigrationsMiddleware
|
||||||
|
|
@ -251,7 +268,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Customize_migrations_end_point()
|
public async Task Customize_migrations_end_point()
|
||||||
|
|
@ -272,12 +289,10 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
})
|
})
|
||||||
.ConfigureServices(services =>
|
.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
services.AddEntityFrameworkSqlServer();
|
services.AddDbContext<BloggingContextWithMigrations>(optionsBuilder =>
|
||||||
services.AddScoped<BloggingContextWithMigrations>();
|
{
|
||||||
|
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||||
var optionsBuilder = new DbContextOptionsBuilder();
|
});
|
||||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
|
||||||
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
|
|
||||||
});
|
});
|
||||||
var server = new TestServer(builder);
|
var server = new TestServer(builder);
|
||||||
|
|
||||||
|
|
@ -290,45 +305,27 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Pass_thru_when_context_not_in_services()
|
public async Task Pass_thru_when_context_not_in_services()
|
||||||
{
|
{
|
||||||
using (var database = SqlServerTestStore.CreateScratch())
|
var logProvider = new TestLoggerProvider();
|
||||||
{
|
|
||||||
var logProvider = new TestLoggerProvider();
|
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
var builder = new WebHostBuilder()
|
||||||
.Configure(app =>
|
.Configure(app =>
|
||||||
{
|
{
|
||||||
app.UseDatabaseErrorPage();
|
app.UseDatabaseErrorPage();
|
||||||
app.UseMiddleware<ContextNotRegisteredInServicesMiddleware>();
|
app.UseMiddleware<ContextNotRegisteredInServicesMiddleware>();
|
||||||
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
|
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
|
||||||
})
|
});
|
||||||
.ConfigureServices(
|
var server = new TestServer(builder);
|
||||||
services =>
|
|
||||||
{
|
|
||||||
services.AddEntityFrameworkSqlServer();
|
|
||||||
var optionsBuilder = new DbContextOptionsBuilder();
|
|
||||||
if (!PlatformHelper.IsMono)
|
|
||||||
{
|
|
||||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
optionsBuilder.UseInMemoryDatabase("Scratch");
|
|
||||||
}
|
|
||||||
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
|
|
||||||
});
|
|
||||||
var server = new TestServer(builder);
|
|
||||||
|
|
||||||
var ex = await Assert.ThrowsAsync<SqlException>(async () =>
|
var ex = await Assert.ThrowsAsync<SqlException>(async () =>
|
||||||
await server.CreateClient().GetAsync("http://localhost/"));
|
await server.CreateClient().GetAsync("http://localhost/"));
|
||||||
|
|
||||||
Assert.True(logProvider.Logger.Messages.Any(m =>
|
Assert.True(logProvider.Logger.Messages.Any(m =>
|
||||||
m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext)))));
|
m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext)))));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ContextNotRegisteredInServicesMiddleware
|
class ContextNotRegisteredInServicesMiddleware
|
||||||
|
|
@ -338,15 +335,28 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
|
|
||||||
public virtual Task Invoke(HttpContext context)
|
public virtual Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var options = context.RequestServices.GetService<DbContextOptions>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
var db = new BloggingContext(options);
|
{
|
||||||
db.Blogs.Add(new Blog());
|
var optionsBuilder = new DbContextOptionsBuilder()
|
||||||
db.SaveChanges();
|
.UseLoggerFactory(context.RequestServices.GetService<ILoggerFactory>());
|
||||||
throw new Exception("SaveChanges should have thrown");
|
if (!PlatformHelper.IsMono)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
optionsBuilder.UseInMemoryDatabase("Scratch");
|
||||||
|
}
|
||||||
|
|
||||||
|
var db = new BloggingContext(optionsBuilder.Options);
|
||||||
|
db.Blogs.Add(new Blog());
|
||||||
|
db.SaveChanges();
|
||||||
|
throw new Exception("SaveChanges should have thrown");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Pass_thru_when_exception_in_logic()
|
public async Task Pass_thru_when_exception_in_logic()
|
||||||
|
|
@ -355,12 +365,12 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
{
|
{
|
||||||
var logProvider = new TestLoggerProvider();
|
var logProvider = new TestLoggerProvider();
|
||||||
|
|
||||||
var server = SetupTestServer<BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(logProvider);
|
var server = SetupTestServer<BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(database, logProvider);
|
||||||
|
|
||||||
var ex = await Assert.ThrowsAsync<SqlException>(async () =>
|
var ex = await Assert.ThrowsAsync<SqlException>(async () =>
|
||||||
await server.CreateClient().GetAsync("http://localhost/"));
|
await server.CreateClient().GetAsync("http://localhost/"));
|
||||||
|
|
||||||
Assert.True(logProvider.Logger.Messages.Any(m =>
|
Assert.True(logProvider.Logger.Messages.ToList().Any(m =>
|
||||||
m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_Exception"))));
|
m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_Exception"))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -379,18 +389,21 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
[ConditionalFact]
|
||||||
[OSSkipCondition(OperatingSystems.Linux)]
|
[OSSkipCondition(OperatingSystems.Linux)]
|
||||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||||
public async Task Error_page_displayed_when_exception_wrapped()
|
public async Task Error_page_displayed_when_exception_wrapped()
|
||||||
{
|
{
|
||||||
TestServer server = SetupTestServer<BloggingContext, WrappedExceptionMiddleware>();
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
{
|
||||||
|
TestServer server = SetupTestServer<BloggingContext, WrappedExceptionMiddleware>(database);
|
||||||
|
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Contains("I wrapped your exception", content);
|
Assert.Contains("I wrapped your exception", content);
|
||||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
|
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class WrappedExceptionMiddleware
|
class WrappedExceptionMiddleware
|
||||||
|
|
@ -414,30 +427,25 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TestServer SetupTestServer<TContext, TMiddleware>(ILoggerProvider logProvider = null)
|
private static TestServer SetupTestServer<TContext, TMiddleware>(SqlServerTestStore database, ILoggerProvider logProvider = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
{
|
{
|
||||||
using (var database = SqlServerTestStore.CreateScratch())
|
var builder = new WebHostBuilder()
|
||||||
{
|
.Configure(app =>
|
||||||
var builder = new WebHostBuilder()
|
{
|
||||||
.Configure(app =>
|
app.UseDatabaseErrorPage();
|
||||||
|
|
||||||
|
app.UseMiddleware<TMiddleware>();
|
||||||
|
|
||||||
|
if (logProvider != null)
|
||||||
{
|
{
|
||||||
app.UseDatabaseErrorPage();
|
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
|
||||||
|
}
|
||||||
app.UseMiddleware<TMiddleware>();
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
if (logProvider != null)
|
{
|
||||||
{
|
services.AddDbContext<TContext>(optionsBuilder =>
|
||||||
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.ConfigureServices(services =>
|
|
||||||
{
|
{
|
||||||
services.AddEntityFrameworkSqlServer();
|
|
||||||
|
|
||||||
services.AddScoped<TContext>();
|
|
||||||
|
|
||||||
var optionsBuilder = new DbContextOptionsBuilder();
|
|
||||||
if (!PlatformHelper.IsMono)
|
if (!PlatformHelper.IsMono)
|
||||||
{
|
{
|
||||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||||
|
|
@ -446,10 +454,9 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
{
|
{
|
||||||
optionsBuilder.UseInMemoryDatabase("Scratch");
|
optionsBuilder.UseInMemoryDatabase("Scratch");
|
||||||
}
|
}
|
||||||
services.AddSingleton(optionsBuilder.Options);
|
|
||||||
});
|
});
|
||||||
return new TestServer(builder);
|
});
|
||||||
}
|
return new TestServer(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static UrlEncoder _urlEncoder = UrlEncoder.Default;
|
private static UrlEncoder _urlEncoder = UrlEncoder.Default;
|
||||||
|
|
|
||||||
|
|
@ -98,9 +98,10 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
})
|
})
|
||||||
.ConfigureServices(services =>
|
.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
services.AddEntityFrameworkSqlServer();
|
services.AddDbContext<BloggingContextWithMigrations>(options =>
|
||||||
services.AddScoped<BloggingContextWithMigrations>();
|
{
|
||||||
services.AddSingleton(optionsBuilder.Options);
|
options.UseSqlServer(database.ConnectionString);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
var server = new TestServer(builder);
|
var server = new TestServer(builder);
|
||||||
|
|
||||||
|
|
@ -205,16 +206,14 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
{
|
{
|
||||||
using (var database = SqlServerTestStore.CreateScratch())
|
using (var database = SqlServerTestStore.CreateScratch())
|
||||||
{
|
{
|
||||||
var optionsBuilder = new DbContextOptionsBuilder();
|
|
||||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
var builder = new WebHostBuilder()
|
||||||
.Configure(app => app.UseMigrationsEndPoint())
|
.Configure(app => app.UseMigrationsEndPoint())
|
||||||
.ConfigureServices(services =>
|
.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
services.AddEntityFrameworkSqlServer();
|
services.AddDbContext<BloggingContextWithSnapshotThatThrows>(optionsBuilder =>
|
||||||
services.AddScoped<BloggingContextWithSnapshotThatThrows>();
|
{
|
||||||
services.AddSingleton(optionsBuilder.Options);
|
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
var server = new TestServer(builder);
|
var server = new TestServer(builder);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
|
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.CreateTable("Blog",
|
migrationBuilder.CreateTable("Blogs",
|
||||||
c => new
|
c => new
|
||||||
{
|
{
|
||||||
BlogId = c.Column<int>().Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
|
BlogId = c.Column<int>().Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
|
||||||
|
|
@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
||||||
|
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.DropTable("Blog");
|
migrationBuilder.DropTable("Blogs");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue