public override SqlCeTestStore CreateTestStore()
            {
                var testStore = SqlCeTestStore.CreateScratch(true);

                _options = new DbContextOptionsBuilder()
                           .UseSqlCe(testStore.Connection, b => b.ApplyConfiguration())
                           .UseInternalServiceProvider(_serviceProvider)
                           .Options;

                using (var context = new GraphUpdatesContext(_options))
                {
                    context.Database.EnsureClean();
                    Seed(context);
                }

                //var testStore = SqlCeTestStore.GetOrCreateShared(DatabaseName, () =>
                //{
                //    var options = new DbContextOptionsBuilder()
                //        .UseSqlCe(SqlCeTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                //        .UseInternalServiceProvider(_serviceProvider)
                //        .Options;

                //    using (var context = new GraphUpdatesContext(options))
                //    {
                //        context.Database.EnsureDeleted();
                //        Seed(context);
                //    }
                //});


                return(testStore);
            }
Exemple #2
0
        private static async Task EnsuredDeleted_noop_when_database_doesnt_exist_test(bool async)
        {
            using (var testDatabase = await SqlCeTestStore.CreateScratchAsync(createDatabase: false))
            {
                using (var context = new 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);
                }
            }
        }
Exemple #3
0
 private static async Task EnsureCreated_can_create_physical_database_and_schema_test(bool async)
 {
     using (var testDatabase = await SqlCeTestStore.CreateScratchAsync(createDatabase: false))
     {
         await RunDatabaseCreationTest(testDatabase, async);
     }
 }
Exemple #4
0
        private static async Task Create_creates_physical_database_but_not_tables_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: false))
            {
                var creator = GetDatabaseCreator(testDatabase);

                Assert.False(creator.Exists());

                if (async)
                {
                    await creator.CreateAsync();
                }
                else
                {
                    creator.Create();
                }

                Assert.True(creator.Exists());

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

                Assert.Equal(0, (testDatabase.Query <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES")).Count());

                Assert.True(testDatabase.Exists());
            }
        }
Exemple #5
0
            public async Task Can_use_one_context_nested_inside_another_of_the_same_type()
            {
                using (SqlCeTestStore.GetNorthwindStore())
                {
                    var serviceProvider = new ServiceCollection()
                                          .AddEntityFrameworkSqlCe()
                                          .BuildServiceProvider();

                    using (var context1 = new NorthwindContext(serviceProvider))
                    {
                        var customers1 = await context1.Customers.ToListAsync();

                        Assert.Equal(91, customers1.Count);
                        Assert.Equal(91, context1.ChangeTracker.Entries().Count());

                        using (var context2 = new NorthwindContext(serviceProvider))
                        {
                            Assert.Equal(0, context2.ChangeTracker.Entries().Count());

                            var customers2 = await context2.Customers.ToListAsync();

                            Assert.Equal(91, customers2.Count);
                            Assert.Equal(91, context2.ChangeTracker.Entries().Count());

                            Assert.Equal(customers1[0].CustomerID, customers2[0].CustomerID);
                            Assert.NotSame(customers1[0], customers2[0]);
                        }
                    }
                }
            }
Exemple #6
0
 private static async Task EnsureCreated_can_create_schema_in_existing_database_test(bool async)
 {
     using (var testDatabase = await SqlCeTestStore.CreateScratchAsync())
     {
         await RunDatabaseCreationTest(testDatabase, async);
     }
 }
Exemple #7
0
 private static IServiceProvider CreateContextServices(SqlCeTestStore testStore)
 => ((IInfrastructure <IServiceProvider>) new BloggingContext(
         new DbContextOptionsBuilder()
         .UseSqlCe(testStore.ConnectionString)
         .UseInternalServiceProvider(new ServiceCollection()
                                     .AddEntityFrameworkSqlCe()
                                     .AddScoped <IRelationalDatabaseCreator, TestDatabaseCreator>().BuildServiceProvider()).Options))
 .Instance;
Exemple #8
0
        private static async Task Exists_returns_true_when_database_exists_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                var creator = GetDatabaseCreator(testDatabase);

                Assert.True(async ? await creator.ExistsAsync() : creator.Exists());
            }
        }
