예제 #1
0
 public void Try_Restore_Bacpac()
 {
     try
     {
         SqlBacpacImpl.RestoreBacpac(context, ConnectionString, "NSaga", GetBacpacFilePath());
         SqlHelpers.DbExists(ConnectionString, "Nsaga").Should().BeTrue();
     }
     finally
     {
         SqlHelpers.DropDatabase(ConnectionString, "Nsaga");
     }
 }
예제 #2
0
        public void DatabaseExists_DoesExist_ReturnTrue()
        {
            // Arrange
            var dbName = "WillExists";

            SqlHelpers.ExecuteSql(ConnectionString, $"Create database {dbName}");

            // Act
            var dbExists = SqlServerAliases.DatabaseExists(context, ConnectionString, dbName);

            // Assert
            dbExists.Should().BeTrue();
        }
예제 #3
0
        public void CreateDatabase_Creates_Correctly()
        {
            // Act
            var dbName = "Unknown";

            SqlServerAliases.CreateDatabase(context, ConnectionString, dbName);

            // Assert
            SqlHelpers.DbExists(ConnectionString, dbName).Should().BeTrue();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #4
0
        public void DropDatabase_ActuallyDrops()
        {
            //Arrange
            var dbName = "WillBeDropped";

            SqlHelpers.ExecuteSql(ConnectionString, $"Create database {dbName}");

            // Act
            SqlServerAliases.DropDatabase(context, ConnectionString, dbName);

            // Assert
            SqlHelpers.DbExists(ConnectionString, dbName).Should().BeFalse();
        }
예제 #5
0
        public void CreateDatabaseIfNotExists()
        {
            // Act
            var dbName = "CakeTest";

            SqlServerAliases.CreateDatabaseIfNotExists(context, ConnectionString, dbName);

            // Assert
            SqlHelpers.DbExists(ConnectionString, dbName).Should().BeTrue();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #6
0
        public void CreateDatabaseIfNotExists_DbExists_DoesNotThrow()
        {
            // Act
            var dbName = "ToBeRecreatedAgain";

            SqlServerAliases.CreateDatabase(context, ConnectionString, dbName);
            SqlServerAliases.CreateDatabaseIfNotExists(context, ConnectionString, dbName);

            // Assert
            SqlHelpers.DbExists(ConnectionString, dbName).Should().BeTrue();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #7
0
 public void Dispose()
 {
     SqlHelpers.DropDatabase(ConnectionString, "ForFileExecution");
     SqlHelpers.DropDatabase(ConnectionString, "WillBeDropped");
     SqlHelpers.DropDatabase(ConnectionString, "CakeTest");
     SqlHelpers.DropDatabase(ConnectionString, "ToBeRecreated");
     SqlHelpers.DropDatabase(ConnectionString, "ToBeRecreatedAgain");
     SqlHelpers.DropDatabase(ConnectionString, "ForSqlExecution");
     SqlHelpers.DropDatabase(ConnectionString, "Unknown");
     SqlHelpers.DropDatabase(ConnectionString, "test");
     SqlHelpers.DropDatabase(ConnectionString, "hack");
     SqlHelpers.DropDatabase(ConnectionString, "test]] create database hack--");
     SqlHelpers.ExecuteSql(ConnectionString, "if (select DB_ID('some'')) is null create database hack--')) is not null drop database [some')) is null create database hack--]");
 }
예제 #8
0
        public void CreateDatabase_SqlNameInjection_DoesNotInject()
        {
            // Act
            SqlServerAliases.CreateDatabase(context, ConnectionString, "test] create database hack--");

            // Assert
            SqlHelpers.DbExists(ConnectionString, "test").Should().BeFalse();
            SqlHelpers.DbExists(ConnectionString, "hack").Should().BeFalse();
            SqlHelpers.DbExists(ConnectionString, "test] create database hack--").Should().BeTrue();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, "if (select DB_ID('test')) is not null drop database [test]");
            SqlHelpers.ExecuteSql(ConnectionString, "if (select DB_ID('hack')) is not null drop database [hack]");
            SqlHelpers.ExecuteSql(ConnectionString, "if (select DB_ID('test] create database hack--')) is not null drop database [test]] create database hack--]");
        }
예제 #9
0
        public void CreateDatabase_DbAlreadyExists_Throws()
        {
            // Act
            var dbName = "Unknown";

            SqlHelpers.ExecuteSql(ConnectionString, $"create database {dbName}");

            // Assert
            Action act = () => SqlServerAliases.CreateDatabase(context, ConnectionString, dbName);

            act.ShouldThrow <SqlException>();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #10
0
        public void CreateDatabaseIfNotExists_WithPrimaryFile()
        {
            // Act
            var dbName         = "CakeTest";
            var mdfFilePath    = Path.Combine(Path.GetTempPath(), "MyCakeTest.mdf");
            var createSettings = new CreateDatabaseSettings().WithPrimaryFile(mdfFilePath);

            SqlServerAliases.CreateDatabaseIfNotExists(context, ConnectionString, dbName, createSettings);

            // Assert
            SqlHelpers.DbExists(ConnectionString, dbName).Should().BeTrue();
            File.Exists(mdfFilePath).Should().BeTrue();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #11
0
        public void DropAndCreateDatabase_Always_RemovesTable()
        {
            //Arrange
            var dbName    = "ToBeRecreated";
            var tableName = "WillNotExist";

            SqlHelpers.ExecuteSql(ConnectionString, $"Create database {dbName}");
            SqlHelpers.ExecuteSql(ConnectionString, $"create table [{dbName}].dbo.{tableName} (id int null)");
            SqlHelpers.TableExists(ConnectionString, dbName, tableName).Should().BeTrue();

            // Act
            SqlServerAliases.DropAndCreateDatabase(context, ConnectionString, dbName);

            // Assert
            SqlHelpers.TableExists(ConnectionString, dbName, tableName).Should().BeFalse();
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #12
0
        public void DropDatabase_LiteralInjection_DoesNotInject()
        {
            // Act
            try
            {
                SqlServerAliases.DropDatabase(context, ConnectionString, "some')) is null create database hack--");
            }
            catch (Exception)
            {
                // do nothing
            }

            // Assert
            SqlHelpers.DbExists(ConnectionString, "hack").Should().BeFalse();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, "if (select DB_ID('hack')) is not null drop database [hack]");
        }
예제 #13
0
        public void ExecuteSqlFile_does_not_change_connection_state()
        {
            const string dbName = "ForFileExecution";

            SqlHelpers.ExecuteSql(ConnectionString, $"create database {dbName}");
            try
            {
                using (var connection = SqlServerAliases.OpenSqlConnection(context, ConnectionString))
                    using (var monitoringSubject = connection.Monitor())
                    {
                        SqlServerAliases.ExecuteSqlFile(context, connection, GetSqlFilePath());
                        monitoringSubject.Should().NotRaise(nameof(connection.StateChange));
                    }
            }
            finally
            {
                SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
            }
        }
예제 #14
0
        public void ExecuteSqlFile_Executes_Successfuly()
        {
            //Arrange
            var connectionString = @"data source=(LocalDb)\v12.0;Database=ForFileExecution";
            var dbName           = "ForFileExecution";
            var tableName1       = "WillExist1";
            var tableName2       = "WillExist2";

            SqlHelpers.ExecuteSql(ConnectionString, $"Create database {dbName}");
            var sqlFilePath = GetSqlFilePath();

            // Act
            SqlServerAliases.ExecuteSqlFile(context, connectionString, sqlFilePath);

            // Assert
            SqlHelpers.TableExists(ConnectionString, dbName, tableName1).Should().BeTrue();
            SqlHelpers.TableExists(ConnectionString, dbName, tableName2).Should().BeTrue();
            SqlServerAliases.DropDatabase(context, ConnectionString, dbName);
        }
예제 #15
0
        public void DropDatabase_LiteralInjection_DoesNotInject()
        {
            // Act
            try
            {
                SqlServerAliases.DropDatabase(context, ConnectionString, "some')) is null create database hack--");
            }
#pragma warning disable CC0004 // Catch block cannot be empty
            catch
            {
                // do nothing
            }
#pragma warning restore CC0004 // Catch block cannot be empty

            // Assert
            SqlHelpers.DbExists(ConnectionString, "hack").Should().BeFalse();

            // Cleanup
            SqlHelpers.ExecuteSql(ConnectionString, "if (select DB_ID('hack')) is not null drop database [hack]");
        }
예제 #16
0
        public void DropAndCreateDatabase_WithCreateParams()
        {
            //Arrange
            var dbName    = "ToBeRecreated";
            var tableName = "WillNotExist";

            SqlHelpers.ExecuteSql(ConnectionString, $"create database {dbName}");
            SqlHelpers.ExecuteSql(ConnectionString, $"create table [{dbName}].dbo.{tableName} (id int null)");
            SqlHelpers.TableExists(ConnectionString, dbName, tableName).Should().BeTrue();
            var mdfFilePath    = Path.Combine(Path.GetTempPath(), "MyCakeTest.mdf");
            var logFilePath    = Path.Combine(Path.GetTempPath(), "MyCakeTest.ldf");
            var createSettings = new CreateDatabaseSettings().WithPrimaryFile(mdfFilePath).WithLogFile(logFilePath);

            // Act
            SqlServerAliases.DropAndCreateDatabase(context, ConnectionString, dbName, createSettings);

            // Assert
            File.Exists(mdfFilePath).Should().BeTrue();
            File.Exists(logFilePath).Should().BeTrue();
        }
예제 #17
0
        public void Try_Publish_Dacpac()
        {
            var dbName = "DacpacTestDb";

            try
            {
                // Arrange
                SqlHelpers.ExecuteSql(ConnectionString, $"create database {dbName}");

                // Act
                SqlDacpacImpl.PublishDacpacFile(context, ConnectionString, dbName, GetDacpacFilePath());

                // Assert
                SqlHelpers.TableExists(ConnectionString, dbName, "Table1").Should().BeTrue();
            }
            finally
            {
                SqlHelpers.DropDatabase(ConnectionString, dbName);
            }
        }
예제 #18
0
        public void RestoreDatabase_MinimalInformation_DoesNotThrow()
        {
            var originalDbName = "CakeRestoreTest";

            try
            {
                //Arrange
                var path = GetBackupFilePath();

                // Act
                SqlBackupsImpl.RestoreSqlBackup(context, ConnectionString, new FilePath(path), new RestoreSqlBackupSettings());

                // Assert
                SqlHelpers.DbExists(ConnectionString, originalDbName);
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, originalDbName);
            }
        }
        public void RestoreMultipleDatabase_MinimalInformation_DoesNotThrow()
        {
            var originalDbName = "CakeRestoreTest";

            try
            {
                //Arrange
                var pathList             = GetMultipleBackupFilePaths();
                var differentialPathList = GetMultipleBackupFilePaths("differentialMultiFilesBackup*.bak");

                // Act
                RestoreSqlBackupImpl.RestoreSqlBackup(_context, ConnectionString, new RestoreSqlBackupSettings(), pathList, differentialBackupFiles: differentialPathList);

                // Assert
                SqlHelpers.DbExists(ConnectionString, originalDbName);
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, originalDbName);
            }
        }
예제 #20
0
        public void RestoreDatabase_MinimalInformation_DoesNotThrow()
        {
            const string originalDbName = "CakeRestoreTest";

            try
            {
                // Arrange
                var path = GetBackupFilePath();

                // Act
                RestoreSqlBackupImpl.RestoreSqlBackup(_context, ConnectionString, new RestoreSqlBackupSettings(), new List <FilePath> {
                    new FilePath(path)
                });

                // Assert
                SqlHelpers.DbExists(ConnectionString, originalDbName).Should().BeTrue();
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, originalDbName);
            }
        }
예제 #21
0
        public void ExecuteSqlCommand_CreatesTables()
        {
            //Arrange
            var dbName     = "ForSqlExecution";
            var tableName1 = "WillExist1";
            var tableName2 = "WillExist2";

            SqlHelpers.ExecuteSql(ConnectionString, $"Create database {dbName}");
            var sql = $@"
            create table [{dbName}].dbo.{tableName1} (id int null);
            Go
            create table [{dbName}].dbo.{tableName2} (id int null);
            Go
            ";

            // Act
            SqlServerAliases.ExecuteSqlCommand(context, ConnectionString, sql);

            // Assert
            SqlHelpers.TableExists(ConnectionString, dbName, tableName1).Should().BeTrue();
            SqlHelpers.TableExists(ConnectionString, dbName, tableName2).Should().BeTrue();
            SqlHelpers.ExecuteSql(ConnectionString, $"drop database {dbName}");
        }
예제 #22
0
        public void RestoreMultipleDatabase_DatabaseRename_DoesNotThrow()
        {
            const string databaseName = "NewRandomDatabase";

            try
            {
                // Arrange
                var pathList             = GetMultipleBackupFilePaths();
                var differentialPathList = GetMultipleBackupFilePaths("differentialMultiFilesBackup*.bak");

                RestoreSqlBackupImpl.RestoreSqlBackup(_context, ConnectionString, new RestoreSqlBackupSettings {
                    NewDatabaseName = databaseName
                }, pathList, differentialBackupFiles: differentialPathList);

                // Assert
                SqlHelpers.DbExists(ConnectionString, databaseName).Should().BeTrue();
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, databaseName);
            }
        }
