2017-01-01 11:15:09 -07:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-01 11:15:09 -07:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
package issues_test
|
2017-01-01 11:15:09 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 03:37:59 -06:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-17 05:34:35 -07:00
|
|
|
|
2017-01-01 11:15:09 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullRequest_LoadAttributes(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.NoError(t, pr.LoadAttributes(db.DefaultContext))
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NotNil(t, pr.Merger)
|
|
|
|
assert.Equal(t, pr.MergerID, pr.Merger.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_LoadIssue(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.NoError(t, pr.LoadIssue(db.DefaultContext))
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.NoError(t, pr.LoadIssue(db.DefaultContext))
|
2017-01-08 20:15:07 -07:00
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:31:55 -07:00
|
|
|
func TestPullRequest_LoadBaseRepo(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NotNil(t, pr.BaseRepo)
|
|
|
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
|
2017-01-08 20:15:07 -07:00
|
|
|
assert.NotNil(t, pr.BaseRepo)
|
|
|
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:31:55 -07:00
|
|
|
func TestPullRequest_LoadHeadRepo(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.NoError(t, pr.LoadHeadRepo(db.DefaultContext))
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NotNil(t, pr.HeadRepo)
|
|
|
|
assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestMerge
|
|
|
|
|
|
|
|
// TODO TestNewPullRequest
|
|
|
|
|
|
|
|
func TestPullRequestsNewest(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
prs, count, err := issues_model.PullRequests(1, &issues_model.PullRequestsOptions{
|
2021-09-24 05:32:56 -06:00
|
|
|
ListOptions: db.ListOptions{
|
2020-01-24 12:00:29 -07:00
|
|
|
Page: 1,
|
|
|
|
},
|
2017-01-01 11:15:09 -07:00
|
|
|
State: "open",
|
|
|
|
SortType: "newest",
|
|
|
|
Labels: []string{},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-01-16 23:03:40 -07:00
|
|
|
assert.EqualValues(t, 3, count)
|
|
|
|
if assert.Len(t, prs, 3) {
|
|
|
|
assert.EqualValues(t, 5, prs[0].ID)
|
|
|
|
assert.EqualValues(t, 2, prs[1].ID)
|
|
|
|
assert.EqualValues(t, 1, prs[2].ID)
|
2017-08-28 03:17:45 -06:00
|
|
|
}
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequestsOldest(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
prs, count, err := issues_model.PullRequests(1, &issues_model.PullRequestsOptions{
|
2021-09-24 05:32:56 -06:00
|
|
|
ListOptions: db.ListOptions{
|
2020-01-24 12:00:29 -07:00
|
|
|
Page: 1,
|
|
|
|
},
|
2017-01-01 11:15:09 -07:00
|
|
|
State: "open",
|
|
|
|
SortType: "oldest",
|
|
|
|
Labels: []string{},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-01-16 23:03:40 -07:00
|
|
|
assert.EqualValues(t, 3, count)
|
|
|
|
if assert.Len(t, prs, 3) {
|
|
|
|
assert.EqualValues(t, 1, prs[0].ID)
|
|
|
|
assert.EqualValues(t, 2, prs[1].ID)
|
|
|
|
assert.EqualValues(t, 5, prs[2].ID)
|
2017-08-28 03:17:45 -06:00
|
|
|
}
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUnmergedPullRequest(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-11-19 01:12:33 -07:00
|
|
|
pr, err := issues_model.GetUnmergedPullRequest(db.DefaultContext, 1, 1, "branch2", "master", issues_model.PullRequestFlowGithub)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), pr.ID)
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
_, err = issues_model.GetUnmergedPullRequest(db.DefaultContext, 1, 9223372036854775807, "branch1", "master", issues_model.PullRequestFlowGithub)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.Error(t, err)
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
2022-01-03 12:45:58 -07:00
|
|
|
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "branch2")
|
2022-01-03 12:45:58 -07:00
|
|
|
assert.NoError(t, err)
|
2023-04-22 15:56:27 -06:00
|
|
|
assert.True(t, exist)
|
2022-01-03 12:45:58 -07:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
exist, err = issues_model.HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "not_exist_branch")
|
2022-01-03 12:45:58 -07:00
|
|
|
assert.NoError(t, err)
|
2023-04-22 15:56:27 -06:00
|
|
|
assert.False(t, exist)
|
2022-01-03 12:45:58 -07:00
|
|
|
}
|
|
|
|
|
2017-01-01 11:15:09 -07:00
|
|
|
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-05-08 00:39:32 -06:00
|
|
|
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(1, "branch2")
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
for _, pr := range prs {
|
|
|
|
assert.Equal(t, int64(1), pr.HeadRepoID)
|
|
|
|
assert.Equal(t, "branch2", pr.HeadBranch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(1, "master")
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
pr := prs[0]
|
|
|
|
assert.Equal(t, int64(2), pr.ID)
|
|
|
|
assert.Equal(t, int64(1), pr.BaseRepoID)
|
|
|
|
assert.Equal(t, "master", pr.BaseBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByIndex(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
pr, err := issues_model.GetPullRequestByIndex(db.DefaultContext, 1, 2)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), pr.BaseRepoID)
|
|
|
|
assert.Equal(t, int64(2), pr.Index)
|
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
_, err = issues_model.GetPullRequestByIndex(db.DefaultContext, 9223372036854775807, 9223372036854775807)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.Error(t, err)
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2021-10-05 08:41:48 -06:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
_, err = issues_model.GetPullRequestByIndex(db.DefaultContext, 1, 0)
|
2021-10-05 08:41:48 -06:00
|
|
|
assert.Error(t, err)
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByID(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
pr, err := issues_model.GetPullRequestByID(db.DefaultContext, 1)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), pr.ID)
|
|
|
|
assert.Equal(t, int64(2), pr.IssueID)
|
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
_, err = issues_model.GetPullRequestByID(db.DefaultContext, 9223372036854775807)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.Error(t, err)
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByIssueID(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
pr, err := issues_model.GetPullRequestByIssueID(db.DefaultContext, 2)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), pr.IssueID)
|
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
_, err = issues_model.GetPullRequestByIssueID(db.DefaultContext, 9223372036854775807)
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.Error(t, err)
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_Update(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2017-02-07 04:47:55 -07:00
|
|
|
pr.BaseBranch = "baseBranch"
|
|
|
|
pr.HeadBranch = "headBranch"
|
2017-01-01 11:15:09 -07:00
|
|
|
pr.Update()
|
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.Equal(t, "baseBranch", pr.BaseBranch)
|
|
|
|
assert.Equal(t, "headBranch", pr.HeadBranch)
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.CheckConsistencyFor(t, pr)
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_UpdateCols(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 03:37:59 -06:00
|
|
|
pr := &issues_model.PullRequest{
|
2017-01-01 11:15:09 -07:00
|
|
|
ID: 1,
|
|
|
|
BaseBranch: "baseBranch",
|
|
|
|
HeadBranch: "headBranch",
|
|
|
|
}
|
2020-01-09 22:50:23 -07:00
|
|
|
assert.NoError(t, pr.UpdateCols("head_branch"))
|
2017-01-01 11:15:09 -07:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2017-01-01 11:15:09 -07:00
|
|
|
assert.Equal(t, "master", pr.BaseBranch)
|
|
|
|
assert.Equal(t, "headBranch", pr.HeadBranch)
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.CheckConsistencyFor(t, pr)
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequestList_LoadAttributes(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 11:15:09 -07:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
prs := []*issues_model.PullRequest{
|
2022-08-15 20:22:25 -06:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.NoError(t, issues_model.PullRequestList(prs).LoadAttributes())
|
2017-01-01 11:15:09 -07:00
|
|
|
for _, pr := range prs {
|
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, pr.IssueID, pr.Issue.ID)
|
|
|
|
}
|
2017-01-08 20:15:07 -07:00
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.NoError(t, issues_model.PullRequestList([]*issues_model.PullRequest{}).LoadAttributes())
|
2017-01-01 11:15:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestAddTestPullRequestTask
|
|
|
|
|
2018-08-13 13:04:39 -06:00
|
|
|
func TestPullRequest_IsWorkInProgress(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-13 13:04:39 -06:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2022-11-19 01:12:33 -07:00
|
|
|
pr.LoadIssue(db.DefaultContext)
|
2018-08-13 13:04:39 -06:00
|
|
|
|
|
|
|
assert.False(t, pr.IsWorkInProgress())
|
|
|
|
|
|
|
|
pr.Issue.Title = "WIP: " + pr.Issue.Title
|
|
|
|
assert.True(t, pr.IsWorkInProgress())
|
|
|
|
|
|
|
|
pr.Issue.Title = "[wip]: " + pr.Issue.Title
|
|
|
|
assert.True(t, pr.IsWorkInProgress())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-13 13:04:39 -06:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2022-11-19 01:12:33 -07:00
|
|
|
pr.LoadIssue(db.DefaultContext)
|
2018-08-13 13:04:39 -06:00
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.Empty(t, pr.GetWorkInProgressPrefix(db.DefaultContext))
|
2018-08-13 13:04:39 -06:00
|
|
|
|
|
|
|
original := pr.Issue.Title
|
|
|
|
pr.Issue.Title = "WIP: " + original
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.Equal(t, "WIP:", pr.GetWorkInProgressPrefix(db.DefaultContext))
|
2018-08-13 13:04:39 -06:00
|
|
|
|
|
|
|
pr.Issue.Title = "[wip] " + original
|
2022-11-19 01:12:33 -07:00
|
|
|
assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix(db.DefaultContext))
|
2018-08-13 13:04:39 -06:00
|
|
|
}
|
2022-06-13 03:37:59 -06:00
|
|
|
|
|
|
|
func TestDeleteOrphanedObjects(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
countBefore, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 3, orphaned)
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
2022-06-13 03:37:59 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, countBefore, countAfter)
|
|
|
|
}
|