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.

253 lines
7.6 KiB

  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. "code.gitea.io/gitea/modules/util"
  10. api "code.gitea.io/sdk/gitea"
  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: util.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) {
  61. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  62. milestones, err := GetMilestonesByRepoID(repo.ID)
  63. assert.NoError(t, err)
  64. assert.Len(t, milestones, repo.NumMilestones)
  65. for _, milestone := range milestones {
  66. assert.EqualValues(t, repoID, milestone.RepoID)
  67. }
  68. }
  69. test(1)
  70. test(2)
  71. test(3)
  72. milestones, err := GetMilestonesByRepoID(NonexistentID)
  73. assert.NoError(t, err)
  74. assert.Len(t, milestones, 0)
  75. }
  76. func TestGetMilestones(t *testing.T) {
  77. assert.NoError(t, PrepareTestDatabase())
  78. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  79. test := func(sortType string, sortCond func(*Milestone) int) {
  80. for _, page := range []int{0, 1} {
  81. milestones, err := GetMilestones(repo.ID, page, false, sortType)
  82. assert.NoError(t, err)
  83. assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones)
  84. values := make([]int, len(milestones))
  85. for i, milestone := range milestones {
  86. values[i] = sortCond(milestone)
  87. }
  88. assert.True(t, sort.IntsAreSorted(values))
  89. milestones, err = GetMilestones(repo.ID, page, true, sortType)
  90. assert.NoError(t, err)
  91. assert.Len(t, milestones, repo.NumClosedMilestones)
  92. values = make([]int, len(milestones))
  93. for i, milestone := range milestones {
  94. values[i] = sortCond(milestone)
  95. }
  96. assert.True(t, sort.IntsAreSorted(values))
  97. }
  98. }
  99. test("furthestduedate", func(milestone *Milestone) int {
  100. return -int(milestone.DeadlineUnix)
  101. })
  102. test("leastcomplete", func(milestone *Milestone) int {
  103. return milestone.Completeness
  104. })
  105. test("mostcomplete", func(milestone *Milestone) int {
  106. return -milestone.Completeness
  107. })
  108. test("leastissues", func(milestone *Milestone) int {
  109. return milestone.NumIssues
  110. })
  111. test("mostissues", func(milestone *Milestone) int {
  112. return -milestone.NumIssues
  113. })
  114. test("soonestduedate", func(milestone *Milestone) int {
  115. return int(milestone.DeadlineUnix)
  116. })
  117. }
  118. func TestUpdateMilestone(t *testing.T) {
  119. assert.NoError(t, PrepareTestDatabase())
  120. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  121. milestone.Name = "newMilestoneName"
  122. milestone.Content = "newMilestoneContent"
  123. assert.NoError(t, UpdateMilestone(milestone))
  124. AssertExistsAndLoadBean(t, milestone)
  125. CheckConsistencyFor(t, &Milestone{})
  126. }
  127. func TestCountRepoMilestones(t *testing.T) {
  128. assert.NoError(t, PrepareTestDatabase())
  129. test := func(repoID int64) {
  130. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  131. count, err := countRepoMilestones(x, repoID)
  132. assert.NoError(t, err)
  133. assert.EqualValues(t, repo.NumMilestones, count)
  134. }
  135. test(1)
  136. test(2)
  137. test(3)
  138. count, err := countRepoMilestones(x, NonexistentID)
  139. assert.NoError(t, err)
  140. assert.EqualValues(t, 0, count)
  141. }
  142. func TestCountRepoClosedMilestones(t *testing.T) {
  143. assert.NoError(t, PrepareTestDatabase())
  144. test := func(repoID int64) {
  145. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  146. count, err := CountRepoClosedMilestones(repoID)
  147. assert.NoError(t, err)
  148. assert.EqualValues(t, repo.NumClosedMilestones, count)
  149. }
  150. test(1)
  151. test(2)
  152. test(3)
  153. count, err := CountRepoClosedMilestones(NonexistentID)
  154. assert.NoError(t, err)
  155. assert.EqualValues(t, 0, count)
  156. }
  157. func TestMilestoneStats(t *testing.T) {
  158. assert.NoError(t, PrepareTestDatabase())
  159. test := func(repoID int64) {
  160. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  161. open, closed, err := MilestoneStats(repoID)
  162. assert.NoError(t, err)
  163. assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, open)
  164. assert.EqualValues(t, repo.NumClosedMilestones, closed)
  165. }
  166. test(1)
  167. test(2)
  168. test(3)
  169. open, closed, err := MilestoneStats(NonexistentID)
  170. assert.NoError(t, err)
  171. assert.EqualValues(t, 0, open)
  172. assert.EqualValues(t, 0, closed)
  173. }
  174. func TestChangeMilestoneStatus(t *testing.T) {
  175. assert.NoError(t, PrepareTestDatabase())
  176. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  177. assert.NoError(t, ChangeMilestoneStatus(milestone, true))
  178. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
  179. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  180. assert.NoError(t, ChangeMilestoneStatus(milestone, false))
  181. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
  182. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  183. }
  184. func TestChangeMilestoneIssueStats(t *testing.T) {
  185. assert.NoError(t, PrepareTestDatabase())
  186. issue := AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
  187. "is_closed=0").(*Issue)
  188. issue.IsClosed = true
  189. _, err := x.Cols("is_closed").Update(issue)
  190. assert.NoError(t, err)
  191. assert.NoError(t, changeMilestoneIssueStats(x.NewSession(), issue))
  192. CheckConsistencyFor(t, &Milestone{})
  193. issue.IsClosed = false
  194. _, err = x.Cols("is_closed").Update(issue)
  195. assert.NoError(t, err)
  196. assert.NoError(t, changeMilestoneIssueStats(x.NewSession(), issue))
  197. CheckConsistencyFor(t, &Milestone{})
  198. }
  199. func TestChangeMilestoneAssign(t *testing.T) {
  200. assert.NoError(t, PrepareTestDatabase())
  201. issue := AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
  202. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  203. oldMilestoneID := issue.MilestoneID
  204. issue.MilestoneID = 2
  205. assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID))
  206. AssertExistsAndLoadBean(t, &Comment{
  207. IssueID: issue.ID,
  208. Type: CommentTypeMilestone,
  209. MilestoneID: issue.MilestoneID,
  210. OldMilestoneID: oldMilestoneID,
  211. })
  212. CheckConsistencyFor(t, &Milestone{}, &Issue{})
  213. }
  214. func TestDeleteMilestoneByRepoID(t *testing.T) {
  215. assert.NoError(t, PrepareTestDatabase())
  216. assert.NoError(t, DeleteMilestoneByRepoID(1, 1))
  217. AssertNotExistsBean(t, &Milestone{ID: 1})
  218. CheckConsistencyFor(t, &Repository{ID: 1})
  219. assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
  220. }