Exemple #9
0
        private static async Task HasTables_returns_false_when_database_exists_but_has_no_tables_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                var creator = GetDatabaseCreator(testDatabase);

                Assert.False(async ? await((TestDatabaseCreator)creator).HasTablesAsyncBase() : ((TestDatabaseCreator)creator).HasTablesBase());
            }
        }
Exemple #10
0
 public async Task Can_query_with_implicit_services_and_OnConfiguring()
 {
     using (SqlCeTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext())
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemple #11
0
 public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring()
 {
     using (SqlCeTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(SqlCeTestStore.NorthwindConnectionString))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemple #12
0
 public NotificationEntitiesSqlCeFixture()
 {
     _options = new DbContextOptionsBuilder()
                .UseSqlCe(SqlCeTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                .UseInternalServiceProvider(new ServiceCollection()
                                            .AddEntityFrameworkSqlCe()
                                            .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                            .BuildServiceProvider())
                .Options;
 }
Exemple #13
0
        private static async Task HasTables_returns_true_when_database_exists_and_has_any_tables_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                testDatabase.ExecuteNonQuery("CREATE TABLE SomeTable (Id uniqueidentifier)");

                var creator = GetDatabaseCreator(testDatabase);

                Assert.True(async ? await((TestDatabaseCreator)creator).HasTablesAsyncBase() : ((TestDatabaseCreator)creator).HasTablesBase());
            }
        }
 public override SqlCeTestStore CreateTestStore()
 {
     return(SqlCeTestStore.GetOrCreateShared(DatabaseName, () =>
     {
         using (var context = new FindContext(_options))
         {
             context.Database.EnsureClean();
             Seed(context);
         }
     }));
 }
Exemple #15
0
 public async Task Can_pass_context_options_to_constructor_and_use_in_builder()
 {
     using (SqlCeTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(new DbContextOptionsBuilder()
                                                   .UseSqlCe(SqlCeTestStore.NorthwindConnectionString, b => b.ApplyConfiguration()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
 protected override void CreateAndSeedDatabase(string databaseName, Func <MonsterContext> createContext, Action <MonsterContext> seed)
 {
     _testStore = SqlCeTestStore.GetOrCreateShared(databaseName, () =>
     {
         using (var context = createContext())
         {
             context.Database.EnsureCreated();
             seed(context);
         }
     });
 }
            public override DbContext CreateContext(SqlCeTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseSqlCe(testStore.Connection, b => b.ApplyConfiguration())
                                     .UseInternalServiceProvider(_serviceProvider);

                var context = new FieldMappingContext(optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);

                return(context);
            }
Exemple #18
0
 public async Task Can_query_with_implicit_services_and_explicit_config()
 {
     using (SqlCeTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder()
                    .UseSqlCe(SqlCeTestStore.NorthwindConnectionString, b => b.ApplyConfiguration()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
        public void Empty_Migration_Creates_Database()
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: false))
            {
                using (var context = CreateContext(testDatabase))
                {
                    context.Database.Migrate();

                    Assert.True(context.GetService <IRelationalDatabaseCreator>().Exists());
                }
            }
        }
            public override DbContext CreateContext(SqlCeTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseSqlCe(testStore.Connection)
                                     .UseInternalServiceProvider(_serviceProvider);

                var context = new AdvancedPatternsMasterContext(optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);

                return(context);
            }
Exemple #21
0
        private static async Task RunDatabaseCreationTest(SqlCeTestStore testStore, bool async)
        {
            using (var context = new BloggingContext(testStore))
            {
                var creator = context.GetService <IRelationalDatabaseCreator>();

                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 = testStore.Query <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ");
                Assert.Equal(1, tables.Count());
                Assert.Equal("Blogs", tables.Single());

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

                Assert.Equal(
                    new[]
                {
                    "Blogs.AndChew (varbinary)",
                    "Blogs.AndRow (rowversion)",
                    "Blogs.Cheese (nvarchar)",
                    "Blogs.CupOfChar (int)",
                    "Blogs.ErMilan (int)",
                    "Blogs.Fuse (smallint)",
                    "Blogs.George (bit)",
                    "Blogs.Key1 (nvarchar)",
                    "Blogs.Key2 (varbinary)",
                    "Blogs.NotFigTime (datetime)",
                    "Blogs.On (real)",
                    "Blogs.OrNothing (float)",
                    "Blogs.TheGu (uniqueidentifier)",
                    "Blogs.ToEat (tinyint)",
                    "Blogs.WayRound (bigint)"
                },
                    columns);
            }
        }
Exemple #22
0
            public async Task Can_register_context_and_configuration_with_DI_container_and_have_both_injected()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddTransient <MyController>()
                                      .AddTransient <NorthwindContext>()
                                      .AddSingleton(new DbContextOptionsBuilder()
                                                    .UseSqlCe(SqlCeTestStore.NorthwindConnectionString, b => b.ApplyConfiguration()).Options).BuildServiceProvider();

                using (SqlCeTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
            public FindSqlCeFixture()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkSqlCe()
                                      .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                      .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                      .BuildServiceProvider();

                _options = new DbContextOptionsBuilder()
                           .UseSqlCe(SqlCeTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                           .UseInternalServiceProvider(serviceProvider)
                           .EnableSensitiveDataLogging()
                           .Options;
            }
Exemple #24
0
 public async Task Can_query_with_explicit_services_and_OnConfiguring()
 {
     using (SqlCeTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder().UseInternalServiceProvider(
                        new ServiceCollection()
                        .AddEntityFrameworkSqlCe()
                        .BuildServiceProvider()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemple #25
0
            public async Task Can_register_context_with_DI_container_and_have_it_injected()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkSqlCe()
                                      .AddTransient <NorthwindContext>()
                                      .AddTransient <MyController>()
                                      .AddSingleton(p => new DbContextOptionsBuilder().UseInternalServiceProvider(p).Options)
                                      .BuildServiceProvider();

                using (SqlCeTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
Exemple #26
0
        private static async Task Exists_returns_true_when_database_exists_test(bool async)
        {
            using (var testDatabase = await SqlCeTestStore.CreateScratchAsync(createDatabase: true))
            {
                using (var context = new BloggingContext(testDatabase))
                {
                    var creator = context.GetService <IRelationalDatabaseCreator>();

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

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }
Exemple #27
0
 public void Throws_on_attempt_to_use_context_with_no_store()
 {
     using (SqlCeTestStore.GetNorthwindStore())
     {
         Assert.Equal(
             CoreStrings.NoProviderConfigured,
             Assert.Throws <InvalidOperationException>(() =>
         {
             using (var context = new NorthwindContext())
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }
            public override SqlCeTestStore CreateTestStore()
            {
                return(SqlCeTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new DbContextOptionsBuilder()
                                         .UseSqlCe(SqlCeTestStore.CreateConnectionString(DatabaseName))
                                         .UseInternalServiceProvider(_serviceProvider);

                    using (var context = new AdvancedPatternsMasterContext(optionsBuilder.Options))
                    {
                        context.Database.EnsureClean();
                        Seed(context);
                    }
                }));
            }
Exemple #29
0
        private static async Task HasTables_throws_when_database_doesnt_exist_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: false))
            {
                var creator = GetDatabaseCreator(testDatabase);

                var errorNumber = async
                    ? (await Assert.ThrowsAsync <SqlCeException>(() => ((TestDatabaseCreator)creator).HasTablesAsyncBase())).NativeError
                    : Assert.Throws <SqlCeException>(() => ((TestDatabaseCreator)creator).HasTablesBase()).NativeError;

                Assert.Equal(
                    25046, // The database file cannot be found. Check the path to the database.
                    errorNumber);
            }
        }
Exemple #30
0
            public NullKeysSqlCeFixture()
            {
                var name             = "StringsContext";
                var connectionString = SqlCeTestStore.CreateConnectionString(name);

                _options = new DbContextOptionsBuilder()
                           .UseSqlCe(connectionString, b => b.ApplyConfiguration())
                           .UseInternalServiceProvider(new ServiceCollection()
                                                       .AddEntityFrameworkSqlCe()
                                                       .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                                       .BuildServiceProvider())
                           .Options;

                _testStore = SqlCeTestStore.GetOrCreateShared(name, EnsureCreated);
            }