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.

239 lines
6.8 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. "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. // TODO TestPullRequest_APIFormat
  29. func TestPullRequest_GetBaseRepo(t *testing.T) {
  30. assert.NoError(t, PrepareTestDatabase())
  31. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  32. assert.NoError(t, pr.GetBaseRepo())
  33. assert.NotNil(t, pr.BaseRepo)
  34. assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
  35. assert.NoError(t, pr.GetBaseRepo())
  36. assert.NotNil(t, pr.BaseRepo)
  37. assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
  38. }
  39. func TestPullRequest_GetHeadRepo(t *testing.T) {
  40. assert.NoError(t, PrepareTestDatabase())
  41. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  42. assert.NoError(t, pr.GetHeadRepo())
  43. assert.NotNil(t, pr.HeadRepo)
  44. assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
  45. }
  46. // TODO TestMerge
  47. // TODO TestNewPullRequest
  48. func TestPullRequestsNewest(t *testing.T) {
  49. assert.NoError(t, PrepareTestDatabase())
  50. prs, count, err := PullRequests(1, &PullRequestsOptions{
  51. Page: 1,
  52. State: "open",
  53. SortType: "newest",
  54. Labels: []string{},
  55. })
  56. assert.NoError(t, err)
  57. assert.Equal(t, int64(2), count)
  58. if assert.Len(t, prs, 2) {
  59. assert.Equal(t, int64(2), prs[0].ID)
  60. assert.Equal(t, int64(1), prs[1].ID)
  61. }
  62. }
  63. func TestPullRequestsOldest(t *testing.T) {
  64. assert.NoError(t, PrepareTestDatabase())
  65. prs, count, err := PullRequests(1, &PullRequestsOptions{
  66. Page: 1,
  67. State: "open",
  68. SortType: "oldest",
  69. Labels: []string{},
  70. })
  71. assert.NoError(t, err)
  72. assert.Equal(t, int64(2), count)
  73. if assert.Len(t, prs, 2) {
  74. assert.Equal(t, int64(1), prs[0].ID)
  75. assert.Equal(t, int64(2), prs[1].ID)
  76. }
  77. }
  78. func TestGetUnmergedPullRequest(t *testing.T) {
  79. assert.NoError(t, PrepareTestDatabase())
  80. pr, err := GetUnmergedPullRequest(1, 1, "branch2", "master")
  81. assert.NoError(t, err)
  82. assert.Equal(t, int64(2), pr.ID)
  83. pr, err = GetUnmergedPullRequest(1, 9223372036854775807, "branch1", "master")
  84. assert.Error(t, err)
  85. assert.True(t, IsErrPullRequestNotExist(err))
  86. }
  87. func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
  88. assert.NoError(t, PrepareTestDatabase())
  89. prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")
  90. assert.NoError(t, err)
  91. assert.Len(t, prs, 1)
  92. for _, pr := range prs {
  93. assert.Equal(t, int64(1), pr.HeadRepoID)
  94. assert.Equal(t, "branch2", pr.HeadBranch)
  95. }
  96. }
  97. func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
  98. assert.NoError(t, PrepareTestDatabase())
  99. prs, err := GetUnmergedPullRequestsByBaseInfo(1, "master")
  100. assert.NoError(t, err)
  101. assert.Len(t, prs, 1)
  102. pr := prs[0]
  103. assert.Equal(t, int64(2), pr.ID)
  104. assert.Equal(t, int64(1), pr.BaseRepoID)
  105. assert.Equal(t, "master", pr.BaseBranch)
  106. }
  107. func TestGetPullRequestByIndex(t *testing.T) {
  108. assert.NoError(t, PrepareTestDatabase())
  109. pr, err := GetPullRequestByIndex(1, 2)
  110. assert.NoError(t, err)
  111. assert.Equal(t, int64(1), pr.BaseRepoID)
  112. assert.Equal(t, int64(2), pr.Index)
  113. pr, err = GetPullRequestByIndex(9223372036854775807, 9223372036854775807)
  114. assert.Error(t, err)
  115. assert.True(t, IsErrPullRequestNotExist(err))
  116. }
  117. func TestGetPullRequestByID(t *testing.T) {
  118. assert.NoError(t, PrepareTestDatabase())
  119. pr, err := GetPullRequestByID(1)
  120. assert.NoError(t, err)
  121. assert.Equal(t, int64(1), pr.ID)
  122. assert.Equal(t, int64(2), pr.IssueID)
  123. _, err = GetPullRequestByID(9223372036854775807)
  124. assert.Error(t, err)
  125. assert.True(t, IsErrPullRequestNotExist(err))
  126. }
  127. func TestGetPullRequestByIssueID(t *testing.T) {
  128. assert.NoError(t, PrepareTestDatabase())
  129. pr, err := GetPullRequestByIssueID(2)
  130. assert.NoError(t, err)
  131. assert.Equal(t, int64(2), pr.IssueID)
  132. pr, err = GetPullRequestByIssueID(9223372036854775807)
  133. assert.Error(t, err)
  134. assert.True(t, IsErrPullRequestNotExist(err))
  135. }
  136. func TestPullRequest_Update(t *testing.T) {
  137. assert.NoError(t, PrepareTestDatabase())
  138. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  139. pr.BaseBranch = "baseBranch"
  140. pr.HeadBranch = "headBranch"
  141. pr.Update()
  142. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest)
  143. assert.Equal(t, "baseBranch", pr.BaseBranch)
  144. assert.Equal(t, "headBranch", pr.HeadBranch)
  145. CheckConsistencyFor(t, pr)
  146. }
  147. func TestPullRequest_UpdateCols(t *testing.T) {
  148. assert.NoError(t, PrepareTestDatabase())
  149. pr := &PullRequest{
  150. ID: 1,
  151. BaseBranch: "baseBranch",
  152. HeadBranch: "headBranch",
  153. }
  154. pr.UpdateCols("head_branch")
  155. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  156. assert.Equal(t, "master", pr.BaseBranch)
  157. assert.Equal(t, "headBranch", pr.HeadBranch)
  158. CheckConsistencyFor(t, pr)
  159. }
  160. // TODO TestPullRequest_UpdatePatch
  161. // TODO TestPullRequest_PushToBaseRepo
  162. func TestPullRequest_AddToTaskQueue(t *testing.T) {
  163. assert.NoError(t, PrepareTestDatabase())
  164. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  165. pr.AddToTaskQueue()
  166. select {
  167. case id := <-pullRequestQueue.Queue():
  168. assert.EqualValues(t, strconv.FormatInt(pr.ID, 10), id)
  169. case <-time.After(time.Second):
  170. assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
  171. }
  172. assert.True(t, pullRequestQueue.Exist(pr.ID))
  173. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  174. assert.Equal(t, PullRequestStatusChecking, pr.Status)
  175. }
  176. func TestPullRequestList_LoadAttributes(t *testing.T) {
  177. assert.NoError(t, PrepareTestDatabase())
  178. prs := []*PullRequest{
  179. AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest),
  180. AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest),
  181. }
  182. assert.NoError(t, PullRequestList(prs).LoadAttributes())
  183. for _, pr := range prs {
  184. assert.NotNil(t, pr.Issue)
  185. assert.Equal(t, pr.IssueID, pr.Issue.ID)
  186. }
  187. assert.NoError(t, PullRequestList([]*PullRequest{}).LoadAttributes())
  188. }
  189. // TODO TestAddTestPullRequestTask
  190. func TestChangeUsernameInPullRequests(t *testing.T) {
  191. assert.NoError(t, PrepareTestDatabase())
  192. const newUsername = "newusername"
  193. assert.NoError(t, ChangeUsernameInPullRequests("user1", newUsername))
  194. prs := make([]*PullRequest, 0, 10)
  195. assert.NoError(t, x.Where("head_user_name = ?", newUsername).Find(&prs))
  196. assert.Len(t, prs, 2)
  197. for _, pr := range prs {
  198. assert.Equal(t, newUsername, pr.HeadUserName)
  199. }
  200. CheckConsistencyFor(t, &PullRequest{})
  201. }