aboutsummaryrefslogtreecommitdiffstats
path: root/internal/services/migration_executor_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/services/migration_executor_test.go')
-rw-r--r--internal/services/migration_executor_test.go230
1 files changed, 230 insertions, 0 deletions
diff --git a/internal/services/migration_executor_test.go b/internal/services/migration_executor_test.go
new file mode 100644
index 0000000..00e7402
--- /dev/null
+++ b/internal/services/migration_executor_test.go
@@ -0,0 +1,230 @@
+package services
+
+import (
+ "errors"
+ "testing"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/bolttest"
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+ "git.sr.ht/~eugenetriguba/bolt/internal/sqlparse"
+ "git.sr.ht/~eugenetriguba/checkmate/assert"
+)
+
+func TestExecutorApply_WithTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE tmp(id INT PRIMARY KEY);",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 0)
+}
+
+func TestExecutorApply_WithoutTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE tmp(id INT PRIMARY KEY);",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyReturnValue: bolttest.ApplyReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 1)
+}
+
+func TestExecutorApply_ReadUpgradeScriptErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{},
+ Err: expectedErr,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorApply_ApplyWithTxErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: true,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorApply_ApplyErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: false,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorRevert_WithTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "DROP TABLE tmp;",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1)
+ assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
+}
+
+func TestExecutorRevert_WithoutTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "DROP TABLE tmp;",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertReturnValue: bolttest.RevertReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
+ assert.Equal(t, migrationDbRepo.RevertCallCount, 1)
+}
+
+func TestExecutorRevert_ReadDowngradeScriptErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{},
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorRevert_RevertWithTxErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: true,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorRevert_RevertErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: false,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}