Esempio n. 1
0
        public async Task Exception_while_applying_migrations()
        {
            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            var options = new DbContextOptions().UseSqlServer(database.Connection.ConnectionString);

            TestServer server = TestServer.Create(app =>
            {
                app.UseServices(services =>
                {
                    services.AddEntityFramework().AddSqlServer();
                    services.AddScoped <BloggingContextWithSnapshotThatThrows>();
                    services.AddInstance <DbContextOptions>(options);
                });

                app.UseMigrationsEndPoint();
            });

            var formData = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("context", typeof(BloggingContextWithSnapshotThatThrows).AssemblyQualifiedName)
            });

            var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
                                                                          await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData));

            Assert.Equal(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_Exception", typeof(BloggingContextWithSnapshotThatThrows)), ex.Message);
            Assert.Equal("Welcome to the invalid migration!", ex.InnerException.Message);
        }
Esempio n. 2
0
        private static async Task <TestServer> SetupTestServer <TContext, TMiddleware>(ILoggerProvider logProvider = null)
            where TContext : DbContext
        {
            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            return(TestServer.Create(app =>
            {
                app.UseServices(services =>
                {
                    services.AddEntityFramework()
                    .AddSqlServer();

                    services.AddScoped <TContext>();
                    services.AddInstance <DbContextOptions>(
                        new DbContextOptions()
                        .UseSqlServer(database.Connection.ConnectionString));
                });

                app.UseDatabaseErrorPage();

                app.UseMiddleware <TMiddleware>();

                if (logProvider != null)
                {
                    app.ApplicationServices.GetService <ILoggerFactory>().AddProvider(logProvider);
                }
            }));
        }
Esempio n. 3
0
        public async Task Pass_thru_when_context_not_in_services()
        {
            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            var logProvider = new TestLoggerProvider();

            var server = TestServer.Create(app =>
            {
                app.UseServices(services =>
                {
                    services.AddEntityFramework()
                    .AddSqlServer();

                    services.AddInstance <DbContextOptions>(
                        new DbContextOptions()
                        .UseSqlServer(database.Connection.ConnectionString));
                });

                app.UseDatabaseErrorPage();

                app.UseMiddleware <ContextNotRegisteredInServicesMiddleware>();

                app.ApplicationServices.GetService <ILoggerFactory>().AddProvider(logProvider);
            });

            var ex = await Assert.ThrowsAsync <SqlException>(async() =>
                                                             await server.CreateClient().GetAsync("http://localhost/"));

            Assert.True(logProvider.Logger.Messages.Any(m =>
                                                        m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext)))));
        }
Esempio n. 4
0
        public async Task Customize_migrations_end_point()
        {
            var migrationsEndpoint = "/MyCustomEndPoints/ApplyMyMigrationsHere";

            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            var server = TestServer.Create(app =>
            {
                app.UseServices(services =>
                {
                    services.AddEntityFramework().AddSqlServer();
                    services.AddScoped <BloggingContextWithMigrations>();
                    services.AddInstance <DbContextOptions>(new DbContextOptions().UseSqlServer(database.Connection.ConnectionString));
                });

                var options = DatabaseErrorPageOptions.ShowAll;
                options.MigrationsEndPointPath = new PathString(migrationsEndpoint);
                app.UseDatabaseErrorPage(options);

                app.UseMiddleware <PendingMigrationsMiddleware>();
            });


            HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

            var content = await response.Content.ReadAsStringAsync();

            Assert.Contains("req.open(\"POST\", \"" + migrationsEndpoint + "\", true);", content);
        }
Esempio n. 5
0
        private async Task Migration_request(bool useCustomPath)
        {
            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            var options = new DbContextOptions().UseSqlServer(database.Connection.ConnectionString);
            var path    = useCustomPath ? new PathString("/EndPoints/ApplyMyMigrations") : MigrationsEndPointOptions.DefaultPath;

            TestServer server = TestServer.Create(app =>
            {
                app.UseServices(services =>
                {
                    services.AddEntityFramework().AddSqlServer();
                    services.AddScoped <BloggingContextWithMigrations>();
                    services.AddInstance <DbContextOptions>(options);
                });

                if (useCustomPath)
                {
                    app.UseMigrationsEndPoint(new MigrationsEndPointOptions {
                        Path = path
                    });
                }
                else
                {
                    app.UseMigrationsEndPoint();
                }
            });

            using (var db = BloggingContextWithMigrations.CreateWithoutExternalServiceProvider(options))
            {
                Assert.False(db.Database.AsRelational().Exists());

                var formData = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("context", typeof(BloggingContextWithMigrations).AssemblyQualifiedName)
                });

                HttpResponseMessage response = await server.CreateClient()
                                               .PostAsync("http://localhost" + path, formData);

                Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

                Assert.True(db.Database.AsRelational().Exists());
                var services          = (MigrationsDataStoreServices)db.Configuration.DataStoreServices;
                var appliedMigrations = services.Migrator.GetDatabaseMigrations();
                Assert.Equal(2, appliedMigrations.Count);
                Assert.Equal("111111111111111_MigrationOne", appliedMigrations.ElementAt(0).GetMigrationId());
                Assert.Equal("222222222222222_MigrationTwo", appliedMigrations.ElementAt(1).GetMigrationId());
            }
        }
Esempio n. 6
0
        public async Task Pass_thru_when_exception_in_logic()
        {
            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            var logProvider = new TestLoggerProvider();

            var server = await SetupTestServer <BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(logProvider);

            var ex = await Assert.ThrowsAsync <SqlException>(async() =>
                                                             await server.CreateClient().GetAsync("http://localhost/"));

            Assert.True(logProvider.Logger.Messages.Any(m =>
                                                        m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_Exception"))));
        }