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 GetMgProcessedMigrations_Returns_Records() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); DbMock.GetAppliedMigrations( new List <Migration> { new() { Iteration = 1, MigrationId = "0001_migration_1" }, new() { Iteration = 1, MigrationId = "0002_migration_2" }, }.ToArray());
public void GetMgProcessedSeeders_Returns_Records() { ConfigManagerMock.ConfigReturns(null); DbMock.SeedingTableExists(true); DbMock.GetAppliedSeeders( new List <Seed> { new() { SeedId = "0001_seed_1" }, new() { SeedId = "0002_seed_2" }, }.ToArray());
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 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 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 GetMgLatestIteration_Returns_Null_If_Migration_Table_Does_Not_Exist() { DbMock.MigrationTableExists(false); ConfigManagerMock.ConfigReturns(null); var command = new GetMgLatestIteration(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <IterationResult>(); Assert.Empty(result); }
public void InvokeMgSeeding_Throws_If_Seeders_Table_Does_Not_Exist_And_CreateTableIfNotExist_Is_False() { ConfigManagerMock.SeedersDirectory("migrations"); ConfigManagerMock.ConfigReturns(null); DbMock.SeedingTableExists(false); var command = new InvokeMgSeeding(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; Assert.Throws <Exception>(() => command.Invoke()?.OfType <bool>()?.First()); }
public void InvokeMgRollback_Throws_If_Migration_Table_Does_Not_Exist() { ConfigManagerMock.ConfigReturns(null); ConfigManagerMock.RollbackDirectory("migrations/rollback"); DbMock.MigrationTableExists(false); var command = new InvokeMgRollback(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; Assert.Throws <Exception>(() => command.Invoke()?.OfType <bool>()?.First()); }
public void DbCmdlet_Returns_Clean_Config_Values() { ConfigManagerMock.ConfigReturns(new MgConfig { Directories = new MgDirs { Base = "/dev/migrations", Rollback = "/dev/migrations/rollback", Rollout = "/dev/migrations/rollout", Seeders = "/dev/migrations/seeders", }, Auth = new MgAuth { Postgres = new MgDb { Host = "localhost", Port = 3333, Database = "TestDB", Password = "******", Schema = "public", Username = "******" } }, EnvFile = ".backend.env", EnvMapping = new Dictionary <string, string> { { "MG_DB_USERNAME", "DB_USERNAME" } }, ReplaceVariables = true }); var command = new DbCmdletMockItem(GetMockedDependencies()) { ConfigFile = "migratio.yml" }; var coninfo = command.GetConnectionDetails(); Assert.Equal("postgres", coninfo.Username); Assert.Equal("TestDB", coninfo.Database); Assert.Equal("password", coninfo.Password); Assert.Equal("localhost", coninfo.Host); Assert.Equal(3333, coninfo.Port); Assert.Equal("public", coninfo.Schema); }
public void GetMgLatestIteration_Returns_Records() { DbMock.MigrationTableExists(true); DbMock.GetLatestIteration(1); ConfigManagerMock.ConfigReturns(null); var command = new GetMgLatestIteration(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <IterationResult>()?.First(); Assert.NotNull(result); Assert.Equal(1, result.Iteration); }
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 NewMgMigrationTable_Returns_False_If_Table_Exists() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); var command = new NewMgMigrationTable(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <MgResult>().First(); Assert.False(result.Successful); DbMock.VerifyMigrationTableExists(Times.Once()); DbMock.VerifyCreateMigrationTable(Times.Never()); }
public void DbCmdlet_Returns_Cli_Value_First() { ConfigManagerMock.ConfigReturns(null); var command = new DbCmdletMockItem(GetMockedDependencies()) { Username = "******", Database = "cli_db", Host = "cli_host", Port = 1234, Schema = "cli_schema" }; var coninfo = command.GetConnectionDetails(); Assert.Equal("cli_uname", coninfo.Username); Assert.Equal("cli_db", coninfo.Database); Assert.Equal("password", coninfo.Password); Assert.Equal("cli_host", coninfo.Host); Assert.Equal(1234, coninfo.Port); Assert.Equal("cli_schema", coninfo.Schema); }
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 SecretManagerTests() { _envMock = new EnvironmentManagerMock(); _fileManagerMock = new FileManagerMock(); _configManagerMock = new ConfigManagerMock(); }
public InvokeMgRolloutTests() { ConfigManagerMock.Resolve(null, null, "migrations", "migrations"); ConfigManagerMock.Resolve(null, null, "migrations/rollout", "migrations/rollout"); ConfigManagerMock.Resolve <bool?>(null, false, false, false); }
public InvokeMgRollbackTests() { ConfigManagerMock.Resolve(null, null, "migrations", "migrations"); ConfigManagerMock.Resolve(null, null, "migrations/rollback", "migrations/rollback"); }
public InvokeMgSeedingTests() { ConfigManagerMock.Resolve(null, null, "migrations", "migrations"); ConfigManagerMock.Resolve(null, null, "migrations/seeders", "migrations/seeders"); ConfigManagerMock.Resolve <bool?>(null, false, false, false); }