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.

353 lines
9.8 KiB

Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
  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_ClearLabels(t *testing.T) {
  73. var tests = []struct {
  74. issueID int64
  75. doerID int64
  76. }{
  77. {1, 2}, // non-pull-request, has labels
  78. {2, 2}, // pull-request, has labels
  79. {3, 2}, // pull-request, has no labels
  80. }
  81. for _, test := range tests {
  82. assert.NoError(t, PrepareTestDatabase())
  83. issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue)
  84. doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User)
  85. assert.NoError(t, issue.ClearLabels(doer))
  86. AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
  87. }
  88. }
  89. func TestUpdateIssueCols(t *testing.T) {
  90. assert.NoError(t, PrepareTestDatabase())
  91. issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
  92. const newTitle = "New Title for unit test"
  93. issue.Title = newTitle
  94. prevContent := issue.Content
  95. issue.Content = "This should have no effect"
  96. now := time.Now().Unix()
  97. assert.NoError(t, updateIssueCols(x, issue, "name"))
  98. then := time.Now().Unix()
  99. updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
  100. assert.EqualValues(t, newTitle, updatedIssue.Title)
  101. assert.EqualValues(t, prevContent, updatedIssue.Content)
  102. AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
  103. }
  104. func TestIssues(t *testing.T) {
  105. assert.NoError(t, PrepareTestDatabase())
  106. for _, test := range []struct {
  107. Opts IssuesOptions
  108. ExpectedIssueIDs []int64
  109. }{
  110. {
  111. IssuesOptions{
  112. AssigneeID: 1,
  113. SortType: "oldest",
  114. },
  115. []int64{1, 6},
  116. },
  117. {
  118. IssuesOptions{
  119. RepoIDs: []int64{1, 3},
  120. SortType: "oldest",
  121. Page: 1,
  122. PageSize: 4,
  123. },
  124. []int64{1, 2, 3, 5},
  125. },
  126. {
  127. IssuesOptions{
  128. LabelIDs: []int64{1},
  129. Page: 1,
  130. PageSize: 4,
  131. },
  132. []int64{2, 1},
  133. },
  134. {
  135. IssuesOptions{
  136. LabelIDs: []int64{1, 2},
  137. Page: 1,
  138. PageSize: 4,
  139. },
  140. []int64{}, // issues with **both** label 1 and 2, none of these issues matches, TODO: add more tests
  141. },
  142. } {
  143. issues, err := Issues(&test.Opts)
  144. assert.NoError(t, err)
  145. if assert.Len(t, issues, len(test.ExpectedIssueIDs)) {
  146. for i, issue := range issues {
  147. assert.EqualValues(t, test.ExpectedIssueIDs[i], issue.ID)
  148. }
  149. }
  150. }
  151. }
  152. func TestGetUserIssueStats(t *testing.T) {
  153. assert.NoError(t, PrepareTestDatabase())
  154. for _, test := range []struct {
  155. Opts UserIssueStatsOptions
  156. ExpectedIssueStats IssueStats
  157. }{
  158. {
  159. UserIssueStatsOptions{
  160. UserID: 1,
  161. RepoID: 1,
  162. FilterMode: FilterModeAll,
  163. },
  164. IssueStats{
  165. YourRepositoriesCount: 0,
  166. AssignCount: 1,
  167. CreateCount: 1,
  168. OpenCount: 0,
  169. ClosedCount: 0,
  170. },
  171. },
  172. {
  173. UserIssueStatsOptions{
  174. UserID: 1,
  175. FilterMode: FilterModeAssign,
  176. },
  177. IssueStats{
  178. YourRepositoriesCount: 0,
  179. AssignCount: 2,
  180. CreateCount: 2,
  181. OpenCount: 2,
  182. ClosedCount: 0,
  183. },
  184. },
  185. {
  186. UserIssueStatsOptions{
  187. UserID: 1,
  188. FilterMode: FilterModeCreate,
  189. },
  190. IssueStats{
  191. YourRepositoriesCount: 0,
  192. AssignCount: 2,
  193. CreateCount: 2,
  194. OpenCount: 2,
  195. ClosedCount: 0,
  196. },
  197. },
  198. {
  199. UserIssueStatsOptions{
  200. UserID: 2,
  201. UserRepoIDs: []int64{1, 2},
  202. FilterMode: FilterModeAll,
  203. IsClosed: true,
  204. },
  205. IssueStats{
  206. YourRepositoriesCount: 2,
  207. AssignCount: 0,
  208. CreateCount: 2,
  209. OpenCount: 2,
  210. ClosedCount: 2,
  211. },
  212. },
  213. {
  214. UserIssueStatsOptions{
  215. UserID: 1,
  216. FilterMode: FilterModeMention,
  217. },
  218. IssueStats{
  219. YourRepositoriesCount: 0,
  220. AssignCount: 2,
  221. CreateCount: 2,
  222. OpenCount: 0,
  223. ClosedCount: 0,
  224. },
  225. },
  226. } {
  227. stats, err := GetUserIssueStats(test.Opts)
  228. if !assert.NoError(t, err) {
  229. continue
  230. }
  231. assert.Equal(t, test.ExpectedIssueStats, *stats)
  232. }
  233. }
  234. func TestIssue_loadTotalTimes(t *testing.T) {
  235. assert.NoError(t, PrepareTestDatabase())
  236. ms, err := GetIssueByID(2)
  237. assert.NoError(t, err)
  238. assert.NoError(t, ms.loadTotalTimes(x))
  239. assert.Equal(t, int64(3662), ms.TotalTrackedTime)
  240. }
  241. func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
  242. assert.NoError(t, PrepareTestDatabase())
  243. total, ids, err := SearchIssueIDsByKeyword("issue2", 1, 10, 0)
  244. assert.NoError(t, err)
  245. assert.EqualValues(t, 1, total)
  246. assert.EqualValues(t, []int64{2}, ids)
  247. total, ids, err = SearchIssueIDsByKeyword("first", 1, 10, 0)
  248. assert.NoError(t, err)
  249. assert.EqualValues(t, 1, total)
  250. assert.EqualValues(t, []int64{1}, ids)
  251. total, ids, err = SearchIssueIDsByKeyword("for", 1, 10, 0)
  252. assert.NoError(t, err)
  253. assert.EqualValues(t, 4, total)
  254. assert.EqualValues(t, []int64{1, 2, 3, 5}, ids)
  255. // issue1's comment id 2
  256. total, ids, err = SearchIssueIDsByKeyword("good", 1, 10, 0)
  257. assert.NoError(t, err)
  258. assert.EqualValues(t, 1, total)
  259. assert.EqualValues(t, []int64{1}, ids)
  260. }
  261. func testInsertIssue(t *testing.T, title, content string) {
  262. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  263. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  264. var issue = Issue{
  265. RepoID: repo.ID,
  266. PosterID: user.ID,
  267. Title: title,
  268. Content: content,
  269. }
  270. err := NewIssue(repo, &issue, nil, nil)
  271. assert.NoError(t, err)
  272. var newIssue Issue
  273. has, err := x.ID(issue.ID).Get(&newIssue)
  274. assert.NoError(t, err)
  275. assert.True(t, has)
  276. assert.EqualValues(t, issue.Title, newIssue.Title)
  277. assert.EqualValues(t, issue.Content, newIssue.Content)
  278. // there are 4 issues and max index is 4 on repository 1, so this one should 5
  279. assert.EqualValues(t, 5, newIssue.Index)
  280. _, err = x.ID(issue.ID).Delete(new(Issue))
  281. assert.NoError(t, err)
  282. }
  283. func TestIssue_InsertIssue(t *testing.T) {
  284. assert.NoError(t, PrepareTestDatabase())
  285. testInsertIssue(t, "my issue1", "special issue's comments?")
  286. testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?")
  287. }
  288. func TestIssue_ResolveMentions(t *testing.T) {
  289. assert.NoError(t, PrepareTestDatabase())
  290. testSuccess := func(owner, repo, doer string, mentions []string, expected []int64) {
  291. o := AssertExistsAndLoadBean(t, &User{LowerName: owner}).(*User)
  292. r := AssertExistsAndLoadBean(t, &Repository{OwnerID: o.ID, LowerName: repo}).(*Repository)
  293. issue := &Issue{RepoID: r.ID}
  294. d := AssertExistsAndLoadBean(t, &User{LowerName: doer}).(*User)
  295. resolved, err := issue.ResolveMentionsByVisibility(DefaultDBContext(), d, mentions)
  296. assert.NoError(t, err)
  297. ids := make([]int64, len(resolved))
  298. for i, user := range resolved {
  299. ids[i] = user.ID
  300. }
  301. sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
  302. assert.EqualValues(t, expected, ids)
  303. }
  304. // Public repo, existing user
  305. testSuccess("user2", "repo1", "user1", []string{"user5"}, []int64{5})
  306. // Public repo, non-existing user
  307. testSuccess("user2", "repo1", "user1", []string{"nonexisting"}, []int64{})
  308. // Public repo, doer
  309. testSuccess("user2", "repo1", "user1", []string{"user1"}, []int64{})
  310. // Private repo, team member
  311. testSuccess("user17", "big_test_private_4", "user20", []string{"user2"}, []int64{2})
  312. // Private repo, not a team member
  313. testSuccess("user17", "big_test_private_4", "user20", []string{"user5"}, []int64{})
  314. // Private repo, whole team
  315. testSuccess("user17", "big_test_private_4", "user15", []string{"owners"}, []int64{18})
  316. }