Example #1
0
        //  [Test]
        public void Borrower_ShouldPersistAndRecall()
        {
            //---------------Set up test pack-------------------
            var quote = new QuoteBuilder()
                        .WithRandomProps()
                        .WithProp(b => b.Id = 0)
                        .WithValidCompanyId()
                        .Build();

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

            using (var ctx = new QBMSDbContext(_localDb.CreateConnection()))
            {
                //ctx.Set<Quote>().Add(quote);
                //ctx.Set(typeof (Quote)).Add(quote);
                //ctx.Set(quote.GetType()).Add(quote);
                //ctx.Entry(quote).State = EntityState.Added;

                ctx.Quotes.Add(quote);
                ctx.SaveChanges();
            }

            using (var ctx = new QBMSDbContext(_localDb.CreateConnection()))
            {
                //---------------Execute Test ----------------------
                var result = ctx.Set <Quote>().Single();
                //---------------Test Result -----------------------
                PropertyAssert.AreEqual(quote, result, "Company");
            }
        }
        public void ShouldBeAbleToPersistAndRecall()
        {
            using (var db = new TempDBLocalDb())
            {
                var runner = new MigrationsRunner(db.ConnectionString, Console.WriteLine);
                runner.MigrateToLatest();

                //---------------Set up test pack-------------------

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

                //---------------Execute Test ----------------------
                // first, put your left leg in
                var item= ItemBuilder.BuildRandom();

                using (var context = new LendingLibraryContext(db.CreateConnection()))
                {
                    context.Items.Add(item);
                    context.SaveChangesWithErrorReporting();
                }
                // shake it all about
                using (var context = new LendingLibraryContext(db.CreateConnection()))
                {
                    var persisted = context.Items.FirstOrDefault(o => o.ItemId == item.ItemId);
                    Assert.IsNotNull(persisted);
                    Assert.AreEqual(item.ItemId, persisted.ItemId);
                    Assert.AreEqual(item.Description, persisted.Description);
                    Assert.AreEqual(item.Mimetype, persisted.Mimetype);
                    Assert.AreEqual(item.Photo, persisted.Photo);
                }
                //---------------Test Result -----------------------
            }
        }
Example #3
0
        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.CreateConnection()))
                {
                    ctx.EntitiesWithDecimalValues.Clear();
                    ctx.SaveChangesWithErrorReporting();
                }

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

            using (var db = new TempDBLocalDb())
            {
                //---------------Assert Precondition----------------
                var conn = db.CreateConnection();
                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));
        }
Example #5
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.CreateConnection();
                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));
        }
Example #6
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.CreateConnection())
                {
                    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 -----------------------
            }
        }
        public void StaticConstructor_ShouldNotAllowEntityMigrations()
        {
            //---------------Set up test pack-------------------
            using (var tempDb = new TempDBLocalDb())
            using (var dbConnection = tempDb.CreateConnection())
            using (var context = new LendingLibraryContext(dbConnection))
            {
                try
                {
                    // force Entity to do whatever it does when it
                    // spins up but don't care if the query onto Persons
                    //  fails (which it will at time of writing because
                    //  we haven't written any migrations to create the
                    // table)
                    context.People.ToArray();
                }
                catch
                {
                }
                //---------------Assert Precondition----------------
                //---------------Execute Test ----------------------
                using (var cmd = dbConnection.CreateCommand())
                {
                    cmd.CommandText =
                        @"select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = '__MigrationHistory';";
                    using (var reader = cmd.ExecuteReader())
                    {
                        //---------------Test Result -----------------------
                        Assert.IsFalse(reader.Read());
                    }
                }

            }
        }
 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.CreateConnection()))
         {
             ctx.Cows.Add(cow);
             ctx.SaveChanges();
         }
         using (var ctx = new MooContext(db.CreateConnection()))
         {
             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);
         }
     }
 }
        public void ShouldBeAbleToPersistAndRecall()
        {
            using (var db = new TempDBLocalDb())
            {
                var runner = new MigrationsRunner(db.ConnectionString, Console.WriteLine);
                runner.MigrateToLatest();

                //---------------Set up test pack-------------------

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

                //---------------Execute Test ----------------------
                // first, put your left leg in
                var person = CreatePerson();
                
                using (var context = new LendingLibraryContext(db.CreateConnection()))
                {
                    context.People.Add(person);
                    context.SaveChangesWithErrorReporting();
                }
                // shake it all about
                using (var context = new LendingLibraryContext(db.CreateConnection()))
                {
                    var persisted = context.People.FirstOrDefault(o => o.PersonId == person.PersonId);
                    Assert.IsNotNull(persisted);
                    Assert.AreEqual(person.Email, persisted.Email);
                    Assert.AreEqual(person.FirstName,persisted.FirstName);
                    Assert.AreEqual(person.PersonId,persisted.PersonId);
                    Assert.AreEqual(person.Surname,persisted.Surname);
                    Assert.AreEqual(person.PhoneNumber,persisted.PhoneNumber);
                    Assert.AreEqual(person.Photo,persisted.Photo);
                }

                //---------------Test Result -----------------------
            }
        }
Example #10
0
        public void ShouldHaveTableAfterMigration_(string tableName)
        {
            //---------------Set up test pack-------------------
            using (var connection = _migratedDb.CreateConnection())
            {
                //---------------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 -----------------------
            }
        }
        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 = null;
            using (var db = new TempDBLocalDb(new[] { createTable, insertData }))
            {
                theFile = db.DatabaseFile;
                Assert.IsTrue(File.Exists(theFile));
                //---------------Set up test pack-------------------

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

                var conn = db.CreateConnection();
                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));
        }
        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.CreateConnection())
                {
                    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 -----------------------
            }
        }
        public void Dispose_ShouldRemoveTheTempDatabase()
        {
            //---------------Set up test pack-------------------
            string file = null;
            using (var db = new TempDBLocalDb())
            {
                //---------------Assert Precondition----------------
                var conn = db.CreateConnection();
                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];";
                var rdr = cmd.ExecuteReader();
                file = db.DatabaseFile;
                Assert.IsTrue(File.Exists(file));

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

                //---------------Test Result -----------------------
            }
            Assert.IsFalse(File.Exists(file));
        }