private static async Task Returns_true_when_database_exists_test(bool async, bool ambientTransaction, bool useCanConnect, bool file)
        {
            using var testDatabase = file
                ? SqlServerTestStore.CreateInitialized("ExistingBloggingFile", useFileName: true)
                : SqlServerTestStore.GetOrCreateInitialized("ExistingBlogging");
            using var context = new BloggingContext(testDatabase);
            var creator = GetDatabaseCreator(context);

            await context.Database.CreateExecutionStrategy().ExecuteAsync(
                async() =>
            {
                using (CreateTransactionScope(ambientTransaction))
                {
                    if (useCanConnect)
                    {
                        Assert.True(async ? await creator.CanConnectAsync() : creator.CanConnect());
                    }
                    else
                    {
                        Assert.True(async ? await creator.ExistsAsync() : creator.Exists());
                    }
                }
            });

            Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
        }
Esempio n. 2
0
 public async Task Returns_false_when_database_exists_but_has_no_tables(bool async, bool ambientTransaction)
 {
     using (var testDatabase = SqlServerTestStore.GetOrCreateInitialized("Empty"))
     {
         var creator = GetDatabaseCreator(testDatabase);
         using (CreateTransactionScope(ambientTransaction))
         {
             Assert.False(async ? await creator.HasTablesAsyncBase() : creator.HasTablesBase());
         }
     }
 }
Esempio n. 3
0
    public async Task Throws_if_database_already_exists(bool async)
    {
        using var testDatabase = SqlServerTestStore.GetOrCreateInitialized("ExistingBlogging");
        var creator = GetDatabaseCreator(testDatabase);

        var ex = async
            ? await Assert.ThrowsAsync <SqlException>(() => creator.CreateAsync())
            : Assert.Throws <SqlException>(() => creator.Create());

        Assert.Equal(
            1801, // Database with given name already exists
            ex.Number);
    }
Esempio n. 4
0
        public async Task Creates_schema_in_existing_database_test(bool async, bool ambientTransaction)
        {
            using (var testDatabase = SqlServerTestStore.GetOrCreateInitialized("ExistingBlogging" + (async ? "Async" : "")))
            {
                using (var context = new BloggingContext(testDatabase))
                {
                    var creator = GetDatabaseCreator(context);

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

                    if (testDatabase.ConnectionState != ConnectionState.Open)
                    {
                        await testDatabase.OpenConnectionAsync();
                    }

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

                    var columns = (await testDatabase.QueryAsync <string>(
                                       "SELECT TABLE_NAME + '.' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Blogs'")).ToList();
                    Assert.Equal(14, columns.Count);
                    Assert.Contains(columns, c => c == "Blogs.Key1");
                    Assert.Contains(columns, c => c == "Blogs.Key2");
                    Assert.Contains(columns, c => c == "Blogs.Cheese");
                    Assert.Contains(columns, c => c == "Blogs.ErMilan");
                    Assert.Contains(columns, c => c == "Blogs.George");
                    Assert.Contains(columns, c => c == "Blogs.TheGu");
                    Assert.Contains(columns, c => c == "Blogs.NotFigTime");
                    Assert.Contains(columns, c => c == "Blogs.ToEat");
                    Assert.Contains(columns, c => c == "Blogs.OrNothing");
                    Assert.Contains(columns, c => c == "Blogs.Fuse");
                    Assert.Contains(columns, c => c == "Blogs.WayRound");
                    Assert.Contains(columns, c => c == "Blogs.On");
                    Assert.Contains(columns, c => c == "Blogs.AndChew");
                    Assert.Contains(columns, c => c == "Blogs.AndRow");
                }
            }
        }
        public void It_creates_unique_query_cache_key()
        {
            using (var testStore = SqlServerTestStore.GetOrCreateInitialized(nameof(CompiledQueryCacheKeyGeneratorTest)))
            {
                object     key1, key2;
                Expression query;
                using (var context1 = new QueryKeyCacheContext(CreateOptions(testStore, rowNumberPaging: true)))
                {
                    var generator = context1.GetService <ICompiledQueryCacheKeyGenerator>();
                    query = context1.Set <Poco1>().Skip(4).Take(10).Expression;
                    key1  = generator.GenerateCacheKey(query, false);
                }

                using (var context2 = new QueryKeyCacheContext(CreateOptions(testStore, rowNumberPaging: false)))
                {
                    var generator = context2.GetService <ICompiledQueryCacheKeyGenerator>();
                    key2 = generator.GenerateCacheKey(query, false);
                }

                Assert.NotEqual(key1, key2);
            }
        }