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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. LabelIDs: []int64{1},
  174. Page: 1,
  175. PageSize: 4,
  176. },
  177. []int64{2, 1},
  178. },
  179. {
  180. IssuesOptions{
  181. LabelIDs: []int64{1, 2},
  182. Page: 1,
  183. PageSize: 4,
  184. },
  185. []int64{}, // issues with **both** label 1 and 2, none of these issues matches, TODO: add more tests
  186. },
  187. } {
  188. issues, err := Issues(&test.Opts)
  189. assert.NoError(t, err)
  190. if assert.Len(t, issues, len(test.ExpectedIssueIDs)) {
  191. for i, issue := range issues {
  192. assert.EqualValues(t, test.ExpectedIssueIDs[i], issue.ID)
  193. }
  194. }
  195. }
  196. }
  197. func TestGetUserIssueStats(t *testing.T) {
  198. assert.NoError(t, PrepareTestDatabase())
  199. for _, test := range []struct {
  200. Opts UserIssueStatsOptions
  201. ExpectedIssueStats IssueStats
  202. }{
  203. {
  204. UserIssueStatsOptions{
  205. UserID: 1,
  206. RepoID: 1,
  207. FilterMode: FilterModeAll,
  208. },
  209. IssueStats{
  210. YourRepositoriesCount: 0,
  211. AssignCount: 1,
  212. CreateCount: 1,
  213. OpenCount: 0,
  214. ClosedCount: 0,
  215. },
  216. },
  217. {
  218. UserIssueStatsOptions{
  219. UserID: 1,
  220. FilterMode: FilterModeAssign,
  221. },
  222. IssueStats{
  223. YourRepositoriesCount: 0,
  224. AssignCount: 2,
  225. CreateCount: 2,
  226. OpenCount: 2,
  227. ClosedCount: 0,
  228. },
  229. },
  230. {
  231. UserIssueStatsOptions{
  232. UserID: 1,
  233. FilterMode: FilterModeCreate,
  234. },
  235. IssueStats{
  236. YourRepositoriesCount: 0,
  237. AssignCount: 2,
  238. CreateCount: 2,
  239. OpenCount: 2,
  240. ClosedCount: 0,
  241. },
  242. },
  243. {
  244. UserIssueStatsOptions{
  245. UserID: 2,
  246. UserRepoIDs: []int64{1, 2},
  247. FilterMode: FilterModeAll,
  248. IsClosed: true,
  249. },
  250. IssueStats{
  251. YourRepositoriesCount: 2,
  252. AssignCount: 0,
  253. CreateCount: 2,
  254. OpenCount: 2,
  255. ClosedCount: 2,
  256. },
  257. },
  258. } {
  259. stats, err := GetUserIssueStats(test.Opts)
  260. if !assert.NoError(t, err) {
  261. continue
  262. }
  263. assert.Equal(t, test.ExpectedIssueStats, *stats)
  264. }
  265. }
  266. func TestIssue_loadTotalTimes(t *testing.T) {
  267. assert.NoError(t, PrepareTestDatabase())
  268. ms, err := GetIssueByID(2)
  269. assert.NoError(t, err)
  270. assert.NoError(t, ms.loadTotalTimes(x))
  271. assert.Equal(t, int64(3662), ms.TotalTrackedTime)
  272. }