From ca606d508b6b9592841e08b19175775146e42e3e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 18 Aug 2024 18:55:12 -0400 Subject: Add a few more tests to the migration service --- .gitignore | 1 + internal/services/migration_test.go | 108 ++++++++++++++++++++++++++++++++++++ tools/test.sh | 2 +- 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b6760b7..94723d2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ # Test output /cover.out +/cover.html diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 6828d28..1cfc3ba 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -662,6 +662,60 @@ func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { assert.ErrorIs(t, err, expectedErr) } +func TestApplyMigration_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}, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyMigration_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}, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ @@ -944,6 +998,60 @@ func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { assert.ErrorIs(t, err, expectedErr) } +func TestRevertMigration_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}, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertMigration_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}, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ diff --git a/tools/test.sh b/tools/test.sh index 94826a9..0e9292e 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -12,5 +12,5 @@ if [[ ! " ${ALLOWED_DB[@]} " =~ " $1 " ]]; then exit 1 fi -env $(cat "$BASE_DIR/.env.$1" | xargs) go test -p 1 -tags "$1" -cover ./... +env $(cat "$BASE_DIR/.env.$1" | xargs) go test -p 1 -tags "$1" -cover -coverprofile cover.out ./... -- cgit v1.2.3