public async Task Can_use_one_context_nested_inside_another_of_the_same_type()
            {
                using (JetTestStore.GetNorthwindStore())
                {
                    var serviceProvider = new ServiceCollection()
                                          .AddEntityFrameworkJet()
                                          .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]);
                        }
                    }
                }
            }
 private static async Task EnsureCreated_can_create_physical_database_and_schema_test(bool async)
 {
     using (var testDatabase = await JetTestStore.CreateScratchAsync(createDatabase: false))
     {
         await RunDatabaseCreationTest(testDatabase, async);
     }
 }
        private static async Task RunDatabaseCreationTest(JetTestStore 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.State != ConnectionState.Open)
                {
                    await testStore.OpenAsync();
                }

                var tables = testStore.Query <string>("SELECT NAME FROM (SHOW TABLES)");
                Assert.Equal(1, tables.Count());
                Assert.Equal("Blogs", tables.Single());

                var columns = (testStore.Query <string>(
                                   "SELECT Table + '.' + Name + ' (' + TypeName + ')' FROM (SHOW TABLECOLUMNS) ORDER BY Table, Name")).ToArray();
                Assert.Equal(14, columns.Length);
            }
        }
        private static async Task EnsuredDeleted_noop_when_database_doesnt_exist_test(bool async)
        {
            using (var testDatabase = await JetTestStore.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);
                }
            }
        }
 private static async Task EnsureCreated_can_create_schema_in_existing_database_test(bool async)
 {
     using (var testDatabase = await JetTestStore.CreateScratchAsync())
     {
         await RunDatabaseCreationTest(testDatabase, async);
     }
 }
Exemplo n.º 6
0
        private static async Task Create_creates_physical_database_but_not_tables_test(bool async)
        {
            using (var testDatabase = JetTestStore.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.State != ConnectionState.Open)
                {
                    await testDatabase.OpenAsync();
                }

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

                Assert.True(testDatabase.Exists());
            }
        }
Exemplo n.º 7
0
        private static async Task Exists_returns_true_when_database_exists_test(bool async)
        {
            using (var testDatabase = JetTestStore.CreateScratch(createDatabase: true))
            {
                var creator = GetDatabaseCreator(testDatabase);

                Assert.True(async ? await creator.ExistsAsync() : creator.Exists());
            }
        }
