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.

252 lines
7.9 KiB

  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. assert.Len(t, labels, 2)
  83. assert.EqualValues(t, 1, labels[0].ID)
  84. assert.EqualValues(t, 2, labels[1].ID)
  85. }
  86. func TestGetLabelsByRepoID(t *testing.T) {
  87. assert.NoError(t, PrepareTestDatabase())
  88. testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) {
  89. labels, err := GetLabelsByRepoID(repoID, sortType)
  90. assert.NoError(t, err)
  91. assert.Len(t, labels, len(expectedIssueIDs))
  92. for i, label := range labels {
  93. assert.EqualValues(t, expectedIssueIDs[i], label.ID)
  94. }
  95. }
  96. testSuccess(1, "leastissues", []int64{2, 1})
  97. testSuccess(1, "mostissues", []int64{1, 2})
  98. testSuccess(1, "reversealphabetically", []int64{2, 1})
  99. testSuccess(1, "default", []int64{1, 2})
  100. }
  101. func TestGetLabelsByIssueID(t *testing.T) {
  102. assert.NoError(t, PrepareTestDatabase())
  103. labels, err := GetLabelsByIssueID(1)
  104. assert.NoError(t, err)
  105. assert.Len(t, labels, 1)
  106. assert.EqualValues(t, 1, labels[0].ID)
  107. labels, err = GetLabelsByIssueID(NonexistentID)
  108. assert.NoError(t, err)
  109. assert.Len(t, labels, 0)
  110. }
  111. func TestUpdateLabel(t *testing.T) {
  112. assert.NoError(t, PrepareTestDatabase())
  113. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  114. label.Color = "#ffff00"
  115. label.Name = "newLabelName"
  116. assert.NoError(t, UpdateLabel(label))
  117. newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  118. assert.Equal(t, *label, *newLabel)
  119. CheckConsistencyFor(t, &Label{}, &Repository{})
  120. }
  121. func TestDeleteLabel(t *testing.T) {
  122. assert.NoError(t, PrepareTestDatabase())
  123. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  124. assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
  125. AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
  126. assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
  127. AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
  128. assert.NoError(t, DeleteLabel(NonexistentID, NonexistentID))
  129. CheckConsistencyFor(t, &Label{}, &Repository{})
  130. }
  131. func TestHasIssueLabel(t *testing.T) {
  132. assert.NoError(t, PrepareTestDatabase())
  133. assert.True(t, HasIssueLabel(1, 1))
  134. assert.False(t, HasIssueLabel(1, 2))
  135. assert.False(t, HasIssueLabel(NonexistentID, NonexistentID))
  136. }
  137. func TestNewIssueLabel(t *testing.T) {
  138. assert.NoError(t, PrepareTestDatabase())
  139. label := AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  140. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  141. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  142. // add new IssueLabel
  143. prevNumIssues := label.NumIssues
  144. assert.NoError(t, NewIssueLabel(issue, label, doer))
  145. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  146. AssertExistsAndLoadBean(t, &Comment{
  147. Type: CommentTypeLabel,
  148. PosterID: doer.ID,
  149. IssueID: issue.ID,
  150. LabelID: label.ID,
  151. Content: "1",
  152. })
  153. assert.EqualValues(t, prevNumIssues+1, label.NumIssues)
  154. // re-add existing IssueLabel
  155. assert.NoError(t, NewIssueLabel(issue, label, doer))
  156. CheckConsistencyFor(t, &Issue{}, &Label{})
  157. }
  158. func TestNewIssueLabels(t *testing.T) {
  159. assert.NoError(t, PrepareTestDatabase())
  160. label1 := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  161. label2 := AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  162. issue := AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue)
  163. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  164. assert.NoError(t, NewIssueLabels(issue, []*Label{label1, label2}, doer))
  165. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
  166. AssertExistsAndLoadBean(t, &Comment{
  167. Type: CommentTypeLabel,
  168. PosterID: doer.ID,
  169. IssueID: issue.ID,
  170. LabelID: label1.ID,
  171. Content: "1",
  172. })
  173. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
  174. label1 = AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  175. assert.EqualValues(t, 3, label1.NumIssues)
  176. assert.EqualValues(t, 1, label1.NumClosedIssues)
  177. label2 = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  178. assert.EqualValues(t, 1, label2.NumIssues)
  179. assert.EqualValues(t, 1, label2.NumClosedIssues)
  180. // corner case: test empty slice
  181. assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer))
  182. CheckConsistencyFor(t, &Issue{}, &Label{})
  183. }
  184. func TestDeleteIssueLabel(t *testing.T) {
  185. assert.NoError(t, PrepareTestDatabase())
  186. testSuccess := func(labelID, issueID, doerID int64) {
  187. label := AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  188. issue := AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue)
  189. doer := AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User)
  190. expectedNumIssues := label.NumIssues
  191. expectedNumClosedIssues := label.NumClosedIssues
  192. if BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) {
  193. expectedNumIssues--
  194. if issue.IsClosed {
  195. expectedNumClosedIssues--
  196. }
  197. }
  198. assert.NoError(t, DeleteIssueLabel(issue, label, doer))
  199. AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID})
  200. AssertExistsAndLoadBean(t, &Comment{
  201. Type: CommentTypeLabel,
  202. PosterID: doerID,
  203. IssueID: issueID,
  204. LabelID: labelID,
  205. }, `content=""`)
  206. label = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  207. assert.EqualValues(t, expectedNumIssues, label.NumIssues)
  208. assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues)
  209. }
  210. testSuccess(1, 1, 2)
  211. testSuccess(2, 5, 2)
  212. testSuccess(1, 1, 2) // delete non-existent IssueLabel
  213. CheckConsistencyFor(t, &Issue{}, &Label{})
  214. }