2019-02-21 01:54:05 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-02-21 01:54:05 +01:00
|
|
|
|
|
|
|
package issues
|
|
|
|
|
|
|
|
import (
|
2022-01-27 09:30:51 +01:00
|
|
|
"context"
|
2019-02-21 01:54:05 +01:00
|
|
|
"testing"
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-07-23 20:36:32 +02:00
|
|
|
"code.gitea.io/gitea/models/issues"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-09-16 05:15:21 +02:00
|
|
|
"code.gitea.io/gitea/modules/indexer/issues/internal"
|
2024-03-02 16:42:31 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2019-02-21 01:54:05 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-12-15 06:39:34 +01:00
|
|
|
_ "code.gitea.io/gitea/models"
|
2023-09-08 06:51:15 +02:00
|
|
|
_ "code.gitea.io/gitea/models/actions"
|
|
|
|
_ "code.gitea.io/gitea/models/activities"
|
2024-11-01 21:02:52 +01:00
|
|
|
_ "code.gitea.io/gitea/models/forgefed"
|
2021-12-15 06:39:34 +01:00
|
|
|
|
2019-02-21 01:54:05 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:10 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-21 01:54:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2023-09-28 03:38:53 +02:00
|
|
|
unittest.MainTest(m)
|
2019-02-21 01:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
func TestDBSearchIssues(t *testing.T) {
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2019-02-24 17:16:20 +01:00
|
|
|
|
|
|
|
setting.Indexer.IssueType = "db"
|
2019-10-15 15:39:51 +02:00
|
|
|
InitIssueIndexer(true)
|
2019-02-21 01:54:05 +01:00
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
t.Run("search issues with keyword", searchIssueWithKeyword)
|
|
|
|
t.Run("search issues in repo", searchIssueInRepo)
|
|
|
|
t.Run("search issues by ID", searchIssueByID)
|
|
|
|
t.Run("search issues is pr", searchIssueIsPull)
|
|
|
|
t.Run("search issues is closed", searchIssueIsClosed)
|
|
|
|
t.Run("search issues by milestone", searchIssueByMilestoneID)
|
|
|
|
t.Run("search issues by label", searchIssueByLabelID)
|
|
|
|
t.Run("search issues by time", searchIssueByTime)
|
|
|
|
t.Run("search issues with order", searchIssueWithOrder)
|
|
|
|
t.Run("search issues in project", searchIssueInProject)
|
|
|
|
t.Run("search issues with paginator", searchIssueWithPaginator)
|
|
|
|
}
|
2019-02-21 01:54:05 +01:00
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
func searchIssueWithKeyword(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
Keyword: "issue2",
|
|
|
|
RepoIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
Keyword: "first",
|
|
|
|
RepoIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
Keyword: "for",
|
|
|
|
RepoIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{11, 5, 3, 2, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
Keyword: "good",
|
|
|
|
RepoIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{1},
|
|
|
|
},
|
|
|
|
}
|
2019-02-21 06:01:28 +01:00
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
2019-02-21 01:54:05 +01:00
|
|
|
}
|
2023-09-14 18:35:53 +02:00
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
func searchIssueInRepo(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
RepoIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{11, 5, 3, 2, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
RepoIDs: []int64{2},
|
|
|
|
},
|
|
|
|
[]int64{7, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
RepoIDs: []int64{3},
|
|
|
|
},
|
|
|
|
[]int64{12, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
RepoIDs: []int64{4},
|
|
|
|
},
|
|
|
|
[]int64{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
RepoIDs: []int64{5},
|
|
|
|
},
|
|
|
|
[]int64{15},
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 18:35:53 +02:00
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 18:35:53 +02:00
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
func searchIssueByID(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-09-14 18:35:53 +02:00
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
PosterID: optional.Some(int64(1)),
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{11, 6, 3, 2, 1},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
|
|
|
{
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
AssigneeID: optional.Some(int64(1)),
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{6, 1},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2024-07-23 20:36:32 +02:00
|
|
|
{
|
|
|
|
// NOTE: This tests no assignees filtering and also ToSearchOptions() to ensure it will set AssigneeID to 0 when it is passed as -1.
|
|
|
|
opts: *ToSearchOptions("", &issues.IssuesOptions{AssigneeID: -1}),
|
|
|
|
expectedIDs: []int64{22, 21, 16, 15, 14, 13, 12, 11, 20, 5, 19, 18, 10, 7, 4, 9, 8, 3, 2},
|
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
{
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
MentionID: optional.Some(int64(4)),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{1},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
|
|
|
{
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
ReviewedID: optional.Some(int64(1)),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
{
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
ReviewRequestedID: optional.Some(int64(1)),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{12},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
{
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
SubscriberID: optional.Some(int64(1)),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{11, 6, 5, 3, 2, 1},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2023-09-21 13:59:50 +02:00
|
|
|
{
|
|
|
|
// issue 20 request user 15 and team 5 which user 15 belongs to
|
|
|
|
// the review request number of issue 20 should be 1
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
ReviewRequestedID: optional.Some(int64(15)),
|
2023-09-21 13:59:50 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{12, 20},
|
2023-09-21 13:59:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// user 20 approved the issue 20, so return nothing
|
2024-03-13 09:25:53 +01:00
|
|
|
opts: SearchOptions{
|
|
|
|
ReviewRequestedID: optional.Some(int64(20)),
|
2023-09-21 13:59:50 +02:00
|
|
|
},
|
2024-03-13 09:25:53 +01:00
|
|
|
expectedIDs: []int64{},
|
2023-09-21 13:59:50 +02:00
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueIsPull(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-03-02 16:42:31 +01:00
|
|
|
IsPull: optional.Some(false),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
[]int64{17, 16, 15, 14, 13, 6, 5, 18, 10, 7, 4, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-03-02 16:42:31 +01:00
|
|
|
IsPull: optional.Some(true),
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2024-02-24 13:38:43 +01:00
|
|
|
[]int64{22, 21, 12, 11, 20, 19, 9, 8, 3, 2},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueIsClosed(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
2023-09-14 18:35:53 +02:00
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-03-02 16:42:31 +01:00
|
|
|
IsClosed: optional.Some(false),
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2024-02-24 13:38:43 +01:00
|
|
|
[]int64{22, 21, 17, 16, 15, 14, 13, 12, 11, 20, 6, 19, 18, 10, 7, 9, 8, 3, 2, 1},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-03-02 16:42:31 +01:00
|
|
|
IsClosed: optional.Some(true),
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
[]int64{5, 4},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueByMilestoneID(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
MilestoneIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{2},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2023-09-16 05:15:21 +02:00
|
|
|
MilestoneIDs: []int64{3},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
[]int64{3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueByLabelID(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
IncludedLabelIDs: []int64{1},
|
|
|
|
},
|
|
|
|
[]int64{2, 1},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2023-09-16 05:15:21 +02:00
|
|
|
IncludedLabelIDs: []int64{4},
|
|
|
|
},
|
|
|
|
[]int64{2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
ExcludedLabelIDs: []int64{1},
|
|
|
|
},
|
2024-02-24 13:38:43 +01:00
|
|
|
[]int64{22, 21, 17, 16, 15, 14, 13, 12, 11, 20, 6, 5, 19, 18, 10, 7, 4, 9, 8, 3},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueByTime(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-03-13 09:25:53 +01:00
|
|
|
UpdatedAfterUnix: optional.Some(int64(0)),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
2024-02-24 13:38:43 +01:00
|
|
|
[]int64{22, 21, 17, 16, 15, 14, 13, 12, 11, 20, 6, 5, 19, 18, 10, 7, 4, 9, 8, 3, 2, 1},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueWithOrder(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
|
|
|
SortBy: internal.SortByCreatedAsc,
|
|
|
|
},
|
2024-02-24 13:38:43 +01:00
|
|
|
[]int64{1, 2, 3, 8, 9, 4, 7, 10, 18, 19, 5, 6, 20, 11, 12, 13, 14, 15, 16, 17, 21, 22},
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueInProject(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-03-13 09:25:53 +01:00
|
|
|
ProjectID: optional.Some(int64(1)),
|
2023-09-16 05:15:21 +02:00
|
|
|
},
|
|
|
|
[]int64{5, 3, 2, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-05-27 10:59:54 +02:00
|
|
|
ProjectColumnID: optional.Some(int64(1)),
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
|
|
|
[]int64{1},
|
|
|
|
},
|
2023-10-25 13:51:49 +02:00
|
|
|
{
|
|
|
|
SearchOptions{
|
2024-05-27 10:59:54 +02:00
|
|
|
ProjectColumnID: optional.Some(int64(0)), // issue with in default column
|
2023-10-25 13:51:49 +02:00
|
|
|
},
|
|
|
|
[]int64{2},
|
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, _, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchIssueWithPaginator(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
opts SearchOptions
|
|
|
|
expectedIDs []int64
|
|
|
|
expectedTotal int64
|
|
|
|
}{
|
2023-09-14 18:35:53 +02:00
|
|
|
{
|
|
|
|
SearchOptions{
|
2023-09-16 05:15:21 +02:00
|
|
|
Paginator: &db.ListOptions{
|
|
|
|
PageSize: 5,
|
|
|
|
},
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2024-02-24 13:38:43 +01:00
|
|
|
[]int64{22, 21, 17, 16, 15},
|
|
|
|
22,
|
2023-09-14 18:35:53 +02:00
|
|
|
},
|
2023-09-16 05:15:21 +02:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
issueIDs, total, err := SearchIssues(context.TODO(), &test.opts)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-16 05:15:21 +02:00
|
|
|
assert.Equal(t, test.expectedIDs, issueIDs)
|
|
|
|
assert.Equal(t, test.expectedTotal, total)
|
2023-09-14 18:35:53 +02:00
|
|
|
}
|
|
|
|
}
|