本站源代码
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.

280 lines
8.7KB

  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/gitea/modules/structs"
  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 TestGetLabelInRepoByNames(t *testing.T) {
  69. assert.NoError(t, PrepareTestDatabase())
  70. labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2"})
  71. assert.NoError(t, err)
  72. assert.Len(t, labelIDs, 2)
  73. assert.Equal(t, int64(1), labelIDs[0])
  74. assert.Equal(t, int64(2), labelIDs[1])
  75. }
  76. func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) {
  77. assert.NoError(t, PrepareTestDatabase())
  78. // label3 doesn't exists.. See labels.yml
  79. labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"})
  80. assert.NoError(t, err)
  81. assert.Len(t, labelIDs, 2)
  82. assert.Equal(t, int64(1), labelIDs[0])
  83. assert.Equal(t, int64(2), labelIDs[1])
  84. assert.NoError(t, err)
  85. }
  86. func TestGetLabelInRepoByID(t *testing.T) {
  87. assert.NoError(t, PrepareTestDatabase())
  88. label, err := GetLabelInRepoByID(1, 1)
  89. assert.NoError(t, err)
  90. assert.EqualValues(t, 1, label.ID)
  91. _, err = GetLabelInRepoByID(1, -1)
  92. assert.True(t, IsErrLabelNotExist(err))
  93. _, err = GetLabelInRepoByID(NonexistentID, NonexistentID)
  94. assert.True(t, IsErrLabelNotExist(err))
  95. }
  96. func TestGetLabelsInRepoByIDs(t *testing.T) {
  97. assert.NoError(t, PrepareTestDatabase())
  98. labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, NonexistentID})
  99. assert.NoError(t, err)
  100. if assert.Len(t, labels, 2) {
  101. assert.EqualValues(t, 1, labels[0].ID)
  102. assert.EqualValues(t, 2, labels[1].ID)
  103. }
  104. }
  105. func TestGetLabelsByRepoID(t *testing.T) {
  106. assert.NoError(t, PrepareTestDatabase())
  107. testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) {
  108. labels, err := GetLabelsByRepoID(repoID, sortType)
  109. assert.NoError(t, err)
  110. assert.Len(t, labels, len(expectedIssueIDs))
  111. for i, label := range labels {
  112. assert.EqualValues(t, expectedIssueIDs[i], label.ID)
  113. }
  114. }
  115. testSuccess(1, "leastissues", []int64{2, 1})
  116. testSuccess(1, "mostissues", []int64{1, 2})
  117. testSuccess(1, "reversealphabetically", []int64{2, 1})
  118. testSuccess(1, "default", []int64{1, 2})
  119. }
  120. func TestGetLabelsByIssueID(t *testing.T) {
  121. assert.NoError(t, PrepareTestDatabase())
  122. labels, err := GetLabelsByIssueID(1)
  123. assert.NoError(t, err)
  124. if assert.Len(t, labels, 1) {
  125. assert.EqualValues(t, 1, labels[0].ID)
  126. }
  127. labels, err = GetLabelsByIssueID(NonexistentID)
  128. assert.NoError(t, err)
  129. assert.Len(t, labels, 0)
  130. }
  131. func TestUpdateLabel(t *testing.T) {
  132. assert.NoError(t, PrepareTestDatabase())
  133. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  134. label.Color = "#ffff00"
  135. label.Name = "newLabelName"
  136. assert.NoError(t, UpdateLabel(label))
  137. newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  138. assert.Equal(t, *label, *newLabel)
  139. CheckConsistencyFor(t, &Label{}, &Repository{})
  140. }
  141. func TestDeleteLabel(t *testing.T) {
  142. assert.NoError(t, PrepareTestDatabase())
  143. label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  144. assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
  145. AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
  146. assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
  147. AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
  148. assert.NoError(t, DeleteLabel(NonexistentID, NonexistentID))
  149. CheckConsistencyFor(t, &Label{}, &Repository{})
  150. }
  151. func TestHasIssueLabel(t *testing.T) {
  152. assert.NoError(t, PrepareTestDatabase())
  153. assert.True(t, HasIssueLabel(1, 1))
  154. assert.False(t, HasIssueLabel(1, 2))
  155. assert.False(t, HasIssueLabel(NonexistentID, NonexistentID))
  156. }
  157. func TestNewIssueLabel(t *testing.T) {
  158. assert.NoError(t, PrepareTestDatabase())
  159. label := AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  160. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  161. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  162. // add new IssueLabel
  163. prevNumIssues := label.NumIssues
  164. assert.NoError(t, NewIssueLabel(issue, label, doer))
  165. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  166. AssertExistsAndLoadBean(t, &Comment{
  167. Type: CommentTypeLabel,
  168. PosterID: doer.ID,
  169. IssueID: issue.ID,
  170. LabelID: label.ID,
  171. Content: "1",
  172. })
  173. label = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  174. assert.EqualValues(t, prevNumIssues+1, label.NumIssues)
  175. // re-add existing IssueLabel
  176. assert.NoError(t, NewIssueLabel(issue, label, doer))
  177. CheckConsistencyFor(t, &Issue{}, &Label{})
  178. }
  179. func TestNewIssueLabels(t *testing.T) {
  180. assert.NoError(t, PrepareTestDatabase())
  181. label1 := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  182. label2 := AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  183. issue := AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue)
  184. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  185. assert.NoError(t, NewIssueLabels(issue, []*Label{label1, label2}, doer))
  186. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
  187. AssertExistsAndLoadBean(t, &Comment{
  188. Type: CommentTypeLabel,
  189. PosterID: doer.ID,
  190. IssueID: issue.ID,
  191. LabelID: label1.ID,
  192. Content: "1",
  193. })
  194. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
  195. label1 = AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
  196. assert.EqualValues(t, 3, label1.NumIssues)
  197. assert.EqualValues(t, 1, label1.NumClosedIssues)
  198. label2 = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
  199. assert.EqualValues(t, 1, label2.NumIssues)
  200. assert.EqualValues(t, 1, label2.NumClosedIssues)
  201. // corner case: test empty slice
  202. assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer))
  203. CheckConsistencyFor(t, &Issue{}, &Label{})
  204. }
  205. func TestDeleteIssueLabel(t *testing.T) {
  206. assert.NoError(t, PrepareTestDatabase())
  207. testSuccess := func(labelID, issueID, doerID int64) {
  208. label := AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  209. issue := AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue)
  210. doer := AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User)
  211. expectedNumIssues := label.NumIssues
  212. expectedNumClosedIssues := label.NumClosedIssues
  213. if BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) {
  214. expectedNumIssues--
  215. if issue.IsClosed {
  216. expectedNumClosedIssues--
  217. }
  218. }
  219. assert.NoError(t, DeleteIssueLabel(issue, label, doer))
  220. AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID})
  221. AssertExistsAndLoadBean(t, &Comment{
  222. Type: CommentTypeLabel,
  223. PosterID: doerID,
  224. IssueID: issueID,
  225. LabelID: labelID,
  226. }, `content=""`)
  227. label = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  228. assert.EqualValues(t, expectedNumIssues, label.NumIssues)
  229. assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues)
  230. }
  231. testSuccess(1, 1, 2)
  232. testSuccess(2, 5, 2)
  233. testSuccess(1, 1, 2) // delete non-existent IssueLabel
  234. CheckConsistencyFor(t, &Issue{}, &Label{})
  235. }
上海开阖软件有限公司 沪ICP备12045867号-1