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.

240 lines
7.3 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. api "code.gitea.io/sdk/gitea"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestMilestone_State(t *testing.T) {
  13. assert.Equal(t, api.StateOpen, (&Milestone{IsClosed: false}).State())
  14. assert.Equal(t, api.StateClosed, (&Milestone{IsClosed: true}).State())
  15. }
  16. func TestMilestone_APIFormat(t *testing.T) {
  17. milestone := &Milestone{
  18. ID: 3,
  19. RepoID: 4,
  20. Name: "milestoneName",
  21. Content: "milestoneContent",
  22. IsClosed: false,
  23. NumOpenIssues: 5,
  24. NumClosedIssues: 6,
  25. Deadline: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
  26. }
  27. assert.Equal(t, api.Milestone{
  28. ID: milestone.ID,
  29. State: api.StateOpen,
  30. Title: milestone.Name,
  31. Description: milestone.Content,
  32. OpenIssues: milestone.NumOpenIssues,
  33. ClosedIssues: milestone.NumClosedIssues,
  34. Deadline: &milestone.Deadline,
  35. }, *milestone.APIFormat())
  36. }
  37. func TestNewMilestone(t *testing.T) {
  38. assert.NoError(t, PrepareTestDatabase())
  39. milestone := &Milestone{
  40. RepoID: 1,
  41. Name: "milestoneName",
  42. Content: "milestoneContent",
  43. }
  44. assert.NoError(t, NewMilestone(milestone))
  45. AssertExistsAndLoadBean(t, milestone)
  46. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  47. }
  48. func TestGetMilestoneByRepoID(t *testing.T) {
  49. assert.NoError(t, PrepareTestDatabase())
  50. milestone, err := GetMilestoneByRepoID(1, 1)
  51. assert.NoError(t, err)
  52. assert.EqualValues(t, 1, milestone.ID)
  53. assert.EqualValues(t, 1, milestone.RepoID)
  54. _, err = GetMilestoneByRepoID(NonexistentID, NonexistentID)
  55. assert.True(t, IsErrMilestoneNotExist(err))
  56. }
  57. func TestGetMilestonesByRepoID(t *testing.T) {
  58. assert.NoError(t, PrepareTestDatabase())
  59. test := func(repoID int64) {
  60. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  61. milestones, err := GetMilestonesByRepoID(repo.ID)
  62. assert.NoError(t, err)
  63. assert.Len(t, milestones, repo.NumMilestones)
  64. for _, milestone := range milestones {
  65. assert.EqualValues(t, repoID, milestone.RepoID)
  66. }
  67. }
  68. test(1)
  69. test(2)
  70. test(3)
  71. milestones, err := GetMilestonesByRepoID(NonexistentID)
  72. assert.NoError(t, err)
  73. assert.Len(t, milestones, 0)
  74. }
  75. func TestGetMilestones(t *testing.T) {
  76. assert.NoError(t, PrepareTestDatabase())
  77. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  78. test := func(sortType string, sortCond func(*Milestone) int) {
  79. for _, page := range []int{0, 1} {
  80. milestones, err := GetMilestones(repo.ID, page, false, sortType)
  81. assert.NoError(t, err)
  82. assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones)
  83. values := make([]int, len(milestones))
  84. for i, milestone := range milestones {
  85. values[i] = sortCond(milestone)
  86. }
  87. assert.True(t, sort.IntsAreSorted(values))
  88. milestones, err = GetMilestones(repo.ID, page, true, sortType)
  89. assert.NoError(t, err)
  90. assert.Len(t, milestones, repo.NumClosedMilestones)
  91. values = make([]int, len(milestones))
  92. for i, milestone := range milestones {
  93. values[i] = sortCond(milestone)
  94. }
  95. assert.True(t, sort.IntsAreSorted(values))
  96. }
  97. }
  98. test("furthestduedate", func(milestone *Milestone) int {
  99. return -int(milestone.DeadlineUnix)
  100. })
  101. test("leastcomplete", func(milestone *Milestone) int {
  102. return milestone.Completeness
  103. })
  104. test("mostcomplete", func(milestone *Milestone) int {
  105. return -milestone.Completeness
  106. })
  107. test("leastissues", func(milestone *Milestone) int {
  108. return milestone.NumIssues
  109. })
  110. test("mostissues", func(milestone *Milestone) int {
  111. return -milestone.NumIssues
  112. })
  113. test("soonestduedate", func(milestone *Milestone) int {
  114. return int(milestone.DeadlineUnix)
  115. })
  116. }
  117. func TestUpdateMilestone(t *testing.T) {
  118. assert.NoError(t, PrepareTestDatabase())
  119. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  120. milestone.Name = "newMilestoneName"
  121. milestone.Content = "newMilestoneContent"
  122. assert.NoError(t, UpdateMilestone(milestone))
  123. AssertExistsAndLoadBean(t, milestone)
  124. CheckConsistencyFor(t, &Milestone{})
  125. }
  126. func TestCountRepoMilestones(t *testing.T) {
  127. assert.NoError(t, PrepareTestDatabase())
  128. test := func(repoID int64) {
  129. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  130. assert.EqualValues(t, repo.NumMilestones, countRepoMilestones(x, repoID))
  131. }
  132. test(1)
  133. test(2)
  134. test(3)
  135. assert.EqualValues(t, 0, countRepoMilestones(x, NonexistentID))
  136. }
  137. func TestCountRepoClosedMilestones(t *testing.T) {
  138. assert.NoError(t, PrepareTestDatabase())
  139. test := func(repoID int64) {
  140. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  141. assert.EqualValues(t, repo.NumClosedMilestones, CountRepoClosedMilestones(repoID))
  142. }
  143. test(1)
  144. test(2)
  145. test(3)
  146. assert.EqualValues(t, 0, countRepoMilestones(x, NonexistentID))
  147. }
  148. func TestMilestoneStats(t *testing.T) {
  149. assert.NoError(t, PrepareTestDatabase())
  150. test := func(repoID int64) {
  151. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  152. open, closed := MilestoneStats(repoID)
  153. assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, open)
  154. assert.EqualValues(t, repo.NumClosedMilestones, closed)
  155. }
  156. test(1)
  157. test(2)
  158. test(3)
  159. open, closed := MilestoneStats(NonexistentID)
  160. assert.EqualValues(t, 0, open)
  161. assert.EqualValues(t, 0, closed)
  162. }
  163. func TestChangeMilestoneStatus(t *testing.T) {
  164. assert.NoError(t, PrepareTestDatabase())
  165. milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
  166. assert.NoError(t, ChangeMilestoneStatus(milestone, true))
  167. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
  168. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  169. assert.NoError(t, ChangeMilestoneStatus(milestone, false))
  170. AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
  171. CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
  172. }
  173. func TestChangeMilestoneIssueStats(t *testing.T) {
  174. assert.NoError(t, PrepareTestDatabase())
  175. issue := AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
  176. "is_closed=0").(*Issue)
  177. issue.IsClosed = true
  178. _, err := x.Cols("is_closed").Update(issue)
  179. assert.NoError(t, err)
  180. assert.NoError(t, changeMilestoneIssueStats(x.NewSession(), issue))
  181. CheckConsistencyFor(t, &Milestone{})
  182. issue.IsClosed = false
  183. _, err = x.Cols("is_closed").Update(issue)
  184. assert.NoError(t, err)
  185. assert.NoError(t, changeMilestoneIssueStats(x.NewSession(), issue))
  186. CheckConsistencyFor(t, &Milestone{})
  187. }
  188. func TestChangeMilestoneAssign(t *testing.T) {
  189. assert.NoError(t, PrepareTestDatabase())
  190. issue := AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
  191. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  192. oldMilestoneID := issue.MilestoneID
  193. issue.MilestoneID = 2
  194. assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID))
  195. AssertExistsAndLoadBean(t, &Comment{
  196. IssueID: issue.ID,
  197. Type: CommentTypeMilestone,
  198. MilestoneID: issue.MilestoneID,
  199. OldMilestoneID: oldMilestoneID,
  200. })
  201. CheckConsistencyFor(t, &Milestone{}, &Issue{})
  202. }
  203. func TestDeleteMilestoneByRepoID(t *testing.T) {
  204. assert.NoError(t, PrepareTestDatabase())
  205. assert.NoError(t, DeleteMilestoneByRepoID(1, 1))
  206. AssertNotExistsBean(t, &Milestone{ID: 1})
  207. CheckConsistencyFor(t, &Repository{ID: 1})
  208. assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
  209. }