Exemplo n.º 8
0
        private static async Task HasTables_returns_false_when_database_exists_but_has_no_tables_test(bool async)
        {
            using (var testDatabase = JetTestStore.CreateScratch(createDatabase: true))
            {
                var creator = GetDatabaseCreator(testDatabase);

                Assert.False(async ? await((TestDatabaseCreator)creator).HasTablesAsyncBase() : ((TestDatabaseCreator)creator).HasTablesBase());
            }
        }
 public async Task Can_query_with_implicit_services_and_OnConfiguring()
 {
     using (JetTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext())
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
 public NotificationEntitiesJetFixture()
 {
     _options = new DbContextOptionsBuilder()
                .UseJet(JetTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                .UseInternalServiceProvider(new ServiceCollection()
                                            .AddEntityFrameworkJet()
                                            .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                            .BuildServiceProvider())
                .Options;
 }
 public async Task Can_pass_connection_string_to_constructor_and_use_in_OnConfiguring()
 {
     using (JetTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(JetTestStore.NorthwindConnectionString))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
 public async Task Can_pass_context_options_to_constructor_and_use_in_builder()
 {
     using (JetTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(new DbContextOptionsBuilder()
                                                   .UseJet(JetTestStore.NorthwindConnectionString, b => b.ApplyConfiguration()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemplo n.º 13
0
 public override JetTestStore CreateTestStore()
 {
     return(JetTestStore.GetOrCreateShared(DatabaseName, () =>
     {
         using (var context = new LoadContext(_options))
         {
             context.Database.EnsureCreated();
             Seed(context);
         }
     }));
 }
Exemplo n.º 14
0
        private static async Task HasTables_returns_true_when_database_exists_and_has_any_tables_test(bool async)
        {
            using (var testDatabase = JetTestStore.CreateScratch(createDatabase: true))
            {
                testDatabase.ExecuteNonQuery("CREATE TABLE SomeTable (Id uniqueidentifier)");

                var creator = GetDatabaseCreator(testDatabase);

                Assert.True(async ? await((TestDatabaseCreator)creator).HasTablesAsyncBase() : ((TestDatabaseCreator)creator).HasTablesBase());
            }
        }
Exemplo n.º 15
0
            public override DbContext CreateContext(JetTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseJet(testStore.Connection)
                                     .UseInternalServiceProvider(_serviceProvider);

                var context = new AdvancedPatternsMasterContext(optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);

                return(context);
            }
Exemplo n.º 16
0
        public void Empty_Migration_Creates_Database()
        {
            using (var testDatabase = JetTestStore.CreateScratch(createDatabase: false))
            {
                using (var context = CreateContext(testDatabase))
                {
                    context.Database.Migrate();

                    Assert.True(context.GetService <IRelationalDatabaseCreator>().Exists());
                }
            }
        }
Exemplo n.º 17
0
        private static async Task Create_throws_if_database_already_exists_test(bool async)
        {
            using (var testDatabase = JetTestStore.CreateScratch(createDatabase: true))
            {
                var creator = GetDatabaseCreator(testDatabase);

                string errorDescription =
                    async
                        ? (await Assert.ThrowsAsync <Exception>(() => creator.CreateAsync())).Message
                        : Assert.Throws <Exception>(() => creator.Create()).Message;
            }
        }
 public async Task Can_query_with_implicit_services_and_explicit_config()
 {
     using (JetTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder()
                    .UseJet(JetTestStore.NorthwindConnectionString, b => b.ApplyConfiguration()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemplo n.º 19
0
            public override DbContext CreateContext(JetTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseJet(testStore.Connection, b => b.ApplyConfiguration())
                                     .UseInternalServiceProvider(_serviceProvider);

                var context = new FieldMappingContext(optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);

                return(context);
            }
            public NullKeysJetFixture()
            {
                var name             = "StringsContext";
                var connectionString = JetTestStore.CreateConnectionString(name);

                _options = new DbContextOptionsBuilder()
                           .UseJet(connectionString, b => b.ApplyConfiguration())
                           .UseInternalServiceProvider(new ServiceCollection()
                                                       .AddEntityFrameworkJet()
                                                       .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                                       .BuildServiceProvider())
                           .Options;
            }
            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()
                                                    .UseJet(JetTestStore.NorthwindConnectionString, b => b.ApplyConfiguration()).Options).BuildServiceProvider();

                using (JetTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
            public async Task Can_register_context_with_DI_container_and_have_it_injected()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkJet()
                                      .AddTransient <NorthwindContext>()
                                      .AddTransient <MyController>()
                                      .AddSingleton(p => new DbContextOptionsBuilder().UseInternalServiceProvider(p).Options)
                                      .BuildServiceProvider();

                using (JetTestStore.GetNorthwindStore())
                {
                    await serviceProvider.GetRequiredService <MyController>().TestAsync();
                }
            }
        private static async Task Exists_returns_true_when_database_exists_test(bool async)
        {
            using (var testDatabase = await JetTestStore.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);
                }
            }
        }
 public async Task Can_query_with_explicit_services_and_OnConfiguring()
 {
     using (JetTestStore.GetNorthwindStore())
     {
         using (var context = new NorthwindContext(
                    new DbContextOptionsBuilder().UseInternalServiceProvider(
                        new ServiceCollection()
                        .AddEntityFrameworkJet()
                        .BuildServiceProvider()).Options))
         {
             Assert.Equal(91, await context.Customers.CountAsync());
         }
     }
 }
Exemplo n.º 25
0
            public LoadJetFixture()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkJet()
                                      .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                      .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                      .BuildServiceProvider(validateScopes: true);

                _options = new DbContextOptionsBuilder()
                           .UseJet(JetTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                           .UseInternalServiceProvider(serviceProvider)
                           .EnableSensitiveDataLogging()
                           .Options;
            }
Exemplo n.º 26
0
            public override JetTestStore CreateTestStore()
            {
                return(JetTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new DbContextOptionsBuilder()
                                         .UseJet(JetTestStore.CreateConnectionString(DatabaseName))
                                         .UseInternalServiceProvider(_serviceProvider);

                    using (var context = new AdvancedPatternsMasterContext(optionsBuilder.Options))
                    {
                        context.Database.EnsureClean();
                        Seed(context);
                    }
                }));
            }
Exemplo n.º 27
0
            public override JetTestStore CreateTestStore()
            {
                return(JetTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new DbContextOptionsBuilder()
                                         .UseJet(JetTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                                         .UseInternalServiceProvider(_serviceProvider);

                    using (var context = new FieldMappingContext(optionsBuilder.Options))
                    {
                        context.Database.EnsureClean();
                        Seed(context);
                    }
                }));
            }
Exemplo n.º 28
0
        private static async Task HasTables_throws_when_database_doesnt_exist_test(bool async)
        {
            using (var testDatabase = JetTestStore.CreateScratch(createDatabase: false))
            {
                var creator = GetDatabaseCreator(testDatabase);

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

                Assert.Equal(
                    25046, // The database file cannot be found. Check the path to the database.
                    errorNumber);
            }
        }
Exemplo n.º 29
0
        private static BloggingContext CreateContext(JetTestStore testStore)
        {
            var serviceProvider =
                new ServiceCollection()
                .AddEntityFrameworkJet()
                .BuildServiceProvider();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder
            .UseJet(testStore.ConnectionString)
            .UseInternalServiceProvider(serviceProvider);

            return(new BloggingContext(serviceProvider, optionsBuilder.Options));
        }
 public void Throws_on_attempt_to_use_context_with_no_store()
 {
     using (JetTestStore.GetNorthwindStore())
     {
         Assert.Equal(
             CoreStrings.NoProviderConfigured,
             Assert.Throws <InvalidOperationException>(() =>
         {
             using (var context = new NorthwindContext())
             {
                 Assert.Equal(91, context.Customers.Count());
             }
         }).Message);
     }
 }