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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "testing"
  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 := 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. }