From e5658d1f61415054029b66cd2f703f1f40138460 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 18 Oct 2025 10:43:39 -0400 Subject: Add support for migration dependencies --- internal/services/migration_executor_test.go | 230 +++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 internal/services/migration_executor_test.go (limited to 'internal/services/migration_executor_test.go') 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) +} -- cgit v1.2.3