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

issue_label_test.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. "html/template"
  7. "testing"
  8. api "code.gitea.io/sdk/gitea"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // TODO TestGetLabelTemplateFile
  12. func TestLabel_APIFormat(t *testing.T) {
  13. assert.NoError(t, PrepareTestDatabase())
  14. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  15. assert.Equal(t, api.Label{
  16. ID: label.ID,
  17. Name: label.Name,
  18. Color: "abcdef",
  19. }, *label.APIFormat())
  20. }
  21. func TestLabel_CalOpenIssues(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  24. label.CalOpenIssues()
  25. assert.EqualValues(t, 2, label.NumOpenIssues)
  26. }
  27. func TestLabel_ForegroundColor(t *testing.T) {
  28. assert.NoError(t, PrepareTestDatabase())
  29. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  30. assert.Equal(t, template.CSS("#000"), label.ForegroundColor())
  31. label = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  32. assert.Equal(t, template.CSS("#fff"), label.ForegroundColor())
  33. }
  34. func TestNewLabels(t *testing.T) {
  35. assert.NoError(t, PrepareTestDatabase())
  36. labels := []*Label{
  37. {RepoID: 2, Name: "labelName2", Color: "#123456"},
  38. {RepoID: 3, Name: "labelName3", Color: "#234567"},
  39. }
  40. for _, label := range labels {
  41. AssertNotExistsBean(t, label)
  42. }
  43. assert.NoError(t, NewLabels(labels...))
  44. for _, label := range labels {
  45. AssertExistsAndLoadBean(t, label, Cond("id = ?", label.ID))
  46. }
  47. CheckConsistencyFor(t, &Label{}, &Repository{})
  48. }
  49. func TestGetLabelByID(t *testing.T) {
  50. assert.NoError(t, PrepareTestDatabase())
  51. label, err := GetLabelByID(1)
  52. assert.NoError(t, err)
  53. assert.EqualValues(t, 1, label.ID)
  54. _, err = GetLabelByID(NonexistentID)
  55. assert.True(t, IsErrLabelNotExist(err))
  56. }
  57. func TestGetLabelInRepoByName(t *testing.T) {
  58. assert.NoError(t, PrepareTestDatabase())
  59. label, err := GetLabelInRepoByName(1, "label1")
  60. assert.NoError(t, err)
  61. assert.EqualValues(t, 1, label.ID)
  62. assert.Equal(t, "label1", label.Name)
  63. _, err = GetLabelInRepoByName(1, "")
  64. assert.True(t, IsErrLabelNotExist(err))
  65. _, err = GetLabelInRepoByName(NonexistentID, "nonexistent")
  66. assert.True(t, IsErrLabelNotExist(err))
  67. }
  68. func TestGetLabelInRepoByID(t *testing.T) {
  69. assert.NoError(t, PrepareTestDatabase())
  70. label, err := GetLabelInRepoByID(1, 1)
  71. assert.NoError(t, err)
  72. assert.EqualValues(t, 1, label.ID)
  73. _, err = GetLabelInRepoByID(1, -1)
  74. assert.True(t, IsErrLabelNotExist(err))
  75. _, err = GetLabelInRepoByID(NonexistentID, NonexistentID)
  76. assert.True(t, IsErrLabelNotExist(err))
  77. }
  78. func TestGetLabelsInRepoByIDs(t *testing.T) {
  79. assert.NoError(t, PrepareTestDatabase())
  80. labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, NonexistentID})
  81. assert.NoError(t, err)
  82. if assert.Len(t, labels, 2) {
  83. assert.EqualValues(t, 1, labels[0].ID)
  84. assert.EqualValues(t, 2, labels[1].ID)
  85. }
  86. }
  87. func TestGetLabelsByRepoID(t *testing.T) {
  88. assert.NoError(t, PrepareTestDatabase())
  89. testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) {
  90. labels, err := GetLabelsByRepoID(repoID, sortType)
  91. assert.NoError(t, err)
  92. assert.Len(t, labels, len(expectedIssueIDs))
  93. for i, label := range labels {
  94. assert.EqualValues(t, expectedIssueIDs[i], label.ID)
  95. }
  96. }
  97. testSuccess(1, "leastissues", []int64{2, 1})
  98. testSuccess(1, "mostissues", []int64{1, 2})
  99. testSuccess(1, "reversealphabetically", []int64{2, 1})
  100. testSuccess(1, "default", []int64{1, 2})
  101. }
  102. func TestGetLabelsByIssueID(t *testing.T) {
  103. assert.NoError(t, PrepareTestDatabase())
  104. labels, err := GetLabelsByIssueID(1)
  105. assert.NoError(t, err)
  106. if assert.Len(t, labels, 1) {
  107. assert.EqualValues(t, 1, labels[0].ID)
  108. }
  109. labels, err = GetLabelsByIssueID(NonexistentID)
  110. assert.NoError(t, err)
  111. assert.Len(t, labels, 0)
  112. }
  113. func TestUpdateLabel(t *testing.T) {
  114. assert.NoError(t, PrepareTestDatabase())
  115. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  116. label.Color = "#ffff00"
  117. label.Name = "newLabelName"
  118. assert.NoError(t, UpdateLabel(label))
  119. newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  120. assert.Equal(t, *label, *newLabel)
  121. CheckConsistencyFor(t, &Label{}, &Repository{})
  122. }
  123. func TestDeleteLabel(t *testing.T) {
  124. assert.NoError(t, PrepareTestDatabase())
  125. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  126. assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
  127. AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
  128. assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
  129. AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
  130. assert.NoError(t, DeleteLabel(NonexistentID, NonexistentID))
  131. CheckConsistencyFor(t, &Label{}, &Repository{})
  132. }
  133. func TestHasIssueLabel(t *testing.T) {
  134. assert.NoError(t, PrepareTestDatabase())
  135. assert.True(t, HasIssueLabel(1, 1))
  136. assert.False(t, HasIssueLabel(1, 2))
  137. assert.False(t, HasIssueLabel(NonexistentID, NonexistentID))
  138. }
  139. func TestNewIssueLabel(t *testing.T) {
  140. assert.NoError(t, PrepareTestDatabase())
  141. label := AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  142. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  143. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  144. // add new IssueLabel
  145. prevNumIssues := label.NumIssues
  146. assert.NoError(t, NewIssueLabel(issue, label, doer))
  147. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  148. AssertExistsAndLoadBean(t, &Comment{
  149. Type: CommentTypeLabel,
  150. PosterID: doer.ID,
  151. IssueID: issue.ID,
  152. LabelID: label.ID,
  153. Content: "1",
  154. })
  155. assert.EqualValues(t, prevNumIssues+1, label.NumIssues)
  156. // re-add existing IssueLabel
  157. assert.NoError(t, NewIssueLabel(issue, label, doer))
  158. CheckConsistencyFor(t, &Issue{}, &Label{})
  159. }
  160. func TestNewIssueLabels(t *testing.T) {
  161. assert.NoError(t, PrepareTestDatabase())
  162. label1 := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  163. label2 := AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  164. issue := AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue)
  165. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  166. assert.NoError(t, NewIssueLabels(issue, []*Label{label1, label2}, doer))
  167. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
  168. AssertExistsAndLoadBean(t, &Comment{
  169. Type: CommentTypeLabel,
  170. PosterID: doer.ID,
  171. IssueID: issue.ID,
  172. LabelID: label1.ID,
  173. Content: "1",
  174. })
  175. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
  176. label1 = AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  177. assert.EqualValues(t, 3, label1.NumIssues)
  178. assert.EqualValues(t, 1, label1.NumClosedIssues)
  179. label2 = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  180. assert.EqualValues(t, 1, label2.NumIssues)
  181. assert.EqualValues(t, 1, label2.NumClosedIssues)
  182. // corner case: test empty slice
  183. assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer))
  184. CheckConsistencyFor(t, &Issue{}, &Label{})
  185. }
  186. func TestDeleteIssueLabel(t *testing.T) {
  187. assert.NoError(t, PrepareTestDatabase())
  188. testSuccess := func(labelID, issueID, doerID int64) {
  189. label := AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  190. issue := AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue)
  191. doer := AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User)
  192. expectedNumIssues := label.NumIssues
  193. expectedNumClosedIssues := label.NumClosedIssues
  194. if BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) {
  195. expectedNumIssues--
  196. if issue.IsClosed {
  197. expectedNumClosedIssues--
  198. }
  199. }
  200. assert.NoError(t, DeleteIssueLabel(issue, label, doer))
  201. AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID})
  202. AssertExistsAndLoadBean(t, &Comment{
  203. Type: CommentTypeLabel,
  204. PosterID: doerID,
  205. IssueID: issueID,
  206. LabelID: labelID,
  207. }, `content=""`)
  208. label = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  209. assert.EqualValues(t, expectedNumIssues, label.NumIssues)
  210. assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues)
  211. }
  212. testSuccess(1, 1, 2)
  213. testSuccess(2, 5, 2)
  214. testSuccess(1, 1, 2) // delete non-existent IssueLabel
  215. CheckConsistencyFor(t, &Issue{}, &Label{})
  216. }