public void InvokeMgRollback_Returns_If_Latest_Iteration_Is_Zero() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); DbMock.GetLatestIteration(0); DbMock.GetAppliedScriptsForLatestIteration(new[] { new Migration { Iteration = 1, MigrationId = "one.sql" } }); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/rollback/one.sql" }); ConfigManagerMock.RollbackDirectory("migrations/rollback"); var command = new InvokeMgRollback(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <MgResult>()?.First(); Assert.False(result.Successful); }
protected BaseCmdletTest(MockBehavior behavior = MockBehavior.Strict) { DbMock = new DatabaseProviderMock(behavior); FileManagerMock = new FileManagerMock(behavior); EnvironmentManagerMock = new EnvironmentManagerMock(behavior); ConfigManagerMock = new ConfigManagerMock(behavior); SecretManagerMock = new SecretManager(EnvironmentManagerMock.Object, FileManagerMock.Object, ConfigManagerMock.Object); }
public void InvokeMgSeeding_Returns_If_All_Scripts_Are_Applied() { ConfigManagerMock.ConfigReturns(null); DbMock.SeedingTableExists(true); ConfigManagerMock.SeedersDirectory("migrations/seeders"); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/one.sql", "migrations/two.sql" }); DbMock.GetAppliedSeeders(new Seed[] { new() { SeedId = "one.sql" }, new() { SeedId = "two.sql" } });
public void InvokeMgRollout_Returns_If_All_Scripts_Are_Applied() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); ConfigManagerMock.RolloutDirectory("migrations/rollout"); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/one.sql", "migrations/two.sql" }); DbMock.GetAppliedMigrations(new Migration[] { new() { Iteration = 1, MigrationId = "one.sql" }, new() { Iteration = 1, MigrationId = "two.sql" } });
public void GetMgUsedVariables_Throws_If_File_Does_Not_Exist() { FileManagerMock.FileExists("migrations/rollout/one.sql", false); var command = new GetMgUsedVariables(GetMockedDependencies()) { MigrationFile = "migrations/rollout/one.sql" }; Assert.Throws <Exception>(() => command.Invoke()?.OfType <string[]>()?.First()); }
public void InvokeMgRollback_Rollbacks_Correct_Migrations() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); DbMock.GetLatestIteration(1); DbMock.GetAppliedScriptsForLatestIteration(new[] { new Migration { Iteration = 1, MigrationId = "two" }, new Migration { Iteration = 1, MigrationId = "three" }, }); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/rollback/one.sql", "migrations/rollback/two.sql", "migrations/rollback/three.sql", }); ConfigManagerMock.RollbackDirectory("migrations/rollback"); FileManagerMock.ReadAllText("migrations/rollback/two.sql", "rollback 2"); FileManagerMock.ReadAllText("migrations/rollback/three.sql", "rollback 3"); DbMock.RunTransactionAny(1); var command = new InvokeMgRollback(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <string>().ToArray(); Assert.Contains("Found 2 migrations applied in iteration 1", result); Assert.Contains("Migration one was not applied in latest iteration, skipping", result); Assert.Contains("Adding rollback of migration: two to transaction", result); FileManagerMock.VerifyReadAllText("migrations/rollback/one.sql", Times.Never()); var transactions = "rollback 2;" + Environment.NewLine + "DELETE FROM \"public\".\"MIGRATIONS\" WHERE \"MIGRATION_ID\" = 'two' AND \"ITERATION\" = '1';" + Environment.NewLine + "rollback 3;" + Environment.NewLine + "DELETE FROM \"public\".\"MIGRATIONS\" WHERE \"MIGRATION_ID\" = 'three' AND \"ITERATION\" = '1';" + Environment.NewLine; DbMock.VerifyRunTransaction(transactions); }
public void GetMgUsedVariables_Returns_Correct_Variables() { FileManagerMock.FileExists("migrations/rollout/one.sql", true); FileManagerMock.ReadAllText("migrations/rollout/one.sql", "SELECT ${{VAR_ME}} FROM ${{VAR_YOU}}"); var command = new GetMgUsedVariables(GetMockedDependencies()) { MigrationFile = "migrations/rollout/one.sql" }; var result = command.Invoke()?.OfType <string[]>()?.First(); Assert.Equal(new[] { "VAR_ME", "VAR_YOU" }, result); }
public void GetMgUsedVariables_Returns_Empty_When_No_Variables_Is_Used() { FileManagerMock.FileExists("migrations/rollout/one.sql", true); FileManagerMock.ReadAllText("migrations/rollout/one.sql", "SELECT ME FROM YOU"); var command = new GetMgUsedVariables(GetMockedDependencies()) { MigrationFile = "migrations/rollout/one.sql" }; var result = command.Invoke()?.OfType <string[]>()?.First(); Assert.Empty(result); }
public void InvokeMgRollout_Returns_If_No_Scripts_Found() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); FileManagerMock.GetAllFilesInFolder(Array.Empty <string>()); ConfigManagerMock.RolloutDirectory("migrations/rollout"); var command = new InvokeMgRollout(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <MgResult>()?.First(); Assert.False(result.Successful); }
public void InvokeMgRollout_Creates_Migration_Table_If_CreateTableIfNotExist_Is_True() { ConfigManagerMock.ConfigReturns(null); ConfigManagerMock.RollbackDirectory("migrations/rollback"); DbMock.MigrationTableExists(false); DbMock.CreateMigrationTable(1); FileManagerMock.GetAllFilesInFolder(Array.Empty <string>()); ConfigManagerMock.RolloutDirectory("migrations/rollout"); var command = new InvokeMgRollout(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******", CreateTableIfNotExist = true }; command.Invoke()?.OfType <string>()?.ToArray(); DbMock.VerifyCreateMigrationTable(Times.Once()); }
public void NewMgSeeder_Creates_Seeds_Directory_If_Not_Exists() { var seedPath = Path.Join("migrations", "seeders"); ConfigManagerMock.SeedersDirectory(seedPath); FileManagerMock.DirectoryExists(seedPath, false); FileManagerMock.CreateDirectory(seedPath); FileManagerMock.GetFilePrefix("test_prefix"); FileManagerMock.GetFormattedName("file_name"); FileManagerMock.FileExists("migrations/seeders/test_prefix_file_name.sql", false); FileManagerMock.CreateFile("migrations/seeders/test_prefix_file_name.sql"); var command = new NewMgSeeder(GetMockedDependencies()) { Name = "This is my migration" }; var result = command.Invoke()?.OfType <string>().First(); Assert.NotNull(result); Assert.Contains(seedPath, result); FileManagerMock.VerifyCreateDirectory(seedPath, Times.Once()); FileManagerMock.VerifyCreateFile("migrations/seeders/test_prefix_file_name.sql", Times.Once()); }
public ConfigurationManagerTests() { _fileManagerMock = new FileManagerMock(); }
public SecretManagerTests() { _envMock = new EnvironmentManagerMock(); _fileManagerMock = new FileManagerMock(); _configManagerMock = new ConfigManagerMock(); }