code.gitea.io/gitea@v1.21.7/models/migrations/v1_14/v176_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package v1_14 //nolint 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/migrations/base" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func Test_RemoveInvalidLabels(t *testing.T) { 15 // Models used by the migration 16 type Comment struct { 17 ID int64 `xorm:"pk autoincr"` 18 Type int `xorm:"INDEX"` 19 IssueID int64 `xorm:"INDEX"` 20 LabelID int64 21 ShouldRemain bool // <- Flag for testing the migration 22 } 23 24 type Issue struct { 25 ID int64 `xorm:"pk autoincr"` 26 RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"` 27 Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository. 28 } 29 30 type Repository struct { 31 ID int64 `xorm:"pk autoincr"` 32 OwnerID int64 `xorm:"UNIQUE(s) index"` 33 LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"` 34 } 35 36 type Label struct { 37 ID int64 `xorm:"pk autoincr"` 38 RepoID int64 `xorm:"INDEX"` 39 OrgID int64 `xorm:"INDEX"` 40 } 41 42 type IssueLabel struct { 43 ID int64 `xorm:"pk autoincr"` 44 IssueID int64 `xorm:"UNIQUE(s)"` 45 LabelID int64 `xorm:"UNIQUE(s)"` 46 ShouldRemain bool // <- Flag for testing the migration 47 } 48 49 // load and prepare the test database 50 x, deferable := base.PrepareTestEnv(t, 0, new(Comment), new(Issue), new(Repository), new(IssueLabel), new(Label)) 51 if x == nil || t.Failed() { 52 defer deferable() 53 return 54 } 55 defer deferable() 56 57 var issueLabels []*IssueLabel 58 ilPreMigration := map[int64]*IssueLabel{} 59 ilPostMigration := map[int64]*IssueLabel{} 60 61 var comments []*Comment 62 comPreMigration := map[int64]*Comment{} 63 comPostMigration := map[int64]*Comment{} 64 65 // Get pre migration values 66 if err := x.Find(&issueLabels); err != nil { 67 t.Errorf("Unable to find issueLabels: %v", err) 68 return 69 } 70 for _, issueLabel := range issueLabels { 71 ilPreMigration[issueLabel.ID] = issueLabel 72 } 73 if err := x.Find(&comments); err != nil { 74 t.Errorf("Unable to find comments: %v", err) 75 return 76 } 77 for _, comment := range comments { 78 comPreMigration[comment.ID] = comment 79 } 80 81 // Run the migration 82 if err := RemoveInvalidLabels(x); err != nil { 83 t.Errorf("unable to RemoveInvalidLabels: %v", err) 84 } 85 86 // Get the post migration values 87 issueLabels = issueLabels[:0] 88 if err := x.Find(&issueLabels); err != nil { 89 t.Errorf("Unable to find issueLabels: %v", err) 90 return 91 } 92 for _, issueLabel := range issueLabels { 93 ilPostMigration[issueLabel.ID] = issueLabel 94 } 95 comments = comments[:0] 96 if err := x.Find(&comments); err != nil { 97 t.Errorf("Unable to find comments: %v", err) 98 return 99 } 100 for _, comment := range comments { 101 comPostMigration[comment.ID] = comment 102 } 103 104 // Finally test results of the migration 105 for id, comment := range comPreMigration { 106 post, ok := comPostMigration[id] 107 if ok { 108 if !comment.ShouldRemain { 109 t.Errorf("Comment[%d] remained but should have been deleted", id) 110 } 111 assert.Equal(t, comment, post) 112 } else if comment.ShouldRemain { 113 t.Errorf("Comment[%d] was deleted but should have remained", id) 114 } 115 } 116 117 for id, il := range ilPreMigration { 118 post, ok := ilPostMigration[id] 119 if ok { 120 if !il.ShouldRemain { 121 t.Errorf("IssueLabel[%d] remained but should have been deleted", id) 122 } 123 assert.Equal(t, il, post) 124 } else if il.ShouldRemain { 125 t.Errorf("IssueLabel[%d] was deleted but should have remained", id) 126 } 127 } 128 }