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.

236 lines
6.7 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestPullRequest_LoadAttributes(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  12. assert.NoError(t, pr.LoadAttributes())
  13. assert.NotNil(t, pr.Merger)
  14. assert.Equal(t, pr.MergerID, pr.Merger.ID)
  15. }
  16. func TestPullRequest_LoadIssue(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  19. assert.NoError(t, pr.LoadIssue())
  20. assert.NotNil(t, pr.Issue)
  21. assert.Equal(t, int64(2), pr.Issue.ID)
  22. assert.NoError(t, pr.LoadIssue())
  23. assert.NotNil(t, pr.Issue)
  24. assert.Equal(t, int64(2), pr.Issue.ID)
  25. }
  26. func TestPullRequest_LoadBaseRepo(t *testing.T) {
  27. assert.NoError(t, PrepareTestDatabase())
  28. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  29. assert.NoError(t, pr.LoadBaseRepo())
  30. assert.NotNil(t, pr.BaseRepo)
  31. assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
  32. assert.NoError(t, pr.LoadBaseRepo())
  33. assert.NotNil(t, pr.BaseRepo)
  34. assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
  35. }
  36. func TestPullRequest_LoadHeadRepo(t *testing.T) {
  37. assert.NoError(t, PrepareTestDatabase())
  38. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  39. assert.NoError(t, pr.LoadHeadRepo())
  40. assert.NotNil(t, pr.HeadRepo)
  41. assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
  42. }
  43. // TODO TestMerge
  44. // TODO TestNewPullRequest
  45. func TestPullRequestsNewest(t *testing.T) {
  46. assert.NoError(t, PrepareTestDatabase())
  47. prs, count, err := PullRequests(1, &PullRequestsOptions{
  48. ListOptions: ListOptions{
  49. Page: 1,
  50. },
  51. State: "open",
  52. SortType: "newest",
  53. Labels: []string{},
  54. })
  55. assert.NoError(t, err)
  56. assert.EqualValues(t, 3, count)
  57. if assert.Len(t, prs, 3) {
  58. assert.EqualValues(t, 5, prs[0].ID)
  59. assert.EqualValues(t, 2, prs[1].ID)
  60. assert.EqualValues(t, 1, prs[2].ID)
  61. }
  62. }
  63. func TestPullRequestsOldest(t *testing.T) {
  64. assert.NoError(t, PrepareTestDatabase())
  65. prs, count, err := PullRequests(1, &PullRequestsOptions{
  66. ListOptions: ListOptions{
  67. Page: 1,
  68. },
  69. State: "open",
  70. SortType: "oldest",
  71. Labels: []string{},
  72. })
  73. assert.NoError(t, err)
  74. assert.EqualValues(t, 3, count)
  75. if assert.Len(t, prs, 3) {
  76. assert.EqualValues(t, 1, prs[0].ID)
  77. assert.EqualValues(t, 2, prs[1].ID)
  78. assert.EqualValues(t, 5, prs[2].ID)
  79. }
  80. }
  81. func TestGetUnmergedPullRequest(t *testing.T) {
  82. assert.NoError(t, PrepareTestDatabase())
  83. pr, err := GetUnmergedPullRequest(1, 1, "branch2", "master")
  84. assert.NoError(t, err)
  85. assert.Equal(t, int64(2), pr.ID)
  86. _, err = GetUnmergedPullRequest(1, 9223372036854775807, "branch1", "master")
  87. assert.Error(t, err)
  88. assert.True(t, IsErrPullRequestNotExist(err))
  89. }
  90. func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
  91. assert.NoError(t, PrepareTestDatabase())
  92. prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")
  93. assert.NoError(t, err)
  94. assert.Len(t, prs, 1)
  95. for _, pr := range prs {
  96. assert.Equal(t, int64(1), pr.HeadRepoID)
  97. assert.Equal(t, "branch2", pr.HeadBranch)
  98. }
  99. }
  100. func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
  101. assert.NoError(t, PrepareTestDatabase())
  102. prs, err := GetUnmergedPullRequestsByBaseInfo(1, "master")
  103. assert.NoError(t, err)
  104. assert.Len(t, prs, 1)
  105. pr := prs[0]
  106. assert.Equal(t, int64(2), pr.ID)
  107. assert.Equal(t, int64(1), pr.BaseRepoID)
  108. assert.Equal(t, "master", pr.BaseBranch)
  109. }
  110. func TestGetPullRequestByIndex(t *testing.T) {
  111. assert.NoError(t, PrepareTestDatabase())
  112. pr, err := GetPullRequestByIndex(1, 2)
  113. assert.NoError(t, err)
  114. assert.Equal(t, int64(1), pr.BaseRepoID)
  115. assert.Equal(t, int64(2), pr.Index)
  116. _, err = GetPullRequestByIndex(9223372036854775807, 9223372036854775807)
  117. assert.Error(t, err)
  118. assert.True(t, IsErrPullRequestNotExist(err))
  119. }
  120. func TestGetPullRequestByID(t *testing.T) {
  121. assert.NoError(t, PrepareTestDatabase())
  122. pr, err := GetPullRequestByID(1)
  123. assert.NoError(t, err)
  124. assert.Equal(t, int64(1), pr.ID)
  125. assert.Equal(t, int64(2), pr.IssueID)
  126. _, err = GetPullRequestByID(9223372036854775807)
  127. assert.Error(t, err)
  128. assert.True(t, IsErrPullRequestNotExist(err))
  129. }
  130. func TestGetPullRequestByIssueID(t *testing.T) {
  131. assert.NoError(t, PrepareTestDatabase())
  132. pr, err := GetPullRequestByIssueID(2)
  133. assert.NoError(t, err)
  134. assert.Equal(t, int64(2), pr.IssueID)
  135. _, err = GetPullRequestByIssueID(9223372036854775807)
  136. assert.Error(t, err)
  137. assert.True(t, IsErrPullRequestNotExist(err))
  138. }
  139. func TestPullRequest_Update(t *testing.T) {
  140. assert.NoError(t, PrepareTestDatabase())
  141. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  142. pr.BaseBranch = "baseBranch"
  143. pr.HeadBranch = "headBranch"
  144. pr.Update()
  145. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest)
  146. assert.Equal(t, "baseBranch", pr.BaseBranch)
  147. assert.Equal(t, "headBranch", pr.HeadBranch)
  148. CheckConsistencyFor(t, pr)
  149. }
  150. func TestPullRequest_UpdateCols(t *testing.T) {
  151. assert.NoError(t, PrepareTestDatabase())
  152. pr := &PullRequest{
  153. ID: 1,
  154. BaseBranch: "baseBranch",
  155. HeadBranch: "headBranch",
  156. }
  157. assert.NoError(t, pr.UpdateCols("head_branch"))
  158. pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
  159. assert.Equal(t, "master", pr.BaseBranch)
  160. assert.Equal(t, "headBranch", pr.HeadBranch)
  161. CheckConsistencyFor(t, pr)
  162. }
  163. func TestPullRequestList_LoadAttributes(t *testing.T) {
  164. assert.NoError(t, PrepareTestDatabase())
  165. prs := []*PullRequest{
  166. AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest),
  167. AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest),
  168. }
  169. assert.NoError(t, PullRequestList(prs).LoadAttributes())
  170. for _, pr := range prs {
  171. assert.NotNil(t, pr.Issue)
  172. assert.Equal(t, pr.IssueID, pr.Issue.ID)
  173. }
  174. assert.NoError(t, PullRequestList([]*PullRequest{}).LoadAttributes())
  175. }
  176. // TODO TestAddTestPullRequestTask
  177. func TestPullRequest_IsWorkInProgress(t *testing.T) {
  178. assert.NoError(t, PrepareTestDatabase())
  179. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
  180. pr.LoadIssue()
  181. assert.False(t, pr.IsWorkInProgress())
  182. pr.Issue.Title = "WIP: " + pr.Issue.Title
  183. assert.True(t, pr.IsWorkInProgress())
  184. pr.Issue.Title = "[wip]: " + pr.Issue.Title
  185. assert.True(t, pr.IsWorkInProgress())
  186. }
  187. func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {
  188. assert.NoError(t, PrepareTestDatabase())
  189. pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
  190. pr.LoadIssue()
  191. assert.Empty(t, pr.GetWorkInProgressPrefix())
  192. original := pr.Issue.Title
  193. pr.Issue.Title = "WIP: " + original
  194. assert.Equal(t, "WIP:", pr.GetWorkInProgressPrefix())
  195. pr.Issue.Title = "[wip] " + original
  196. assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix())
  197. }