Beispiel #1
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);
                }
            }
        }
Beispiel #2
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);
     }
 }
Beispiel #3
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());
            }
        }
Beispiel #4
0
        public OneToOneQuerySqlCeFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlCe()
                  .ServiceCollection()
                  .AddSingleton(TestSqlCeModelSource.GetFactory(OnModelCreating))
                  .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            var database = SqlCeTestStore.CreateScratch(createDatabase: true);

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlCe(database.Connection.ConnectionString);
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();

                AddTestData(context);
            }
        }
Beispiel #5
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);
     }
 }
Beispiel #6
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).HasTablesAsync() : ((TestDatabaseCreator)creator).HasTables());
            }
        }
Beispiel #7
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());
            }
        }
Beispiel #8
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("Blog", 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(19, columns.Length);

                Assert.Equal(
                    new[]
                {
                    "Blog.AndChew (image)",
                    "Blog.AndRow (rowversion)",
                    "Blog.Cheese (nvarchar)",
                    "Blog.CupOfChar (int)",
                    "Blog.ErMilan (int)",
                    "Blog.Fuse (smallint)",
                    "Blog.George (bit)",
                    "Blog.Key1 (nvarchar)",
                    "Blog.Key2 (varbinary)",
                    "Blog.NotFigTime (datetime)",
                    "Blog.NotToEat (smallint)",
                    "Blog.On (real)",
                    "Blog.OrNothing (float)",
                    "Blog.OrULong (int)",
                    "Blog.OrUShort (numeric)",
                    "Blog.OrUSkint (bigint)",
                    "Blog.TheGu (uniqueidentifier)",
                    "Blog.ToEat (tinyint)",
                    "Blog.WayRound (bigint)"
                },
                    columns);
            }
        }
Beispiel #9
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).HasTablesAsync() : ((TestDatabaseCreator)creator).HasTables());
            }
        }
            public override DbContext CreateContext(SqlCeTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder();

                optionsBuilder.UseSqlCe(testStore.Connection);

                var context = new GraphUpdatesContext(_serviceProvider, optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);
                return(context);
            }
        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());
                }
            }
        }
Beispiel #12
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);
                }
            }
        }
Beispiel #13
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).HasTablesAsync())).NativeError
                    : Assert.Throws <SqlCeException>(() => ((TestDatabaseCreator)creator).HasTables()).NativeError;

                Assert.Equal(
                    25046, // The database file cannot be found. Check the path to the database.
                    errorNumber);
            }
        }
        private static BloggingContext CreateContext(SqlCeTestStore testStore)
        {
            var serviceProvider =
                new ServiceCollection()
                .AddEntityFramework()
                .AddSqlCe()
                .ServiceCollection()
                .BuildServiceProvider();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlCe(testStore.Connection.ConnectionString);

            return(new BloggingContext(serviceProvider, optionsBuilder.Options));
        }
Beispiel #15
0
        private static async Task Delete_throws_when_database_doesnt_exist_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: false))
            {
                var creator = GetDatabaseCreator(testDatabase);

                if (async)
                {
                    await Assert.ThrowsAsync <SqlCeException>(() => creator.DeleteAsync());
                }
                else
                {
                    Assert.Throws <SqlCeException>(() => creator.Delete());
                }
            }
        }
Beispiel #16
0
        private static async Task Create_throws_if_database_already_exists_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                var creator = GetDatabaseCreator(testDatabase);

                var errorNumber =
                    async
                        ? (await Assert.ThrowsAsync <SqlCeException>(() => creator.CreateAsync())).NativeError
                        : Assert.Throws <SqlCeException>(() => creator.Create()).NativeError;

                Assert.Equal(
                    25114, // File already exists. Try using a different database name.
                    errorNumber);
            }
        }
        public MappingQuerySqlCeFixture()
        {
            _serviceProvider = new ServiceCollection()
                               .AddEntityFramework()
                               .AddSqlCe()
                               .ServiceCollection()
                               .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory())
                               .BuildServiceProvider();

            _testDatabase = SqlCeNorthwindContext.GetSharedStore();

            var optionsBuilder = new DbContextOptionsBuilder().UseModel(CreateModel());

            optionsBuilder.UseSqlCe(_testDatabase.Connection.ConnectionString);
            _options = optionsBuilder.Options;
        }
Beispiel #18
0
            public NullKeysSqlServerCeFixture()
            {
                _serviceProvider = new ServiceCollection()
                                   .AddEntityFramework()
                                   .AddSqlCe()
                                   .ServiceCollection()
                                   .AddSingleton(TestSqlCeModelSource.GetFactory(OnModelCreating))
                                   .BuildServiceProvider();

                var optionsBuilder = new DbContextOptionsBuilder();

                optionsBuilder.UseSqlCe(SqlCeTestStore.CreateConnectionString("StringsContext"));
                _options = optionsBuilder.Options;

                EnsureCreated();
            }
            public override SqlCeTestStore CreateTestStore()
            {
                return(SqlCeTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new DbContextOptionsBuilder();
                    optionsBuilder.UseSqlCe(SqlCeTestStore.CreateConnectionString(DatabaseName));

                    using (var context = new GraphUpdatesContext(_serviceProvider, optionsBuilder.Options))
                    {
                        context.Database.EnsureDeleted();
                        if (context.Database.EnsureCreated())
                        {
                            Seed(context);
                        }
                    }
                }));
            }
