Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

migrate_test.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2022 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 models
  5. import (
  6. "strconv"
  7. "testing"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/foreignreference"
  10. issues_model "code.gitea.io/gitea/models/issues"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestMigrate_InsertMilestones(t *testing.T) {
  17. assert.NoError(t, unittest.PrepareTestDatabase())
  18. reponame := "repo1"
  19. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
  20. name := "milestonetest1"
  21. ms := &issues_model.Milestone{
  22. RepoID: repo.ID,
  23. Name: name,
  24. }
  25. err := InsertMilestones(ms)
  26. assert.NoError(t, err)
  27. unittest.AssertExistsAndLoadBean(t, ms)
  28. repoModified := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}).(*repo_model.Repository)
  29. assert.EqualValues(t, repo.NumMilestones+1, repoModified.NumMilestones)
  30. unittest.CheckConsistencyFor(t, &issues_model.Milestone{})
  31. }
  32. func assertCreateIssues(t *testing.T, isPull bool) {
  33. assert.NoError(t, unittest.PrepareTestDatabase())
  34. reponame := "repo1"
  35. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
  36. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  37. label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  38. milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}).(*issues_model.Milestone)
  39. assert.EqualValues(t, milestone.ID, 1)
  40. reaction := &issues_model.Reaction{
  41. Type: "heart",
  42. UserID: owner.ID,
  43. }
  44. foreignIndex := int64(12345)
  45. title := "issuetitle1"
  46. is := &Issue{
  47. RepoID: repo.ID,
  48. MilestoneID: milestone.ID,
  49. Repo: repo,
  50. Title: title,
  51. Content: "issuecontent1",
  52. IsPull: isPull,
  53. PosterID: owner.ID,
  54. Poster: owner,
  55. IsClosed: true,
  56. Labels: []*Label{label},
  57. Reactions: []*issues_model.Reaction{reaction},
  58. ForeignReference: &foreignreference.ForeignReference{
  59. ForeignIndex: strconv.FormatInt(foreignIndex, 10),
  60. RepoID: repo.ID,
  61. Type: foreignreference.TypeIssue,
  62. },
  63. }
  64. err := InsertIssues(is)
  65. assert.NoError(t, err)
  66. i := unittest.AssertExistsAndLoadBean(t, &Issue{Title: title}).(*Issue)
  67. assert.Nil(t, i.ForeignReference)
  68. err = i.LoadAttributes()
  69. assert.NoError(t, err)
  70. assert.EqualValues(t, strconv.FormatInt(foreignIndex, 10), i.ForeignReference.ForeignIndex)
  71. unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: owner.ID, IssueID: i.ID})
  72. }
  73. func TestMigrate_CreateIssuesIsPullFalse(t *testing.T) {
  74. assertCreateIssues(t, false)
  75. }
  76. func TestMigrate_CreateIssuesIsPullTrue(t *testing.T) {
  77. assertCreateIssues(t, true)
  78. }
  79. func TestMigrate_InsertIssueComments(t *testing.T) {
  80. assert.NoError(t, unittest.PrepareTestDatabase())
  81. issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  82. _ = issue.LoadRepo(db.DefaultContext)
  83. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID}).(*user_model.User)
  84. reaction := &issues_model.Reaction{
  85. Type: "heart",
  86. UserID: owner.ID,
  87. }
  88. comment := &Comment{
  89. PosterID: owner.ID,
  90. Poster: owner,
  91. IssueID: issue.ID,
  92. Issue: issue,
  93. Reactions: []*issues_model.Reaction{reaction},
  94. }
  95. err := InsertIssueComments([]*Comment{comment})
  96. assert.NoError(t, err)
  97. issueModified := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  98. assert.EqualValues(t, issue.NumComments+1, issueModified.NumComments)
  99. unittest.CheckConsistencyFor(t, &Issue{})
  100. }
  101. func TestMigrate_InsertPullRequests(t *testing.T) {
  102. assert.NoError(t, unittest.PrepareTestDatabase())
  103. reponame := "repo1"
  104. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
  105. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  106. i := &Issue{
  107. RepoID: repo.ID,
  108. Repo: repo,
  109. Title: "title1",
  110. Content: "issuecontent1",
  111. IsPull: true,
  112. PosterID: owner.ID,
  113. Poster: owner,
  114. }
  115. p := &PullRequest{
  116. Issue: i,
  117. }
  118. err := InsertPullRequests(p)
  119. assert.NoError(t, err)
  120. _ = unittest.AssertExistsAndLoadBean(t, &PullRequest{IssueID: i.ID}).(*PullRequest)
  121. unittest.CheckConsistencyFor(t, &Issue{}, &PullRequest{})
  122. }
  123. func TestMigrate_InsertReleases(t *testing.T) {
  124. assert.NoError(t, unittest.PrepareTestDatabase())
  125. a := &repo_model.Attachment{
  126. UUID: "a0eebc91-9c0c-4ef7-bb6e-6bb9bd380a12",
  127. }
  128. r := &Release{
  129. Attachments: []*repo_model.Attachment{a},
  130. }
  131. err := InsertReleases(r)
  132. assert.NoError(t, err)
  133. }