You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v176_test.go 3.3KB

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