Beispiel #20
0
        private static IServiceProvider CreateContextServices(SqlCeTestStore testStore)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddEntityFramework()
            .AddSqlCe();

            serviceCollection.AddScoped <SqlCeDatabaseCreator, TestDatabaseCreator>();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlCe(testStore.Connection.ConnectionString);

            return(((IInfrastructure <IServiceProvider>) new BloggingContext(
                        serviceCollection.BuildServiceProvider(),
                        optionsBuilder.Options))
                   .Instance);
        }
Beispiel #21
0
        private static async Task CreateTables_creates_schema_in_existing_database_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                var serviceCollection = new ServiceCollection();
                serviceCollection
                .AddEntityFramework()
                .AddSqlCe();

                var serviceProvider = serviceCollection.BuildServiceProvider();

                var optionsBuilder = new DbContextOptionsBuilder();
                optionsBuilder.UseSqlCe(testDatabase.Connection.ConnectionString);

                using (var context = new BloggingContext(serviceProvider, optionsBuilder.Options))
                {
                    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 = testDatabase.Query <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
                    Assert.Equal(1, tables.Count());
                    Assert.Equal("Blog", tables.Single());

                    var columns = testDatabase.Query <string>("SELECT TABLE_NAME + '.' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS");
                    Assert.Equal(2, columns.Count());
                    Assert.True(columns.Any(c => c == "Blog.Id"));
                    Assert.True(columns.Any(c => c == "Blog.Name"));
                }
            }
        }
Beispiel #22
0
        private static async Task EnsuredCreated_is_noop_when_database_exists_and_has_schema_test(bool async)
        {
            using (var testDatabase = await SqlCeTestStore.CreateScratchAsync(createDatabase: false))
            {
                using (var context = new 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);
                }
            }
        }
        public BuiltInDataTypesSqlCeFixture()
        {
            _testStore = SqlCeTestStore.CreateScratch(createDatabase: true);

            _serviceProvider = new ServiceCollection()
                               .AddEntityFramework()
                               .AddSqlCe()
                               .ServiceCollection()
                               .AddSingleton(TestSqlCeModelSource.GetFactory(OnModelCreating))
                               .BuildServiceProvider();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlCe(_testStore.Connection);

            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();
            }
        }
Beispiel #24
0
        private static async Task Delete_will_delete_database_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                testDatabase.Connection.Close();

                var creator = GetDatabaseCreator(testDatabase);

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

                if (async)
                {
                    await creator.DeleteAsync();
                }
                else
                {
                    creator.Delete();
                }

                Assert.False(async ? await creator.ExistsAsync() : creator.Exists());
            }
        }
Beispiel #25
0
        private static async Task EnsureDeleted_will_delete_database_test(bool async, bool openConnection)
        {
            using (var testDatabase = await SqlCeTestStore.CreateScratchAsync(createDatabase: true))
            {
                if (!openConnection)
                {
                    testDatabase.Connection.Close();
                }

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

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

                    if (openConnection)
                    {
                        Assert.Throws <IOException>(() => context.Database.EnsureDeleted());
                    }
                    else
                    {
                        if (async)
                        {
                            Assert.True(await context.Database.EnsureDeletedAsync());
                        }
                        else
                        {
                            Assert.True(context.Database.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);
                    }
                }
            }
        }
Beispiel #26
0
 private static IRelationalDatabaseCreator GetDatabaseCreator(SqlCeTestStore testStore)
 => CreateContextServices(testStore).GetRequiredService <IRelationalDatabaseCreator>();
Beispiel #27
0
 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseSqlCe(SqlCeTestStore.CreateConnectionString(_databaseName));
 }
        public InheritanceSqlCeFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlCe()
                  .ServiceCollection()
                  .AddSingleton(TestSqlCeModelSource.GetFactory(OnModelCreating))
                  .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            var testStore = SqlCeTestStore.CreateScratch(createDatabase: true);

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder
            .EnableSensitiveDataLogging()
            .UseSqlCe(testStore.Connection);

            _options = optionsBuilder.Options;

            // TODO: Do this via migrations

            testStore.ExecuteNonQuery(@"
                CREATE TABLE Country (
                    Id int NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL
                );");

            testStore.ExecuteNonQuery(@"
                CREATE TABLE Animal (
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int NOT NULL,
                    IsFlightless bit NOT NULL,
                    EagleId nvarchar(100),
                    [Group] int,
                    FoundOn tinyint,
                    Discriminator nvarchar(255) NOT NULL
                );");

            testStore.ExecuteNonQuery(@"
                ALTER TABLE [Animal]
                    ADD CONSTRAINT[EagleId_FK]
                    FOREIGN KEY ([EagleId])
                    REFERENCES[Animal]([Species]) 
                    ON DELETE NO ACTION ON UPDATE NO ACTION;");

            testStore.ExecuteNonQuery(@"
                ALTER TABLE [Animal]
                    ADD CONSTRAINT[CountryId_FK]
                    FOREIGN KEY ([CountryId])
                    REFERENCES[Country]([Id]) 
                    ON DELETE NO ACTION ON UPDATE NO ACTION;");

            testStore.ExecuteNonQuery(@"
                CREATE TABLE Plant(
                    Genus int NOT NULL,
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int NULL,
                    HasThorns bit
                );");

            testStore.ExecuteNonQuery(@"
                ALTER TABLE [Plant]
                    ADD CONSTRAINT[PlantCountryId_FK]
                    FOREIGN KEY ([CountryId])
                    REFERENCES[Country]([Id]) 
                    ON DELETE NO ACTION ON UPDATE NO ACTION;");

            using (var context = CreateContext())
            {
                SeedData(context);
            }
        }
Beispiel #29
0
 public BloggingContext(SqlCeTestStore testStore)
     : base(CreateServiceProvider())
 {
     _testStore = testStore;
 }