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.
278 lines
9.2 KiB
278 lines
9.2 KiB
8 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package issues_test
|
||
8 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
2 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestPullRequest_LoadAttributes(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest)
|
||
8 years ago
|
assert.NoError(t, pr.LoadAttributes())
|
||
|
assert.NotNil(t, pr.Merger)
|
||
|
assert.Equal(t, pr.MergerID, pr.Merger.ID)
|
||
|
}
|
||
|
|
||
|
func TestPullRequest_LoadIssue(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest)
|
||
8 years ago
|
assert.NoError(t, pr.LoadIssue())
|
||
|
assert.NotNil(t, pr.Issue)
|
||
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
||
8 years ago
|
assert.NoError(t, pr.LoadIssue())
|
||
|
assert.NotNil(t, pr.Issue)
|
||
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
func TestPullRequest_LoadBaseRepo(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest)
|
||
5 years ago
|
assert.NoError(t, pr.LoadBaseRepo())
|
||
8 years ago
|
assert.NotNil(t, pr.BaseRepo)
|
||
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
||
5 years ago
|
assert.NoError(t, pr.LoadBaseRepo())
|
||
8 years ago
|
assert.NotNil(t, pr.BaseRepo)
|
||
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
func TestPullRequest_LoadHeadRepo(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest)
|
||
5 years ago
|
assert.NoError(t, pr.LoadHeadRepo())
|
||
8 years ago
|
assert.NotNil(t, pr.HeadRepo)
|
||
|
assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
|
||
|
}
|
||
|
|
||
|
// TODO TestMerge
|
||
|
|
||
|
// TODO TestNewPullRequest
|
||
|
|
||
|
func TestPullRequestsNewest(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
prs, count, err := issues_model.PullRequests(1, &issues_model.PullRequestsOptions{
|
||
3 years ago
|
ListOptions: db.ListOptions{
|
||
5 years ago
|
Page: 1,
|
||
|
},
|
||
8 years ago
|
State: "open",
|
||
|
SortType: "newest",
|
||
|
Labels: []string{},
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
5 years ago
|
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)
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestPullRequestsOldest(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
prs, count, err := issues_model.PullRequests(1, &issues_model.PullRequestsOptions{
|
||
3 years ago
|
ListOptions: db.ListOptions{
|
||
5 years ago
|
Page: 1,
|
||
|
},
|
||
8 years ago
|
State: "open",
|
||
|
SortType: "oldest",
|
||
|
Labels: []string{},
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
5 years ago
|
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)
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestGetUnmergedPullRequest(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr, err := issues_model.GetUnmergedPullRequest(1, 1, "branch2", "master", issues_model.PullRequestFlowGithub)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, int64(2), pr.ID)
|
||
|
|
||
2 years ago
|
_, err = issues_model.GetUnmergedPullRequest(1, 9223372036854775807, "branch1", "master", issues_model.PullRequestFlowGithub)
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
|
|
||
2 years ago
|
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "branch2")
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, true, exist)
|
||
|
|
||
2 years ago
|
exist, err = issues_model.HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "not_exist_branch")
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, false, exist)
|
||
|
}
|
||
|
|
||
8 years ago
|
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(1, "branch2")
|
||
8 years ago
|
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) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(1, "master")
|
||
8 years ago
|
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) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr, err := issues_model.GetPullRequestByIndex(db.DefaultContext, 1, 2)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, int64(1), pr.BaseRepoID)
|
||
|
assert.Equal(t, int64(2), pr.Index)
|
||
|
|
||
2 years ago
|
_, err = issues_model.GetPullRequestByIndex(db.DefaultContext, 9223372036854775807, 9223372036854775807)
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
||
3 years ago
|
|
||
2 years ago
|
_, err = issues_model.GetPullRequestByIndex(db.DefaultContext, 1, 0)
|
||
3 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestGetPullRequestByID(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr, err := issues_model.GetPullRequestByID(db.DefaultContext, 1)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, int64(1), pr.ID)
|
||
|
assert.Equal(t, int64(2), pr.IssueID)
|
||
|
|
||
2 years ago
|
_, err = issues_model.GetPullRequestByID(db.DefaultContext, 9223372036854775807)
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestGetPullRequestByIssueID(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr, err := issues_model.GetPullRequestByIssueID(db.DefaultContext, 2)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, int64(2), pr.IssueID)
|
||
|
|
||
2 years ago
|
_, err = issues_model.GetPullRequestByIssueID(db.DefaultContext, 9223372036854775807)
|
||
8 years ago
|
assert.Error(t, err)
|
||
2 years ago
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestPullRequest_Update(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest)
|
||
8 years ago
|
pr.BaseBranch = "baseBranch"
|
||
|
pr.HeadBranch = "headBranch"
|
||
8 years ago
|
pr.Update()
|
||
|
|
||
2 years ago
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID}).(*issues_model.PullRequest)
|
||
8 years ago
|
assert.Equal(t, "baseBranch", pr.BaseBranch)
|
||
|
assert.Equal(t, "headBranch", pr.HeadBranch)
|
||
3 years ago
|
unittest.CheckConsistencyFor(t, pr)
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestPullRequest_UpdateCols(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
pr := &issues_model.PullRequest{
|
||
8 years ago
|
ID: 1,
|
||
|
BaseBranch: "baseBranch",
|
||
|
HeadBranch: "headBranch",
|
||
|
}
|
||
5 years ago
|
assert.NoError(t, pr.UpdateCols("head_branch"))
|
||
8 years ago
|
|
||
2 years ago
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest)
|
||
8 years ago
|
assert.Equal(t, "master", pr.BaseBranch)
|
||
|
assert.Equal(t, "headBranch", pr.HeadBranch)
|
||
3 years ago
|
unittest.CheckConsistencyFor(t, pr)
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestPullRequestList_LoadAttributes(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
8 years ago
|
|
||
2 years ago
|
prs := []*issues_model.PullRequest{
|
||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}).(*issues_model.PullRequest),
|
||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}).(*issues_model.PullRequest),
|
||
8 years ago
|
}
|
||
2 years ago
|
assert.NoError(t, issues_model.PullRequestList(prs).LoadAttributes())
|
||
8 years ago
|
for _, pr := range prs {
|
||
|
assert.NotNil(t, pr.Issue)
|
||
|
assert.Equal(t, pr.IssueID, pr.Issue.ID)
|
||
|
}
|
||
8 years ago
|
|
||
2 years ago
|
assert.NoError(t, issues_model.PullRequestList([]*issues_model.PullRequest{}).LoadAttributes())
|
||
8 years ago
|
}
|
||
|
|
||
|
// TODO TestAddTestPullRequestTask
|
||
|
|
||
6 years ago
|
func TestPullRequest_IsWorkInProgress(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
6 years ago
|
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}).(*issues_model.PullRequest)
|
||
6 years ago
|
pr.LoadIssue()
|
||
|
|
||
|
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) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
6 years ago
|
|
||
2 years ago
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}).(*issues_model.PullRequest)
|
||
6 years ago
|
pr.LoadIssue()
|
||
|
|
||
|
assert.Empty(t, pr.GetWorkInProgressPrefix())
|
||
|
|
||
|
original := pr.Issue.Title
|
||
|
pr.Issue.Title = "WIP: " + original
|
||
|
assert.Equal(t, "WIP:", pr.GetWorkInProgressPrefix())
|
||
|
|
||
|
pr.Issue.Title = "[wip] " + original
|
||
|
assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix())
|
||
|
}
|
||
2 years ago
|
|
||
|
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)
|
||
|
|
||
|
orphaned, err := db.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
|
||
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 3, orphaned)
|
||
|
|
||
|
err = db.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
||
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, countBefore, countAfter)
|
||
|
}
|