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
11 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 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
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. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "github.com/stretchr/testify/assert"
  11. "xorm.io/builder"
  12. )
  13. func TestMilestone_State(t *testing.T) {
  14. assert.Equal(t, api.StateOpen, (&Milestone{IsClosed: false}).State())
  15. assert.Equal(t, api.StateClosed, (&Milestone{IsClosed: true}).State())
  16. }
  17. func TestNewMilestone(t *testing.T) {
  18. assert.NoError(t, PrepareTestDatabase())
  19. milestone := &Milestone{
  20. RepoID: 1,
  21. Name: "milestoneName",
  22. Content: "milestoneContent",
  23. }
  24. assert.NoError(t, NewMilestone(milestone))
  25. AssertExistsAndLoadBean(t, milestone)
  26. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  27. }
  28. func TestGetMilestoneByRepoID(t *testing.T) {
  29. assert.NoError(t, PrepareTestDatabase())
  30. milestone, err := GetMilestoneByRepoID(1, 1)
  31. assert.NoError(t, err)
  32. assert.EqualValues(t, 1, milestone.ID)
  33. assert.EqualValues(t, 1, milestone.RepoID)
  34. _, err = GetMilestoneByRepoID(NonexistentID, NonexistentID)
  35. assert.True(t, IsErrMilestoneNotExist(err))
  36. }
  37. func TestGetMilestonesByRepoID(t *testing.T) {
  38. assert.NoError(t, PrepareTestDatabase())
  39. test := func(repoID int64, state api.StateType) {
  40. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  41. milestones, err := GetMilestonesByRepoID(repo.ID, state, ListOptions{})
  42. assert.NoError(t, err)
  43. var n int
  44. switch state {
  45. case api.StateClosed:
  46. n = repo.NumClosedMilestones
  47. case api.StateAll:
  48. n = repo.NumMilestones
  49. case api.StateOpen:
  50. fallthrough
  51. default:
  52. n = repo.NumOpenMilestones
  53. }
  54. assert.Len(t, milestones, n)
  55. for _, milestone := range milestones {
  56. assert.EqualValues(t, repoID, milestone.RepoID)
  57. }
  58. }
  59. test(1, api.StateOpen)
  60. test(1, api.StateAll)
  61. test(1, api.StateClosed)
  62. test(2, api.StateOpen)
  63. test(2, api.StateAll)
  64. test(2, api.StateClosed)
  65. test(3, api.StateOpen)
  66. test(3, api.StateClosed)
  67. test(3, api.StateAll)
  68. milestones, err := GetMilestonesByRepoID(NonexistentID, api.StateOpen, ListOptions{})
  69. assert.NoError(t, err)
  70. assert.Len(t, milestones, 0)
  71. }
  72. func TestGetMilestones(t *testing.T) {
  73. assert.NoError(t, PrepareTestDatabase())
  74. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  75. test := func(sortType string, sortCond func(*Milestone) int) {
  76. for _, page := range []int{0, 1} {
  77. milestones, err := GetMilestones(repo.ID, page, false, sortType)
  78. assert.NoError(t, err)
  79. assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones)
  80. values := make([]int, len(milestones))
  81. for i, milestone := range milestones {
  82. values[i] = sortCond(milestone)
  83. }
  84. assert.True(t, sort.IntsAreSorted(values))
  85. milestones, err = GetMilestones(repo.ID, page, true, sortType)
  86. assert.NoError(t, err)
  87. assert.Len(t, milestones, repo.NumClosedMilestones)
  88. values = make([]int, len(milestones))
  89. for i, milestone := range milestones {
  90. values[i] = sortCond(milestone)
  91. }
  92. assert.True(t, sort.IntsAreSorted(values))
  93. }
  94. }
  95. test("furthestduedate", func(milestone *Milestone) int {
  96. return -int(milestone.DeadlineUnix)
  97. })
  98. test("leastcomplete", func(milestone *Milestone) int {
  99. return milestone.Completeness
  100. })
  101. test("mostcomplete", func(milestone *Milestone) int {
  102. return -milestone.Completeness
  103. })
  104. test("leastissues", func(milestone *Milestone) int {
  105. return milestone.NumIssues
  106. })
  107. test("mostissues", func(milestone *Milestone) int {
  108. return -milestone.NumIssues
  109. })
  110. test("soonestduedate", func(milestone *Milestone) int {
  111. return int(milestone.DeadlineUnix)
  112. })
  113. }
  114. func TestUpdateMilestone(t *testing.T) {
  115. assert.NoError(t, PrepareTestDatabase())
  116. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  117. milestone.Name = " newMilestoneName "
  118. milestone.Content = "newMilestoneContent"
  119. assert.NoError(t, UpdateMilestone(milestone, milestone.IsClosed))
  120. milestone = AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  121. assert.EqualValues(t, "newMilestoneName", milestone.Name)
  122. CheckConsistencyFor(t, &Milestone{})
  123. }
  124. func TestCountRepoMilestones(t *testing.T) {
  125. assert.NoError(t, PrepareTestDatabase())
  126. test := func(repoID int64) {
  127. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  128. count, err := countRepoMilestones(x, repoID)
  129. assert.NoError(t, err)
  130. assert.EqualValues(t, repo.NumMilestones, count)
  131. }
  132. test(1)
  133. test(2)
  134. test(3)
  135. count, err := countRepoMilestones(x, NonexistentID)
  136. assert.NoError(t, err)
  137. assert.EqualValues(t, 0, count)
  138. }
  139. func TestCountRepoClosedMilestones(t *testing.T) {
  140. assert.NoError(t, PrepareTestDatabase())
  141. test := func(repoID int64) {
  142. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  143. count, err := CountRepoClosedMilestones(repoID)
  144. assert.NoError(t, err)
  145. assert.EqualValues(t, repo.NumClosedMilestones, count)
  146. }
  147. test(1)
  148. test(2)
  149. test(3)
  150. count, err := CountRepoClosedMilestones(NonexistentID)
  151. assert.NoError(t, err)
  152. assert.EqualValues(t, 0, count)
  153. }
  154. func TestChangeMilestoneStatus(t *testing.T) {
  155. assert.NoError(t, PrepareTestDatabase())
  156. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  157. assert.NoError(t, ChangeMilestoneStatus(milestone, true))
  158. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
  159. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  160. assert.NoError(t, ChangeMilestoneStatus(milestone, false))
  161. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
  162. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  163. }
  164. func TestUpdateMilestoneClosedNum(t *testing.T) {
  165. assert.NoError(t, PrepareTestDatabase())
  166. issue := AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
  167. "is_closed=0").(*Issue)
  168. issue.IsClosed = true
  169. issue.ClosedUnix = timeutil.TimeStampNow()
  170. _, err := x.ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
  171. assert.NoError(t, err)
  172. assert.NoError(t, updateMilestoneClosedNum(x, issue.MilestoneID))
  173. CheckConsistencyFor(t, &Milestone{})
  174. issue.IsClosed = false
  175. issue.ClosedUnix = 0
  176. _, err = x.ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
  177. assert.NoError(t, err)
  178. assert.NoError(t, updateMilestoneClosedNum(x, issue.MilestoneID))
  179. CheckConsistencyFor(t, &Milestone{})
  180. }
  181. func TestChangeMilestoneAssign(t *testing.T) {
  182. assert.NoError(t, PrepareTestDatabase())
  183. issue := AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
  184. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  185. assert.NotNil(t, issue)
  186. assert.NotNil(t, doer)
  187. oldMilestoneID := issue.MilestoneID
  188. issue.MilestoneID = 2
  189. assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID))
  190. AssertExistsAndLoadBean(t, &Comment{
  191. IssueID: issue.ID,
  192. Type: CommentTypeMilestone,
  193. MilestoneID: issue.MilestoneID,
  194. OldMilestoneID: oldMilestoneID,
  195. })
  196. CheckConsistencyFor(t, &Milestone{}, &Issue{})
  197. }
  198. func TestDeleteMilestoneByRepoID(t *testing.T) {
  199. assert.NoError(t, PrepareTestDatabase())
  200. assert.NoError(t, DeleteMilestoneByRepoID(1, 1))
  201. AssertNotExistsBean(t, &Milestone{ID: 1})
  202. CheckConsistencyFor(t, &Repository{ID: 1})
  203. assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
  204. }
  205. func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
  206. assert.NoError(t, PrepareTestDatabase())
  207. miles := MilestoneList{
  208. AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone),
  209. }
  210. assert.NoError(t, miles.LoadTotalTrackedTimes())
  211. assert.Equal(t, int64(3682), miles[0].TotalTrackedTime)
  212. }
  213. func TestCountMilestonesByRepoIDs(t *testing.T) {
  214. assert.NoError(t, PrepareTestDatabase())
  215. milestonesCount := func(repoID int64) (int, int) {
  216. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  217. return repo.NumOpenMilestones, repo.NumClosedMilestones
  218. }
  219. repo1OpenCount, repo1ClosedCount := milestonesCount(1)
  220. repo2OpenCount, repo2ClosedCount := milestonesCount(2)
  221. openCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), false)
  222. assert.NoError(t, err)
  223. assert.EqualValues(t, repo1OpenCount, openCounts[1])
  224. assert.EqualValues(t, repo2OpenCount, openCounts[2])
  225. closedCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), true)
  226. assert.NoError(t, err)
  227. assert.EqualValues(t, repo1ClosedCount, closedCounts[1])
  228. assert.EqualValues(t, repo2ClosedCount, closedCounts[2])
  229. }
  230. func TestGetMilestonesByRepoIDs(t *testing.T) {
  231. assert.NoError(t, PrepareTestDatabase())
  232. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  233. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  234. test := func(sortType string, sortCond func(*Milestone) int) {
  235. for _, page := range []int{0, 1} {
  236. openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType)
  237. assert.NoError(t, err)
  238. assert.Len(t, openMilestones, repo1.NumOpenMilestones+repo2.NumOpenMilestones)
  239. values := make([]int, len(openMilestones))
  240. for i, milestone := range openMilestones {
  241. values[i] = sortCond(milestone)
  242. }
  243. assert.True(t, sort.IntsAreSorted(values))
  244. closedMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, true, sortType)
  245. assert.NoError(t, err)
  246. assert.Len(t, closedMilestones, repo1.NumClosedMilestones+repo2.NumClosedMilestones)
  247. values = make([]int, len(closedMilestones))
  248. for i, milestone := range closedMilestones {
  249. values[i] = sortCond(milestone)
  250. }
  251. assert.True(t, sort.IntsAreSorted(values))
  252. }
  253. }
  254. test("furthestduedate", func(milestone *Milestone) int {
  255. return -int(milestone.DeadlineUnix)
  256. })
  257. test("leastcomplete", func(milestone *Milestone) int {
  258. return milestone.Completeness
  259. })
  260. test("mostcomplete", func(milestone *Milestone) int {
  261. return -milestone.Completeness
  262. })
  263. test("leastissues", func(milestone *Milestone) int {
  264. return milestone.NumIssues
  265. })
  266. test("mostissues", func(milestone *Milestone) int {
  267. return -milestone.NumIssues
  268. })
  269. test("soonestduedate", func(milestone *Milestone) int {
  270. return int(milestone.DeadlineUnix)
  271. })
  272. }
  273. func TestLoadTotalTrackedTime(t *testing.T) {
  274. assert.NoError(t, PrepareTestDatabase())
  275. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  276. assert.NoError(t, milestone.LoadTotalTrackedTime())
  277. assert.Equal(t, int64(3682), milestone.TotalTrackedTime)
  278. }
  279. func TestGetMilestonesStats(t *testing.T) {
  280. assert.NoError(t, PrepareTestDatabase())
  281. test := func(repoID int64) {
  282. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  283. stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID}))
  284. assert.NoError(t, err)
  285. assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount)
  286. assert.EqualValues(t, repo.NumClosedMilestones, stats.ClosedCount)
  287. }
  288. test(1)
  289. test(2)
  290. test(3)
  291. stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": NonexistentID}))
  292. assert.NoError(t, err)
  293. assert.EqualValues(t, 0, stats.OpenCount)
  294. assert.EqualValues(t, 0, stats.ClosedCount)
  295. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  296. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  297. milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []int64{repo1.ID, repo2.ID}))
  298. assert.NoError(t, err)
  299. assert.EqualValues(t, repo1.NumOpenMilestones+repo2.NumOpenMilestones, milestoneStats.OpenCount)
  300. assert.EqualValues(t, repo1.NumClosedMilestones+repo2.NumClosedMilestones, milestoneStats.ClosedCount)
  301. }