public void Try_Restore_Bacpac() { try { SqlBacpacImpl.RestoreBacpac(context, ConnectionString, "NSaga", GetBacpacFilePath()); SqlHelpers.DbExists(ConnectionString, "Nsaga").Should().BeTrue(); } finally { SqlHelpers.DropDatabase(ConnectionString, "Nsaga"); } }
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(); }
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}"); }
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(); }
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}"); }
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}"); }
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--]"); }
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--]"); }
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}"); }
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}"); }
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}"); }
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]"); }
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}"); } }
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); }
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]"); }
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(); }
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); } }
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); } }
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); } }
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}"); }
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); } }
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); } }
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); } }
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); } }