Пример #1
0
        public void ShouldScriptDropForeignKey()
        {
            SetupResult.For(templateManager.DropForeignKey).Return(@"DropForeignKey.vm");
            mocks.ReplayAll();

            DatabaseStub   db      = new DatabaseStub("DB");
            TableStub      fkTable = db.AddStubbedTable("Customer");
            ColumnStub     fkCol   = fkTable.AddStubbedColumn("BillingID", "INT");
            ForeignKeyStub fk      = fkCol.AddForeignKeyStub("FK_Customer_BillingID");

            TableStub  pkTable = db.AddStubbedTable("Billing");
            ColumnStub pkCol   = pkTable.AddStubbedColumn("BillingID", "INT");

            fk.primaryColumns.Add(pkCol);
            fk.primaryTable = pkTable;

            SqlScript script = scriptBuilder.Drop(fk);
            string    sql    = script.ToScript();

            Console.WriteLine(sql);
            Assert.IsTrue(sql.Contains("IF EXISTS"), "Missing IF EXISTS");
            Assert.IsTrue(sql.Contains("ALTER TABLE [dbo].[Customer]"), "Missing ALTER TABLE");
            Assert.IsTrue(sql.Contains("DROP CONSTRAINT"), "Missing DROP CONSTRAINT");
            Assert.IsTrue(sql.Contains("FK_Customer_BillingID"), "Missing FK_Customer_BillingID");
        }
Пример #2
0
        public void ItShouldCallNotCreateSchemaTableMethodIfEnsureSchemaVersionTableAndTableExists()
        {
            var database = new DatabaseStub(1);
            var schema   = new Schema(database, new LoggerStub());

            schema.EnsureSchemaVersionTable();

            Assert.That(database.Statements.Any(x => x.Contains(@"CREATE TABLE [dbo].[SchemaInfo]")), Is.False);
        }
Пример #3
0
        public void ItShouldProduceSqlForCreatingSchemaVersionTable()
        {
            var database = new DatabaseStub(0);
            var schema   = new Schema(database, new LoggerStub());

            schema.CreateSchemaVersionTable();

            Assert.That(database.Statements.Any(x => x.Contains(@"CREATE TABLE [dbo].[SchemaInfo]")));
        }
Пример #4
0
        public void ShouldReturnTableDoesNotExist()
        {
            DatabaseStub srcDB    = new DatabaseStub("SourceDB");
            TableStub    srcTable = srcDB.AddStubbedTable("Table1");

            DatabaseStub targetDB = new DatabaseStub("TargetDB");

            Assert.IsFalse(comparer.Table(srcTable).ExistsIn(targetDB));
        }
Пример #5
0
        public void ItShouldCreateSqlForUpdatingSchemaToSpecificVersionWhenRequested()
        {
            var database = new DatabaseStub(1);
            var schema   = new Schema(database, new LoggerStub());

            schema.UpdateSchemaVersionTo(10);

            Assert.That(database.Statements.Any(x => x.Contains(@"INSERT INTO [dbo].[SchemaInfo]")));
            Assert.That(database.Statements.Any(x => x.Contains(@"10")));
        }
Пример #6
0
        public void ItShouldCreateSqlForFetchingSchemaVersionWhenRequested()
        {
            var database = new DatabaseStub(1);
            var schema   = new Schema(database, new LoggerStub());

            var result = schema.GetLatestSchemaVersion();

            Assert.That(database.Statements.Any(x => x.Contains(@"SELECT Max([Version])")));
            Assert.That(result, Is.EqualTo(1));
        }
            public void then_should_return_expected_result()
            {
                // given
                var database = new DatabaseStub { SingleResult = "Hello You" };

                // when
                string result = database.SingleStoredProcedure<string>("MySpName");

                // then
                result.ShouldEqual("Hello You");
            }
Пример #8
0
        public void ItShouldNotApplyMigrationsSinceLastVersionWhenVersionBiggerThanMigration()
        {
            var database = new DatabaseStub(3);
            var logger   = new LoggerStub();
            var schema   = new Schema(database, logger);
            var migrator = new Migrator(schema, typeof(FirstMigration).Assembly, logger);

            migrator.MigrateToLatest();

            Assert.That(database.Statements.Any(x => x.Contains("FirstMigration")), Is.False);
        }
Пример #9
0
        public void ItShouldApplyMigrationsSinceLastVersionAndUpdateSchemaTable()
        {
            var database = new DatabaseStub();
            var logger   = new LoggerStub();
            var schema   = new Schema(database, logger);
            var migrator = new Migrator(schema, typeof(FirstMigration).Assembly, logger);

            migrator.MigrateToLatest();

            Assert.That(database.Statements.Any(x => x.Contains("FirstMigration")));
        }
Пример #10
0
        public void ShouldReturnColumnDoesNotExistWhenTableDoesNotExist()
        {
            DatabaseStub srcDB    = new DatabaseStub("SourceDB");
            TableStub    srcTable = srcDB.AddStubbedTable("Table1");

            srcTable.AddStubbedColumn("Col1", "INT");

            DatabaseStub targetDB = new DatabaseStub("TargetDB");

            Assert.IsFalse(comparer.Column(srcTable.Columns[0]).ExistsIn(targetDB));
        }