예제 #23
0
        public void RestoreDatabase_DatabaseRename_DoesNotThrow()
        {
            var databaseName = "NewRandomDatabase";

            try
            {
                //Arrange
                var path = GetBackupFilePath();

                SqlBackupsImpl.RestoreSqlBackup(context, ConnectionString, new FilePath(path), new RestoreSqlBackupSettings()
                {
                    NewDatabaseName = databaseName
                });

                // Assert
                SqlHelpers.DbExists(ConnectionString, databaseName);
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, databaseName);
            }
        }
예제 #24
0
        public void RestoreDatabase_DatabaseRename_DoesNotThrow()
        {
            const string databaseName = "NewRandomDatabase";

            try
            {
                // Arrange
                var path = GetBackupFilePath();

                RestoreSqlBackupImpl.RestoreSqlBackup(_context, ConnectionString, new RestoreSqlBackupSettings {
                    NewDatabaseName = databaseName
                }, new List <FilePath> {
                    new FilePath(path)
                });

                // Assert
                SqlHelpers.DbExists(ConnectionString, databaseName).Should().BeTrue();
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, databaseName);
            }
        }
예제 #25
0
        public void RestoreMultipleDatabase_WithReplace_DoesNotThrow()
        {
            const string originalDbName = "CakeRestoreTest";

            try
            {
                // Arrange
                var pathList             = GetMultipleBackupFilePaths();
                var differentialPathList = GetMultipleBackupFilePaths("differentialMultiFilesBackup*.bak");

                // Act
                RestoreSqlBackupImpl.RestoreSqlBackup(_context, ConnectionString, new RestoreSqlBackupSettings {
                    WithReplace = true
                }, pathList, differentialBackupFiles: differentialPathList);

                // Assert
                SqlHelpers.DbExists(ConnectionString, originalDbName).Should().BeTrue();
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, originalDbName);
            }
        }
        public void RestoreDatabase_MoveLocation_DoesNotThrow()
        {
            var newDatabaseName = "RestoredFromTest.Cake";

            try
            {
                //Arrange
                var path = GetBackupFilePath();

                // Act
                SqlBackupsImpl.RestoreSqlBackup(context, ConnectionString, new FilePath(path), new RestoreSqlBackupSettings()
                {
                    NewDatabaseName = newDatabaseName, NewStorageFolder = new DirectoryPath(System.IO.Path.GetTempPath())
                });

                // Assert
                SqlHelpers.DbExists(ConnectionString, newDatabaseName);
            }
            finally
            {
                // Cleanup
                SqlHelpers.DropDatabase(ConnectionString, newDatabaseName);
            }
        }