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.

issue_test.go 4.7KB

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