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.

v177_test.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "code.gitea.io/gitea/modules/timeutil"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func Test_DeleteOrphanedIssueLabels(t *testing.T) {
  11. // Create the models used in the migration
  12. type IssueLabel struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. IssueID int64 `xorm:"UNIQUE(s)"`
  15. LabelID int64 `xorm:"UNIQUE(s)"`
  16. }
  17. type Label struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. RepoID int64 `xorm:"INDEX"`
  20. OrgID int64 `xorm:"INDEX"`
  21. Name string
  22. Description string
  23. Color string `xorm:"VARCHAR(7)"`
  24. NumIssues int
  25. NumClosedIssues int
  26. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  27. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  28. }
  29. // Prepare and load the testing database
  30. x, deferable := base.PrepareTestEnv(t, 0, new(IssueLabel), new(Label))
  31. if x == nil || t.Failed() {
  32. defer deferable()
  33. return
  34. }
  35. defer deferable()
  36. var issueLabels []*IssueLabel
  37. preMigration := map[int64]*IssueLabel{}
  38. postMigration := map[int64]*IssueLabel{}
  39. // Load issue labels that exist in the database pre-migration
  40. if err := x.Find(&issueLabels); err != nil {
  41. assert.NoError(t, err)
  42. return
  43. }
  44. for _, issueLabel := range issueLabels {
  45. preMigration[issueLabel.ID] = issueLabel
  46. }
  47. // Run the migration
  48. if err := DeleteOrphanedIssueLabels(x); err != nil {
  49. assert.NoError(t, err)
  50. return
  51. }
  52. // Load the remaining issue-labels
  53. issueLabels = issueLabels[:0]
  54. if err := x.Find(&issueLabels); err != nil {
  55. assert.NoError(t, err)
  56. return
  57. }
  58. for _, issueLabel := range issueLabels {
  59. postMigration[issueLabel.ID] = issueLabel
  60. }
  61. // Now test what is left
  62. if _, ok := postMigration[2]; ok {
  63. t.Errorf("Orphaned Label[2] survived the migration")
  64. return
  65. }
  66. if _, ok := postMigration[5]; ok {
  67. t.Errorf("Orphaned Label[5] survived the migration")
  68. return
  69. }
  70. for id, post := range postMigration {
  71. pre := preMigration[id]
  72. assert.Equal(t, pre, post, "migration changed issueLabel %d", id)
  73. }
  74. }