Пример #11
0
        public void ItShouldInformIfNoMigrationsFoundInAssembly()
        {
            var database = new DatabaseStub();
            var logger   = new LoggerStub();
            var schema   = new Schema(database, logger);
            var migrator = new Migrator(schema, typeof(Schema).Assembly, logger);

            migrator.MigrateToLatest();

            Assert.That(logger.Logs.Any(x => x.Contains(@"No Migrations Found")));
        }
Пример #12
0
        public void ItShouldSkipFindFirstMigrationIfVersionHigher()
        {
            var database = new DatabaseStub(3);
            var logger   = new LoggerStub();
            var schema   = new Schema(database, logger);
            var migrator = new Migrator(schema, typeof(FirstMigration).Assembly, logger);

            migrator.MigrateToLatest();

            Assert.That(logger.Logs.Any(x => x.Contains(@"No Migrations Found")));
        }
            public void then_should_not_have_any_arguements()
            {
                // given
                var database = new DatabaseStub();

                // when
                database.QueryStoredProcedure<string>("MySpName");

                // then
                Sql executedSql = database.QuerySql;
                executedSql.Arguments.ShouldBeEmpty();
            }
Пример #14
0
        public void ShouldReturnForeignKeyDoesNotExistWhenTableDoesNotExist()
        {
            DatabaseStub srcDB    = new DatabaseStub("SourceDB");
            TableStub    srcTable = srcDB.AddStubbedTable("Table1");
            ColumnStub   srcCol   = srcTable.AddStubbedColumn("Col1", "INT");

            srcCol.AddForeignKeyStub("FK1");

            DatabaseStub targetDB = new DatabaseStub("TargetDB");

            Assert.IsFalse(comparer.ForeignKey(srcTable.ForeignKeys[0]).ExistsIn(targetDB));
        }
            public void then_should_return_expected_result()
            {
                // given
                var database = new DatabaseStub { QueryResult = new object[] { "Hello You" } };

                // when
                string[] result = database.QueryStoredProcedure<string>("MySpName").ToArray();

                // then
                result.ShouldNotBeEmpty();
                result[0].ShouldEqual("Hello You");
            }
            public void then_should_not_have_any_arguements()
            {
                // given
                var database = new DatabaseStub();

                // when
                database.SingleStoredProcedure <string>("MySpName");

                // then
                Sql executedSql = database.SingleSql;

                executedSql.Arguments.ShouldBeEmpty();
            }
            public void then_should_call_sp_with_sp_name()
            {
                // given
                var database = new DatabaseStub();

                // when
                database.QueryStoredProcedure<string>("MySpName");

                // then
                Sql executedSql = database.QuerySql;
                executedSql.SQL.ShouldContain("EXEC");
                executedSql.SQL.ShouldContain("MySpName");
            }
            public void then_should_return_expected_result()
            {
                // given
                var database = new DatabaseStub {
                    SingleResult = "Hello You"
                };

                // when
                string result = database.SingleStoredProcedure <string>("MySpName");

                // then
                result.ShouldEqual("Hello You");
            }
            public void then_should_call_sp_with_sp_name()
            {
                // given
                var database = new DatabaseStub();

                // when
                database.SingleStoredProcedure <string>("MySpName");

                // then
                Sql executedSql = database.SingleSql;

                executedSql.SQL.ShouldContain("EXEC");
                executedSql.SQL.ShouldContain("MySpName");
            }
