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

265 lines
7.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. "strconv"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestPullRequest_LoadAttributes(t *testing.T) {
  12. assert.NoError(t, PrepareTestDatabase())
  13. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  14. assert.NoError(t, pr.LoadAttributes())
  15. assert.NotNil(t, pr.Merger)
  16. assert.Equal(t, pr.MergerID, pr.Merger.ID)
  17. }
  18. func TestPullRequest_LoadIssue(t *testing.T) {
  19. assert.NoError(t, PrepareTestDatabase())
  20. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  21. assert.NoError(t, pr.LoadIssue())
  22. assert.NotNil(t, pr.Issue)
  23. assert.Equal(t, int64(2), pr.Issue.ID)
  24. assert.NoError(t, pr.LoadIssue())
  25. assert.NotNil(t, pr.Issue)
  26. assert.Equal(t, int64(2), pr.Issue.ID)
  27. }
  28. func TestPullRequest_APIFormat(t *testing.T) {
  29. assert.NoError(t, PrepareTestDatabase())
  30. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  31. assert.NoError(t, pr.LoadAttributes())
  32. assert.NoError(t, pr.LoadIssue())
  33. apiPullRequest := pr.APIFormat()
  34. assert.NotNil(t, apiPullRequest)
  35. assert.Nil(t, apiPullRequest.Head)
  36. }
  37. func TestPullRequest_GetBaseRepo(t *testing.T) {
  38. assert.NoError(t, PrepareTestDatabase())
  39. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  40. assert.NoError(t, pr.GetBaseRepo())
  41. assert.NotNil(t, pr.BaseRepo)
  42. assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
  43. assert.NoError(t, pr.GetBaseRepo())
  44. assert.NotNil(t, pr.BaseRepo)
  45. assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
  46. }
  47. func TestPullRequest_GetHeadRepo(t *testing.T) {
  48. assert.NoError(t, PrepareTestDatabase())
  49. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  50. assert.NoError(t, pr.GetHeadRepo())
  51. assert.NotNil(t, pr.HeadRepo)
  52. assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
  53. }
  54. // TODO TestMerge
  55. // TODO TestNewPullRequest
  56. func TestPullRequestsNewest(t *testing.T) {
  57. assert.NoError(t, PrepareTestDatabase())
  58. prs, count, err := PullRequests(1, &PullRequestsOptions{
  59. Page: 1,
  60. State: "open",
  61. SortType: "newest",
  62. Labels: []string{},
  63. })
  64. assert.NoError(t, err)
  65. assert.Equal(t, int64(2), count)
  66. if assert.Len(t, prs, 2) {
  67. assert.Equal(t, int64(2), prs[0].ID)
  68. assert.Equal(t, int64(1), prs[1].ID)
  69. }
  70. }
  71. func TestPullRequestsOldest(t *testing.T) {
  72. assert.NoError(t, PrepareTestDatabase())
  73. prs, count, err := PullRequests(1, &PullRequestsOptions{
  74. Page: 1,
  75. State: "open",
  76. SortType: "oldest",
  77. Labels: []string{},
  78. })
  79. assert.NoError(t, err)
  80. assert.Equal(t, int64(2), count)
  81. if assert.Len(t, prs, 2) {
  82. assert.Equal(t, int64(1), prs[0].ID)
  83. assert.Equal(t, int64(2), prs[1].ID)
  84. }
  85. }
  86. func TestGetUnmergedPullRequest(t *testing.T) {
  87. assert.NoError(t, PrepareTestDatabase())
  88. pr, err := GetUnmergedPullRequest(1, 1, "branch2", "master")
  89. assert.NoError(t, err)
  90. assert.Equal(t, int64(2), pr.ID)
  91. _, err = GetUnmergedPullRequest(1, 9223372036854775807, "branch1", "master")
  92. assert.Error(t, err)
  93. assert.True(t, IsErrPullRequestNotExist(err))
  94. }
  95. func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
  96. assert.NoError(t, PrepareTestDatabase())
  97. prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")
  98. assert.NoError(t, err)
  99. assert.Len(t, prs, 1)
  100. for _, pr := range prs {
  101. assert.Equal(t, int64(1), pr.HeadRepoID)
  102. assert.Equal(t, "branch2", pr.HeadBranch)
  103. }
  104. }
  105. func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
  106. assert.NoError(t, PrepareTestDatabase())
  107. prs, err := GetUnmergedPullRequestsByBaseInfo(1, "master")
  108. assert.NoError(t, err)
  109. assert.Len(t, prs, 1)
  110. pr := prs[0]
  111. assert.Equal(t, int64(2), pr.ID)
  112. assert.Equal(t, int64(1), pr.BaseRepoID)
  113. assert.Equal(t, "master", pr.BaseBranch)
  114. }
  115. func TestGetPullRequestByIndex(t *testing.T) {
  116. assert.NoError(t, PrepareTestDatabase())
  117. pr, err := GetPullRequestByIndex(1, 2)
  118. assert.NoError(t, err)
  119. assert.Equal(t, int64(1), pr.BaseRepoID)
  120. assert.Equal(t, int64(2), pr.Index)
  121. _, err = GetPullRequestByIndex(9223372036854775807, 9223372036854775807)
  122. assert.Error(t, err)
  123. assert.True(t, IsErrPullRequestNotExist(err))
  124. }
  125. func TestGetPullRequestByID(t *testing.T) {
  126. assert.NoError(t, PrepareTestDatabase())
  127. pr, err := GetPullRequestByID(1)
  128. assert.NoError(t, err)
  129. assert.Equal(t, int64(1), pr.ID)
  130. assert.Equal(t, int64(2), pr.IssueID)
  131. _, err = GetPullRequestByID(9223372036854775807)
  132. assert.Error(t, err)
  133. assert.True(t, IsErrPullRequestNotExist(err))
  134. }
  135. func TestGetPullRequestByIssueID(t *testing.T) {
  136. assert.NoError(t, PrepareTestDatabase())
  137. pr, err := GetPullRequestByIssueID(2)
  138. assert.NoError(t, err)
  139. assert.Equal(t, int64(2), pr.IssueID)
  140. _, err = GetPullRequestByIssueID(9223372036854775807)
  141. assert.Error(t, err)
  142. assert.True(t, IsErrPullRequestNotExist(err))
  143. }
  144. func TestPullRequest_Update(t *testing.T) {
  145. assert.NoError(t, PrepareTestDatabase())
  146. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  147. pr.BaseBranch = "baseBranch"
  148. pr.HeadBranch = "headBranch"
  149. pr.Update()
  150. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest)
  151. assert.Equal(t, "baseBranch", pr.BaseBranch)
  152. assert.Equal(t, "headBranch", pr.HeadBranch)
  153. CheckConsistencyFor(t, pr)
  154. }
  155. func TestPullRequest_UpdateCols(t *testing.T) {
  156. assert.NoError(t, PrepareTestDatabase())
  157. pr := &PullRequest{
  158. ID: 1,
  159. BaseBranch: "baseBranch",
  160. HeadBranch: "headBranch",
  161. }
  162. pr.UpdateCols("head_branch")
  163. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  164. assert.Equal(t, "master", pr.BaseBranch)
  165. assert.Equal(t, "headBranch", pr.HeadBranch)
  166. CheckConsistencyFor(t, pr)
  167. }
  168. // TODO TestPullRequest_UpdatePatch
  169. // TODO TestPullRequest_PushToBaseRepo
  170. func TestPullRequest_AddToTaskQueue(t *testing.T) {
  171. assert.NoError(t, PrepareTestDatabase())
  172. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  173. pr.AddToTaskQueue()
  174. select {
  175. case id := <-pullRequestQueue.Queue():
  176. assert.EqualValues(t, strconv.FormatInt(pr.ID, 10), id)
  177. case <-time.After(time.Second):
  178. assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
  179. }
  180. assert.True(t, pullRequestQueue.Exist(pr.ID))
  181. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  182. assert.Equal(t, PullRequestStatusChecking, pr.Status)
  183. }
  184. func TestPullRequestList_LoadAttributes(t *testing.T) {
  185. assert.NoError(t, PrepareTestDatabase())
  186. prs := []*PullRequest{
  187. AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest),
  188. AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest),
  189. }
  190. assert.NoError(t, PullRequestList(prs).LoadAttributes())
  191. for _, pr := range prs {
  192. assert.NotNil(t, pr.Issue)
  193. assert.Equal(t, pr.IssueID, pr.Issue.ID)
  194. }
  195. assert.NoError(t, PullRequestList([]*PullRequest{}).LoadAttributes())
  196. }
  197. // TODO TestAddTestPullRequestTask
  198. func TestPullRequest_IsWorkInProgress(t *testing.T) {
  199. assert.NoError(t, PrepareTestDatabase())
  200. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
  201. pr.LoadIssue()
  202. assert.False(t, pr.IsWorkInProgress())
  203. pr.Issue.Title = "WIP: " + pr.Issue.Title
  204. assert.True(t, pr.IsWorkInProgress())
  205. pr.Issue.Title = "[wip]: " + pr.Issue.Title
  206. assert.True(t, pr.IsWorkInProgress())
  207. }
  208. func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {
  209. assert.NoError(t, PrepareTestDatabase())
  210. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
  211. pr.LoadIssue()
  212. assert.Empty(t, pr.GetWorkInProgressPrefix())
  213. original := pr.Issue.Title
  214. pr.Issue.Title = "WIP: " + original
  215. assert.Equal(t, "WIP:", pr.GetWorkInProgressPrefix())
  216. pr.Issue.Title = "[wip] " + original
  217. assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix())
  218. }
上海开阖软件有限公司 沪ICP备12045867号-1