improve test

This commit is contained in:
yp05327 2023-07-27 01:49:52 +00:00
parent a583c64fd0
commit 3b6ff9ba0a
7 changed files with 391 additions and 93 deletions

View File

@ -72,79 +72,79 @@
-
id: 7
repo_id: 58
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 1
repo_id: 1
name: 'opening-pr'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 8
repo_id: 58
name: 'user-fork-new-commit'
repo_id: 1
name: 'closed-pr'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 40
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 9
repo_id: 58
name: 'user-fork-no-commit'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 40
repo_id: 1
name: 'merged-pr'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 10
repo_id: 59
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 1
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
repo_id: 1
name: 'closed-pr-with-deleted-branch'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 39
is_deleted: true
deleted_by_id: 39
deleted_unix: 1489927700
-
id: 11
repo_id: 59
name: 'org-fork-new-commit'
repo_id: 1
name: 'merged-pr-with-deleted-branch'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927691
commit_time: 1489927680
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
is_deleted: true
deleted_by_id: 39
deleted_unix: 1489927700
-
id: 12
repo_id: 59
name: 'org-fork-no-commit'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927691
repo_id: 1
name: 'deleted-branch'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
is_deleted: true
deleted_by_id: 39
deleted_unix: 1489927700
-
id: 13
repo_id: 60
repo_id: 58
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
@ -156,6 +156,78 @@
-
id: 14
repo_id: 58
name: 'user-fork-new-commit'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 40
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 15
repo_id: 58
name: 'user-fork-no-commit'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 40
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 16
repo_id: 59
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 1
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 17
repo_id: 59
name: 'org-fork-new-commit'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927691
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 18
repo_id: 59
name: 'org-fork-no-commit'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927691
pusher_id: 39
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 19
repo_id: 60
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 1
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 20
repo_id: 60
name: 'private-org-fork-new-commit'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
@ -167,7 +239,7 @@
deleted_unix: 0
-
id: 15
id: 21
repo_id: 60
name: 'private-org-fork-no-commit'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
@ -179,7 +251,7 @@
deleted_unix: 0
-
id: 16
id: 22
repo_id: 60
name: 'private-org-fork-no-permission-new-commit'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
@ -191,7 +263,7 @@
deleted_unix: 0
-
id: 17
id: 23
repo_id: 60
name: 'private-org-fork-no-permission-no-commit'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
@ -201,3 +273,27 @@
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 24
repo_id: 1
name: 'same-name-branch-in-pr'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 40
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 25
repo_id: 58
name: 'same-name-branch-in-pr'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927680
pusher_id: 40
is_deleted: false
deleted_by_id: 0
deleted_unix: 0

View File

@ -304,3 +304,105 @@
created_unix: 946684830
updated_unix: 978307200
is_locked: false
-
id: 19
repo_id: 1
index: 6
poster_id: 39
original_author_id: 0
name: opening pr for recently new branch search test
content: content
milestone_id: 0
priority: 0
is_closed: false
is_pull: true
num_comments: 0
created_unix: 946684830
updated_unix: 978307200
is_locked: false
-
id: 20
repo_id: 1
index: 7
poster_id: 39
original_author_id: 0
name: closed pr for recently new branch search test
content: content
milestone_id: 0
priority: 0
is_closed: true
is_pull: true
num_comments: 0
created_unix: 946684830
updated_unix: 978307200
is_locked: false
-
id: 21
repo_id: 1
index: 8
poster_id: 39
original_author_id: 0
name: merged pr for recently new branch search test
content: content
milestone_id: 0
priority: 0
is_closed: false
is_pull: true
num_comments: 0
created_unix: 946684830
updated_unix: 978307200
is_locked: false
-
id: 22
repo_id: 1
index: 9
poster_id: 39
original_author_id: 0
name: closed pr with deleted branch for recently new branch search test
content: content
milestone_id: 0
priority: 0
is_closed: true
is_pull: true
num_comments: 0
created_unix: 946684830
updated_unix: 978307200
is_locked: false
-
id: 23
repo_id: 1
index: 10
poster_id: 39
original_author_id: 0
name: merged pr with deleted branch for recently new branch search test
content: content
milestone_id: 0
priority: 0
is_closed: false
is_pull: true
num_comments: 0
created_unix: 946684830
updated_unix: 978307200
is_locked: false
-
id: 24
repo_id: 58
index: 1
poster_id: 40
original_author_id: 0
name: pr with same branch name for recently new branch search test
content: content
milestone_id: 0
priority: 0
is_closed: false
is_pull: true
num_comments: 0
created_unix: 946684830
updated_unix: 978307200
is_locked: false

View File

@ -1,27 +1,39 @@
-
group_id: 1
max_index: 5
max_index: 10
-
group_id: 2
max_index: 2
-
group_id: 3
max_index: 2
-
group_id: 10
max_index: 1
-
group_id: 32
max_index: 2
-
group_id: 48
max_index: 1
-
group_id: 42
max_index: 1
-
group_id: 50
max_index: 1
-
group_id: 51
max_index: 1
-
group_id: 58
max_index: 1

View File

@ -76,3 +76,81 @@
base_branch: master
merge_base: 2a47ca4b614a9f5a
has_merged: false
-
id: 7
type: 0 # gitea pull request
status: 2 # mergable
issue_id: 19
index: 4
head_repo_id: 1
base_repo_id: 1
head_branch: opening-pr
base_branch: master
merge_base: cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d
has_merged: false
-
id: 8
type: 0 # gitea pull request
status: 2 # mergable
issue_id: 20
index: 5
head_repo_id: 1
base_repo_id: 1
head_branch: closed-pr
base_branch: master
merge_base: cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d
has_merged: false
-
id: 9
type: 0 # gitea pull request
status: 3 # manually merged
issue_id: 21
index: 6
head_repo_id: 1
base_repo_id: 1
head_branch: merged-pr
base_branch: master
merge_base: cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d
has_merged: true
-
id: 10
type: 0 # gitea pull request
status: 2 # mergable
issue_id: 22
index: 7
head_repo_id: 1
base_repo_id: 1
head_branch: closed-pr-with-deleted-branch
base_branch: master
merge_base: cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d
has_merged: false
-
id: 11
type: 0 # gitea pull request
status: 3 # manually merged
issue_id: 23
index: 8
head_repo_id: 1
base_repo_id: 1
head_branch: merged-pr-with-deleted-branch
base_branch: master
merge_base: cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d
has_merged: true
-
id: 12
type: 0 # gitea pull request
status: 2 # mergable
issue_id: 25
index: 1
head_repo_id: 58
base_repo_id: 1
head_branch: same-name-branch-in-pr
base_branch: master
merge_base: cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d
has_merged: false

View File

@ -12,7 +12,7 @@
num_issues: 2
num_closed_issues: 1
num_pulls: 3
num_closed_pulls: 0
num_closed_pulls: 1
num_milestones: 3
num_closed_milestones: 1
num_projects: 1

View File

@ -429,15 +429,10 @@ func FindRecentlyPushedNewBranches(ctx context.Context, opts *FindRecentlyPushed
// find branches which have already created PRs
prBranchIds := builder.Select("branch.id").From("branch").
InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id").
InnerJoin("issue", "issue.id = pull_request.issue_id").
Where(builder.And(
builder.Eq{"pull_request.base_repo_id": opts.BaseRepo.ID},
builder.Eq{"pull_request.base_branch": opts.BaseRepo.DefaultBranch},
builder.In("pull_request.head_repo_id", repoIDs),
builder.Or(
builder.Eq{"issue.is_closed": true},
builder.Eq{"pull_request.has_merged": true},
),
))
if opts.CommitAfterUnix == 0 {
@ -449,7 +444,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, opts *FindRecentlyPushed
return nil, err
}
if opts.ListOptions.PageSize == 0 && opts.ListOptions.Page == 0 {
if opts.ListOptions.PageSize == 0 {
opts.ListOptions.PageSize = 2
opts.ListOptions.Page = 1
}

View File

@ -195,67 +195,82 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
user42 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 42})
tests := []struct {
name string
actor *user_model.User
count int
commitAfterUnix int64
want []string
name string
opts *git_model.FindRecentlyPushedNewBranchesOptions
count int
want []int64
}{
// user2 is the owner of the repo and the organization
// user39 is the owner of the repo and the organization
// in repo 1, user39 has opening/closed/merged pr and closed/merged pr with deleted branch
{
name: "new branch of the repo",
actor: user39,
count: 2,
commitAfterUnix: 1489927670,
want: []string{"new-commit", "org-fork-new-commit"},
name: "new branch of the repo, org fork repo, pr branches and deleted branch",
opts: &git_model.FindRecentlyPushedNewBranchesOptions{
Actor: user39,
CommitAfterUnix: 1489927670,
ListOptions: db.ListOptions{
PageSize: 10,
Page: 1,
},
},
count: 2,
want: []int64{5, 17}, // "new-commit", "org-fork-new-commit"
},
// we have 2 branches with the same name in repo1 and repo58
// and repo58's branch has a pr, but repo1's branch doesn't
// in this case, we should get repo1's branch but not repo58's branch
{
name: "new branch from user fork repo and same name branch",
opts: &git_model.FindRecentlyPushedNewBranchesOptions{
Actor: user40,
CommitAfterUnix: 1489927670,
ListOptions: db.ListOptions{
PageSize: 10,
Page: 1,
},
},
count: 2,
want: []int64{14, 24}, // "user-fork-new-commit", "same-name-branch-in-pr"
},
{
name: "new branch of org fork repo",
actor: user39,
count: 1,
commitAfterUnix: 1489927690,
want: []string{"org-fork-new-commit"},
name: "new branch from private org with code permisstion repo",
opts: &git_model.FindRecentlyPushedNewBranchesOptions{
Actor: user41,
CommitAfterUnix: 1489927670,
},
count: 1,
want: []int64{20}, // "private-org-fork-new-commit"
},
{
name: "new branch from user fork repo",
actor: user40,
count: 1,
commitAfterUnix: 1489927670,
want: []string{"user-fork-new-commit"},
name: "new branch from private org with no code permisstion repo",
opts: &git_model.FindRecentlyPushedNewBranchesOptions{
Actor: user42,
CommitAfterUnix: 1489927670,
},
count: 0,
want: []int64{},
},
{
name: "new branch from private org with code permisstion repo",
actor: user41,
count: 1,
commitAfterUnix: 1489927670,
want: []string{"private-org-fork-new-commit"},
},
{
name: "new branch from private org with no code permisstion repo",
actor: user42,
count: 0,
commitAfterUnix: 1489927670,
want: []string{"new-commit", "org-fork-new-commit"},
name: "test commitAfterUnix option",
opts: &git_model.FindRecentlyPushedNewBranchesOptions{
Actor: user39,
CommitAfterUnix: 1489927690,
},
count: 1,
want: []int64{17}, // "org-fork-new-commit"
},
}
opts := &git_model.FindRecentlyPushedNewBranchesOptions{
Repo: repo,
BaseRepo: repo,
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts.Actor = tt.actor
opts.CommitAfterUnix = tt.commitAfterUnix
branches, err := git_model.FindRecentlyPushedNewBranches(db.DefaultContext, opts)
tt.opts.Repo = repo
tt.opts.BaseRepo = repo
branches, err := git_model.FindRecentlyPushedNewBranches(db.DefaultContext, tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.count, len(branches))
for i := 1; i < tt.count; i++ {
assert.Equal(t, tt.want[i], branches[i].Name)
assert.Equal(t, tt.want[i], branches[i].ID)
}
})
}
// TODO:test pr branch
}