parent
e450dd707e
commit
e3180baea5
|
|
@ -76,17 +76,20 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Existing_database_not_using_migrations_exception_passes_thru()
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContext, DatabaseErrorButNoMigrationsMiddleware>();
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
class DatabaseErrorButNoMigrationsMiddleware
|
||||
{
|
||||
|
|
@ -97,7 +100,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
{
|
||||
var db = context.RequestServices.GetService<BloggingContext>();
|
||||
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.SaveChanges();
|
||||
|
|
@ -105,12 +108,14 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Error_page_displayed_no_migrations()
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContext, NoMigrationsMiddleware>();
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContext, NoMigrationsMiddleware>(database);
|
||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
|
@ -119,6 +124,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", ">"), content);
|
||||
}
|
||||
}
|
||||
|
||||
class NoMigrationsMiddleware
|
||||
{
|
||||
|
|
@ -134,12 +140,14 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Error_page_displayed_pending_migrations()
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContextWithMigrations, PendingMigrationsMiddleware>();
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContextWithMigrations, PendingMigrationsMiddleware>(database);
|
||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
|
@ -152,6 +160,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
|
||||
Assert.DoesNotContain(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", ">"), content);
|
||||
}
|
||||
}
|
||||
|
||||
class PendingMigrationsMiddleware
|
||||
{
|
||||
|
|
@ -167,20 +176,25 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Error_page_displayed_pending_model_changes()
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContextWithPendingModelChanges, PendingModelChangesMiddleware>();
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContextWithPendingModelChanges, PendingModelChangesMiddleware>(database);
|
||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingChangesTitle", typeof(BloggingContextWithPendingModelChanges).Name), content);
|
||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommand").Replace(">", ">"), content);
|
||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommand").Replace(">", ">"), content);
|
||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandCLI").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
|
||||
|
|
@ -199,12 +213,14 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Error_page_then_apply_migrations()
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContextWithMigrations, ApplyMigrationsMiddleware>();
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContextWithMigrations, ApplyMigrationsMiddleware>(database);
|
||||
var client = server.CreateClient();
|
||||
|
||||
var expectedMigrationsEndpoint = "/ApplyDatabaseMigrations";
|
||||
|
|
@ -236,6 +252,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
content = await response.Content.ReadAsStringAsync();
|
||||
Assert.Equal("Saved a Blog", content);
|
||||
}
|
||||
}
|
||||
|
||||
class ApplyMigrationsMiddleware
|
||||
{
|
||||
|
|
@ -251,7 +268,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Customize_migrations_end_point()
|
||||
|
|
@ -272,12 +289,10 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
})
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddEntityFrameworkSqlServer();
|
||||
services.AddScoped<BloggingContextWithMigrations>();
|
||||
|
||||
var optionsBuilder = new DbContextOptionsBuilder();
|
||||
services.AddDbContext<BloggingContextWithMigrations>(optionsBuilder =>
|
||||
{
|
||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
|
||||
});
|
||||
});
|
||||
var server = new TestServer(builder);
|
||||
|
||||
|
|
@ -290,12 +305,10 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Pass_thru_when_context_not_in_services()
|
||||
{
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
var logProvider = new TestLoggerProvider();
|
||||
|
||||
|
|
@ -305,21 +318,6 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
app.UseDatabaseErrorPage();
|
||||
app.UseMiddleware<ContextNotRegisteredInServicesMiddleware>();
|
||||
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
|
||||
})
|
||||
.ConfigureServices(
|
||||
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);
|
||||
|
||||
|
|
@ -329,7 +327,6 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
Assert.True(logProvider.Logger.Messages.Any(m =>
|
||||
m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext)))));
|
||||
}
|
||||
}
|
||||
|
||||
class ContextNotRegisteredInServicesMiddleware
|
||||
{
|
||||
|
|
@ -338,15 +335,28 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
|
||||
public virtual Task Invoke(HttpContext context)
|
||||
{
|
||||
var options = context.RequestServices.GetService<DbContextOptions>();
|
||||
var db = new BloggingContext(options);
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder()
|
||||
.UseLoggerFactory(context.RequestServices.GetService<ILoggerFactory>());
|
||||
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.MacOSX)]
|
||||
public async Task Pass_thru_when_exception_in_logic()
|
||||
|
|
@ -355,12 +365,12 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
{
|
||||
var logProvider = new TestLoggerProvider();
|
||||
|
||||
var server = SetupTestServer<BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(logProvider);
|
||||
var server = SetupTestServer<BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(database, logProvider);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<SqlException>(async () =>
|
||||
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"))));
|
||||
}
|
||||
}
|
||||
|
|
@ -379,12 +389,14 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(Skip = "aspnet/Diagnostics#350")]
|
||||
[ConditionalFact]
|
||||
[OSSkipCondition(OperatingSystems.Linux)]
|
||||
[OSSkipCondition(OperatingSystems.MacOSX)]
|
||||
public async Task Error_page_displayed_when_exception_wrapped()
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContext, WrappedExceptionMiddleware>();
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
TestServer server = SetupTestServer<BloggingContext, WrappedExceptionMiddleware>(database);
|
||||
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
|
||||
|
||||
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
|
||||
|
|
@ -392,6 +404,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
Assert.Contains("I wrapped your exception", content);
|
||||
Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
|
||||
}
|
||||
}
|
||||
|
||||
class WrappedExceptionMiddleware
|
||||
{
|
||||
|
|
@ -414,10 +427,8 @@ 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
|
||||
{
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
var builder = new WebHostBuilder()
|
||||
.Configure(app =>
|
||||
|
|
@ -433,11 +444,8 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
})
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddEntityFrameworkSqlServer();
|
||||
|
||||
services.AddScoped<TContext>();
|
||||
|
||||
var optionsBuilder = new DbContextOptionsBuilder();
|
||||
services.AddDbContext<TContext>(optionsBuilder =>
|
||||
{
|
||||
if (!PlatformHelper.IsMono)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||
|
|
@ -446,11 +454,10 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
{
|
||||
optionsBuilder.UseInMemoryDatabase("Scratch");
|
||||
}
|
||||
services.AddSingleton(optionsBuilder.Options);
|
||||
});
|
||||
});
|
||||
return new TestServer(builder);
|
||||
}
|
||||
}
|
||||
|
||||
private static UrlEncoder _urlEncoder = UrlEncoder.Default;
|
||||
private static string UrlEncode(string content)
|
||||
|
|
|
|||
|
|
@ -98,9 +98,10 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
})
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddEntityFrameworkSqlServer();
|
||||
services.AddScoped<BloggingContextWithMigrations>();
|
||||
services.AddSingleton(optionsBuilder.Options);
|
||||
services.AddDbContext<BloggingContextWithMigrations>(options =>
|
||||
{
|
||||
options.UseSqlServer(database.ConnectionString);
|
||||
});
|
||||
});
|
||||
var server = new TestServer(builder);
|
||||
|
||||
|
|
@ -205,16 +206,14 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
{
|
||||
using (var database = SqlServerTestStore.CreateScratch())
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder();
|
||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.Configure(app => app.UseMigrationsEndPoint())
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddEntityFrameworkSqlServer();
|
||||
services.AddScoped<BloggingContextWithSnapshotThatThrows>();
|
||||
services.AddSingleton(optionsBuilder.Options);
|
||||
services.AddDbContext<BloggingContextWithSnapshotThatThrows>(optionsBuilder =>
|
||||
{
|
||||
optionsBuilder.UseSqlServer(database.ConnectionString);
|
||||
});
|
||||
});
|
||||
var server = new TestServer(builder);
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Tests
|
|||
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable("Blog",
|
||||
migrationBuilder.CreateTable("Blogs",
|
||||
c => new
|
||||
{
|
||||
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)
|
||||
{
|
||||
migrationBuilder.DropTable("Blog");
|
||||
migrationBuilder.DropTable("Blogs");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue