aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-03-02 07:35:21 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-03-02 07:35:21 -0500
commit55d3999d9efdeb0a87959e93d473cb64c8ba8c48 (patch)
tree18f733eaa298501095ce0be15a3f28de11ff5e9c
parentAdd service tests (diff)
downloadcli-55d3999d9efdeb0a87959e93d473cb64c8ba8c48.zip
Add proper tests for ApplyAllMigrations and ApplyUpToVersion
-rw-r--r--internal/services/migration.go2
-rw-r--r--internal/services/migration_test.go202
2 files changed, 204 insertions, 0 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go
index 48859d6..c0d043b 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -105,6 +105,8 @@ func (ms MigrationService) ApplyUpToVersion(version string) error {
return fmt.Errorf("migration with version %s does not exist", version)
}
if targetMigration.Applied {
+ // Assumption: If the target migration is applied, all migrations before
+ // it must also be applied.
return fmt.Errorf(
"migration with version %s is already applied, nothing to apply",
version,
diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go
index 73045ef..e3d3244 100644
--- a/internal/services/migration_test.go
+++ b/internal/services/migration_test.go
@@ -593,3 +593,205 @@ func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) {
assert.ErrorIs(t, err, expectedErr)
}
+
+func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
+ }
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyAllMigrations()
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ "002": {Version: "002", Applied: true},
+ }
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyAllMigrations()
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 0)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0)
+}
+
+func TestApplyAllMigrations_ApplyMigrationErr(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ }
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyAllMigrations()
+
+ assert.ErrorIs(t, err, expectedErr)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 0)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0)
+}
+
+func TestApplyUpToVersion_ListMigrationsErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
+ }
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyUpToVersion("001")
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ }
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ }
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyUpToVersion("002")
+
+ assert.ErrorContains(t, err, "migration with version 002 does not exist")
+}
+
+func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ }
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ }
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyUpToVersion("001")
+
+ assert.ErrorContains(t, err, "migration with version 001 is already applied")
+}
+
+func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ }
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Err: expectedErr,
+ },
+ }
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyUpToVersion("001")
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestApplyUpToVersion_AppliesMigrations(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ "002": {Version: "002", Applied: false},
+ "003": {Version: "003", Applied: false},
+ }
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ }
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyUpToVersion("002")
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2)
+}