Esempio n. 1
0
        private static async Task EnsuredDeleted_noop_when_database_doesnt_exist_test(bool async, bool file)
        {
            using (var testDatabase = SqlServerTestStore.CreateScratch(createDatabase: false, useFileName: file))
            {
                using (var context = new SqlServerDatabaseCreatorTest.BloggingContext(testDatabase))
                {
                    var creator = context.GetService <IRelationalDatabaseCreator>();

                    Assert.False(async ? await creator.ExistsAsync() : creator.Exists());

                    if (async)
                    {
                        Assert.False(await creator.EnsureDeletedAsync());
                    }
                    else
                    {
                        Assert.False(creator.EnsureDeleted());
                    }

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);

                    Assert.False(async ? await creator.ExistsAsync() : creator.Exists());

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }
Esempio n. 2
0
        private static async Task CreateTables_creates_schema_in_existing_database_test(bool async)
        {
            using (var testDatabase = SqlServerTestStore.CreateScratch(createDatabase: true))
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkSqlServer()
                                      .BuildServiceProvider();

                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseInternalServiceProvider(serviceProvider)
                                     .UseSqlServer(testDatabase.ConnectionString, b => b.ApplyConfiguration());

                using (var context = new SqlServerDatabaseCreatorTest.BloggingContext(testDatabase))
                {
                    var creator = (RelationalDatabaseCreator)context.GetService <IDatabaseCreator>();

                    if (async)
                    {
                        await creator.CreateTablesAsync();
                    }
                    else
                    {
                        creator.CreateTables();
                    }

                    if (testDatabase.Connection.State != ConnectionState.Open)
                    {
                        await testDatabase.Connection.OpenAsync();
                    }

                    var tables = await testDatabase.QueryAsync <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");

                    Assert.Equal(1, tables.Count());
                    Assert.Equal("Blogs", tables.Single());

                    var columns = await testDatabase.QueryAsync <string>("SELECT TABLE_NAME + '.' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Blogs'");

                    Assert.Equal(15, columns.Count());
                    Assert.True(columns.Any(c => c == "Blogs.Key1"));
                    Assert.True(columns.Any(c => c == "Blogs.Key2"));
                    Assert.True(columns.Any(c => c == "Blogs.Cheese"));
                    Assert.True(columns.Any(c => c == "Blogs.ErMilan"));
                    Assert.True(columns.Any(c => c == "Blogs.George"));
                    Assert.True(columns.Any(c => c == "Blogs.TheGu"));
                    Assert.True(columns.Any(c => c == "Blogs.NotFigTime"));
                    Assert.True(columns.Any(c => c == "Blogs.ToEat"));
                    Assert.True(columns.Any(c => c == "Blogs.CupOfChar"));
                    Assert.True(columns.Any(c => c == "Blogs.OrNothing"));
                    Assert.True(columns.Any(c => c == "Blogs.Fuse"));
                    Assert.True(columns.Any(c => c == "Blogs.WayRound"));
                    Assert.True(columns.Any(c => c == "Blogs.On"));
                    Assert.True(columns.Any(c => c == "Blogs.AndChew"));
                    Assert.True(columns.Any(c => c == "Blogs.AndRow"));
                }
            }
        }
Esempio n. 3
0
        private static async Task RunDatabaseCreationTest(SqlServerTestStore testStore, bool async)
        {
            using (var context = new SqlServerDatabaseCreatorTest.BloggingContext(testStore))
            {
                var creator = context.GetService <IRelationalDatabaseCreator>();

                Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);

                if (async)
                {
                    Assert.True(await creator.EnsureCreatedAsync());
                }
                else
                {
                    Assert.True(creator.EnsureCreated());
                }

                Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);

                if (testStore.Connection.State != ConnectionState.Open)
                {
                    await testStore.Connection.OpenAsync();
                }

                var tables = await testStore.QueryAsync <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");

                Assert.Equal(1, tables.Count());
                Assert.Equal("Blogs", tables.Single());

                var columns = (await testStore.QueryAsync <string>(
                                   "SELECT TABLE_NAME + '.' + COLUMN_NAME + ' (' + DATA_TYPE + ')' FROM INFORMATION_SCHEMA.COLUMNS  WHERE TABLE_NAME = 'Blogs' ORDER BY TABLE_NAME, COLUMN_NAME")).ToArray();
                Assert.Equal(15, columns.Length);

                Assert.Equal(
                    new[]
                {
                    "Blogs.AndChew (varbinary)",
                    "Blogs.AndRow (timestamp)",
                    "Blogs.Cheese (nvarchar)",
                    "Blogs.CupOfChar (int)",
                    "Blogs.ErMilan (int)",
                    "Blogs.Fuse (smallint)",
                    "Blogs.George (bit)",
                    "Blogs.Key1 (nvarchar)",
                    "Blogs.Key2 (varbinary)",
                    "Blogs.NotFigTime (datetime2)",
                    "Blogs.On (real)",
                    "Blogs.OrNothing (float)",
                    "Blogs.TheGu (uniqueidentifier)",
                    "Blogs.ToEat (tinyint)",
                    "Blogs.WayRound (bigint)"
                },
                    columns);
            }
        }
Esempio n. 4
0
        private static async Task Exists_returns_true_when_database_exists_test(bool async, bool file)
        {
            using (var testDatabase = SqlServerTestStore.CreateScratch(createDatabase: true, useFileName: file))
            {
                using (var context = new SqlServerDatabaseCreatorTest.BloggingContext(testDatabase))
                {
                    var creator = context.GetService <IRelationalDatabaseCreator>();

                    Assert.True(async ? await creator.ExistsAsync() : creator.Exists());

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }
Esempio n. 5
0
        private static async Task EnsuredCreated_is_noop_when_database_exists_and_has_schema_test(bool async, bool file)
        {
            using (var testDatabase = SqlServerTestStore.CreateScratch(createDatabase: false, useFileName: file))
            {
                using (var context = new SqlServerDatabaseCreatorTest.BloggingContext(testDatabase))
                {
                    context.Database.EnsureCreated();

                    if (async)
                    {
                        Assert.False(await context.Database.EnsureCreatedAsync());
                    }
                    else
                    {
                        Assert.False(context.Database.EnsureCreated());
                    }

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }