github.com/adharshmk96/stk@v1.2.3/pkg/sqlMigrator/helpers_test.go (about) 1 package sqlmigrator_test 2 3 import ( 4 "fmt" 5 "os" 6 "path" 7 "testing" 8 9 sqlmigrator "github.com/adharshmk96/stk/pkg/sqlMigrator" 10 "github.com/adharshmk96/stk/testutils" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestInitializeMigrationsFolder(t *testing.T) { 15 t.Run("creates a migrations folder", func(t *testing.T) { 16 ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false) 17 err := sqlmigrator.InitializeMigrationsFolder(ctx) 18 assert.NoError(t, err) 19 20 _, err = os.Stat(path.Join(ctx.WorkDir, ctx.LogFile)) 21 assert.NoError(t, err) 22 }) 23 24 t.Run("does not create a more log file if it already exists", func(t *testing.T) { 25 26 tempDir, removeDir := testutils.CreateTempDirectory(t) 27 28 defer removeDir() 29 30 ctx := sqlmigrator.NewContext(tempDir, sqlmigrator.SQLiteDB, "migrator.log", false) 31 32 err := sqlmigrator.InitializeMigrationsFolder(ctx) 33 assert.NoError(t, err) 34 35 numFiles := testutils.GetNumberOfFilesInFolder(t, tempDir) 36 assert.Equal(t, 1, numFiles) 37 38 err = sqlmigrator.InitializeMigrationsFolder(ctx) 39 assert.NoError(t, err) 40 41 numFiles = testutils.GetNumberOfFilesInFolder(t, tempDir) 42 assert.Equal(t, 1, numFiles) 43 44 }) 45 } 46 47 func TestLoadUncommitedMigration(t *testing.T) { 48 t.Run("returns an empty migration entry if the log file is empty", func(t *testing.T) { 49 ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false) 50 migration := sqlmigrator.LoadUnappliedMigrations(ctx) 51 assert.Empty(t, migration) 52 assert.Equal(t, 0, len(migration)) 53 }) 54 55 t.Run("returns uncommitted migration entries from the log file", func(t *testing.T) { 56 logFile_content := func() string { 57 fileContent := "" 58 for i := 1; i <= 3; i++ { 59 fileContent += fmt.Sprintf("%d_create_users_table_up\n", i) 60 } 61 for i := 4; i <= 6; i++ { 62 fileContent += fmt.Sprintf("%d_create_other_table_down\n", i) 63 } 64 return fileContent 65 }() 66 67 ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false) 68 logPath := path.Join(ctx.WorkDir, ctx.LogFile) 69 err := os.WriteFile(logPath, []byte(logFile_content), 0644) 70 assert.NoError(t, err) 71 72 ctx.LoadMigrationEntries() 73 migrations := sqlmigrator.LoadUnappliedMigrations(ctx) 74 assert.NotEmpty(t, migrations) 75 76 expected := func() []*sqlmigrator.MigrationFileEntry { 77 migrationEntry := []*sqlmigrator.MigrationFileEntry{} 78 for i := 4; i <= 6; i++ { 79 migrationEntry = append(migrationEntry, &sqlmigrator.MigrationFileEntry{ 80 Number: i, 81 Name: "create_other_table", 82 Committed: false, 83 }) 84 } 85 86 return migrationEntry 87 }() 88 89 for i, migration := range migrations { 90 assert.Equal(t, expected[i].Name, migration.Name) 91 assert.Equal(t, expected[i].Number, migration.Number) 92 assert.Equal(t, expected[i].Committed, migration.Committed) 93 } 94 95 }) 96 } 97 98 func TestLoadCommittedMigration(t *testing.T) { 99 t.Run("returns an empty migration entry if the log file is empty", func(t *testing.T) { 100 ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false) 101 migration := sqlmigrator.LoadAppliedMigrations(ctx) 102 assert.Empty(t, migration) 103 }) 104 105 t.Run("returns committed migration entries from the log file", func(t *testing.T) { 106 logFile_content := func() string { 107 fileContent := "" 108 for i := 1; i <= 3; i++ { 109 fileContent += fmt.Sprintf("%d_create_users_table_up\n", i) 110 } 111 for i := 4; i <= 6; i++ { 112 fileContent += fmt.Sprintf("%d_create_users_table_down\n", i) 113 } 114 return fileContent 115 }() 116 117 ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false) 118 logPath := path.Join(ctx.WorkDir, ctx.LogFile) 119 err := os.WriteFile(logPath, []byte(logFile_content), 0644) 120 assert.NoError(t, err) 121 122 ctx.LoadMigrationEntries() 123 migrations := sqlmigrator.LoadAppliedMigrations(ctx) 124 assert.NotEmpty(t, migrations) 125 126 expected := func() []*sqlmigrator.MigrationFileEntry { 127 migrationEntry := []*sqlmigrator.MigrationFileEntry{} 128 for i := 1; i <= 3; i++ { 129 migrationEntry = append(migrationEntry, &sqlmigrator.MigrationFileEntry{ 130 Number: i, 131 Name: "create_users_table", 132 Committed: true, 133 }) 134 } 135 136 return migrationEntry 137 }() 138 139 for i, migration := range migrations { 140 assert.Equal(t, expected[i].Name, migration.Name) 141 assert.Equal(t, expected[i].Number, migration.Number) 142 assert.Equal(t, expected[i].Committed, migration.Committed) 143 } 144 145 }) 146 }