Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_test.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2017 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. "sort"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestIssue_ReplaceLabels(t *testing.T) {
  12. assert.NoError(t, PrepareTestDatabase())
  13. testSuccess := func(issueID int64, labelIDs []int64) {
  14. issue := AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue)
  15. repo := AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository)
  16. doer := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  17. labels := make([]*Label, len(labelIDs))
  18. for i, labelID := range labelIDs {
  19. labels[i] = AssertExistsAndLoadBean(t, &Label{ID: labelID, RepoID: repo.ID}).(*Label)
  20. }
  21. assert.NoError(t, issue.ReplaceLabels(labels, doer))
  22. AssertCount(t, &IssueLabel{IssueID: issueID}, len(labelIDs))
  23. for _, labelID := range labelIDs {
  24. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID})
  25. }
  26. }
  27. testSuccess(1, []int64{2})
  28. testSuccess(1, []int64{1, 2})
  29. testSuccess(1, []int64{})
  30. }
  31. func TestIssueAPIURL(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  34. err := issue.LoadAttributes()
  35. assert.NoError(t, err)
  36. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL())
  37. }
  38. func TestGetIssuesByIDs(t *testing.T) {
  39. assert.NoError(t, PrepareTestDatabase())
  40. testSuccess := func(expectedIssueIDs []int64, nonExistentIssueIDs []int64) {
  41. issues, err := GetIssuesByIDs(append(expectedIssueIDs, nonExistentIssueIDs...))
  42. assert.NoError(t, err)
  43. actualIssueIDs := make([]int64, len(issues))
  44. for i, issue := range issues {
  45. actualIssueIDs[i] = issue.ID
  46. }
  47. assert.Equal(t, expectedIssueIDs, actualIssueIDs)
  48. }
  49. testSuccess([]int64{1, 2, 3}, []int64{})
  50. testSuccess([]int64{1, 2, 3}, []int64{NonexistentID})
  51. }
  52. func TestGetParticipantsByIssueID(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. checkParticipants := func(issueID int64, userIDs []int) {
  55. participants, err := GetParticipantsByIssueID(issueID)
  56. if assert.NoError(t, err) {
  57. participantsIDs := make([]int, len(participants))
  58. for i, u := range participants {
  59. participantsIDs[i] = int(u.ID)
  60. }
  61. sort.Ints(participantsIDs)
  62. sort.Ints(userIDs)
  63. assert.Equal(t, userIDs, participantsIDs)
  64. }
  65. }
  66. // User 1 is issue1 poster (see fixtures/issue.yml)
  67. // User 2 only labeled issue1 (see fixtures/comment.yml)
  68. // Users 3 and 5 made actual comments (see fixtures/comment.yml)
  69. // User 3 is inactive, thus not active participant
  70. checkParticipants(1, []int{5})
  71. }
  72. func TestIssue_AddLabel(t *testing.T) {
  73. var tests = []struct {
  74. issueID int64
  75. labelID int64
  76. doerID int64
  77. }{
  78. {1, 2, 2}, // non-pull-request, not-already-added label
  79. {1, 1, 2}, // non-pull-request, already-added label
  80. {2, 2, 2}, // pull-request, not-already-added label
  81. {2, 1, 2}, // pull-request, already-added label
  82. }
  83. for _, test := range tests {
  84. assert.NoError(t, PrepareTestDatabase())
  85. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  86. label := AssertExistsAndLoadBean(t, &Label{ID: test.labelID}).(*Label)
  87. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  88. assert.NoError(t, issue.AddLabel(doer, label))
  89. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: test.labelID})
  90. }
  91. }
  92. func TestIssue_AddLabels(t *testing.T) {
  93. var tests = []struct {
  94. issueID int64
  95. labelIDs []int64
  96. doerID int64
  97. }{
  98. {1, []int64{1, 2}, 2}, // non-pull-request
  99. {1, []int64{}, 2}, // non-pull-request, empty
  100. {2, []int64{1, 2}, 2}, // pull-request
  101. {2, []int64{}, 1}, // pull-request, empty
  102. }
  103. for _, test := range tests {
  104. assert.NoError(t, PrepareTestDatabase())
  105. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  106. labels := make([]*Label, len(test.labelIDs))
  107. for i, labelID := range test.labelIDs {
  108. labels[i] = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  109. }
  110. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  111. assert.NoError(t, issue.AddLabels(doer, labels))
  112. for _, labelID := range test.labelIDs {
  113. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: labelID})
  114. }
  115. }
  116. }
  117. func TestIssue_ClearLabels(t *testing.T) {
  118. var tests = []struct {
  119. issueID int64
  120. doerID int64
  121. }{
  122. {1, 2}, // non-pull-request, has labels
  123. {2, 2}, // pull-request, has labels
  124. {3, 2}, // pull-request, has no labels
  125. }
  126. for _, test := range tests {
  127. assert.NoError(t, PrepareTestDatabase())
  128. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  129. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  130. assert.NoError(t, issue.ClearLabels(doer))
  131. AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
  132. }
  133. }
  134. func TestUpdateIssueCols(t *testing.T) {
  135. assert.NoError(t, PrepareTestDatabase())
  136. issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
  137. const newTitle = "New Title for unit test"
  138. issue.Title = newTitle
  139. prevContent := issue.Content
  140. issue.Content = "This should have no effect"
  141. now := time.Now().Unix()
  142. assert.NoError(t, UpdateIssueCols(issue, "name"))
  143. then := time.Now().Unix()
  144. updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
  145. assert.EqualValues(t, newTitle, updatedIssue.Title)
  146. assert.EqualValues(t, prevContent, updatedIssue.Content)
  147. AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
  148. }