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.

281 lines
7.6 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. "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. // User 3 is inactive, thus not active participant
  70. checkParticipants(1, []int{5})
  71. }
  72. func TestIssue_AddLabel(t *testing.T) {
  73. var tests = []struct {
  74. issueID int64
  75. labelID int64
  76. doerID int64
  77. }{
  78. {1, 2, 2}, // non-pull-request, not-already-added label
  79. {1, 1, 2}, // non-pull-request, already-added label
  80. {2, 2, 2}, // pull-request, not-already-added label
  81. {2, 1, 2}, // pull-request, already-added label
  82. }
  83. for _, test := range tests {
  84. assert.NoError(t, PrepareTestDatabase())
  85. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  86. label := AssertExistsAndLoadBean(t, &Label{ID: test.labelID}).(*Label)
  87. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  88. assert.NoError(t, issue.AddLabel(doer, label))
  89. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: test.labelID})
  90. }
  91. }
  92. func TestIssue_AddLabels(t *testing.T) {
  93. var tests = []struct {
  94. issueID int64
  95. labelIDs []int64
  96. doerID int64
  97. }{
  98. {1, []int64{1, 2}, 2}, // non-pull-request
  99. {1, []int64{}, 2}, // non-pull-request, empty
  100. {2, []int64{1, 2}, 2}, // pull-request
  101. {2, []int64{}, 1}, // pull-request, empty
  102. }
  103. for _, test := range tests {
  104. assert.NoError(t, PrepareTestDatabase())
  105. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  106. labels := make([]*Label, len(test.labelIDs))
  107. for i, labelID := range test.labelIDs {
  108. labels[i] = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label)
  109. }
  110. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  111. assert.NoError(t, issue.AddLabels(doer, labels))
  112. for _, labelID := range test.labelIDs {
  113. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: labelID})
  114. }
  115. }
  116. }
  117. func TestIssue_ClearLabels(t *testing.T) {
  118. var tests = []struct {
  119. issueID int64
  120. doerID int64
  121. }{
  122. {1, 2}, // non-pull-request, has labels
  123. {2, 2}, // pull-request, has labels
  124. {3, 2}, // pull-request, has no labels
  125. }
  126. for _, test := range tests {
  127. assert.NoError(t, PrepareTestDatabase())
  128. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  129. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  130. assert.NoError(t, issue.ClearLabels(doer))
  131. AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
  132. }
  133. }
  134. func TestUpdateIssueCols(t *testing.T) {
  135. assert.NoError(t, PrepareTestDatabase())
  136. issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
  137. const newTitle = "New Title for unit test"
  138. issue.Title = newTitle
  139. prevContent := issue.Content
  140. issue.Content = "This should have no effect"
  141. now := time.Now().Unix()
  142. assert.NoError(t, UpdateIssueCols(issue, "name"))
  143. then := time.Now().Unix()
  144. updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
  145. assert.EqualValues(t, newTitle, updatedIssue.Title)
  146. assert.EqualValues(t, prevContent, updatedIssue.Content)
  147. AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
  148. }
  149. func TestIssues(t *testing.T) {
  150. assert.NoError(t, PrepareTestDatabase())
  151. for _, test := range []struct {
  152. Opts IssuesOptions
  153. ExpectedIssueIDs []int64
  154. }{
  155. {
  156. IssuesOptions{
  157. AssigneeID: 1,
  158. SortType: "oldest",
  159. },
  160. []int64{1, 6},
  161. },
  162. {
  163. IssuesOptions{
  164. RepoIDs: []int64{1, 3},
  165. SortType: "oldest",
  166. Page: 1,
  167. PageSize: 4,
  168. },
  169. []int64{1, 2, 3, 5},
  170. },
  171. {
  172. IssuesOptions{
  173. Labels: "1,2",
  174. Page: 1,
  175. PageSize: 4,
  176. },
  177. []int64{5, 2, 1},
  178. },
  179. } {
  180. issues, err := Issues(&test.Opts)
  181. assert.NoError(t, err)
  182. if assert.Len(t, issues, len(test.ExpectedIssueIDs)) {
  183. for i, issue := range issues {
  184. assert.EqualValues(t, test.ExpectedIssueIDs[i], issue.ID)
  185. }
  186. }
  187. }
  188. }
  189. func TestGetUserIssueStats(t *testing.T) {
  190. assert.NoError(t, PrepareTestDatabase())
  191. for _, test := range []struct {
  192. Opts UserIssueStatsOptions
  193. ExpectedIssueStats IssueStats
  194. }{
  195. {
  196. UserIssueStatsOptions{
  197. UserID: 1,
  198. RepoID: 1,
  199. FilterMode: FilterModeAll,
  200. },
  201. IssueStats{
  202. YourRepositoriesCount: 0,
  203. AssignCount: 1,
  204. CreateCount: 1,
  205. OpenCount: 0,
  206. ClosedCount: 0,
  207. },
  208. },
  209. {
  210. UserIssueStatsOptions{
  211. UserID: 1,
  212. FilterMode: FilterModeAssign,
  213. },
  214. IssueStats{
  215. YourRepositoriesCount: 0,
  216. AssignCount: 2,
  217. CreateCount: 2,
  218. OpenCount: 2,
  219. ClosedCount: 0,
  220. },
  221. },
  222. {
  223. UserIssueStatsOptions{
  224. UserID: 1,
  225. FilterMode: FilterModeCreate,
  226. },
  227. IssueStats{
  228. YourRepositoriesCount: 0,
  229. AssignCount: 2,
  230. CreateCount: 2,
  231. OpenCount: 2,
  232. ClosedCount: 0,
  233. },
  234. },
  235. {
  236. UserIssueStatsOptions{
  237. UserID: 2,
  238. UserRepoIDs: []int64{1, 2},
  239. FilterMode: FilterModeAll,
  240. IsClosed: true,
  241. },
  242. IssueStats{
  243. YourRepositoriesCount: 2,
  244. AssignCount: 0,
  245. CreateCount: 2,
  246. OpenCount: 1,
  247. ClosedCount: 2,
  248. },
  249. },
  250. } {
  251. stats, err := GetUserIssueStats(test.Opts)
  252. if !assert.NoError(t, err) {
  253. continue
  254. }
  255. assert.Equal(t, test.ExpectedIssueStats, *stats)
  256. }
  257. }