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

issue_test.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. checkParticipants(1, []int{3, 5})
  70. }
  71. func TestIssue_AddLabel(t *testing.T) {
  72. var tests = []struct {
  73. issueID int64
  74. labelID int64
  75. doerID int64
  76. }{
  77. {1, 2, 2}, // non-pull-request, not-already-added label
  78. {1, 1, 2}, // non-pull-request, already-added label
  79. {2, 2, 2}, // pull-request, not-already-added label
  80. {2, 1, 2}, // pull-request, already-added label
  81. }
  82. for _, test := range tests {
  83. assert.NoError(t, PrepareTestDatabase())
  84. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  85. label := AssertExistsAndLoadBean(t, &Label{ID: test.labelID}).(*Label)
  86. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  87. assert.NoError(t, issue.AddLabel(doer, label))
  88. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: test.labelID})
  89. }
  90. }
  91. func TestIssue_AddLabels(t *testing.T) {
  92. var tests = []struct {
  93. issueID int64
  94. labelIDs []int64
  95. doerID int64
  96. }{
  97. {1, []int64{1, 2}, 2}, // non-pull-request
  98. {1, []int64{}, 2}, // non-pull-request, empty
  99. {2, []int64{1, 2}, 2}, // pull-request
  100. {2, []int64{}, 1}, // pull-request, empty
  101. }
  102. for _, test := range tests {
  103. assert.NoError(t, PrepareTestDatabase())
  104. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  105. labels := make([]*Label, len(test.labelIDs))
  106. for i, labelID := range test.labelIDs {
  107. labels[i] = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  108. }
  109. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  110. assert.NoError(t, issue.AddLabels(doer, labels))
  111. for _, labelID := range test.labelIDs {
  112. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: labelID})
  113. }
  114. }
  115. }
  116. func TestIssue_ClearLabels(t *testing.T) {
  117. var tests = []struct {
  118. issueID int64
  119. doerID int64
  120. }{
  121. {1, 2}, // non-pull-request, has labels
  122. {2, 2}, // pull-request, has labels
  123. {3, 2}, // pull-request, has no labels
  124. }
  125. for _, test := range tests {
  126. assert.NoError(t, PrepareTestDatabase())
  127. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  128. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  129. assert.NoError(t, issue.ClearLabels(doer))
  130. AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
  131. }
  132. }
  133. func TestUpdateIssueCols(t *testing.T) {
  134. assert.NoError(t, PrepareTestDatabase())
  135. issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
  136. const newTitle = "New Title for unit test"
  137. issue.Title = newTitle
  138. prevContent := issue.Content
  139. issue.Content = "This should have no effect"
  140. now := time.Now().Unix()
  141. assert.NoError(t, UpdateIssueCols(issue, "name"))
  142. then := time.Now().Unix()
  143. updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
  144. assert.EqualValues(t, newTitle, updatedIssue.Title)
  145. assert.EqualValues(t, prevContent, updatedIssue.Content)
  146. AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
  147. }