public void WhenUsingSharedDatabaseAndMigrator_ShouldOnlyMigrateTheFirstTime()
        {
            using (var db = new TempDBLocalDb())
            {
                var sql = ENTITY_PERSISTENCE_CONTEXT_SQL;
                EntityPersistenceTester.CreateFor <SomeEntityWithDecimalValue>()
                .WithContext <EntityPersistenceContext>()
                .WithDbMigrator(cs => new DbSchemaImporter(cs, sql))
                .WithSharedDatabase(db)
                .ShouldPersistAndRecall();

                // need to clear for the test to work again
                using (var ctx = new EntityPersistenceContext(db.OpenConnection()))
                {
                    ctx.EntitiesWithDecimalValues.Clear();
                    ctx.SaveChangesWithErrorReporting();
                }

                Assert.DoesNotThrow(() =>
                {
                    EntityPersistenceTester.CreateFor <SomeEntityWithDecimalValue>()
                    .WithContext <EntityPersistenceContext>()
                    .WithDbMigrator(cs => new DbSchemaImporter(cs, sql))
                    .WithSharedDatabase(db)
                    .ShouldPersistAndRecall();
                });
            }
        }
Exemple #2
0
        public void Dispose_ShouldRemoveTheTempDatabase()
        {
            //---------------Set up test pack-------------------
            string file;

            using (var db = new TempDBLocalDb())
            {
                //---------------Assert Precondition----------------
                var conn = db.OpenConnection();
                var cmd  = conn.CreateCommand();
                cmd.CommandText = "create table [test] ([id] int primary key identity, [name] nvarchar(128));";
                cmd.ExecuteNonQuery();
                cmd             = conn.CreateCommand();
                cmd.CommandText = "insert into [test] ([name]) values ('the name');";
                cmd.ExecuteNonQuery();
                cmd             = conn.CreateCommand();
                cmd.CommandText = "select * from [test];";
                cmd.ExecuteReader();
                file = db.DatabasePath;
                Assert.IsTrue(File.Exists(file));

                //---------------Execute Test ----------------------

                //---------------Test Result -----------------------
            }
            Assert.IsFalse(File.Exists(file));
        }
Exemple #3
0
        public void Dispose_ShouldCloseManagedConnectionsBeforeAttemptingToDeleteTheFile()
        {
            var    createTable = "create table TheTable(id int primary key, name nvarchar(128));";
            var    insertData  = "insert into TheTable(id, name) values (1, 'one');";
            var    selectData  = "select * from TheTable; "; //"select name from TheTable where id = 1;";
            string theFile;

            using (var db = new TempDBLocalDb(new[] { createTable, insertData }))
            {
                theFile = db.DatabasePath;
                Assert.IsTrue(File.Exists(theFile));
                //---------------Set up test pack-------------------

                //---------------Assert Precondition----------------

                var conn = db.OpenConnection();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = selectData;
                    using (var rdr = cmd.ExecuteReader())
                    {
                        Assert.IsTrue(rdr.Read());
                        Assert.AreEqual("one", rdr["name"].ToString());
                    }
                }
                //---------------Execute Test ----------------------
            }
            //---------------Test Result -----------------------
            Assert.IsFalse(File.Exists(theFile));
        }
Exemple #4
0
        public void GetConnection_ShouldReturnValidConnection()
        {
            var createTable = "create table TheTable(id int primary key, name nvarchar(128));";
            var insertData  = "insert into TheTable(id, name) values (1, 'one');";
            var selectData  = "select name from TheTable where id = 1;";

            using (var db = new TempDBLocalDb(new[] { createTable, insertData }))
            {
                //---------------Set up test pack-------------------

                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                using (var conn = db.OpenConnection())
                {
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = selectData;
                        using (var rdr = cmd.ExecuteReader())
                        {
                            Assert.IsTrue(rdr.Read());
                            Assert.AreEqual("one", rdr["name"].ToString());
                        }
                    }
                }

                //---------------Test Result -----------------------
            }
        }
 private void ExpectScriptsCanSupportDatabase(IEnumerable <string> result)
 {
     using (var db = new TempDBLocalDb())
     {
         var migrator = new DbSchemaImporter(
             db.ConnectionString,
             string.Join("\r\n", result)
             );
         migrator.MigrateToLatest();
         var cow = RandomValueGen.GetRandom <Cow>();
         using (var ctx = new MooContext(db.OpenConnection()))
         {
             ctx.Cows.Add(cow);
             ctx.SaveChanges();
         }
         using (var ctx = new MooContext(db.OpenConnection()))
         {
             var allCows = ctx.Cows.ToArray();
             Expect(allCows).To.Contain.Exactly(1).Item();
             var stored = allCows[0].DuckAs <ICow>();
             Expect(cow.DuckAs <ICow>()).To.Deep.Equal(stored);
         }
     }
 }
Exemple #6
0
        public void ShouldHaveTableAfterMigration_(string tableName)
        {
            //---------------Set up test pack-------------------
            using (var connection = _migratedDb.OpenConnection())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = "select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = '" + tableName + "';";
                    using (var reader = cmd.ExecuteReader())
                    {
                        Assert.IsTrue(reader.Read());
                    }
                }

                //---------------Test Result -----------------------
            }
        }