github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/migrations/migrations_test.go (about) 1 // Copyright (c) 2018-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package migrations 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/xzl8028/xenia-server/model" 13 ) 14 15 func TestGetMigrationState(t *testing.T) { 16 th := Setup() 17 defer th.TearDown() 18 19 migrationKey := model.NewId() 20 21 th.DeleteAllJobsByTypeAndMigrationKey(model.JOB_TYPE_MIGRATIONS, migrationKey) 22 23 // Test with no job yet. 24 state, job, err := GetMigrationState(migrationKey, th.App.Srv.Store) 25 assert.Nil(t, err) 26 assert.Nil(t, job) 27 assert.Equal(t, "unscheduled", state) 28 29 // Test with the system table showing the migration as done. 30 system := model.System{ 31 Name: migrationKey, 32 Value: "true", 33 } 34 err = th.App.Srv.Store.System().Save(&system) 35 assert.Nil(t, err) 36 37 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 38 assert.Nil(t, err) 39 assert.Nil(t, job) 40 assert.Equal(t, "completed", state) 41 42 _, err = th.App.Srv.Store.System().PermanentDeleteByName(migrationKey) 43 assert.Nil(t, err) 44 45 // Test with a job scheduled in "pending" state. 46 j1 := &model.Job{ 47 Id: model.NewId(), 48 CreateAt: model.GetMillis(), 49 Data: map[string]string{ 50 JOB_DATA_KEY_MIGRATION: migrationKey, 51 }, 52 Status: model.JOB_STATUS_PENDING, 53 Type: model.JOB_TYPE_MIGRATIONS, 54 } 55 56 j1, err = th.App.Srv.Store.Job().Save(j1) 57 require.Nil(t, err) 58 59 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 60 assert.Nil(t, err) 61 assert.Equal(t, j1.Id, job.Id) 62 assert.Equal(t, "in_progress", state) 63 64 // Test with a job scheduled in "in progress" state. 65 j2 := &model.Job{ 66 Id: model.NewId(), 67 CreateAt: j1.CreateAt + 1, 68 Data: map[string]string{ 69 JOB_DATA_KEY_MIGRATION: migrationKey, 70 }, 71 Status: model.JOB_STATUS_IN_PROGRESS, 72 Type: model.JOB_TYPE_MIGRATIONS, 73 } 74 75 j2, err = th.App.Srv.Store.Job().Save(j2) 76 require.Nil(t, err) 77 78 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 79 assert.Nil(t, err) 80 assert.Equal(t, j2.Id, job.Id) 81 assert.Equal(t, "in_progress", state) 82 83 // Test with a job scheduled in "error" state. 84 j3 := &model.Job{ 85 Id: model.NewId(), 86 CreateAt: j2.CreateAt + 1, 87 Data: map[string]string{ 88 JOB_DATA_KEY_MIGRATION: migrationKey, 89 }, 90 Status: model.JOB_STATUS_ERROR, 91 Type: model.JOB_TYPE_MIGRATIONS, 92 } 93 94 j3, err = th.App.Srv.Store.Job().Save(j3) 95 require.Nil(t, err) 96 97 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 98 assert.Nil(t, err) 99 assert.Equal(t, j3.Id, job.Id) 100 assert.Equal(t, "unscheduled", state) 101 }