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.

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