public async Task Exists_returns_true_when_database_exists(bool async, bool useCanConnect)
        {
            using (var testStore = SqliteTestStore.GetOrCreateInitialized("Empty"))
            {
                var context = CreateContext(testStore.ConnectionString);

                if (useCanConnect)
                {
                    Assert.True(async ? await context.Database.CanConnectAsync() : context.Database.CanConnect());
                }
                else
                {
                    var creator = context.GetService <IRelationalDatabaseCreator>();
                    Assert.True(async ? await creator.ExistsAsync() : creator.Exists());
                }
            }
        }
示例#2
0
        public void It_allows_foreign_key_to_unique_index()
        {
            using (var testStore = SqliteTestStore.GetOrCreateInitialized("ForeignKeyIndexTest"))
            {
                var options = testStore
                              .AddProviderOptions(new DbContextOptionsBuilder())
                              .UseInternalServiceProvider(new ServiceCollection().AddEntityFrameworkSqlite().BuildServiceProvider())
                              .Options;

                testStore.ExecuteNonQuery(
                    @"
CREATE TABLE User (
    Id INTEGER PRIMARY KEY,
    AltId INTEGER NOT NULL UNIQUE
);
CREATE TABLE Comment (
    Id INTEGER PRIMARY KEY,
    UserAltId INTEGER NOT NULL,
    Comment TEXT,
    FOREIGN KEY (UserAltId) REFERENCES User (AltId)
);");

                long id;
                using (var context = new BloggingContext(options))
                {
                    var entry = context.User.Add(
                        new User
                    {
                        AltId = 1356524
                    });
                    context.Comments.Add(
                        new Comment
                    {
                        User = entry.Entity
                    });
                    context.SaveChanges();
                    id = entry.Entity.Id;
                }

                using (var context = new BloggingContext(options))
                {
                    var comment = context.Comments.Include(u => u.User).Single();
                    Assert.Equal(id, comment.User.Id);
                }
            }
        }
示例#3
0
        public void Can_select_appropriate_provider_when_multiple_registered_with_default_service_provider()
        {
            using (SqliteTestStore.GetNorthwindStore()) // CHANGED for Xamarin testing: WAS: SqlServerTestStore
            {
                using (var context = new MultipleProvidersContext())
                {
                    context.UseSqlServer = true;

                    Assert.True(context.Customers.Any());
                }

                using (var context = new MultipleProvidersContext())
                {
                    context.UseSqlServer = false;

                    Assert.False(context.Customers.Any());
                }
            }
        }
示例#4
0
        public override TestStore CreateTestStore(Type testStoreType)
        {
            if (testStoreType == typeof(InMemoryTestStore))
            {
                return(new InMemoryTestStore());
            }

            if (testStoreType == typeof(SqlServerTestStore))
            {
                return(SqlServerTestStore.Create(StoreName));
            }

            if (testStoreType == typeof(SqliteTestStore))
            {
                return(SqliteTestStore.CreateScratch());
            }

            throw new NotImplementedException();
        }
示例#5
0
        public async Task Create_sets_journal_mode_to_wal(bool async)
        {
            using var testStore = SqliteTestStore.GetOrCreate("Create");
            using var context   = CreateContext(testStore.ConnectionString);
            var creator = context.GetService <IRelationalDatabaseCreator>();

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

            testStore.OpenConnection();
            var journalMode = testStore.ExecuteScalar <string>("PRAGMA journal_mode;");

            Assert.Equal("wal", journalMode);
        }
示例#6
0
        public void Can_register_multiple_context_types()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddDbContext <MultipleContext1>()
                                  .AddDbContext <MultipleContext2>()
                                  .BuildServiceProvider();

            using (SqliteTestStore.GetNorthwindStore()) // CHANGED for Xamarin testing: WAS: SqlServerTestStore
            {
                using (var context = serviceProvider.GetRequiredService <MultipleContext1>())
                {
                    Assert.True(context.Customers.Any());
                }

                using (var context = serviceProvider.GetRequiredService <MultipleContext2>())
                {
                    Assert.False(context.Customers.Any());
                }
            }
        }
        public override TestStore CreateTestStore(Type testStoreType)
        {
            if (testStoreType == typeof(InMemoryTestStore))
            {
                return(new InMemoryTestStore());
            }

            /* CHANGED: No SQL Server on Xamarin.iOS
             * if (testStoreType == typeof(SqlServerTestStore))
             * {
             *  return SqlServerTestStore.Create(StoreName);
             * } */

            if (testStoreType == typeof(SqliteTestStore))
            {
                return(SqliteTestStore.CreateScratch());
            }

            throw new NotImplementedException();
        }
示例#8
0
        public BuiltInDataTypesSqliteFixture()
        {
            _testStore = SqliteTestStore.CreateScratch();

            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkSqlite()
                                  .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                  .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                  .BuildServiceProvider();

            _options = new DbContextOptionsBuilder()
                       .UseSqlite(_testStore.Connection)
                       .EnableSensitiveDataLogging()
                       .UseInternalServiceProvider(serviceProvider)
                       .Options;

            using (var context = new DbContext(_options))
            {
                context.Database.EnsureClean();
            }
        }
示例#9
0
        public void It_enforces_foreign_key(bool suppress)
        {
            var serviceProvider = new ServiceCollection().AddEntityFrameworkSqlite().BuildServiceProvider();

            using (var testStore = (SqliteTestStore)SqliteTestStore.GetOrCreate("ForeignKeyTest")
                                   .Initialize(
                       serviceProvider,
                       t => new MyContext(
                           t.AddProviderOptions(new DbContextOptionsBuilder())
                           .UseInternalServiceProvider(serviceProvider).Options),
                       null))
            {
                testStore.CloseConnection();

                var builder = testStore.AddProviderOptions(new DbContextOptionsBuilder().UseInternalServiceProvider(serviceProvider));
                new SqliteDbContextOptionsBuilder(builder).SuppressForeignKeyEnforcement(suppress);

                using (var context = new MyContext(builder.Options))
                {
                    context.Add(
                        new Child
                    {
                        ParentId = 4
                    });
                    if (suppress)
                    {
                        context.SaveChanges();
                    }
                    else
                    {
                        var ex = Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                        // ReSharper disable once PossibleNullReferenceException
                        Assert.Contains("FOREIGN KEY constraint failed", ex.InnerException.Message,
                                        StringComparison.OrdinalIgnoreCase);
                    }
                }
            }
        }
示例#10
0
 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 => optionsBuilder
 .UseSqlite(SqliteTestStore.CreateConnectionString(DatabaseName))
 .UseInternalServiceProvider(_serviceProvider);
示例#11
0
 public override DbContext CreateContext(SqliteTestStore testStore)
 => new LoadContext(_options);
示例#12
0
 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 => optionsBuilder
 .UseSqlite(SqliteTestStore.CreateConnectionString("Northwind"))         // CHANGED for Xamarin testing: WAS: .UseSqlServer(SqlServerTestStore.NorthwindConnectionString, b => b.ApplyConfiguration())
 .UseInternalServiceProvider(_serviceProvider);
示例#13
0
 public override SqliteTestStore CreateTestStore()
 => SqliteTestStore.GetOrCreateShared(DatabaseName, EnsureCreated);
 public SqliteForeignKeyTest()
 {
     _testStore = SqliteTestStore.CreateScratch();
 }
示例#15
0
 protected override TwoDatabasesWithDataContext CreateBackingContext(string databaseName)
 => new TwoDatabasesWithDataContext(Fixture.CreateOptions(SqliteTestStore.Create(databaseName)));