本站源代码
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.

292 lines
8.5KB

  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. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "github.com/stretchr/testify/assert"
  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 TestMilestone_APIFormat(t *testing.T) {
  18. milestone := &Milestone{
  19. ID: 3,
  20. RepoID: 4,
  21. Name: "milestoneName",
  22. Content: "milestoneContent",
  23. IsClosed: false,
  24. NumOpenIssues: 5,
  25. NumClosedIssues: 6,
  26. DeadlineUnix: timeutil.TimeStamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()),
  27. }
  28. assert.Equal(t, api.Milestone{
  29. ID: milestone.ID,
  30. State: api.StateOpen,
  31. Title: milestone.Name,
  32. Description: milestone.Content,
  33. OpenIssues: milestone.NumOpenIssues,
  34. ClosedIssues: milestone.NumClosedIssues,
  35. Deadline: milestone.DeadlineUnix.AsTimePtr(),
  36. }, *milestone.APIFormat())
  37. }
  38. func TestNewMilestone(t *testing.T) {
  39. assert.NoError(t, PrepareTestDatabase())
  40. milestone := &Milestone{
  41. RepoID: 1,
  42. Name: "milestoneName",
  43. Content: "milestoneContent",
  44. }
  45. assert.NoError(t, NewMilestone(milestone))
  46. AssertExistsAndLoadBean(t, milestone)
  47. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  48. }
  49. func TestGetMilestoneByRepoID(t *testing.T) {
  50. assert.NoError(t, PrepareTestDatabase())
  51. milestone, err := GetMilestoneByRepoID(1, 1)
  52. assert.NoError(t, err)
  53. assert.EqualValues(t, 1, milestone.ID)
  54. assert.EqualValues(t, 1, milestone.RepoID)
  55. _, err = GetMilestoneByRepoID(NonexistentID, NonexistentID)
  56. assert.True(t, IsErrMilestoneNotExist(err))
  57. }
  58. func TestGetMilestonesByRepoID(t *testing.T) {
  59. assert.NoError(t, PrepareTestDatabase())
  60. test := func(repoID int64, state api.StateType) {
  61. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  62. milestones, err := GetMilestonesByRepoID(repo.ID, state)
  63. assert.NoError(t, err)
  64. var n int
  65. switch state {
  66. case api.StateClosed:
  67. n = repo.NumClosedMilestones
  68. case api.StateAll:
  69. n = repo.NumMilestones
  70. case api.StateOpen:
  71. fallthrough
  72. default:
  73. n = repo.NumOpenMilestones
  74. }
  75. assert.Len(t, milestones, n)
  76. for _, milestone := range milestones {
  77. assert.EqualValues(t, repoID, milestone.RepoID)
  78. }
  79. }
  80. test(1, api.StateOpen)
  81. test(1, api.StateAll)
  82. test(1, api.StateClosed)
  83. test(2, api.StateOpen)
  84. test(2, api.StateAll)
  85. test(2, api.StateClosed)
  86. test(3, api.StateOpen)
  87. test(3, api.StateClosed)
  88. test(3, api.StateAll)
  89. milestones, err := GetMilestonesByRepoID(NonexistentID, api.StateOpen)
  90. assert.NoError(t, err)
  91. assert.Len(t, milestones, 0)
  92. }
  93. func TestGetMilestones(t *testing.T) {
  94. assert.NoError(t, PrepareTestDatabase())
  95. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  96. test := func(sortType string, sortCond func(*Milestone) int) {
  97. for _, page := range []int{0, 1} {
  98. milestones, err := GetMilestones(repo.ID, page, false, sortType)
  99. assert.NoError(t, err)
  100. assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones)
  101. values := make([]int, len(milestones))
  102. for i, milestone := range milestones {
  103. values[i] = sortCond(milestone)
  104. }
  105. assert.True(t, sort.IntsAreSorted(values))
  106. milestones, err = GetMilestones(repo.ID, page, true, sortType)
  107. assert.NoError(t, err)
  108. assert.Len(t, milestones, repo.NumClosedMilestones)
  109. values = make([]int, len(milestones))
  110. for i, milestone := range milestones {
  111. values[i] = sortCond(milestone)
  112. }
  113. assert.True(t, sort.IntsAreSorted(values))
  114. }
  115. }
  116. test("furthestduedate", func(milestone *Milestone) int {
  117. return -int(milestone.DeadlineUnix)
  118. })
  119. test("leastcomplete", func(milestone *Milestone) int {
  120. return milestone.Completeness
  121. })
  122. test("mostcomplete", func(milestone *Milestone) int {
  123. return -milestone.Completeness
  124. })
  125. test("leastissues", func(milestone *Milestone) int {
  126. return milestone.NumIssues
  127. })
  128. test("mostissues", func(milestone *Milestone) int {
  129. return -milestone.NumIssues
  130. })
  131. test("soonestduedate", func(milestone *Milestone) int {
  132. return int(milestone.DeadlineUnix)
  133. })
  134. }
  135. func TestUpdateMilestone(t *testing.T) {
  136. assert.NoError(t, PrepareTestDatabase())
  137. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  138. milestone.Name = "newMilestoneName"
  139. milestone.Content = "newMilestoneContent"
  140. assert.NoError(t, UpdateMilestone(milestone))
  141. AssertExistsAndLoadBean(t, milestone)
  142. CheckConsistencyFor(t, &Milestone{})
  143. }
  144. func TestCountRepoMilestones(t *testing.T) {
  145. assert.NoError(t, PrepareTestDatabase())
  146. test := func(repoID int64) {
  147. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  148. count, err := countRepoMilestones(x, repoID)
  149. assert.NoError(t, err)
  150. assert.EqualValues(t, repo.NumMilestones, count)
  151. }
  152. test(1)
  153. test(2)
  154. test(3)
  155. count, err := countRepoMilestones(x, NonexistentID)
  156. assert.NoError(t, err)
  157. assert.EqualValues(t, 0, count)
  158. }
  159. func TestCountRepoClosedMilestones(t *testing.T) {
  160. assert.NoError(t, PrepareTestDatabase())
  161. test := func(repoID int64) {
  162. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  163. count, err := CountRepoClosedMilestones(repoID)
  164. assert.NoError(t, err)
  165. assert.EqualValues(t, repo.NumClosedMilestones, count)
  166. }
  167. test(1)
  168. test(2)
  169. test(3)
  170. count, err := CountRepoClosedMilestones(NonexistentID)
  171. assert.NoError(t, err)
  172. assert.EqualValues(t, 0, count)
  173. }
  174. func TestMilestoneStats(t *testing.T) {
  175. assert.NoError(t, PrepareTestDatabase())
  176. test := func(repoID int64) {
  177. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  178. open, closed, err := MilestoneStats(repoID)
  179. assert.NoError(t, err)
  180. assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, open)
  181. assert.EqualValues(t, repo.NumClosedMilestones, closed)
  182. }
  183. test(1)
  184. test(2)
  185. test(3)
  186. open, closed, err := MilestoneStats(NonexistentID)
  187. assert.NoError(t, err)
  188. assert.EqualValues(t, 0, open)
  189. assert.EqualValues(t, 0, closed)
  190. }
  191. func TestChangeMilestoneStatus(t *testing.T) {
  192. assert.NoError(t, PrepareTestDatabase())
  193. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  194. assert.NoError(t, ChangeMilestoneStatus(milestone, true))
  195. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
  196. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  197. assert.NoError(t, ChangeMilestoneStatus(milestone, false))
  198. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
  199. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  200. }
  201. func TestUpdateMilestoneClosedNum(t *testing.T) {
  202. assert.NoError(t, PrepareTestDatabase())
  203. issue := AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
  204. "is_closed=0").(*Issue)
  205. issue.IsClosed = true
  206. issue.ClosedUnix = timeutil.TimeStampNow()
  207. _, err := x.Cols("is_closed", "closed_unix").Update(issue)
  208. assert.NoError(t, err)
  209. assert.NoError(t, updateMilestoneClosedNum(x, issue.MilestoneID))
  210. CheckConsistencyFor(t, &Milestone{})
  211. issue.IsClosed = false
  212. issue.ClosedUnix = 0
  213. _, err = x.Cols("is_closed", "closed_unix").Update(issue)
  214. assert.NoError(t, err)
  215. assert.NoError(t, updateMilestoneClosedNum(x, issue.MilestoneID))
  216. CheckConsistencyFor(t, &Milestone{})
  217. }
  218. func TestChangeMilestoneAssign(t *testing.T) {
  219. assert.NoError(t, PrepareTestDatabase())
  220. issue := AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
  221. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  222. assert.NotNil(t, issue)
  223. assert.NotNil(t, doer)
  224. oldMilestoneID := issue.MilestoneID
  225. issue.MilestoneID = 2
  226. assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID))
  227. AssertExistsAndLoadBean(t, &Comment{
  228. IssueID: issue.ID,
  229. Type: CommentTypeMilestone,
  230. MilestoneID: issue.MilestoneID,
  231. OldMilestoneID: oldMilestoneID,
  232. })
  233. CheckConsistencyFor(t, &Milestone{}, &Issue{})
  234. }
  235. func TestDeleteMilestoneByRepoID(t *testing.T) {
  236. assert.NoError(t, PrepareTestDatabase())
  237. assert.NoError(t, DeleteMilestoneByRepoID(1, 1))
  238. AssertNotExistsBean(t, &Milestone{ID: 1})
  239. CheckConsistencyFor(t, &Repository{ID: 1})
  240. assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
  241. }
  242. func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
  243. assert.NoError(t, PrepareTestDatabase())
  244. miles := MilestoneList{
  245. AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone),
  246. }
  247. assert.NoError(t, miles.LoadTotalTrackedTimes())
  248. assert.Equal(t, miles[0].TotalTrackedTime, int64(3662))
  249. }
上海开阖软件有限公司 沪ICP备12045867号-1