Пример #20
0
        public void ItShouldNotUpdateSchemaVersionTo1IfMigrationNotApplicable()
        {
            var database = new DatabaseStub(3);
            var logger   = new LoggerStub();
            var schema   = new Schema(database, logger);
            var migrator = new Migrator(schema, typeof(FirstMigration).Assembly, logger);

            migrator.MigrateToLatest();

            Assert.That(database.Statements.Any(x => x.Contains(@"INSERT INTO [dbo].[SchemaInfo]
           ([Version])
     VALUES
           (1)")), Is.False);
        }
Пример #21
0
            public void then_should_return_expected_result()
            {
                // given
                var database = new DatabaseStub {
                    QueryResult = new object[] { "Hello You" }
                };

                // when
                string[] result = database.QueryStoredProcedure <string>("MySpName").ToArray();

                // then
                result.ShouldNotBeEmpty();
                result[0].ShouldEqual("Hello You");
            }
Пример #22
0
        public void SetUp()
        {
            templateDir = AppDomain.CurrentDomain.BaseDirectory + @"\..\..\..\Sneal.SqlMigration\Templates";

            mocks           = new MockRepository();
            templateManager = mocks.DynamicMock <ISqlTemplateManager>();

            SetupResult.For(templateManager.TemplateDirectory).Return(templateDir);
            mocks.Replay(templateManager);
            scriptBuilder = new SqlServerScriptBuilder(templateManager);
            mocks.BackToRecord(templateManager);

            db    = new DatabaseStub("DB");
            table = new TableStub(db, "Customer");
        }
Пример #23
0
        internal static DatabaseStub CreateStubbedSourceDB()
        {
            DatabaseStub db = new DatabaseStub("AdeventureWorks");

            TableStub table = new TableStub(db, "Customer");

            table.columns.Add(new ColumnStub(table, "CustomerID", "INT"));
            table.columns.Add(new ColumnStub(table, "FirstName", "NVARCHAR(50)"));
            table.columns.Add(new ColumnStub(table, "LastName", "NVARCHAR(50)"));
            table.columns.Add(new ColumnStub(table, "Email", "NVARCHAR(50)"));
            table.columns.Add(new ColumnStub(table, "PrimaryEmail", "NVARCHAR(50)"));
            table.columns.Add(new ColumnStub(table, "CustomerTypeID", "INT"));

            return(db);
        }
Пример #24
0
        public void SetUp()
        {
            mocks         = new MockRepository();
            scriptBuilder = mocks.CreateMock <IScriptBuilder>();
            msgManager    = mocks.DynamicMock <IScriptMessageManager>();
            connSettings  = mocks.Stub <IConnectionSettings>();

            dbComparer = new DatabaseComparer();

            engine = new MigrationEngine(scriptBuilder, dbComparer);
            engine.MessageManager = msgManager;

            srcDB    = new DatabaseStub("SRC_DB");
            targetDB = new DatabaseStub("TARGET_DB");
        }
            public void then_should_include_paramter_in_sql()
            {
                // given
                var database = new DatabaseStub();

                // when
                database.QueryStoredProcedure<string>("MySpName", new Parameter("Param1", "Value"));

                // then
                Sql executedSql = database.QuerySql;
                executedSql.Arguments.ShouldNotBeEmpty();
                executedSql.Arguments.Any(x => x.Equals("Value")).ShouldBeTrue();

                executedSql.SQL.ShouldContain("Param1");
            }
Пример #26
0
        public void ShouldReturnIndexExists()
        {
            DatabaseStub srcDB    = new DatabaseStub("SourceDB");
            TableStub    srcTable = srcDB.AddStubbedTable("Table1");
            ColumnStub   srcCol   = srcTable.AddStubbedColumn("Col1", "INT");

            srcTable.AddStubbedIndex(srcCol, "IX_COL1");

            DatabaseStub targetDB    = new DatabaseStub("TargetDB");
            TableStub    targetTable = targetDB.AddStubbedTable("Table1");
            ColumnStub   targetCol   = targetTable.AddStubbedColumn("Col1", "INT");

            targetTable.AddStubbedIndex(targetCol, "IX_COL1");

            Assert.IsTrue(comparer.Index(srcTable.Indexes[0]).ExistsIn(targetDB));
        }
            public void then_should_include_paramter_in_sql()
            {
                // given
                var database = new DatabaseStub();

                // when
                database.SingleStoredProcedure <string>("MySpName", new Parameter("Param1", "Value"));

                // then
                Sql executedSql = database.SingleSql;

                executedSql.Arguments.ShouldNotBeEmpty();
                executedSql.Arguments.Any(x => x.Equals("Value")).ShouldBeTrue();

                executedSql.SQL.ShouldContain("Param1");
            }
Пример #28
0
        public void ShouldScriptCreateIndex()
        {
            SetupResult.For(templateManager.CreateIndex).Return(@"CreateIndex.vm");
            mocks.ReplayAll();

            DatabaseStub db     = new DatabaseStub("DB");
            TableStub    table  = new TableStub(db, "Customer");
            ColumnStub   column = table.AddStubbedColumn("LastName", "NVARCHAR(50)");
            IndexStub    index  = table.AddStubbedIndex(column, "IX_LastName");

            index.unique = false;

            SqlScript script = scriptBuilder.Create(index);
            string    sql    = script.ToScript();

            Console.WriteLine(sql);
            Assert.IsTrue(sql.Contains("IF NOT EXISTS"), "Missing IF NOT EXISTS");
            Assert.IsTrue(sql.Contains("CREATE NONCLUSTERED INDEX [IX_LastName]"), "Missing CREATE NONCLUSTERED INDEX");
            Assert.IsTrue(sql.Contains("[dbo].[Customer]"), "Missing [dbo].[Customer]");
            Assert.IsTrue(sql.Contains("LastName"), "Missing LastName");
        }
Пример #29
0
        public void Test_Insert()
        {
            // arrange

            const string testString = "Hello, world!";

            var settings = new DatabaseSettings("./foobar.json");
            var instance = new DatabaseInstance <DatabaseStub>(settings, new JsonProvider <DatabaseStub>());
            var stub     = new DatabaseStub {
                Value = testString
            };

            // act
            instance[1] = stub;
            instance.SaveChanges();

            var secondInstance = new DatabaseInstance <DatabaseStub>(settings, new JsonProvider <DatabaseStub>());
            var value          = secondInstance[1].Value;

            // assert
            Assert.AreEqual(testString, value);
        }
Пример #30
0
 public void SetUp()
 {
     _connection = new Mock <IDbConnectionWrapper>();
     _database   = new DatabaseStub(_connection.Object);
 }