Skip to content

Commit a2822a0

Browse files
committed
Add test and fix bug
1 parent 684d744 commit a2822a0

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

models/issues/issue.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,14 @@ func FindLatestIssues(ctx context.Context, repoID int64, isPull optional.Option[
519519
return issues, err
520520
}
521521

522-
func FindIssuesTitleKeywords(ctx context.Context, repoID int64, keyword string, isPull optional.Option[bool], pageSize int) (IssueList, error) {
522+
func FindIssuesTitleKeywords(ctx context.Context, repoID int64, keyword string, isPull optional.Option[bool], nonIDs []int64, pageSize int) (IssueList, error) {
523523
issues := make([]*Issue, 0, pageSize)
524-
err := db.GetEngine(ctx).Where("repo_id = ?", repoID).
525-
And(isPullToCond(isPull)).
526-
And("name LIKE ?", "%"+keyword+"%").
524+
sess := db.GetEngine(ctx).Where("repo_id = ?", repoID).
525+
And(isPullToCond(isPull))
526+
if len(nonIDs) > 0 {
527+
sess.NotIn("id", nonIDs)
528+
}
529+
err := sess.And("name LIKE ?", "%"+keyword+"%").
527530
OrderBy("created_unix DESC").
528531
Limit(pageSize).
529532
Find(&issues)

services/issue/suggestion.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ func GetSuggestion(ctx context.Context, repo *repo_model.Repository, isPull opti
2424
}
2525
} else {
2626
indexKeyword, _ := strconv.ParseInt(keyword, 10, 64)
27+
nonIDs := []int64{}
2728
if indexKeyword > 0 {
2829
issues, err = issues_model.FindIssuesWithIndexPrefix(ctx, repo.ID, indexKeyword, isPull, pageSize)
2930
if err != nil {
3031
return nil, err
3132
}
33+
for _, issue := range issues {
34+
nonIDs = append(nonIDs, issue.ID)
35+
}
3236
pageSize -= len(issues)
3337
}
3438

3539
if pageSize > 0 {
36-
newIssues, err := issues_model.FindIssuesTitleKeywords(ctx, repo.ID, keyword, isPull, pageSize)
40+
newIssues, err := issues_model.FindIssuesTitleKeywords(ctx, repo.ID, keyword, isPull, nonIDs, pageSize)
3741
if err != nil {
3842
return nil, err
3943
}

services/issue/suggestion_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package issue
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/db"
10+
repo_model "code.gitea.io/gitea/models/repo"
11+
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/modules/optional"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func Test_Suggestion(t *testing.T) {
18+
assert.NoError(t, unittest.PrepareTestDatabase())
19+
20+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
21+
22+
testCases := []struct {
23+
keyword string
24+
isPull optional.Option[bool]
25+
expectedIndexes []int64
26+
}{
27+
{
28+
keyword: "",
29+
expectedIndexes: []int64{5, 4, 3, 2, 1},
30+
},
31+
{
32+
keyword: "1",
33+
expectedIndexes: []int64{1},
34+
},
35+
{
36+
keyword: "issue",
37+
expectedIndexes: []int64{4, 3, 2, 1},
38+
},
39+
{
40+
keyword: "pull",
41+
expectedIndexes: []int64{5},
42+
},
43+
}
44+
45+
for _, testCase := range testCases {
46+
t.Run(testCase.keyword, func(t *testing.T) {
47+
issues, err := GetSuggestion(db.DefaultContext, repo1, testCase.isPull, testCase.keyword)
48+
assert.NoError(t, err)
49+
50+
issueIndexes := make([]int64, 0, len(issues))
51+
for _, issue := range issues {
52+
issueIndexes = append(issueIndexes, issue.Index)
53+
}
54+
assert.EqualValues(t, testCase.expectedIndexes, issueIndexes)
55+
})
56+
}
57+
}

0 commit comments

Comments
 (0)