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.

377 lines
12 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
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. "code.gitea.io/gitea/modules/setting"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "github.com/stretchr/testify/assert"
  12. "xorm.io/builder"
  13. )
  14. func TestMilestone_State(t *testing.T) {
  15. assert.Equal(t, api.StateOpen, (&Milestone{IsClosed: false}).State())
  16. assert.Equal(t, api.StateClosed, (&Milestone{IsClosed: true}).State())
  17. }
  18. func TestNewMilestone(t *testing.T) {
  19. assert.NoError(t, PrepareTestDatabase())
  20. milestone := &Milestone{
  21. RepoID: 1,
  22. Name: "milestoneName",
  23. Content: "milestoneContent",
  24. }
  25. assert.NoError(t, NewMilestone(milestone))
  26. AssertExistsAndLoadBean(t, milestone)
  27. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  28. }
  29. func TestGetMilestoneByRepoID(t *testing.T) {
  30. assert.NoError(t, PrepareTestDatabase())
  31. milestone, err := GetMilestoneByRepoID(1, 1)
  32. assert.NoError(t, err)
  33. assert.EqualValues(t, 1, milestone.ID)
  34. assert.EqualValues(t, 1, milestone.RepoID)
  35. _, err = GetMilestoneByRepoID(NonexistentID, NonexistentID)
  36. assert.True(t, IsErrMilestoneNotExist(err))
  37. }
  38. func TestGetMilestonesByRepoID(t *testing.T) {
  39. assert.NoError(t, PrepareTestDatabase())
  40. test := func(repoID int64, state api.StateType) {
  41. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  42. milestones, err := GetMilestones(GetMilestonesOption{
  43. RepoID: repo.ID,
  44. State: state,
  45. })
  46. assert.NoError(t, err)
  47. var n int
  48. switch state {
  49. case api.StateClosed:
  50. n = repo.NumClosedMilestones
  51. case api.StateAll:
  52. n = repo.NumMilestones
  53. case api.StateOpen:
  54. fallthrough
  55. default:
  56. n = repo.NumOpenMilestones
  57. }
  58. assert.Len(t, milestones, n)
  59. for _, milestone := range milestones {
  60. assert.EqualValues(t, repoID, milestone.RepoID)
  61. }
  62. }
  63. test(1, api.StateOpen)
  64. test(1, api.StateAll)
  65. test(1, api.StateClosed)
  66. test(2, api.StateOpen)
  67. test(2, api.StateAll)
  68. test(2, api.StateClosed)
  69. test(3, api.StateOpen)
  70. test(3, api.StateClosed)
  71. test(3, api.StateAll)
  72. milestones, err := GetMilestones(GetMilestonesOption{
  73. RepoID: NonexistentID,
  74. State: api.StateOpen,
  75. })
  76. assert.NoError(t, err)
  77. assert.Len(t, milestones, 0)
  78. }
  79. func TestGetMilestones(t *testing.T) {
  80. assert.NoError(t, PrepareTestDatabase())
  81. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  82. test := func(sortType string, sortCond func(*Milestone) int) {
  83. for _, page := range []int{0, 1} {
  84. milestones, err := GetMilestones(GetMilestonesOption{
  85. ListOptions: ListOptions{
  86. Page: page,
  87. PageSize: setting.UI.IssuePagingNum,
  88. },
  89. RepoID: repo.ID,
  90. State: api.StateOpen,
  91. SortType: sortType,
  92. })
  93. assert.NoError(t, err)
  94. assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones)
  95. values := make([]int, len(milestones))
  96. for i, milestone := range milestones {
  97. values[i] = sortCond(milestone)
  98. }
  99. assert.True(t, sort.IntsAreSorted(values))
  100. milestones, err = GetMilestones(GetMilestonesOption{
  101. ListOptions: ListOptions{
  102. Page: page,
  103. PageSize: setting.UI.IssuePagingNum,
  104. },
  105. RepoID: repo.ID,
  106. State: api.StateClosed,
  107. Name: "",
  108. SortType: sortType,
  109. })
  110. assert.NoError(t, err)
  111. assert.Len(t, milestones, repo.NumClosedMilestones)
  112. values = make([]int, len(milestones))
  113. for i, milestone := range milestones {
  114. values[i] = sortCond(milestone)
  115. }
  116. assert.True(t, sort.IntsAreSorted(values))
  117. }
  118. }
  119. test("furthestduedate", func(milestone *Milestone) int {
  120. return -int(milestone.DeadlineUnix)
  121. })
  122. test("leastcomplete", func(milestone *Milestone) int {
  123. return milestone.Completeness
  124. })
  125. test("mostcomplete", func(milestone *Milestone) int {
  126. return -milestone.Completeness
  127. })
  128. test("leastissues", func(milestone *Milestone) int {
  129. return milestone.NumIssues
  130. })
  131. test("mostissues", func(milestone *Milestone) int {
  132. return -milestone.NumIssues
  133. })
  134. test("soonestduedate", func(milestone *Milestone) int {
  135. return int(milestone.DeadlineUnix)
  136. })
  137. }
  138. func TestUpdateMilestone(t *testing.T) {
  139. assert.NoError(t, PrepareTestDatabase())
  140. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  141. milestone.Name = " newMilestoneName "
  142. milestone.Content = "newMilestoneContent"
  143. assert.NoError(t, UpdateMilestone(milestone, milestone.IsClosed))
  144. milestone = AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  145. assert.EqualValues(t, "newMilestoneName", milestone.Name)
  146. CheckConsistencyFor(t, &Milestone{})
  147. }
  148. func TestCountRepoMilestones(t *testing.T) {
  149. assert.NoError(t, PrepareTestDatabase())
  150. test := func(repoID int64) {
  151. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  152. count, err := countRepoMilestones(x, repoID)
  153. assert.NoError(t, err)
  154. assert.EqualValues(t, repo.NumMilestones, count)
  155. }
  156. test(1)
  157. test(2)
  158. test(3)
  159. count, err := countRepoMilestones(x, NonexistentID)
  160. assert.NoError(t, err)
  161. assert.EqualValues(t, 0, count)
  162. }
  163. func TestCountRepoClosedMilestones(t *testing.T) {
  164. assert.NoError(t, PrepareTestDatabase())
  165. test := func(repoID int64) {
  166. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  167. count, err := CountRepoClosedMilestones(repoID)
  168. assert.NoError(t, err)
  169. assert.EqualValues(t, repo.NumClosedMilestones, count)
  170. }
  171. test(1)
  172. test(2)
  173. test(3)
  174. count, err := CountRepoClosedMilestones(NonexistentID)
  175. assert.NoError(t, err)
  176. assert.EqualValues(t, 0, count)
  177. }
  178. func TestChangeMilestoneStatus(t *testing.T) {
  179. assert.NoError(t, PrepareTestDatabase())
  180. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  181. assert.NoError(t, ChangeMilestoneStatus(milestone, true))
  182. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
  183. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  184. assert.NoError(t, ChangeMilestoneStatus(milestone, false))
  185. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
  186. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  187. }
  188. func TestUpdateMilestoneClosedNum(t *testing.T) {
  189. assert.NoError(t, PrepareTestDatabase())
  190. issue := AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
  191. "is_closed=0").(*Issue)
  192. issue.IsClosed = true
  193. issue.ClosedUnix = timeutil.TimeStampNow()
  194. _, err := x.ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
  195. assert.NoError(t, err)
  196. assert.NoError(t, updateMilestoneClosedNum(x, issue.MilestoneID))
  197. CheckConsistencyFor(t, &Milestone{})
  198. issue.IsClosed = false
  199. issue.ClosedUnix = 0
  200. _, err = x.ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
  201. assert.NoError(t, err)
  202. assert.NoError(t, updateMilestoneClosedNum(x, issue.MilestoneID))
  203. CheckConsistencyFor(t, &Milestone{})
  204. }
  205. func TestChangeMilestoneAssign(t *testing.T) {
  206. assert.NoError(t, PrepareTestDatabase())
  207. issue := AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
  208. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  209. assert.NotNil(t, issue)
  210. assert.NotNil(t, doer)
  211. oldMilestoneID := issue.MilestoneID
  212. issue.MilestoneID = 2
  213. assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID))
  214. AssertExistsAndLoadBean(t, &Comment{
  215. IssueID: issue.ID,
  216. Type: CommentTypeMilestone,
  217. MilestoneID: issue.MilestoneID,
  218. OldMilestoneID: oldMilestoneID,
  219. })
  220. CheckConsistencyFor(t, &Milestone{}, &Issue{})
  221. }
  222. func TestDeleteMilestoneByRepoID(t *testing.T) {
  223. assert.NoError(t, PrepareTestDatabase())
  224. assert.NoError(t, DeleteMilestoneByRepoID(1, 1))
  225. AssertNotExistsBean(t, &Milestone{ID: 1})
  226. CheckConsistencyFor(t, &Repository{ID: 1})
  227. assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
  228. }
  229. func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
  230. assert.NoError(t, PrepareTestDatabase())
  231. miles := MilestoneList{
  232. AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone),
  233. }
  234. assert.NoError(t, miles.LoadTotalTrackedTimes())
  235. assert.Equal(t, int64(3682), miles[0].TotalTrackedTime)
  236. }
  237. func TestCountMilestonesByRepoIDs(t *testing.T) {
  238. assert.NoError(t, PrepareTestDatabase())
  239. milestonesCount := func(repoID int64) (int, int) {
  240. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  241. return repo.NumOpenMilestones, repo.NumClosedMilestones
  242. }
  243. repo1OpenCount, repo1ClosedCount := milestonesCount(1)
  244. repo2OpenCount, repo2ClosedCount := milestonesCount(2)
  245. openCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), false)
  246. assert.NoError(t, err)
  247. assert.EqualValues(t, repo1OpenCount, openCounts[1])
  248. assert.EqualValues(t, repo2OpenCount, openCounts[2])
  249. closedCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), true)
  250. assert.NoError(t, err)
  251. assert.EqualValues(t, repo1ClosedCount, closedCounts[1])
  252. assert.EqualValues(t, repo2ClosedCount, closedCounts[2])
  253. }
  254. func TestGetMilestonesByRepoIDs(t *testing.T) {
  255. assert.NoError(t, PrepareTestDatabase())
  256. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  257. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  258. test := func(sortType string, sortCond func(*Milestone) int) {
  259. for _, page := range []int{0, 1} {
  260. openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType)
  261. assert.NoError(t, err)
  262. assert.Len(t, openMilestones, repo1.NumOpenMilestones+repo2.NumOpenMilestones)
  263. values := make([]int, len(openMilestones))
  264. for i, milestone := range openMilestones {
  265. values[i] = sortCond(milestone)
  266. }
  267. assert.True(t, sort.IntsAreSorted(values))
  268. closedMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, true, sortType)
  269. assert.NoError(t, err)
  270. assert.Len(t, closedMilestones, repo1.NumClosedMilestones+repo2.NumClosedMilestones)
  271. values = make([]int, len(closedMilestones))
  272. for i, milestone := range closedMilestones {
  273. values[i] = sortCond(milestone)
  274. }
  275. assert.True(t, sort.IntsAreSorted(values))
  276. }
  277. }
  278. test("furthestduedate", func(milestone *Milestone) int {
  279. return -int(milestone.DeadlineUnix)
  280. })
  281. test("leastcomplete", func(milestone *Milestone) int {
  282. return milestone.Completeness
  283. })
  284. test("mostcomplete", func(milestone *Milestone) int {
  285. return -milestone.Completeness
  286. })
  287. test("leastissues", func(milestone *Milestone) int {
  288. return milestone.NumIssues
  289. })
  290. test("mostissues", func(milestone *Milestone) int {
  291. return -milestone.NumIssues
  292. })
  293. test("soonestduedate", func(milestone *Milestone) int {
  294. return int(milestone.DeadlineUnix)
  295. })
  296. }
  297. func TestLoadTotalTrackedTime(t *testing.T) {
  298. assert.NoError(t, PrepareTestDatabase())
  299. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  300. assert.NoError(t, milestone.LoadTotalTrackedTime())
  301. assert.Equal(t, int64(3682), milestone.TotalTrackedTime)
  302. }
  303. func TestGetMilestonesStats(t *testing.T) {
  304. assert.NoError(t, PrepareTestDatabase())
  305. test := func(repoID int64) {
  306. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  307. stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID}))
  308. assert.NoError(t, err)
  309. assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount)
  310. assert.EqualValues(t, repo.NumClosedMilestones, stats.ClosedCount)
  311. }
  312. test(1)
  313. test(2)
  314. test(3)
  315. stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": NonexistentID}))
  316. assert.NoError(t, err)
  317. assert.EqualValues(t, 0, stats.OpenCount)
  318. assert.EqualValues(t, 0, stats.ClosedCount)
  319. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  320. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  321. milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []int64{repo1.ID, repo2.ID}))
  322. assert.NoError(t, err)
  323. assert.EqualValues(t, repo1.NumOpenMilestones+repo2.NumOpenMilestones, milestoneStats.OpenCount)
  324. assert.EqualValues(t, repo1.NumClosedMilestones+repo2.NumClosedMilestones, milestoneStats.ClosedCount)
  325. }